问题描述
我正在使用ThreeJS创建一个人们可以点击多维数据集的交互。然而,这些立方体在点击时表现不同(不同颜色的动画,以保持简单的想法)。
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).
我的想法是创建THREE.Mesh对象的扩展类并添加我的自定义函数和属性。这有助于隔离多维数据集的不同行为并提供更清晰的代码。
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.
我尝试使用,但它似乎只适用于最终扩展其Class类的类。
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?
推荐答案
有几种方法可以制作基于类的系统的JavaScript。 John Resig的Class很棒,但它不是Three.js使用的继承类型。
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.
注意该行:
THREE.Geometry.call( this );
Javascript不提供类继承的内置模型,因此除非您使用库(像John Resig那样将继承带入类构造中,你必须明确地调用super方法。
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.
如果你在类中调用了你的类,它将继承自CubeGeometry:
Your class would inherit from CubeGeometry if, inside your class, you call:
THREE.CubeGeometry.call( this );
您还可能希望将CubeGeometry设置为原型:
You will also likely want to set CubeGeometry to be the prototype:
THREE.MyCubeGeometry.prototype = new THREE.CubeGeometry();
THREE.MyCubeGeometry.prototype.constructor = THREE.MyCubeGeometry;
这篇关于有没有办法扩展ThreeJS对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!