forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgl_depth_texture.html
256 lines (188 loc) · 6.88 KB
/
webgl_depth_texture.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - Depth Texture</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>
#error {
margin: auto;
margin-top: 40px;
display: block;
max-width: 400px;
padding: 20px;
background: #CE0808;
}
</style>
<script id="post-vert" type="x-shader/x-vertex">
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script id="post-frag" type="x-shader/x-fragment">
#include <packing>
varying vec2 vUv;
uniform sampler2D tDiffuse;
uniform sampler2D tDepth;
uniform float cameraNear;
uniform float cameraFar;
float readDepth( sampler2D depthSampler, vec2 coord ) {
float fragCoordZ = texture2D( depthSampler, coord ).x;
float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
}
void main() {
//vec3 diffuse = texture2D( tDiffuse, vUv ).rgb;
float depth = readDepth( tDepth, vUv );
gl_FragColor.rgb = 1.0 - vec3( depth );
gl_FragColor.a = 1.0;
}
</script>
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> webgl - depth texture<br/>
Stores render target depth in a texture attachment.<br/>
Created by <a href="http://twitter.com/mattdesl" target="_blank" rel="noopener">@mattdesl</a>.
<div id="error" style="display: none;">
Your browser does not support <strong>WEBGL_depth_texture</strong>.<br/><br/>
This demo will not work.
</div>
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
let camera, scene, renderer, controls, stats;
let target;
let postScene, postCamera, postMaterial;
let supportsExtension = true;
const params = {
format: THREE.DepthFormat,
type: THREE.UnsignedShortType
};
const formats = { DepthFormat: THREE.DepthFormat, DepthStencilFormat: THREE.DepthStencilFormat };
const types = { UnsignedShortType: THREE.UnsignedShortType, UnsignedIntType: THREE.UnsignedIntType, UnsignedInt248Type: THREE.UnsignedInt248Type };
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer();
if ( renderer.capabilities.isWebGL2 === false && renderer.extensions.has( 'WEBGL_depth_texture' ) === false ) {
supportsExtension = false;
document.querySelector( '#error' ).style.display = 'block';
return;
}
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
stats = new Stats();
document.body.appendChild( stats.dom );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
camera.position.z = 4;
controls = new OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
// Create a render target with depth texture
setupRenderTarget();
// Our scene
setupScene();
// Setup post-processing step
setupPost();
onWindowResize();
window.addEventListener( 'resize', onWindowResize );
//
const gui = new GUI( { width: 300 } );
gui.add( params, 'format', formats ).onChange( setupRenderTarget );
gui.add( params, 'type', types ).onChange( setupRenderTarget );
gui.open();
}
function setupRenderTarget() {
if ( target ) target.dispose();
const format = parseFloat( params.format );
const type = parseFloat( params.type );
target = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight );
target.texture.minFilter = THREE.NearestFilter;
target.texture.magFilter = THREE.NearestFilter;
target.stencilBuffer = ( format === THREE.DepthStencilFormat ) ? true : false;
target.depthTexture = new THREE.DepthTexture();
target.depthTexture.format = format;
target.depthTexture.type = type;
}
function setupPost() {
// Setup post processing stage
postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
postMaterial = new THREE.ShaderMaterial( {
vertexShader: document.querySelector( '#post-vert' ).textContent.trim(),
fragmentShader: document.querySelector( '#post-frag' ).textContent.trim(),
uniforms: {
cameraNear: { value: camera.near },
cameraFar: { value: camera.far },
tDiffuse: { value: null },
tDepth: { value: null }
}
} );
const postPlane = new THREE.PlaneGeometry( 2, 2 );
const postQuad = new THREE.Mesh( postPlane, postMaterial );
postScene = new THREE.Scene();
postScene.add( postQuad );
}
function setupScene() {
scene = new THREE.Scene();
const geometry = new THREE.TorusKnotGeometry( 1, 0.3, 128, 64 );
const material = new THREE.MeshBasicMaterial( { color: 'blue' } );
const count = 50;
const scale = 5;
for ( let i = 0; i < count; i ++ ) {
const r = Math.random() * 2.0 * Math.PI;
const z = ( Math.random() * 2.0 ) - 1.0;
const zScale = Math.sqrt( 1.0 - z * z ) * scale;
const mesh = new THREE.Mesh( geometry, material );
mesh.position.set(
Math.cos( r ) * zScale,
Math.sin( r ) * zScale,
z * scale
);
mesh.rotation.set( Math.random(), Math.random(), Math.random() );
scene.add( mesh );
}
}
function onWindowResize() {
const aspect = window.innerWidth / window.innerHeight;
camera.aspect = aspect;
camera.updateProjectionMatrix();
const dpr = renderer.getPixelRatio();
target.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
if ( ! supportsExtension ) return;
requestAnimationFrame( animate );
// render scene into target
renderer.setRenderTarget( target );
renderer.render( scene, camera );
// render post FX
postMaterial.uniforms.tDiffuse.value = target.texture;
postMaterial.uniforms.tDepth.value = target.depthTexture;
renderer.setRenderTarget( null );
renderer.render( postScene, postCamera );
controls.update(); // required because damping is enabled
stats.update();
}
</script>
</body>
</html>