Joh*_*ohn 4 javascript three.js
我想扩展Three.js Object3D类,但无法弄清楚如何做到这一点.
有这个Stackoverflow问题,我已阅读,重新阅读和尝试,但无法让它为我工作.
谁能提供一些关于如何实现这一目标的具体代码?这就是我现在所拥有的:
var MyObject3D = function() {
THREE.Object3D.call(this);
MyObject3D.prototype = new THREE.CubeGeometry();
MyObject3D.prototype.constructor = MyObject3D;
}
Run Code Online (Sandbox Code Playgroud)
并创建一个实例:
var thing = new MyObject3D();
var testGeo = new THREE.CubeGeometry(10, 10, 10);
var testMat = new THREE.MeshPhongMaterial();
var testMesh = new THREE.Mesh(testGeo, testMat);
thing.add(testMesh);
Run Code Online (Sandbox Code Playgroud)
但是调用MyObject3D实例的"add"方法会返回"thing"没有方法"add"的错误.
这是怎么回事?
Ice*_*You 11
您将原型设置为CubeGeometry,它没有添加方法.根据您尝试实例化对象的方式,看起来您实际上希望您的对象具有Mesh原型.
你很可能想要这样的东西:
var MyObject3D = function() {
// Run the Mesh constructor with the given arguments
THREE.Mesh.apply(this, arguments);
};
// Make MyObject3D have the same methods as Mesh
MyObject3D.prototype = Object.create(THREE.Mesh.prototype);
// Make sure the right constructor gets called
MyObject3D.prototype.constructor = MyObject3D;
Run Code Online (Sandbox Code Playgroud)
然后实例化它:
var testGeo = new THREE.CubeGeometry(20, 20, 20);
var testMat = new Three.MeshNormalMaterial();
var thing = new MyObject3D(testGeo, testMat);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4560 次 |
| 最近记录: |