Setting an instance color is very unreliable

I have loaded an IFC file into an object and added it to the scene. I now need a functionality that will let me highlight an equipment with some custom property. I have managed to identify all items in the three object that belong to my choice, and this is what I do to change the color

for (let equipmentInstanceExpressId of equipmentInstanceExpressIds) {
        for (let item of loadedFragmentsGroup.items) {
            if (item.ids.has(equipmentInstanceExpressId)) {

                item.mesh.instanceColor.array[0] = 0;
                item.mesh.instanceColor.array[1] = 1;
                item.mesh.instanceColor.array[2] = 1;
            }
        }
    }

However, it doesnt change the color reliably. Sometimes only one of the meshes gets changed, sometimes almost none at all. How can I change the instance color of my mesh reliably? It looks something like this, when I want the entire conveyor to be coloured:

I know that I have all the items, because using item.mesh.visible = false turns the whole thing invisible.

That’s not really possible, instance coloring is just an array of matrices, it doesn’t execute, doesn’t have any logic or sorting on it’s own. Ain’t no magic happening there.

So either your code is async, in which case it’s executed in different order every time and sorting the instances differently - or there’s something else happening that gives an impression that instances are colored randomly (ex. meshes are overlapping.)

On a side-note, be sure to call:

instancedMesh.instanceColor.needsUpdate = true;

Whenever you change any color within the instanced mesh.

2 Likes

Indeed, my code is called async. Does that provoke any other problems?

Even though the order is different every time, the missing recolor is consistent thoughout the runs.

My goodness… It seems all it was missing was the needsUpdate = true.

Thank you so much!

1 Like