Is there a way to extend a ThreeJS object?

元气小坏坏 提交于 2019-12-22 11:36:08

问题


I'm using ThreeJS to create an interaction where people can click cubes. However these cubes behave differently when clicked (different color animations, to keep the idea simple).

My idea was to create extension classes of the THREE.Mesh object and add my custom functions and attributes. This would help isolate the different behaviors of the cubes and provide a cleaner code.

I tried using John Resigs' function to extend classes, but it seems to work just for classes that ultimately extend his "Class" class.

Is there a way to do this?


回答1:


There are several ways to make class-based systems in Javascript. John Resig's "Class" is a great one, but it is not the type of inheritance that Three.js uses.

Notice in the Cube class file the line:

THREE.Geometry.call( this );

Javascript does not provide a built-in model for class inheritance, so unless you are using a library (like John Resig's) that bakes inheritance into class construction, you have to call the super method explicitly.

Your class would inherit from CubeGeometry if, inside your class, you call:

THREE.CubeGeometry.call( this );

You will also likely want to set CubeGeometry to be the prototype:

THREE.MyCubeGeometry.prototype = new THREE.CubeGeometry();
THREE.MyCubeGeometry.prototype.constructor = THREE.MyCubeGeometry;


来源:https://stackoverflow.com/questions/10999155/is-there-a-way-to-extend-a-threejs-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!