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

88.08% Statements 170/193
92.5% Branches 37/40
66.67% Functions 18/27
92.35% Lines 169/183

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328        3x       3x                   3x 17x 1x   17x 1x   17x 1x   17x 1x   17x 1x   17x 1x   17x 1x   17x 1x   17x 1x   17x 1x   17x 1x   17x 1x         204x                     1174x 1174x 12x   1174x           16x           16x 6x 6x   16x       16x       16x             16x   16x 16x 16x   16x 16x     16x       1x   1x 1x 1x 1x     1x 1x   1x 1x       1x   1x 1x 1x 1x   1x 1x   1x 1x       1x   1x 1x 1x   1x 1x 1x 1x 1x       1x   1x 1x 1x   1x 1x 1x 1x 1x       1x   1x 1x 1x   1x 1x 1x 1x 1x       1x   1x 1x 1x   1x 1x 1x 1x 1x       1x   1x 1x 1x   1x 1x 1x 1x 1x       1x   1x 1x 1x   1x 1x               1x 1x   1x 1x 1x 1x 1x       1x   1x 1x 1x 1x   1x 1x 1x 1x       1x   1x 1x 1x   1x 1x 1x 1x 1x       5x 1x   4x 4x       1x 1x     9x 1x 1x   1x 5x 5x 5x 5x   5x 5x   5x 5x   5x     1x       1x   1x 1x 1x 1x   1x 1x 1x 1x                  
/*!
 * Copyright 2020 Cognite AS
 */
 
import * as THREE from 'three';
import { PrimitiveAttributes } from '../../../workers/types/parser.types';
import { Sector } from '../../../models/cad/types';
import { TypedArray } from 'three';
import {
  boxGeometry,
  quadGeometry,
  coneGeometry,
  trapeziumGeometry,
  torusLODs,
  nutGeometry
} from './primitiveGeometries';
import { Materials } from './materials';
 
export function* createPrimitives(sector: Sector, materials: Materials) {
  if (hasAny(sector.boxes)) {
    yield createBoxes(sector.boxes, materials.box);
  }
  if (hasAny(sector.circles)) {
    yield createCircles(sector.circles, materials.circle);
  }
  if (hasAny(sector.cones)) {
    yield createCones(sector.cones, materials.cone);
  }
  if (hasAny(sector.eccentricCones)) {
    yield createEccentricCones(sector.eccentricCones, materials.eccentricCone);
  }
  if (hasAny(sector.ellipsoidSegments)) {
    yield createEllipsoidSegments(sector.ellipsoidSegments, materials.ellipsoidSegment);
  }
  if (hasAny(sector.generalCylinders)) {
    yield createGeneralCylinders(sector.generalCylinders, materials.generalCylinder);
  }
  if (hasAny(sector.generalRings)) {
    yield createGeneralRings(sector.generalRings, materials.generalRing);
  }
  if (hasAny(sector.quads)) {
    yield createQuads(sector.quads, materials.quad);
  }
  if (hasAny(sector.sphericalSegments)) {
    yield createSphericalSegments(sector.sphericalSegments, materials.sphericalSegment);
  }
  if (hasAny(sector.torusSegments)) {
    yield createTorusSegments(sector.torusSegments, materials.torusSegment);
  }
  if (hasAny(sector.trapeziums)) {
    yield createTrapeziums(sector.trapeziums, materials.trapezium);
  }
  if (hasAny(sector.nuts)) {
    yield createNuts(sector.nuts, materials.nut);
  }
}
 
function hasAny(attributes: PrimitiveAttributes) {
  return (
    hasAnyElements(attributes.f32Attributes) ||
    hasAnyElements(attributes.f64Attributes) ||
    hasAnyElements(attributes.mat4Attributes) ||
    hasAnyElements(attributes.u8Attributes) ||
    hasAnyElements(attributes.vec3Attributes) ||
    hasAnyElements(attributes.vec4Attributes)
  );
}
 
function hasAnyElements<T extends TypedArray>(attributeMap: Map<string, T>): boolean {
  let anyElements = false;
  for (const value of attributeMap.values()) {
    anyElements = anyElements || value.length > 0;
  }
  return anyElements;
}
 
