Skip to content

Commit 004de43

Browse files
committed
first pass at occlusion in gpurenderer
1 parent 0d1e2d7 commit 004de43

File tree

13 files changed

+452
-20
lines changed

13 files changed

+452
-20
lines changed

examples/files.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@
464464
"webgpu_water",
465465
"webgpu_xr_rollercoaster",
466466
"webgpu_xr_cubes",
467+
"webgpu_xr_occluded_cubes",
467468
"webgpu_xr_native_layers"
468469
],
469470
"webaudio": [
Loading
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>three.js xr - cubes</title>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
7+
<link type="text/css" rel="stylesheet" href="main.css">
8+
</head>
9+
<body>
10+
11+
<div id="info">
12+
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> xr - interactive cubes
13+
</div>
14+
15+
<script type="importmap">
16+
{
17+
"imports": {
18+
"three": "../build/three.webgpu.js",
19+
"three/webgpu": "../build/three.webgpu.js",
20+
"three/tsl": "../build/three.tsl.js",
21+
"three/addons/": "./jsm/"
22+
}
23+
}
24+
</script>
25+
26+
<script type="module">
27+
28+
import * as THREE from 'three';
29+
30+
import { BoxLineGeometry } from 'three/addons/geometries/BoxLineGeometry.js';
31+
import { XRButton } from 'three/addons/webxr/XRButton.js';
32+
import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
33+
34+
const clock = new THREE.Clock();
35+
36+
let container;
37+
let camera, scene, raycaster, renderer;
38+
39+
let room;
40+
let masknode;
41+
42+
let controller, controllerGrip;
43+
let INTERSECTED;
44+
45+
init();
46+
47+
function init() {
48+
49+
container = document.createElement( 'div' );
50+
document.body.appendChild( container );
51+
52+
scene = new THREE.Scene();
53+
scene.background = new THREE.Color( 0x505050 );
54+
55+
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
56+
camera.position.set( 0, 1.6, 3 );
57+
scene.add( camera );
58+
59+
room = new THREE.Object3D();
60+
scene.add( room );
61+
62+
scene.add( new THREE.HemisphereLight( 0xa5a5a5, 0x898989, 3 ) );
63+
64+
const light = new THREE.DirectionalLight( 0xffffff, 3 );
65+
light.position.set( 1, 1, 1 ).normalize();
66+
scene.add( light );
67+
68+
const geometry = new THREE.BoxGeometry( 0.15, 0.15, 0.15 );
69+
70+
for ( let i = 0; i < 200; i ++ ) {
71+
72+
const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
73+
74+
object.position.x = Math.random() * 4 - 2;
75+
object.position.y = Math.random() * 4;
76+
object.position.z = Math.random() * 4 - 2;
77+
78+
object.rotation.x = Math.random() * 2 * Math.PI;
79+
object.rotation.y = Math.random() * 2 * Math.PI;
80+
object.rotation.z = Math.random() * 2 * Math.PI;
81+
82+
object.scale.x = Math.random() + 0.5;
83+
object.scale.y = Math.random() + 0.5;
84+
object.scale.z = Math.random() + 0.5;
85+
86+
object.userData.velocity = new THREE.Vector3();
87+
object.userData.velocity.x = Math.random() * 0.01 - 0.005;
88+
object.userData.velocity.y = Math.random() * 0.01 - 0.005;
89+
object.userData.velocity.z = Math.random() * 0.01 - 0.005;
90+
91+
room.add( object );
92+
93+
}
94+
95+
raycaster = new THREE.Raycaster();
96+
97+
renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: true, colorBufferType: THREE.UnsignedByteType, multiview: true } );
98+
renderer.setPixelRatio( window.devicePixelRatio );
99+
renderer.setSize( window.innerWidth, window.innerHeight );
100+
renderer.setAnimationLoop( animate );
101+
renderer.xr.enabled = true;
102+
container.appendChild( renderer.domElement );
103+
104+
//
105+
106+
function onSelectStart() {
107+
108+
this.userData.isSelecting = true;
109+
110+
}
111+
112+
function onSelectEnd() {
113+
114+
this.userData.isSelecting = false;
115+
116+
}
117+
118+
controller = renderer.xr.getController( 0 );
119+
controller.addEventListener( 'selectstart', onSelectStart );
120+
controller.addEventListener( 'selectend', onSelectEnd );
121+
controller.addEventListener( 'connected', function ( event ) {
122+
123+
const targetRayMode = event.data.targetRayMode;
124+
125+
if ( targetRayMode === 'tracked-pointer' || targetRayMode === 'gaze' ) {
126+
127+
this.add( buildController( event.data ) );
128+
129+
}
130+
131+
} );
132+
controller.addEventListener( 'disconnected', function () {
133+
134+
this.remove( this.children[ 0 ] );
135+
136+
} );
137+
scene.add( controller );
138+
139+
const controllerModelFactory = new XRControllerModelFactory();
140+
141+
controllerGrip = renderer.xr.getControllerGrip( 0 );
142+
controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
143+
scene.add( controllerGrip );
144+
145+
window.addEventListener( 'resize', onWindowResize );
146+
147+
//
148+
149+
document.body.appendChild( XRButton.createButton( renderer , {
150+
'optionalFeatures': [ 'depth-sensing' ],
151+
'depthSensing': { 'usagePreference': [ 'gpu-optimized' ], 'dataFormatPreference': [] } } ) );
152+
153+
}
154+
155+
function buildController( data ) {
156+
157+
let geometry, material;
158+
159+
switch ( data.targetRayMode ) {
160+
161+
case 'tracked-pointer':
162+
163+
geometry = new THREE.BufferGeometry();
164+
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
165+
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
166+
167+
material = new THREE.LineBasicMaterial( { vertexColors: true, blending: THREE.AdditiveBlending } );
168+
169+
return new THREE.Line( geometry, material );
170+
171+
case 'gaze':
172+
173+
geometry = new THREE.RingGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
174+
material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
175+
return new THREE.Mesh( geometry, material );
176+
177+
}
178+
179+
}
180+
181+
function onWindowResize() {
182+
183+
camera.aspect = window.innerWidth / window.innerHeight;
184+
camera.updateProjectionMatrix();
185+
186+
renderer.setSize( window.innerWidth, window.innerHeight );
187+
188+
}
189+
190+
//
191+
192+
function animate() {
193+
194+
const delta = clock.getDelta() * 60;
195+
196+
renderer.xr.applyOcclusion( scene );
197+
198+
if ( controller.userData.isSelecting === true ) {
199+
200+
const cube = room.children[ 0 ];
201+
room.remove( cube );
202+
203+
cube.position.copy( controller.position );
204+
cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
205+
cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
206+
cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
207+
cube.userData.velocity.applyQuaternion( controller.quaternion );
208+
room.add( cube );
209+
210+
}
211+
212+
// find intersections
213+
214+
raycaster.setFromXRController( controller );
215+
216+
const intersects = raycaster.intersectObjects( room.children, false );
217+
218+
if ( intersects.length > 0 ) {
219+
220+
if ( INTERSECTED != intersects[ 0 ].object ) {
221+
222+
if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
223+
224+
INTERSECTED = intersects[ 0 ].object;
225+
INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
226+
INTERSECTED.material.emissive.setHex( 0xff0000 );
227+
228+
}
229+
230+
} else {
231+
232+
if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
233+
234+
INTERSECTED = undefined;
235+
236+
}
237+
238+
// Keep cubes inside room
239+
240+
for ( let i = 0; i < room.children.length; i ++ ) {
241+
242+
const cube = room.children[ i ];
243+
244+
cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
245+
246+
cube.position.add( cube.userData.velocity );
247+
248+
if ( cube.position.x < - 3 || cube.position.x > 3 ) {
249+
250+
cube.position.x = THREE.MathUtils.clamp( cube.position.x, - 3, 3 );
251+
cube.userData.velocity.x = - cube.userData.velocity.x;
252+
253+
}
254+
255+
if ( cube.position.y < 0 || cube.position.y > 6 ) {
256+
257+
cube.position.y = THREE.MathUtils.clamp( cube.position.y, 0, 6 );
258+
cube.userData.velocity.y = - cube.userData.velocity.y;
259+
260+
}
261+
262+
if ( cube.position.z < - 3 || cube.position.z > 3 ) {
263+
264+
cube.position.z = THREE.MathUtils.clamp( cube.position.z, - 3, 3 );
265+
cube.userData.velocity.z = - cube.userData.velocity.z;
266+
267+
}
268+
269+
cube.rotation.x += cube.userData.velocity.x * 2 * delta;
270+
cube.rotation.y += cube.userData.velocity.y * 2 * delta;
271+
cube.rotation.z += cube.userData.velocity.z * 2 * delta;
272+
273+
}
274+
275+
renderer.render( scene, camera );
276+
277+
}
278+
279+
</script>
280+
</body>
281+
</html>

