All files / views/threejs/cad triangleMeshes.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 1/1
100% Lines 20/20

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        2x     2x         4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   4x    
/*!
 * Copyright 2020 Cognite AS
 */
 
import * as THREE from 'three';
import { TriangleMesh } from '../../../models/cad/types';
 
export function createTriangleMeshes(
  triangleMeshes: TriangleMesh[],
  bounds: THREE.Box3,
  material: THREE.ShaderMaterial
): THREE.Mesh[] {
  const result: THREE.Mesh[] = [];
  for (const mesh of triangleMeshes) {
    const geometry = new THREE.BufferGeometry();
    const indices = new THREE.Uint32BufferAttribute(mesh.indices.buffer, 1);
    const vertices = new THREE.Float32BufferAttribute(mesh.vertices.buffer, 3);
    const colors = new THREE.Float32BufferAttribute(mesh.colors.buffer, 3);
    const treeIndices = new THREE.Float32BufferAttribute(mesh.treeIndices.buffer, 1);
    geometry.setIndex(indices);
    geometry.setAttribute('position', vertices);
    geometry.setAttribute('color', colors);
    geometry.setAttribute('treeIndex', treeIndices);
    geometry.boundingBox = bounds.clone();
    geometry.boundingSphere = new THREE.Sphere();
    bounds.getBoundingSphere(geometry.boundingSphere);
 
    const obj = new THREE.Mesh(geometry, material);
    obj.name = `Triangle mesh ${mesh.fileId}`;
    result.push(obj);
  }
  return result;
}