function setAttributes(geometry: THREE.InstancedBufferGeometry, attributes: PrimitiveAttributes) {
  // TODO u8Attributes should probably be renamed to colorAttributes or similar
  // TODO should be enough with 3 elements for color instead of 4
  for (const [name, value] of attributes.u8Attributes) {
    const itemSize = 4;
    // We assume uint8 data is color and that it always is normalized. For now this is probably ok.
    // See comment above
    geometry.setAttribute(`a_${name}`, new THREE.InstancedBufferAttribute(value, itemSize, true));
  }
  for (const [name, value] of attributes.f32Attributes) {
    const itemSize = 1;
    geometry.setAttribute(`a_${name}`, new THREE.InstancedBufferAttribute(value, itemSize));
  }
  for (const [name, value] of attributes.vec3Attributes) {
    const itemSize = 3;
    geometry.setAttribute(`a_${name}`, new THREE.InstancedBufferAttribute(value, itemSize));
  }
  for (const [name, value] of attributes.vec4Attributes) {
    const itemSize = 4;
    geometry.setAttribute(`a_${name}`, new THREE.InstancedBufferAttribute(value, itemSize));
  }
  for (const [name, value] of attributes.mat4Attributes) {
    const buffer = new THREE.InstancedInterleavedBuffer(value, 16);
    for (let column = 0; column < 4; column++) {
      const attribute = new THREE.InterleavedBufferAttribute(buffer, 4, column * 4);
      geometry.setAttribute(`a_${name}_column_${column}`, attribute);
    }
  }
  for (const [name, value] of attributes.f64Attributes) {
    // TODO consider passing this properly from Rust instead
    const float32Value = new Float32Array(value);
    const itemSize = 1;
    geometry.setAttribute(`a_${name}`, new THREE.InstancedBufferAttribute(float32Value, itemSize));
  }
  const nodeIds = attributes.f64Attributes.get('nodeId');
  Iif (!nodeIds) {
    throw new Error('nodeId not present in f64Attributes');
  }
  geometry.maxInstancedCount = nodeIds.length;
}
 
function createBoxes(boxes: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(boxGeometry.index);
  geometry.setAttribute('position', boxGeometry.position);
  geometry.setAttribute('normal', boxGeometry.normal);
  setAttributes(geometry, boxes);
 
  // TODO add frustum culling back for all meshes after adding proper boudning boxes
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
 
  mesh.name = `Primitives (Boxes)`;
  return mesh;
}
 
function createCircles(circles: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(quadGeometry.index);
  geometry.setAttribute('position', quadGeometry.position);
  geometry.setAttribute('normal', quadGeometry.position);
  setAttributes(geometry, circles);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
 
  mesh.name = `Primitives (Circles)`;
  return mesh;
}
 
function createCones(cones: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(coneGeometry.index);
  geometry.setAttribute('position', coneGeometry.position);
  setAttributes(geometry, cones);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.onBeforeRender = () => updateMaterialInverseModelMatrix(material, mesh.matrixWorld);
  mesh.name = `Primitives (Cones)`;
  return mesh;
}
 
function createEccentricCones(eccentricCones: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(coneGeometry.index);
  geometry.setAttribute('position', coneGeometry.position);
  setAttributes(geometry, eccentricCones);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.onBeforeRender = () => updateMaterialInverseModelMatrix(material, mesh.matrixWorld);
  mesh.name = `Primitives (EccentricCones)`;
  return mesh;
}
 
function createEllipsoidSegments(ellipsoidSegments: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(coneGeometry.index);
  geometry.setAttribute('position', coneGeometry.position);
  setAttributes(geometry, ellipsoidSegments);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.onBeforeRender = () => updateMaterialInverseModelMatrix(material, mesh.matrixWorld);
  mesh.name = `Primitives (EllipsoidSegments)`;
  return mesh;
}
 
function createGeneralCylinders(generalCylinders: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(coneGeometry.index);
  geometry.setAttribute('position', coneGeometry.position);
  setAttributes(geometry, generalCylinders);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.onBeforeRender = () => updateMaterialInverseModelMatrix(material, mesh.matrixWorld);
  mesh.name = `Primitives (GeneralCylinders)`;
  return mesh;
}
 
