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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /*!
* Copyright 2020 Cognite AS
*/
import * as THREE from 'three';
import { vec3, mat4 } from 'gl-matrix';
import { SectorModelTransformation, SectorScene, SectorMetadata, WantedSectors } from '../../../models/cad/types';
import { defaultDetermineSectors } from '../../../models/cad/determineSectors';
import { SectorActivator } from '../../../models/cad/initializeSectorLoader';
import { DetermineSectorsDelegate } from '../../../models/cad/delegates';
import { CadLoadingHints } from '../../../models/cad/CadLoadingHints';
import { CadModel } from '../../../models/cad/CadModel';
import { CadRenderHints } from '../../CadRenderHints';
import { suggestCameraConfig } from '../../../utils/cameraUtils';
import { createThreeJsSectorNode } from './createThreeJsSectorNode';
import { SectorNode } from './SectorNode';
import { fromThreeVector3, fromThreeMatrix, toThreeJsBox3, toThreeVector3, toThreeMatrix4 } from '../utilities';
import { RenderMode } from '../materials';
import { Shading, createDefaultShading } from './shading';
interface CadNodeOptions {
shading?: Shading;
}
export interface SuggestedCameraConfig {
position: THREE.Vector3;
target: THREE.Vector3;
near: number;
far: number;
}
const updateVars = {
cameraPosition: vec3.create(),
cameraModelMatrix: mat4.create(),
projectionMatrix: mat4.create()
};
export class CadNode extends THREE.Object3D {
public readonly rootSector: SectorNode;
public readonly modelTransformation: SectorModelTransformation;
private _determineSectors: DetermineSectorsDelegate;
private _simpleActivator: SectorActivator;
private _detailedActivator: SectorActivator;
private _renderHints: CadRenderHints;
private _loadingHints: CadLoadingHints;
private _renderMode: RenderMode;
private readonly _shading: Shading;
private readonly _sectorScene: SectorScene;
private readonly _previousCameraMatrix = new THREE.Matrix4();
private readonly _boundingBoxNode: THREE.Object3D;
constructor(model: CadModel, options?: CadNodeOptions) {
super();
this.type = 'CadNode';
this.name = 'Sector model';
this._shading = (() => {
if (options && options.shading) {
return options.shading;
}
return createDefaultShading({
color(_treeIndex: number) {
return undefined;
}
});
})();
// TODO if shading changes, this needs to be updated - or perhaps shading needs to be set in the constructor?
const { rootSector, simpleActivator, detailedActivator } = createThreeJsSectorNode(model, this._shading.materials);
const { scene, modelTransformation } = model;
this._sectorScene = scene;
this._determineSectors = defaultDetermineSectors;
this._simpleActivator = simpleActivator;
this._detailedActivator = detailedActivator;
this.modelTransformation = modelTransformation;
// Ensure camera matrix is unequal on first frame
this._previousCameraMatrix.elements[0] = Infinity;
// Prepare renderables
this.rootSector = rootSector;
this.add(rootSector);
this._boundingBoxNode = this.createBoundingBoxNode(scene.sectors);
this.add(this._boundingBoxNode);
// Apply default hints
this._renderHints = {};
this._loadingHints = {};
this._renderMode = RenderMode.Color;
this.renderHints = {};
this.loadingHints = {};
this.renderMode = RenderMode.Color;
const indices = [];
for (let i = 0; i < 100; i++) {
indices.push(i);
}
this._shading.updateNodes(indices);
}
set renderMode(mode: RenderMode) {
this._renderMode = mode;
this._shading.materials.box.uniforms.renderMode.value = mode;
this._shading.materials.circle.uniforms.renderMode.value = mode;
this._shading.materials.generalRing.uniforms.renderMode.value = mode;
this._shading.materials.nut.uniforms.renderMode.value = mode;
this._shading.materials.quad.uniforms.renderMode.value = mode;
this._shading.materials.cone.uniforms.renderMode.value = mode;
this._shading.materials.eccentricCone.uniforms.renderMode.value = mode;
this._shading.materials.sphericalSegment.uniforms.renderMode.value = mode;
this._shading.materials.torusSegment.uniforms.renderMode.value = mode;
this._shading.materials.generalCylinder.uniforms.renderMode.value = mode;
this._shading.materials.trapezium.uniforms.renderMode.value = mode;
this._shading.materials.ellipsoidSegment.uniforms.renderMode.value = mode;
this._shading.materials.instancedMesh.uniforms.renderMode.value = mode;
this._shading.materials.triangleMesh.uniforms.renderMode.value = mode;
this._shading.materials.simple.uniforms.renderMode.value = mode;
}
get renderMode() {
return this._renderMode;
}
set renderHints(hints: Readonly<CadRenderHints>) {
this._renderHints = hints;
this._boundingBoxNode.visible = this.shouldRenderSectorBoundingBoxes;
}
get renderHints(): Readonly<CadRenderHints> {
return this._renderHints;
}
set loadingHints(hints: Readonly<CadLoadingHints>) {
this._loadingHints = hints;
}
get loadingHints(): Readonly<CadLoadingHints> {
return this._loadingHints;
}
set determineSectors(determineSectors: DetermineSectorsDelegate) {
this._determineSectors = determineSectors;
}
get determineSectors() {
return this._determineSectors;
}
private get shouldRenderSectorBoundingBoxes(): boolean {
return this._renderHints.showSectorBoundingBoxes || false;
}
public async update(camera: THREE.PerspectiveCamera): Promise<boolean> {
let needsRedraw = false;
const { cameraPosition, cameraModelMatrix, projectionMatrix } = updateVars;
if (!this._previousCameraMatrix.equals(camera.matrixWorld)) {
camera.matrixWorldInverse.getInverse(camera.matrixWorld);
fromThreeVector3(cameraPosition, camera.position, this.modelTransformation);
fromThreeMatrix(cameraModelMatrix, camera.matrixWorld, this.modelTransformation);
fromThreeMatrix(projectionMatrix, camera.projectionMatrix);
const wantedSectors = await this._determineSectors({
scene: this._sectorScene,
cameraFov: camera.fov,
cameraPosition,
cameraModelMatrix,
projectionMatrix,
loadingHints: this.loadingHints
});
needsRedraw = this._detailedActivator.update(wantedSectors.detailed) || needsRedraw;
needsRedraw = this._simpleActivator.update(wantedSectors.simple) || needsRedraw;
if (this.shouldRenderSectorBoundingBoxes) {
this.updateSectorBoundingBoxes(wantedSectors);
}
this._previousCameraMatrix.copy(camera.matrixWorld);
}
needsRedraw = this._detailedActivator.refresh() || needsRedraw;
needsRedraw = this._simpleActivator.refresh() || needsRedraw;
return needsRedraw;
}
public suggestCameraConfig(): SuggestedCameraConfig {
const { position, target, near, far } = suggestCameraConfig(this._sectorScene.root);
return {
position: toThreeVector3(new THREE.Vector3(), position, this.modelTransformation),
target: toThreeVector3(new THREE.Vector3(), target, this.modelTransformation),
near,
far
};
}
private updateSectorBoundingBoxes(wantedSectors: WantedSectors) {
this._boundingBoxNode.children.forEach(x => {
const sectorId = x.userData.sectorId as number;
const boxHelper = x as THREE.Box3Helper;
boxHelper.visible = wantedSectors.detailed.has(sectorId) || wantedSectors.simple.has(sectorId);
});
}
private createBoundingBoxNode(sectors: Map<number, SectorMetadata>): THREE.Object3D {
function sectorDepth(s: SectorMetadata) {
return s.path.length / 2; // Path are on format 'x/y/z/'
}
const maxColorDepth = [...sectors.values()].reduce((max, s) => Math.max(max, sectorDepth(s)), 0.0);
const from = new THREE.Color(0xff0000);
const to = new THREE.Color(0x00ff00);
const colors = [...Array(maxColorDepth).keys()].map(d => {
const color = new THREE.Color().copy(from);
color.lerpHSL(to, d / (maxColorDepth - 1));
return color;
});
const boxesNode = new THREE.Group();
boxesNode.applyMatrix(toThreeMatrix4(this.modelTransformation.modelMatrix));
boxesNode.name = 'Bounding boxes (for debugging)';
sectors.forEach(sector => {
const bbox = toThreeJsBox3(new THREE.Box3(), sector.bounds);
const color = colors[sectorDepth(sector)];
const boxMesh = new THREE.Box3Helper(bbox, color);
boxMesh.name = `${sector.id}`;
boxMesh.userData.sectorId = sector.id;
boxesNode.add(boxMesh);
});
return boxesNode;
}
}
|