创建自定义Object3D类 [英] Creating custom Object3D class

查看:261
本文介绍了创建自定义Object3D类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是来自AS3 / Away3D背景的THREE.js的新手。我正在尝试创建一个自定义对象类,它扩展了THREE.Object3D以添加到我的场景中。 CustomObject将封装许多行为属性和方法。理想情况下,我想传递每个CustomObject它自己的数据对象,这将决定它的外观/移动/行为。封装此代码将使我的main.js更清洁。

I'm new to THREE.js coming from an AS3/Away3D background. I'm trying to create a custom object class that extends THREE.Object3D to add to my scene. CustomObject will encapsulate a lot of behavioural properties and methods. Ideally I'd like to pass each CustomObject it's own data object which will determine how it will look/move/behave. Encapsulating this code will keep my main.js a lot cleaner.

我的问题是我似乎无法将类的实例直接添加到我的场景中。我只能通过CustomObject.getMesh()方法添加网格。是否可以直接将类的实例添加到我的场景中进行渲染?这是我从网上和/ src中找到的一个非常基本的尝试:

My problem is I can't seem to add an instance of the class directly to my scene. I can only add the mesh via the CustomObject.getMesh() method. Is it possible to add an instance of the class directly to my scene to render? Here's a very basic attempt I've put together from what I've been able to find online and in the /src:

function CustomObject(){

    THREE.Object3D.call( this );
    this.type = 'CustomObject';
    this.geometry = new THREE.BoxGeometry( 540, 540, 14 );
    this.mesh = new THREE.Mesh( this.geometry, new THREE.MeshLambertMaterial( { color: 0xff0000 } ) );
}

CustomObject.prototype = Object.create( THREE.Object3D.prototype );
CustomObject.prototype.constructor = THREE.Object3D;

CustomObject.prototype.getMesh = function(){

    return this.mesh;

}

我希望能够将CustomObject类直接添加到场景中,以使对象管理更加清晰。有人能告诉我这是如何实现的吗?

I'd like to be able to add the CustomObject class directly to the scene to make object management a lot cleaner. Can anyone tell me how this is achievable please?

非常感谢提前!

David

推荐答案

如果要将自定义对象添加到直接场景,你可以使用这样的模式:

If you want to add your custom object to the scene directly, you can use a pattern like this one:

function CustomObject() {

    this.type = 'CustomObject';

    this.geometry = new THREE.BoxGeometry( 540, 540, 14 );
    this.material = new THREE.MeshLambertMaterial( { color: 0xff0000 } );

    THREE.Mesh.call( this, this.geometry, this.material );

}

CustomObject.prototype = Object.create( THREE.Mesh.prototype );
CustomObject.prototype.constructor = CustomObject;

CustomObject.prototype.getMesh = function() {

    return this.mesh;

}

var foo = new CustomObject();
scene.add( foo );

three.js r.71

three.js r.71

这篇关于创建自定义Object3D类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