-
Notifications
You must be signed in to change notification settings - Fork 18
/
picking.ts
181 lines (154 loc) · 5.38 KB
/
picking.ts
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
/*!
* Copyright 2020 Cognite AS
*/
import * as THREE from 'three';
import CameraControls from 'camera-controls';
import * as reveal_threejs from '@cognite/reveal/threejs';
import { loadCadModelFromCdfOrUrl, createModelIdentifierFromUrlParams, createClientIfNecessary } from './utils/loaders';
CameraControls.install({ THREE });
function createSphere(point: THREE.Vector3, color: string): THREE.Mesh {
const sphere = new THREE.Mesh(new THREE.SphereGeometry(0.2), new THREE.MeshPhongMaterial({ color }));
sphere.position.copy(point);
return sphere;
}
async function main() {
const urlParams = new URL(location.href).searchParams;
const modelId = createModelIdentifierFromUrlParams(urlParams, '/primitives');
const pickedNodes: Set<number> = new Set();
const pickedObjects: Set<THREE.Mesh> = new Set();
const shading = reveal_threejs.createDefaultShading({
color(treeIndex: number) {
if (pickedNodes.has(treeIndex)) {
return [255, 255, 0, 255];
}
return undefined;
}
});
const scene = new THREE.Scene();
// Add some data for Reveal
const cadModel = await loadCadModelFromCdfOrUrl(modelId, await createClientIfNecessary(modelId));
const cadNode = new reveal_threejs.CadNode(cadModel, { shading });
let modelNeedsUpdate = false;
cadNode.addEventListener('update', () => {
modelNeedsUpdate = true;
});
scene.add(cadNode);
// Add some other geometry
const boxGeometry = new THREE.BoxGeometry(10.0, 4.0, 2.0);
const boxMaterial = new THREE.MeshPhongMaterial({ color: 'red' });
const boxMesh = new THREE.Mesh(boxGeometry, boxMaterial);
scene.add(boxMesh);
// Add some light for the box
for (const position of [
[-20, 40, 50],
[60, 100, -30]
]) {
const light = new THREE.PointLight();
light.position.set(position[0], position[1], position[2]);
scene.add(light);
}
// Set up picking for other objects
const raycaster = new THREE.Raycaster();
// Set up the renderer
const renderer = new THREE.WebGLRenderer();
renderer.setClearColor('#444');
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const { position, target, near, far } = cadNode.suggestCameraConfig();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, near, far);
const controls = new CameraControls(camera, renderer.domElement);
controls.setLookAt(position.x, position.y, position.z, target.x, target.y, target.z);
controls.update(0.0);
camera.updateMatrixWorld();
cadNode.update(camera);
let pickingNeedsUpdate = false;
const clock = new THREE.Clock();
const render = () => {
const delta = clock.getDelta();
const controlsNeedUpdate = controls.update(delta);
if (controlsNeedUpdate) {
cadNode.update(camera);
}
if (controlsNeedUpdate || pickingNeedsUpdate || modelNeedsUpdate) {
renderer.render(scene, camera);
pickingNeedsUpdate = false;
}
requestAnimationFrame(render);
};
render();
const pick = (event: MouseEvent) => {
const rect = renderer.domElement.getBoundingClientRect();
const coords = {
x: ((event.clientX - rect.left) / renderer.domElement.clientWidth) * 2 - 1,
y: ((event.clientY - rect.top) / renderer.domElement.clientHeight) * -2 + 1
};
// Pick in Reveal
const revealPickResult = (() => {
const intersections = reveal_threejs.intersectCadNodes([cadNode], { renderer, camera, coords });
if (intersections.length === 0) {
return;
}
scene.add(createSphere(intersections[0]!.point, 'purple'));
return intersections[0];
})();
// Pick other objects
const otherPickResult = (() => {
raycaster.setFromCamera(coords, camera);
const intersections = raycaster.intersectObjects([boxMesh]);
if (intersections.length === 0) {
return;
}
scene.add(createSphere(intersections[0]!.point, 'orange'));
return intersections[0];
})();
const chosenPickResult = (() => {
if (otherPickResult && revealPickResult) {
if (otherPickResult.distance < revealPickResult.distance) {
return 'other';
} else {
return 'reveal';
}
}
if (otherPickResult) {
return 'other';
}
if (revealPickResult) {
return 'reveal';
}
return 'none';
})();
switch (chosenPickResult) {
case 'other':
const mesh = otherPickResult!.object as THREE.Mesh;
const material = mesh.material as THREE.MeshPhongMaterial;
if (!pickedObjects.has(mesh)) {
pickedObjects.add(mesh);
material.emissive = new THREE.Color('yellow');
} else {
pickedObjects.delete(mesh);
material.emissive = new THREE.Color('black');
}
pickingNeedsUpdate = true;
break;
case 'reveal':
const treeIndex = revealPickResult!.treeIndex;
if (!pickedNodes.has(treeIndex)) {
pickedNodes.add(treeIndex);
} else {
pickedNodes.delete(treeIndex);
}
shading.updateNodes([treeIndex]);
pickingNeedsUpdate = true;
break;
default:
break;
}
};
renderer.domElement.addEventListener('mousedown', pick);
(window as any).scene = scene;
(window as any).THREE = THREE;
(window as any).camera = camera;
(window as any).controls = controls;
(window as any).renderer = renderer;
}
main();