-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraycasterWithGPU.html
304 lines (215 loc) · 7.62 KB
/
raycasterWithGPU.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - interactive cubes (gpu)</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
body {
background-color: #fff;
color: #444;
}
a {
color: #08f;
}
</style>
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - gpu picking
</div>
<div id="container"></div>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/stats.module.js';
import { TrackballControls } from 'three/addons/TrackballControls.js';
import * as BufferGeometryUtils from 'three/addons/BufferGeometryUtils.js';
let container, stats;
let camera, controls, scene, renderer;
let pickingTexture, pickingScene;
let highlightBox;
let chechTextureMesh
const pickingData = [];
const pointer = new THREE.Vector2();
const offset = new THREE.Vector3( 10, 10, 10 );
const clearColor = new THREE.Color();
init();
animate();
function init() {
container = document.getElementById( 'container' );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xffffff );
scene.add( new THREE.AmbientLight( 0xcccccc ) );
const light = new THREE.DirectionalLight( 0xffffff, 3 );
light.position.set( 0, 500, 2000 );
scene.add( light );
const defaultMaterial = new THREE.MeshPhongMaterial( {
color: 0xffffff,
flatShading: true,
vertexColors: true,
shininess: 0
} );
// set up the picking texture to use a 32 bit integer so we can write and read integer ids from it
pickingScene = new THREE.Scene();
pickingTexture = new THREE.WebGLRenderTarget( 1, 1, {
type: THREE.IntType,
format: THREE.RGBAIntegerFormat,
internalFormat: 'RGBA32I',
} );
const pickingMaterial = new THREE.ShaderMaterial( {
glslVersion: THREE.GLSL3,
vertexShader: /* glsl */`
attribute int id;
flat varying int vid;
void main() {
vec3 pos = position;
vid = id;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader: /* glsl */`
layout(location = 0) out int out_id;
flat varying int vid;
void main() {
out_id = vid;
}
`,
} );
function applyId( geometry, id ) {
const position = geometry.attributes.position;
const array = new Int16Array( position.count );
array.fill( id );
const bufferAttribute = new THREE.Int16BufferAttribute( array, 1, false );
bufferAttribute.gpuType = THREE.IntType;
geometry.setAttribute( 'id', bufferAttribute );
}
function applyVertexColors( geometry, color ) {
const position = geometry.attributes.position;
const colors = [];
for ( let i = 0; i < position.count; i ++ ) {
colors.push( color.r, color.g, color.b );
}
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
}
const geometries = [];
const matrix = new THREE.Matrix4();
const quaternion = new THREE.Quaternion();
const color = new THREE.Color();
for ( let i = 0; i < 100; i ++ ) {
const geometry = new THREE.BoxGeometry();
const position = new THREE.Vector3();
position.x = Math.random() * 10000 - 5000;
position.y = Math.random() * 6000 - 3000;
position.z = Math.random() * 8000 - 4000;
const rotation = new THREE.Euler();
rotation.x = Math.random() * 2 * Math.PI;
rotation.y = Math.random() * 2 * Math.PI;
rotation.z = Math.random() * 2 * Math.PI;
const scale = new THREE.Vector3();
scale.x = Math.random() * 200 + 100;
scale.y = Math.random() * 200 + 100;
scale.z = Math.random() * 200 + 100;
quaternion.setFromEuler( rotation );
matrix.compose( position, quaternion, scale );
geometry.applyMatrix4( matrix );
// give the geometry's vertices a random color to be displayed and an integer
// identifier as a vertex attribute so boxes can be identified after being merged.
applyVertexColors( geometry, color.setHex( Math.random() * 0xffffff ) );
applyId( geometry, i );
geometries.push( geometry );
pickingData[ i ] = {
position: position,
rotation: rotation,
scale: scale
};
}
const mergedGeometry = BufferGeometryUtils.mergeGeometries( geometries );
scene.add( new THREE.Mesh( mergedGeometry, defaultMaterial ) );
pickingScene.add( new THREE.Mesh( mergedGeometry, pickingMaterial ) );
highlightBox = new THREE.Mesh(
new THREE.BoxGeometry(),
new THREE.MeshLambertMaterial( { color: 0xffff00 } )
);
scene.add( highlightBox );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
controls = new TrackballControls( camera, renderer.domElement );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
stats = new Stats();
container.appendChild( stats.dom );
chechTextureMesh = new THREE.Mesh(new THREE.PlaneGeometry(500,500,16,16),new THREE.MeshBasicMaterial({color:'blue'}))
scene.add(chechTextureMesh)
renderer.domElement.addEventListener( 'pointermove', onPointerMove );
}
//
function onPointerMove( e ) {
pointer.x = e.clientX;
pointer.y = e.clientY;
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function pick() {
// render the picking scene off-screen
// set the view offset to represent just a single pixel under the mouse
const dpr = window.devicePixelRatio;
camera.setViewOffset(
renderer.domElement.width, renderer.domElement.height,
Math.floor( pointer.x * dpr ), Math.floor( pointer.y * dpr ),
1, 1
);
// render the scene
renderer.setRenderTarget( pickingTexture );
// clear the background to - 1 meaning no item was hit
clearColor.setRGB( - 1, - 1, - 1 );
renderer.setClearColor( clearColor );
renderer.render( pickingScene, camera );
// clear the view offset so rendering returns to normal
camera.clearViewOffset();
// create buffer for reading single pixel
const pixelBuffer = new Int32Array( 4 );
// read the pixel
renderer.readRenderTargetPixels( pickingTexture, 0, 0, 1, 1, pixelBuffer );
const id = pixelBuffer[ 0 ];
if ( id !== - 1 ) {
// move our highlightBox so that it surrounds the picked object
const data = pickingData[ id ];
highlightBox.position.copy( data.position );
highlightBox.rotation.copy( data.rotation );
highlightBox.scale.copy( data.scale ).add( offset );
highlightBox.visible = true;
} else {
highlightBox.visible = false;
}
}
function render() {
controls.update();
pick();
//if(chechTextureMesh) chechTextureMesh.material.map = pickingTexture.textures[0]
renderer.setRenderTarget( null );
renderer.render( scene, camera );
}
</script>
</body>
</html>