This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
webgl2_ubo.html
348 lines (230 loc) · 9.26 KB
/
webgl2_ubo.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D WebGL 2 - Uniform Buffer Objects</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">
</head>
<body>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> - Uniform Buffer Objects
</div>
<div id="container"></div>
<script id="vertexShader1" type="x-shader/x-vertex">
uniform ViewData {
mat4 projectionMatrix;
mat4 viewMatrix;
};
uniform mat4 modelMatrix;
uniform mat3 normalMatrix;
in vec3 position;
in vec3 normal;
out vec3 vPositionEye;
out vec3 vNormalEye;
void main() {
vec4 vertexPositionEye = viewMatrix * modelMatrix * vec4(position, 1.0);
vPositionEye = vertexPositionEye.xyz;
vNormalEye = normalMatrix * normal;
gl_Position = projectionMatrix * vertexPositionEye;
}
</script>
<script id="fragmentShader1" type="x-shader/x-fragment">
precision highp float;
uniform LightingData {
vec3 position;
vec3 ambientColor;
vec3 diffuseColor;
vec3 specularColor;
float shininess;
} Light;
uniform vec3 color;
in vec3 vPositionEye;
in vec3 vNormalEye;
out vec4 fragColor;
void main() {
// a very basic lighting equation (Phong reflection model) for testing
vec3 l = normalize(Light.position - vPositionEye);
vec3 n = normalize(vNormalEye);
vec3 e = -normalize(vPositionEye);
vec3 r = normalize(reflect(- l, n));
float diffuseLightWeighting = max(dot(n, l), 0.0);
float specularLightWeighting = max(dot(r, e), 0.0);
specularLightWeighting = pow(specularLightWeighting, Light.shininess);
vec3 lightWeighting = Light.ambientColor +
Light.diffuseColor * diffuseLightWeighting +
Light.specularColor * specularLightWeighting;
fragColor = vec4(color.rgb * lightWeighting.rgb, 1.0);
}
</script>
<script id="vertexShader2" type="x-shader/x-vertex">
layout(std140) uniform ViewData {
mat4 projectionMatrix;
mat4 viewMatrix;
};
uniform mat4 modelMatrix;
uniform mat3 normalMatrix;
in vec3 position;
in vec3 normal;
in vec2 uv;
out vec3 vPositionEye;
out vec3 vNormalEye;
out vec2 vUv;
void main() {
vec4 vertexPositionEye = viewMatrix * modelMatrix * vec4(position, 1.0);
vPositionEye = vertexPositionEye.xyz;
vNormalEye = normalMatrix * normal;
vUv = uv;
gl_Position = projectionMatrix * vertexPositionEye;
}
</script>
<script id="fragmentShader2" type="x-shader/x-fragment">
precision highp float;
uniform sampler2D diffuseMap;
in vec2 vUv;
in vec3 vPositionEye;
in vec3 vNormalEye;
out vec4 fragColor;
uniform LightingData {
vec3 position;
vec3 ambientColor;
vec3 diffuseColor;
vec3 specularColor;
float shininess;
} Light;
void main() {
// a very basic lighting equation (Phong reflection model) for testing
vec3 l = normalize(Light.position - vPositionEye);
vec3 n = normalize(vNormalEye);
vec3 e = -normalize(vPositionEye);
vec3 r = normalize(reflect(- l, n));
float diffuseLightWeighting = max(dot(n, l), 0.0);
float specularLightWeighting = max(dot(r, e), 0.0);
specularLightWeighting = pow(specularLightWeighting, Light.shininess);
vec3 lightWeighting = Light.ambientColor +
Light.diffuseColor * diffuseLightWeighting +
Light.specularColor * specularLightWeighting;
fragColor = vec4(texture(diffuseMap, vUv).rgb * lightWeighting.rgb, 1.0);
}
</script>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/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';
let camera, scene, renderer, clock;
init();
animate();
function init() {
if (WebGL.isWebGL2Available() === false) {
document.body.appendChild(WebGL.getWebGL2ErrorMessage());
return;
}
const container = document.getElementById('container');
camera = new v3d.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.set(0, 0, 25);
scene = new v3d.Scene();
camera.lookAt(scene.position);
clock = new v3d.Clock();
// geometry
const geometry1 = new v3d.TetrahedronGeometry();
const geometry2 = new v3d.BoxGeometry();
// texture
const texture = new v3d.TextureLoader().load('textures/crate.gif');
// uniforms groups
// Camera and lighting related data are perfect examples of using UBOs since you have to store these
// data just once. They can be shared across all shader programs.
const cameraUniformsGroup = new v3d.UniformsGroup();
cameraUniformsGroup.setName('ViewData');
cameraUniformsGroup.add(new v3d.Uniform(camera.projectionMatrix)); // projection matrix
cameraUniformsGroup.add(new v3d.Uniform(camera.matrixWorldInverse)); // view matrix
const lightingUniformsGroup = new v3d.UniformsGroup();
lightingUniformsGroup.setName('LightingData');
lightingUniformsGroup.add(new v3d.Uniform(new v3d.Vector3(0, 0, 10))); // light position
lightingUniformsGroup.add(new v3d.Uniform(new v3d.Color(0x333333))); // ambient color
lightingUniformsGroup.add(new v3d.Uniform(new v3d.Color(0xaaaaaa))); // diffuse color
lightingUniformsGroup.add(new v3d.Uniform(new v3d.Color(0xcccccc))); // specular color
lightingUniformsGroup.add(new v3d.Uniform(64)); // shininess
// materials
const material1 = new v3d.RawShaderMaterial({
uniforms: {
modelMatrix: { value: null },
normalMatrix: { value: null },
color: { value: null }
},
vertexShader: document.getElementById('vertexShader1').textContent,
fragmentShader: document.getElementById('fragmentShader1').textContent,
glslVersion: v3d.GLSL3
});
const material2 = new v3d.RawShaderMaterial({
uniforms: {
modelMatrix: { value: null },
diffuseMap: { value: null },
},
vertexShader: document.getElementById('vertexShader2').textContent,
fragmentShader: document.getElementById('fragmentShader2').textContent,
glslVersion: v3d.GLSL3
});
// meshes
for (let i = 0; i < 200; i++) {
let mesh;
if (i % 2 === 0) {
mesh = new v3d.Mesh(geometry1, material1.clone());
mesh.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup];
mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld;
mesh.material.uniforms.normalMatrix.value = mesh.normalMatrix;
mesh.material.uniforms.color.value = new v3d.Color(0xffffff * Math.random());
} else {
mesh = new v3d.Mesh(geometry2, material2.clone());
mesh.material.uniformsGroups = [cameraUniformsGroup, lightingUniformsGroup];
mesh.material.uniforms.modelMatrix.value = mesh.matrixWorld;
mesh.material.uniforms.diffuseMap.value = texture;
}
scene.add(mesh);
const s = 1 + Math.random() * 0.5;
mesh.scale.x = s;
mesh.scale.y = s;
mesh.scale.z = s;
mesh.rotation.x = Math.random() * Math.PI;
mesh.rotation.y = Math.random() * Math.PI;
mesh.rotation.z = Math.random() * Math.PI;
mesh.position.x = Math.random() * 40 - 20;
mesh.position.y = Math.random() * 40 - 20;
mesh.position.z = Math.random() * 20 - 10;
}
//
renderer = new v3d.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
//
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
scene.traverse(function(child) {
if (child.isMesh) {
child.rotation.x += delta * 0.5;
child.rotation.y += delta * 0.3;
}
});
renderer.render(scene, camera);
}
</script>
</body>
</html>