src/Three.TSL.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export const bool = TSL.bool;
8888
export const buffer = TSL.buffer;
8989
export const bufferAttribute = TSL.bufferAttribute;
9090
export const bumpMap = TSL.bumpMap;
91+
export const builtin = TSL.builtin;
9192
export const burn = TSL.burn;
9293
export const bvec2 = TSL.bvec2;
9394
export const bvec3 = TSL.bvec3;
@@ -423,6 +424,7 @@ export const samplerComparison = TSL.samplerComparison;
423424
export const saturate = TSL.saturate;
424425
export const saturation = TSL.saturation;
425426
export const screen = TSL.screen;
427+
export const screenRaw = TSL.screenRaw;
426428
export const screenCoordinate = TSL.screenCoordinate;
427429
export const screenSize = TSL.screenSize;
428430
export const screenUV = TSL.screenUV;

src/nodes/TSL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export * from './tsl/TSLBase.js';
5050
export * from './accessors/AccessorsUtils.js';
5151
export * from './accessors/Arrays.js';
5252
export * from './accessors/UniformArrayNode.js';
53+
export * from './accessors/BuiltinNode.js';
5354
export * from './accessors/Bitangent.js';
5455
export * from './accessors/BufferAttributeNode.js';
5556
export * from './accessors/BufferNode.js';

