-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.html
194 lines (189 loc) · 6.44 KB
/
index2.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
<!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">
<style>
body {
color: #fff;
font-family:Monospace;
font-size:13px;
text-align:center;
background-color: #000;
margin: 0px;
overflow: hidden;
}
a {
color: #B2E9FF;
font-weight: bold;
pointer-events: auto;
}
canvas {
position: absolute;
top: 0;
left: 0;
}
#info {
pointer-events: none;
position: absolute;
left: 0;
top: 0px; width: 100%;
padding: 5px;
display: inline-block;
}
#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 = vec3(depth);
gl_FragColor.a = 1.0;
}
</script>
</head>
<body>
<canvas></canvas>
<div id="info">
</div>
<script src="/app/three.min.js"></script>
<script src="/js/shaders/SSAOShader.js"></script>
<script src="/js/controls/OrbitControl.js"></script>
<script>
var camera, scene, renderer, controls;
var target;
var postScene, postCamera;
var supportsExtension = true;
init();
animate();
function init() {
var canvas = document.querySelector('canvas');
var gl;
try {
gl = canvas.getContext('webgl2');
} catch (err) {
console.error(err);
}
var isWebGL2 = Boolean(gl);
renderer = new THREE.WebGLRenderer( {
canvas: canvas,
context: gl
} );
if ( !renderer.extensions.get('WEBGL_depth_texture') ) {
supportsExtension = false;
document.querySelector('#error').style.display = 'block';
return;
}
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
//
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
camera.position.z = -4;
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.rotateSpeed = 0.35;
// Create a multi render target with Float buffers
target = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight );
target.texture.format = THREE.RGBFormat;
target.texture.minFilter = THREE.NearestFilter;
target.texture.magFilter = THREE.NearestFilter;
target.texture.generateMipmaps = false;
target.stencilBuffer = false;
target.depthBuffer = true;
target.depthTexture = new THREE.DepthTexture();
target.depthTexture.type = isWebGL2 ? THREE.FloatType : THREE.UnsignedShortType;
// Our scene
scene = new THREE.Scene();
setupScene();
// Setup post-processing step
setupPost();
onWindowResize();
window.addEventListener( 'resize', onWindowResize, false );
}
function setupPost () {
// Setup post processing stage
postCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
var 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: target.texture },
tDepth: { value: target.depthTexture }
}
});
var postPlane = new THREE.PlaneGeometry(2, 2);
var postQuad = new THREE.Mesh(postPlane, postMaterial);
postScene = new THREE.Scene();
postScene.add(postQuad);
}
function setupScene () {
var diffuse = new THREE.TextureLoader().load('textures/brick_diffuse.jpg');
diffuse.wrapS = diffuse.wrapT = THREE.RepeatWrapping;
// Setup some geometries
var geometry = new THREE.TorusKnotGeometry(1, 0.3, 128, 64);
var material = new THREE.MeshBasicMaterial({ color: 'blue' })
var count = 50;
var scale = 5;
for ( var i = 0; i < count; i ++ ) {
var r = Math.random() * 2.0 * Math.PI;
var z = (Math.random() * 2.0) - 1.0;
var zScale = Math.sqrt(1.0 - z * z) * scale;
var 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() {
var aspect = window.innerWidth / window.innerHeight;
camera.aspect = aspect;
camera.updateProjectionMatrix();
var dpr = renderer.getPixelRatio();
target.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
if ( !supportsExtension ) return;
requestAnimationFrame( animate );
controls.update();
// render scene into target
renderer.render( scene, camera, target );
// render post FX
renderer.render( postScene, postCamera );
}
</script>
</body>
</html>