-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_display_stereo.html
More file actions
250 lines (168 loc) · 6.94 KB
/
webgpu_display_stereo.html
File metadata and controls
250 lines (168 loc) · 6.94 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - stereo effects</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="example.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
<div class="title-wrapper">
<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Stereo Effects</span>
</div>
<small>
Collection of Stereo, Anaglyph and Parallax Barrier effects.
</small>
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/webgpu": "../build/three.webgpu.js",
"three/tsl": "../build/three.tsl.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three/webgpu';
import { stereoPass } from 'three/addons/tsl/display/StereoPassNode.js';
import { anaglyphPass, AnaglyphAlgorithm, AnaglyphColorMode } from 'three/addons/tsl/display/AnaglyphPassNode.js';
import { parallaxBarrierPass } from 'three/addons/tsl/display/ParallaxBarrierPassNode.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { Inspector } from 'three/addons/inspector/Inspector.js';
let camera, scene, renderer, renderPipeline;
let stereo, anaglyph, parallaxBarrier;
let mesh, dummy, timer;
let anaglyphFolder;
const position = new THREE.Vector3();
const params = {
effect: 'stereo',
eyeSep: 0.064,
screenDistance: 3,
anaglyphAlgorithm: 'dubois',
anaglyphColorMode: 'redCyan'
};
const effects = { Stereo: 'stereo', Anaglyph: 'anaglyph', ParallaxBarrier: 'parallaxBarrier' };
const anaglyphAlgorithms = {
'True': AnaglyphAlgorithm.TRUE,
'Grey': AnaglyphAlgorithm.GREY,
'Colour': AnaglyphAlgorithm.COLOUR,
'Half-Colour': AnaglyphAlgorithm.HALF_COLOUR,
'Dubois': AnaglyphAlgorithm.DUBOIS,
'Optimised': AnaglyphAlgorithm.OPTIMISED,
'Compromise': AnaglyphAlgorithm.COMPROMISE
};
const anaglyphColorModes = {
'Red / Cyan': AnaglyphColorMode.RED_CYAN,
'Magenta / Cyan': AnaglyphColorMode.MAGENTA_CYAN,
'Magenta / Green': AnaglyphColorMode.MAGENTA_GREEN
};
init();
function init() {
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.z = 3;
scene = new THREE.Scene();
scene.background = new THREE.CubeTextureLoader()
.setPath( 'textures/cube/Park3Med/' )
.load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
timer = new THREE.Timer();
timer.connect( document );
const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
const textureCube = new THREE.CubeTextureLoader()
.setPath( 'textures/cube/Park3Med/' )
.load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
mesh = new THREE.InstancedMesh( geometry, material, 500 );
mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
dummy = new THREE.Mesh();
for ( let i = 0; i < 500; i ++ ) {
dummy.position.x = Math.random() * 10 - 5;
dummy.position.y = Math.random() * 10 - 5;
dummy.position.z = Math.random() * 10 - 5;
dummy.scale.x = dummy.scale.y = dummy.scale.z = Math.random() * 3 + 1;
dummy.updateMatrix();
mesh.setMatrixAt( i, dummy.matrix );
}
scene.add( mesh );
//
renderer = new THREE.WebGPURenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.inspector = new Inspector();
document.body.appendChild( renderer.domElement );
renderPipeline = new THREE.RenderPipeline( renderer );
stereo = stereoPass( scene, camera );
anaglyph = anaglyphPass( scene, camera );
parallaxBarrier = parallaxBarrierPass( scene, camera );
// Configure anaglyph for physically-correct stereo with zero parallax at scene center
anaglyph.eyeSep = params.eyeSep;
anaglyph.screenDistance = params.screenDistance;
renderPipeline.outputNode = stereo;
const gui = renderer.inspector.createParameters( 'Stereo Settings' );
gui.add( params, 'effect', effects ).onChange( update );
gui.add( params, 'eyeSep', 0.001, 0.15, 0.001 ).onChange( function ( value ) {
stereo.stereo.eyeSep = value;
anaglyph.eyeSep = value; // Anaglyph has direct eyeSep property
parallaxBarrier.stereo.eyeSep = value;
} );
// Anaglyph-specific settings folder
anaglyphFolder = gui.addFolder( 'Anaglyph Options' );
anaglyphFolder.add( params, 'anaglyphAlgorithm', anaglyphAlgorithms ).name( 'Algorithm' ).onChange( function ( value ) {
anaglyph.algorithm = value;
} );
anaglyphFolder.add( params, 'anaglyphColorMode', anaglyphColorModes ).name( 'Color Mode' ).onChange( function ( value ) {
anaglyph.colorMode = value;
} );
anaglyphFolder.add( params, 'screenDistance', 0.5, 10, 0.1 ).name( 'Screen Distance' ).onChange( function ( value ) {
anaglyph.screenDistance = value;
} );
anaglyphFolder.paramList.domElement.style.display = 'none';
window.addEventListener( 'resize', onWindowResize );
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 1;
controls.maxDistance = 25;
}
function update( value ) {
if ( value === 'stereo' ) {
renderPipeline.outputNode = stereo;
anaglyphFolder.paramList.domElement.style.display = 'none';
} else if ( value === 'anaglyph' ) {
renderPipeline.outputNode = anaglyph;
anaglyphFolder.paramList.domElement.style.display = '';
} else if ( value === 'parallaxBarrier' ) {
renderPipeline.outputNode = parallaxBarrier;
anaglyphFolder.paramList.domElement.style.display = 'none';
}
renderPipeline.needsUpdate = true;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function extractPosition( matrix, position ) {
position.x = matrix.elements[ 12 ];
position.y = matrix.elements[ 13 ];
position.z = matrix.elements[ 14 ];
}
function animate() {
timer.update();
const elapsedTime = timer.getElapsed() * 0.1;
for ( let i = 0; i < mesh.count; i ++ ) {
mesh.getMatrixAt( i, dummy.matrix );
extractPosition( dummy.matrix, position );
position.x = 5 * Math.cos( elapsedTime + i );
position.y = 5 * Math.sin( elapsedTime + i * 1.1 );
dummy.matrix.setPosition( position );
mesh.setMatrixAt( i, dummy.matrix );
mesh.instanceMatrix.needsUpdate = true;
}
renderPipeline.render();
}
</script>
</body>
</html>