All files / src/views/threejs/cad shading.ts

13.33% Statements 2/15
0% Branches 0/4
0% Functions 0/2
13.33% Lines 2/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38        1x                     1x                                            
/*!
 * Copyright 2020 Cognite AS
 */
 
import { createMaterials, Materials } from './materials';
 
export interface Shading {
  materials: Materials;
  updateNodes: (treeIndices: number[]) => void;
}
 
export interface DefaultShadingOptions {
  color: (treeIndex: number) => number[] | undefined;
}
 
export function createDefaultShading(options: DefaultShadingOptions): Shading {
  const materials = createMaterials();
  const updateNodes = (treeIndices: number[]) => {
    if (treeIndices.length === 0) {
      return;
    }
    for (const treeIndex of treeIndices) {
      const colorOrUndefined = options.color(treeIndex);
      const color = colorOrUndefined ? colorOrUndefined : [0, 0, 0, 0];
      materials.overrideColorPerTreeIndex.image.data[4 * treeIndex] = color[0];
      materials.overrideColorPerTreeIndex.image.data[4 * treeIndex + 1] = color[1];
      materials.overrideColorPerTreeIndex.image.data[4 * treeIndex + 2] = color[2];
      materials.overrideColorPerTreeIndex.image.data[4 * treeIndex + 3] = color[3];
    }
    materials.overrideColorPerTreeIndex.needsUpdate = true;
  };
 
  return {
    materials,
    updateNodes
  };
}