How to change primitive node colors, gltf 2.0 vs gltf 1.0

With Cesium <= 1.96 and gltf 1.0, I can change a primitive’s colors like so

var primitive = viewer.scene.primitives._primitives[0]._primitives[0];
var material = primitive.getMaterial(materialName);
material.setValue(‘diffuse’, new Cesium.Cartesian4(1, 0, 0, 1));

But with Cesium 1.99 and gltf 2.0, I haven’t figured out the syntax to do so.

I can change the whole primitive’s color with

primitive.color = Cesium.Color.RED;

But to change a node’s color this doesn’t work

primitive.getNode(nodeName).color=Cesium.Color.RED;

However the node is accessible because this hides it

primitive.getNode(nodeName).show=false;

This also doesn’t work

primitive._loader._gltfJsonLoader._gltf.materials[0].pbrMetallicRoughness.baseColorFactor=[1, 0, 0, 1];

With Cesium 1.99 and gltf 2.0, how can I change a primitive’s nodes’ colors ?

Thank you for your help!

I figured it out!

Choose your primitive containing the node

var primitive = viewer.scene.primitives._primitives[0];

View node names like this

Object.keys(primitive._nodesByName);

Choose the node to be colored (1st one in this example)

var nodeName = Object.keys(primitive._nodesByName)[0];

Change the node’s color like this!

primitive.getNode(nodeName)._runtimeNode.node.primitives[0].material.metallicRoughness.baseColorFactor=Cesium.Cartesian4.fromColor(Cesium.Color.RED);

I got inspired by these posts :
Changing material of single mesh in model
How to color specific part of Model

First of all thank you for sharing codes. I’ve tried your solution but it doesn’t work. Check this sandcastle link.

I used the Primitive API, I haven’t tried with the Entity API.

viewer.entities.add uses the Entity API
Cesium.Model.fromGltf uses the Primitive API

I used this reference model to get the colors working with Cesium.Model.fromGltf

Hope this helps.

1 Like