function createGeneralRings(generalRings: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(quadGeometry.index);
  geometry.setAttribute('position', quadGeometry.position);
  setAttributes(geometry, generalRings);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.onBeforeRender = () => updateMaterialInverseModelMatrix(material, mesh.matrixWorld);
  mesh.name = `Primitives (GeneralRings)`;
  return mesh;
}
 
function createSphericalSegments(sphericalSegments: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(coneGeometry.index);
  geometry.setAttribute('position', coneGeometry.position);
  setAttributes(geometry, sphericalSegments);
 
  const radii = sphericalSegments.f32Attributes.get('radius');
  Iif (!radii) {
    throw new Error('Spherical segments missing radius');
  }
  // TODO We need to set the radii manually here because
  // we are reusing the ellipsoid shader. We should
  // consider making this cleaner - either by duplicating
  // this data from Rust or by creating a separate shader for
  // spherical segments
  geometry.setAttribute(`a_horizontalRadius`, new THREE.InstancedBufferAttribute(radii, 1));
  geometry.setAttribute(`a_verticalRadius`, new THREE.InstancedBufferAttribute(radii, 1));
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.onBeforeRender = () => updateMaterialInverseModelMatrix(material, mesh.matrixWorld);
  mesh.name = `Primitives (EllipsoidSegments)`;
  return mesh;
}
 
function createQuads(quads: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(quadGeometry.index);
  geometry.setAttribute('position', quadGeometry.position);
  geometry.setAttribute('normal', quadGeometry.normal);
  setAttributes(geometry, quads);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.name = `Primitives (Quads)`;
  return mesh;
}
 
function createTrapeziums(trapeziums: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(trapeziumGeometry.index);
  geometry.setAttribute('position', trapeziumGeometry.position);
  setAttributes(geometry, trapeziums);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.onBeforeRender = () => updateMaterialInverseModelMatrix(material, mesh.matrixWorld);
  mesh.name = `Primitives (Trapeziums)`;
  return mesh;
}
 
function calcLODDistance(size: number, lodLevel: number, numLevels: number): number {
  if (lodLevel >= numLevels - 1) {
    return 0;
  }
  const scaleFactor = 15; // Seems to be a reasonable number
  return size * scaleFactor ** (numLevels - 1 - lodLevel);
}
 
function createTorusSegments(torusSegments: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const sizes = torusSegments.f32Attributes.get('size');
  Iif (!sizes) {
    throw new Error('Torus segments are missing size attribute');
  }
  const biggestTorus = sizes.reduce((acc, size) => Math.max(acc, size));
  const lod = new THREE.LOD();
  lod.name = 'Primitives (TorusSegments)';
 
  for (const [level, torus] of torusLODs.entries()) {
    const geometry = new THREE.InstancedBufferGeometry();
    geometry.setIndex(torus.index);
    geometry.setAttribute('position', torus.position);
    setAttributes(geometry, torusSegments);
 
    const mesh = new THREE.Mesh(geometry, material);
    mesh.frustumCulled = false;
    // TODO consider removing if not used in shader
    mesh.onBeforeRender = () => updateMaterialInverseModelMatrix(material, mesh.matrixWorld);
    mesh.name = `Primitives (TorusSegments) - LOD ${level}`;
 
    lod.addLevel(mesh, calcLODDistance(biggestTorus, level, torusLODs.length));
  }
 
  return lod;
}
 
function createNuts(nuts: PrimitiveAttributes, material: THREE.ShaderMaterial) {
  const geometry = new THREE.InstancedBufferGeometry();
 
  geometry.setIndex(nutGeometry.index);
  geometry.setAttribute('position', nutGeometry.position);
  geometry.setAttribute('normal', nutGeometry.normal);
  setAttributes(geometry, nuts);
 
  const mesh = new THREE.Mesh(geometry, material);
  mesh.frustumCulled = false;
  mesh.name = `Primitives (Nuts)`;
  return mesh;
}
 
function updateMaterialInverseModelMatrix(
  material: THREE.ShaderMaterial | THREE.RawShaderMaterial,
  matrixWorld: THREE.Matrix4
) {
  material.uniforms.inverseModelMatrix.value.getInverse(matrixWorld);
}