如何更新THREE.Sprite颜色

时间:2014-08-28 21:15:25

标签: three.js sprite

jsfiddle here

我使用默认颜色创建材质,在本例中为0xffffff

然后,我想改变颜色。我无法弄清楚在哪里更改它 - particle.color = 0xffffff * Math.random();不起作用。没有particle.color,但有一个particle.material.color,如果我更改会给我一个错误。

我是否需要为每个精灵创建新材料?有没有办法可以简单地改变THREE.Sprite的颜色?

var material = new THREE.SpriteCanvasMaterial({
        color: 0xffffff,
        program: function (context) {
            context.beginPath();
            context.arc(0, 0, 0.5, 0, PI2, true);
            context.fill();
        }
    });

    for (var i = 0; i < 50; i++) {

        particle = new THREE.Sprite(material);
        particle.position.x = Math.random() * 2 - 1;
        particle.position.y = Math.random() * 2 - 1;
        particle.position.z = Math.random() * 2 - 1;
        particle.position.normalize();
        particle.position.multiplyScalar(Math.random() * 10 + 450);
        particle.scale.x = particle.scale.y = 20;
        particle.color = 0xffffff * Math.random();
        scene.add(particle);
    }

1 个答案:

答案 0 :(得分:4)

particle.material.color = new THREE.Color(0x0066CC);

particle.material.color.set(0x0066CC); Threejs:R.73

来自文档: http://threejs.org/docs/#Reference/Math/Color