如何在THREE.js中克隆Line对象的Mesh?

时间:2019-02-26 22:49:53

标签: javascript three.js line

我正在尝试创建一个10行的for循环。但是line.Clone()给我一个错误,因为它找不到要克隆的网格物体?如果您知道如何添加线的网格,请告诉我。

以下是一些代码:

forward_RT(){

    var spotLight = new THREE.SpotLight( 0xffffff ); //White Color
    spotLight.position.set( 150, 500, -210 );
    scene_Main.add( spotLight );

    var material = new THREE.LineBasicMaterial( { color: 0xff0000 } );
    var geometry = new THREE.Geometry();
    geometry.vertices.push(new THREE.Vector3( spotLight.position.x, spotLight.position.y, spotLight.position.z) );
    geometry.vertices.push(new THREE.Vector3( ray_End_pos_X, ray_End_pos_Y, ray_End_pos_Z) );

    var line = new THREE.Line( geometry, material );

    for(var i=0; i<10; i++){
        //Also tried 
        //var newLine = line.clone(); & scene_Main.add(newLine);
        scene.add(line.clone());

        ray_End_pos_X += 50;
    }
}

1 个答案:

答案 0 :(得分:2)

您的代码有几个问题(例如,您增加了ray_End_pos_X,但不要在循环中使用它)。我怀疑您可能只需要在每次迭代期间更改几何形状即可。

我会从克隆的几何体和共享材料中选择新的线条。我迅速检查了一下,这行得通:

    var rayX = 0, rayY=0, rayZ = 0;

    var material = new THREE.LineBasicMaterial( { color: 0xff0000 } );
    var geometry = new THREE.Geometry();
    geometry.vertices.push(new THREE.Vector3( spotLight.position.x, spotLight.position.y, spotLight.position.z) );
    geometry.vertices.push(new THREE.Vector3( rayX, rayY, rayZ) );

    for(var i=0; i<10; i++) {
        var newLine = new THREE.Line(geometry.clone(), material);
        newLine.geometry.vertices[1].x = rayX;
        this.context.scene.add(newLine);
        rayX += 0.1;
    }

这是它的外观(红线,其他无关紧要):

result screenshot