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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /*!
* Copyright 2020 Cognite AS
*/
import { CadNode } from './CadNode';
import { pickPixelColor, PickingInput } from '../pickPixelColor';
import { RenderMode } from '../materials';
import * as THREE from 'three';
export interface TreeIndexPickingInput extends PickingInput {
cadNode: CadNode;
}
export interface IntersectCadNodesInput {
coords: {
x: number;
y: number;
};
camera: THREE.PerspectiveCamera;
renderer: THREE.WebGLRenderer;
}
export interface IntersectCadNodesResult {
distance: number;
point: THREE.Vector3;
treeIndex: number;
object: THREE.Object3D; // always CadNode
}
const clearColor = new THREE.Color('black');
const clearAlpha = 0.0;
export function intersectCadNodes(cadNodes: CadNode[], input: IntersectCadNodesInput): IntersectCadNodesResult[] {
const results: IntersectCadNodesResult[] = [];
for (const cadNode of cadNodes) {
const result = intersectCadNode(cadNode, input);
if (result) {
results.push(result);
}
}
return results;
}
export function intersectCadNode(cadNode: CadNode, input: IntersectCadNodesInput): IntersectCadNodesResult | undefined {
const { camera, coords, renderer } = input;
const pickingScene = new THREE.Scene();
// TODO consider case where parent does not exist
// TODO add warning if parent has transforms
const oldParent = cadNode.parent!;
pickingScene.add(cadNode);
const pickInput = {
coords,
camera,
renderer,
scene: pickingScene,
cadNode
};
const treeIndex = pickTreeIndex(pickInput);
if (treeIndex === undefined) {
oldParent.add(cadNode);
return;
}
const depth = pickDepth(pickInput);
const viewZ = perspectiveDepthToViewZ(depth, camera.near, camera.far);
const point = getPosition(pickInput, viewZ);
const distance = new THREE.Vector3().subVectors(point, camera.position).length();
oldParent.add(cadNode);
return {
distance,
point,
treeIndex,
object: cadNode
};
}
function pickTreeIndex(input: TreeIndexPickingInput): number | undefined {
const { cadNode } = input;
const previousRenderMode = cadNode.renderMode;
cadNode.renderMode = RenderMode.TreeIndex;
const pixelBuffer = pickPixelColor(input, clearColor, clearAlpha);
cadNode.renderMode = previousRenderMode;
if (pixelBuffer[3] === 0) {
return;
}
const treeIndex = pixelBuffer[0] * 255 * 255 + pixelBuffer[1] * 255 + pixelBuffer[2];
return treeIndex;
}
const rgbaVector = new THREE.Vector4();
const unpackDownscale = 255 / 256;
const unpackFactors = new THREE.Vector4(
unpackDownscale / (256 * 256 * 256),
unpackDownscale / (256 * 256),
unpackDownscale / 256,
unpackDownscale
);
function unpackRGBAToDepth(rgbaBuffer: Uint8Array) {
return rgbaVector
.fromArray(rgbaBuffer)
.multiplyScalar(1 / 255)
.dot(unpackFactors);
}
function perspectiveDepthToViewZ(invClipZ: number, near: number, far: number) {
return (near * far) / ((far - near) * invClipZ - far);
}
function pickDepth(input: TreeIndexPickingInput): number {
const { cadNode } = input;
const previousRenderMode = cadNode.renderMode;
cadNode.renderMode = RenderMode.Depth;
const pixelBuffer = pickPixelColor(input, clearColor, clearAlpha);
cadNode.renderMode = previousRenderMode;
const depth = unpackRGBAToDepth(pixelBuffer);
return depth;
}
const projInv = new THREE.Matrix4();
function getPosition(input: TreeIndexPickingInput, viewZ: number): THREE.Vector3 {
const { camera, coords } = input;
const position = new THREE.Vector3();
projInv.getInverse(camera.projectionMatrix);
position.set(coords.x, coords.y, 0.5).applyMatrix4(projInv);
position.multiplyScalar(viewZ / position.z);
position.applyMatrix4(camera.matrixWorld);
return position;
}
|