Skip to content

Commit 28c2613

Browse files
committed
first pass at occlusion in gpurenderer
1 parent 0d1e2d7 commit 28c2613

File tree

13 files changed

+450
-20
lines changed

13 files changed

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