在按钮上单击three.js更改three.line的颜色

时间:2015-04-18 06:02:33

标签: javascript three.js

我是Three.js的新手,我一直在尝试使用线条基本材质点击按钮时更改线条的颜色,但线条的颜色不会改变。

我的代码

if (color === "color") {
        material = new THREE.LineBasicMaterial({
            color: 0xff0000,
            opacity: 1,
            linewidth: 5
        });


    } else {
        material = new THREE.LineBasicMaterial({
            color: 0x000000,
            opacity: 1,
            linewidth: 1
        });

    }

    var tmp_geo = new THREE.Geometry();
    tmp_geo.vertices.push(new THREE.Vector3( -10, 0, 0 ));
    tmp_geo.vertices.push(new THREE.Vector3( 10, 0, 10 ));

    line = new THREE.Line(tmp_geo, material);

    line.material.needsUpdate = true;
    line.geometry.colorsNeedUpdate = true;

    line.scale.x = line.scale.y = line.scale.z = 1;
    line.originalScale = 1;
    geometries.push(tmp_geo);

    scene.add(line);

我正在使用带有轨迹球控件的webGlRenderer,我的版本是r66。无论如何要做到这一点。我一直在努力寻找解决方案来解决这个问题。请提供任何帮助。

提前致谢

3 个答案:

答案 0 :(得分:2)

此处找到示例链接Change color

onClick = function() {
line.material.color = new THREE.Color(0xffffff * Math.random());
line.material.needsUpdate = true;
};

如果示例不清楚,请在jsfiddle中分享您的代码,以便我可以帮助您。

答案 1 :(得分:1)

此脚本仅为场景初始化素材,如果要更改实景场景中的素材,则必须再次设置素材。

只需按功能调用onclick

line.material.color = new THREE.Color( 0xffffff );         
line.material.needsUpdate = true;

答案 2 :(得分:0)

不知道它是对还是错,但我只是在一行中添加了一个名字(line.name ="行名")和

    var  material;
    if (color === "color") {

        var lineColorChange = scene.getObjectByName(linename);
        lineColorChange.material.color = new THREE.Color(0xff00ff);
        line.geometry.dynamic = true;
        line.material.overdraw = true;
        lineColorChange.material.needsUpdate = true;

    } else {
        material = new THREE.LineBasicMaterial({
            color: 0x000000,
            opacity: 1,
            linewidth: 5
        });

    }

    var tmp_geo = new THREE.Geometry();
    tmp_geo.vertices.push(10,0,10);
    tmp_geo.vertices.push(-10,0,-10);

    line = new THREE.Line(tmp_geo, material);
    line.name = linename;


    geometries.push(tmp_geo);
    scene.add(line);