Skip to content

Commit 4d0154c

Browse files
larsmoacognite-bulldozer[bot]maksnester
authored
feat: add Cognite3DModel.setAllNodeColors (#1132)
* feat: add Cognite3DModel.setAllNodeColors * fix: unused import * doc: mark setAllNodeColors with version 1.1 * Update viewer/src/public/migration/Cognite3DModel.ts Co-authored-by: Maksim Nesterenko <maksim.nesterenko@cognite.com> Co-authored-by: cognite-bulldozer[bot] <51074376+cognite-bulldozer[bot]@users.noreply.github.com> Co-authored-by: Maksim Nesterenko <maksim.nesterenko@cognite.com>
1 parent 26a5355 commit 4d0154c

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

documentation/docs/examples/cad-colors.mdx

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,25 @@ Reveal supports updating the color of individual objects by providing [tree indi
1515
To update the colors of all nodes, loop over all tree indices in the model and update the colors
1616
accordingly:
1717

18-
<!-- TODO: implement eficcient coloring fn and update these examples -->
19-
2018
```jsx runnable
2119
model.iterateNodesByTreeIndex(treeIndex => {
22-
model.setNodeColorByTreeIndex(treeIndex, 127, 127, 127);
20+
const value = treeIndex % 200;
21+
model.setNodeColorByTreeIndex(treeIndex, 200, value, 200);
2322
});
2423
```
2524

25+
> ** New in 1.1.0 **
26+
27+
Notice how colors are applied in batches using this approach. A more performant choice when a single color is
28+
to be applied to all nodes is to use `Cognite3DModel.setAllNodeColors`:
29+
30+
```jsx runnable
31+
model.setAllNodeColors(127, 127, 127);
32+
```
33+
2634
## Reset node colors
2735

28-
Color overrides can be overridden using `Cognite3DModel.resetNodeColorByTreeIndex`, e.g.
36+
Color overrides can be reset using `Cognite3DModel.resetNodeColorByTreeIndex`, e.g.
2937

3038
```jsx runnable
3139
model.iterateNodesByTreeIndex(treeIndex => {

viewer/src/public/migration/Cognite3DModel.ts

+15
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,21 @@ export class Cognite3DModel extends THREE.Object3D implements CogniteModelBase {
453453
return treeIndices.count;
454454
}
455455

456+
/**
457+
* Overrrides color for all nodes in the scene.
458+
* @param r Red component between 0 and 255.
459+
* @param g Green component between 0 and 255.
460+
* @param b Blue component between 0 and 255.
461+
* @version new in 1.1.0
462+
*/
463+
setAllNodeColors(r: number, g: number, b: number): void {
464+
const color: [number, number, number] = [r, g, b];
465+
for (let i = 0; i <= this.cadModel.scene.maxTreeIndex; i++) {
466+
this.nodeColors.set(i, color);
467+
}
468+
this.cadNode.requestNodeUpdate([...this.nodeColors.keys()]);
469+
}
470+
456471
/**
457472
* Restore original colors for all nodes.
458473
*/

0 commit comments

Comments
 (0)