Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferGeometry : Vertex Colors changed, but not updated visually

Tags:

three.js

while using Three.js i met a problem connected with vertexes colors. I created BufferGeometry , which consists of squares made by 2 triangles.

var geometry = new THREE.BufferGeometry();
// filling positions and colors
geometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));
geometry.addAttribute('color', new THREE.BufferAttribute(colors, 3));

var material = new THREE.MeshPhongMaterial(
    {
        side: THREE.OneSide, vertexColors: THREE.VertexColors
    });
GridFloor = new THREE.Mesh(geometry, material);
GridFloor.rotation.x = Math.PI / 2;
GridFloor.geometry.dynamic = true;
GridFloor.geometry.__dirtyColors = true;
scene.add(GridFloor);

Everything works fine, until i change vertex colors. I am changing them , trying to update , but nothing happens...

var newColor = new THREE.Color(0xff0000);
var colors = GridFloor.geometry.attributes.color.array;
    for (var i = 0, j = 0; i < 4; i++, j += 3) {

        var index = INTERSECTED.indices[i % 3] * 3;

        colors[index] = newColor.r;
        colors[index + 1] = newColor.g;
        colors[index + 2] = newColor.b;
    }
GridFloor.geometry.colorsNeedUpdate = true;

INTERSECTED.colorsNeedUpdate = true;

Thank you for your help.

like image 226
Abenteuer Avatar asked Aug 26 '14 18:08

Abenteuer


1 Answers

Here is how you set the needsUpdate flag for BufferGeometry.

bufferGeometry.attributes.attributeName.needsUpdate = true;

three.js r.68

like image 60
WestLangley Avatar answered Nov 15 '22 03:11

WestLangley