forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_postprocessing_procedural.html
173 lines (132 loc) · 4.57 KB
/
webgl_postprocessing_procedural.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - postprocessing procedural effects</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 {
margin: 0px;
background-color: #000;
overflow: hidden;
font-family:Monospace;
font-size:13px;
margin: 0px;
text-align:center;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
display:block;
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Procedural Effects Example by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a><br/><br/>
</div>
<script id="procedural-vert" type="x-shader/x-vertex">
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script id="noiseRandom1D-frag" type="x-shader/x-fragment">
#include <common>
varying vec2 vUv;
void main() {
gl_FragColor.xyz = vec3( rand( vUv ) );
gl_FragColor.w = 1.0;
}
</script>
<script id="noiseRandom2D-frag" type="x-shader/x-fragment">
#include <common>
varying vec2 vUv;
void main() {
vec2 rand2 = vec2( rand( vUv ), rand( vUv + vec2( 0.4, 0.6 ) ) );
gl_FragColor.xyz = mix( mix( vec3( 1.0, 1.0, 1.0 ), vec3( 0.0, 0.0, 1.0 ), rand2.x ), vec3( 0.0 ), rand2.y );
gl_FragColor.w = 1.0;
}
</script>
<script id="noiseRandom3D-frag" type="x-shader/x-fragment">
#include <common>
varying vec2 vUv;
void main() {
vec3 rand3 = vec3( rand( vUv ), rand( vUv + vec2( 0.4, 0.6 ) ), rand( vUv + vec2( 0.6, 0.4 ) ) );
gl_FragColor.xyz = rand3;
gl_FragColor.w = 1.0;
}
</script>
<div id="container"></div>
<script src="../build/three.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src="js/libs/dat.gui.min.js"></script>
<script>
var postCamera, postScene, renderer;
var postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad;
var stats;
var params = { procedure: 'noiseRandom3D' };
init();
animate();
initGui();
// Init gui
function initGui() {
var gui = new dat.GUI();
gui.add( params, 'procedure', [ 'noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D' ] );
}
function init() {
var container = document.getElementById( "container" );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
stats = new Stats();
container.appendChild( stats.dom );
// Setup post processing stage
postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
noiseRandom1DMaterial = new THREE.ShaderMaterial( {
vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
fragmentShader: document.querySelector( '#noiseRandom1D-frag' ).textContent.trim()
} );
noiseRandom2DMaterial = new THREE.ShaderMaterial( {
vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
fragmentShader: document.querySelector( '#noiseRandom2D-frag' ).textContent.trim()
} );
noiseRandom3DMaterial = new THREE.ShaderMaterial( {
vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
fragmentShader: document.querySelector( '#noiseRandom3D-frag' ).textContent.trim()
} );
postMaterial = noiseRandom3DMaterial;
var postPlane = new THREE.PlaneBufferGeometry( 2, 2 );
postQuad = new THREE.Mesh( postPlane, postMaterial );
postScene = new THREE.Scene();
postScene.add( postQuad );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
var width = window.innerWidth;
var height = window.innerHeight;
postCamera.aspect = width / height;
postCamera.updateProjectionMatrix();
renderer.setSize( width, height );
}
function animate() {
requestAnimationFrame( animate );
switch ( params.procedure ) {
case 'noiseRandom1D': postMaterial = noiseRandom1DMaterial; break;
case 'noiseRandom2D': postMaterial = noiseRandom2DMaterial; break;
case 'noiseRandom3D': postMaterial = noiseRandom3DMaterial; break;
}
postQuad.material = postMaterial;
// render post FX
renderer.render( postScene, postCamera );
stats.update();
}
</script>
</body>
</html>