javascript创建一个新课程

时间:2016-10-27 10:41:46

标签: javascript class

我有一些代码可以将对象加载到javascript / three js中。 我希望这个代码在一个不同的类中,所以我有一个更小的代码。

我希望在类

中使用以下代码
var manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {};

var texture = new THREE.Texture();
var onProgress = function ( xhr ) {
    if ( xhr.lengthComputable ) {
        var percentComplete = xhr.loaded / xhr.total * 100;
        console.log( Math.round(percentComplete, 2) + '% downloaded' );
    }
};
var onError = function ( xhr ) {};

var loaderA = new THREE.ImageLoader( manager );
loaderA.load('sig.png', function ( image ) {
    texture.image = image;
    texture.needsUpdate = true;
});

var loaderB = new THREE.OBJLoader( manager );
loaderB.load( 'hoesjes/hoes.obj', function ( object ) {
    object.traverse( function ( child ) {
        if ( child instanceof THREE.Mesh ) {
            child.material.map = texture;
        }
    } );

    this.objectLoader.position.x = xpos;
    this.objectLoader.position.y = ypos;
    this.objectLoader.position.z = zpos;  

    object.position.y = - 20;
    scene.add( object );
}, onProgress, onError );

} 

如果我用下面的构造函数创建一个类是不对的?

class objectLoader

constructor(xpos, ypos, zpos) {
    var manager = new THREE.LoadingManager();
    manager.onProgress = function ( item, loaded, total ) {};
    var texture = new THREE.Texture();

    var onProgress = function ( xhr ) {
        if ( xhr.lengthComputable ) {
            var percentComplete = xhr.loaded / xhr.total * 100;
            console.log( Math.round(percentComplete, 2) + '% downloaded' );
        }
    };
    var onError = function ( xhr ) {};

    var loaderA = new THREE.ImageLoader( manager );
    loaderA.load('sig.png', function ( image ) {
        texture.image = image;
        texture.needsUpdate = true;
    } );

    var loaderB = new THREE.OBJLoader( manager );
    loaderB.load( 'hoesjes/hoes.obj', function ( object ) {
        object.traverse( function ( child ) {
            if ( child instanceof THREE.Mesh ) {
                child.material.map = texture;
            }
        } );
        this.objectLoader.position.x = xpos;
        this.objectLoader.position.y = ypos;
        this.objectLoader.position.z = zpos;  

        object.position.y = - 20;
        scene.add( object );
    }, onProgress, onError );
}

}

如果这是正确的,我如何在我的主页上调用它?提前致谢

0 个答案:

没有答案