forked from Soft8Soft/verge3d-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl2_multiple_rendertargets.html
272 lines (190 loc) · 7.08 KB
/
webgl2_multiple_rendertargets.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webgl - Multiple Render Targets</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">
<!-- Write to G-Buffer -->
<script id="gbuffer-vert" type="x-shader/x-vertex">
in vec3 position;
in vec3 normal;
in vec2 uv;
out vec3 vNormal;
out vec2 vUv;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;
void main() {
vUv = uv;
// get smooth normals
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
vec3 transformedNormal = normalMatrix * normal;
vNormal = normalize(transformedNormal);
gl_Position = projectionMatrix * mvPosition;
}
</script>
<script id="gbuffer-frag" type="x-shader/x-fragment">
precision highp float;
precision highp int;
layout(location = 0) out vec4 gColor;
layout(location = 1) out vec4 gNormal;
uniform sampler2D tDiffuse;
uniform vec2 repeat;
in vec3 vNormal;
in vec2 vUv;
void main() {
// write color to G-Buffer
gColor = texture(tDiffuse, vUv * repeat);
// write normals to G-Buffer
gNormal = vec4(normalize(vNormal), 0.0);
}
</script>
<!-- Read G-Buffer and render to screen -->
<script id="render-vert" type="x-shader/x-vertex">
in vec3 position;
in vec2 uv;
out vec2 vUv;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script id="render-frag" type="x-shader/x-fragment">
precision highp float;
precision highp int;
layout(location = 0) out vec4 pc_FragColor;
in vec2 vUv;
uniform sampler2D tDiffuse;
uniform sampler2D tNormal;
void main() {
vec3 diffuse = texture(tDiffuse, vUv).rgb;
vec3 normal = texture(tNormal, vUv).rgb;
pc_FragColor.rgb = mix(diffuse, normal, step(0.5, vUv.x));
pc_FragColor.a = 1.0;
}
</script>
</head>
<body>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank">threejs</a> webgl - Multiple RenderTargets
</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": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import WebGL from 'v3d/addons/capabilities/WebGL.js';
import { OrbitControls } from 'v3d/addons/controls/OrbitControls.js';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
let camera, scene, renderer, controls;
let renderTarget;
let postScene, postCamera;
const parameters = {
samples: 4,
wireframe: false
};
const gui = new GUI();
gui.add(parameters, 'samples', 0, 4).step(1);
gui.add(parameters, 'wireframe');
gui.onChange(render);
init();
function init() {
if (WebGL.isWebGL2Available() === false) {
document.body.appendChild(WebGL.getWebGL2ErrorMessage());
return;
}
renderer = new v3d.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Create a multi render target with Float buffers
renderTarget = new v3d.WebGLMultipleRenderTargets(
window.innerWidth * window.devicePixelRatio,
window.innerHeight * window.devicePixelRatio,
2
);
for (let i = 0, il = renderTarget.texture.length; i < il; i++) {
renderTarget.texture[i].minFilter = v3d.NearestFilter;
renderTarget.texture[i].magFilter = v3d.NearestFilter;
}
// Name our G-Buffer attachments for debugging
renderTarget.texture[0].name = 'diffuse';
renderTarget.texture[1].name = 'normal';
// Scene setup
scene = new v3d.Scene();
scene.background = new v3d.Color(0x222222);
camera = new v3d.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 50);
camera.position.z = 4;
const loader = new v3d.TextureLoader();
const diffuse = loader.load('textures/hardwood2_diffuse.jpg', render);
diffuse.wrapS = v3d.RepeatWrapping;
diffuse.wrapT = v3d.RepeatWrapping;
scene.add(new v3d.Mesh(
new v3d.TorusKnotGeometry(1, 0.3, 128, 32),
new v3d.RawShaderMaterial({
vertexShader: document.querySelector('#gbuffer-vert').textContent.trim(),
fragmentShader: document.querySelector('#gbuffer-frag').textContent.trim(),
uniforms: {
tDiffuse: { value: diffuse },
repeat: { value: new v3d.Vector2(5, 0.5) }
},
glslVersion: v3d.GLSL3
})
));
// PostProcessing setup
postScene = new v3d.Scene();
postCamera = new v3d.OrthographicCamera(- 1, 1, 1, -1, 0, 1);
postScene.add(new v3d.Mesh(
new v3d.PlaneGeometry(2, 2),
new v3d.RawShaderMaterial({
vertexShader: document.querySelector('#render-vert').textContent.trim(),
fragmentShader: document.querySelector('#render-frag').textContent.trim(),
uniforms: {
tDiffuse: { value: renderTarget.texture[0] },
tNormal: { value: renderTarget.texture[1] },
},
glslVersion: v3d.GLSL3
})
));
// Controls
controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render);
//controls.enableZoom = false;
window.addEventListener('resize', onWindowResize);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
const dpr = renderer.getPixelRatio();
renderTarget.setSize(window.innerWidth * dpr, window.innerHeight * dpr);
render();
}
function render() {
renderTarget.samples = parameters.samples;
scene.traverse(function(child) {
if (child.material !== undefined) {
child.material.wireframe = parameters.wireframe;
}
});
// render scene into target
renderer.setRenderTarget(renderTarget);
renderer.render(scene, camera);
// render post FX
renderer.setRenderTarget(null);
renderer.render(postScene, postCamera);
}
</script>
</body>
</html>