src/nodes/core/NodeBuilder.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,12 @@ class NodeBuilder {
979979

980980
}
981981

982+
getFrag() {
983+
984+
console.warn( 'Abstract function.' );
985+
986+
}
987+
982988
/**
983989
* Whether to flip texture data along its vertical axis or not. WebGL needs
984990
* this method evaluate to `true`, WebGPU to `false`.

src/nodes/display/ScreenNode.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Node from '../core/Node.js';
22
import { NodeUpdateType } from '../core/constants.js';
33
import { uniform } from '../core/UniformNode.js';
4-
import { Fn, nodeImmutable, vec2 } from '../tsl/TSLBase.js';
4+
import { Fn, nodeImmutable, vec2, vec4 } from '../tsl/TSLBase.js';
55

66
import { Vector2 } from '../../math/Vector2.js';
77
import { Vector4 } from '../../math/Vector4.js';
@@ -26,7 +26,7 @@ class ScreenNode extends Node {
2626
/**
2727
* Constructs a new screen node.
2828
*
29-
* @param {('coordinate'|'viewport'|'size'|'uv')} scope - The node's scope.
29+
* @param {('coordinate'|'viewport'|'size'|'uv'|'raw')} scope - The node's scope.
3030
*/
3131
constructor( scope ) {
3232

@@ -62,7 +62,7 @@ class ScreenNode extends Node {
6262
*/
6363
getNodeType() {
6464

65-
if ( this.scope === ScreenNode.VIEWPORT ) return 'vec4';
65+
if ( this.scope === ScreenNode.VIEWPORT || this.scope === ScreenNode.RAW ) return 'vec4';
6666
else return 'vec2';
6767

6868
}
@@ -143,6 +143,10 @@ class ScreenNode extends Node {
143143

144144
output = uniform( viewportVec || ( viewportVec = new Vector4() ) );
145145

146+
} else if ( scope === ScreenNode.RAW ) {
147+
148+
output = vec4( screenRaw.div( screenSize ) );
149+
146150
} else {
147151

148152
output = vec2( screenCoordinate.div( screenSize ) );
@@ -155,7 +159,11 @@ class ScreenNode extends Node {
155159

156160
generate( builder ) {
157161

158-
if ( this.scope === ScreenNode.COORDINATE ) {
162+
if ( this.scope === ScreenNode.RAW ) {
163+
164+
return builder.getFrag();
165+
166+
} else if ( this.scope === ScreenNode.COORDINATE ) {
159167

160168
let coord = builder.getFragCoord();
161169

@@ -183,6 +191,7 @@ ScreenNode.COORDINATE = 'coordinate';
183191
ScreenNode.VIEWPORT = 'viewport';
184192
ScreenNode.SIZE = 'size';
185193
ScreenNode.UV = 'uv';
194+
ScreenNode.RAW = 'raw';
186195

187196
export default ScreenNode;
188197

@@ -230,6 +239,8 @@ export const viewport = /*@__PURE__*/ nodeImmutable( ScreenNode, ScreenNode.VIEW
230239
*/
231240
export const viewportSize = viewport.zw;
232241

242+
export const screenRaw = /*@__PURE__*/ nodeImmutable( ScreenNode, ScreenNode.RAW );//.div( vec4( viewport.z, viewport.w, 1, 1 ) );
243+
233244
/**
234245
* TSL object that represents the current `x`/`y` pixel position on the viewport in physical pixel units.
235246
*

0 commit comments

Comments
 (0)