-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathuniform-buffer-objects.html
279 lines (235 loc) · 8.3 KB
/
uniform-buffer-objects.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<!--
@license twgl.js Copyright (c) 2015, Gregg Tavares All Rights Reserved.
Available via the MIT license.
see: http://github.com/greggman/twgl.js for details
-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta property="og:title" content="TWGL.js - uniform-buffer-objects" />
<meta property="og:type" content="website" />
<meta property="og:image" content="http://twgljs.org/examples/screenshots/uniform-buffer-objects.png" />
<meta property="og:description" content="TWGL.js - uniform-buffer-objects" />
<meta property="og:url" content="http://twgljs.org" />
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@greggman">
<meta name="twitter:creator" content="@greggman">
<meta name="twitter:domain" content="twgljs.org">
<meta name="twitter:title" content="TWGL.js - uniform-buffer-objects">
<meta name="twitter:url" content="http://twgljs.org/examples/uniform-buffer-objects.html">
<meta name="twitter:description" content="TWGL.js - uniform-buffer-objects">
<meta name="twitter:image:src" content="http://twgljs.org/examples/screenshots/uniform-buffer-objects.png">
<link href="/resources/images/twgljs-icon.png" rel="shortcut icon" type="image/png">
<title>twgl.js - uniform buffer objects</title>
<style>
body {
margin: 0;
font-family: monospace;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
#b {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 2;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="b"><a href="http://twgljs.org">twgl.js</a> - uniform-buffer-objects</div>
</body>
<script id="vs" type="notjs">
#version 300 es
uniform View {
mat4 u_viewInverse;
mat4 u_viewProjection;
};
uniform Lights {
mediump vec3 u_lightWorldPos;
mediump vec4 u_lightColor;
} lights[2];
uniform Model {
mat4 u_world;
mat4 u_worldInverseTranspose;
} foo;
in vec4 a_position;
in vec3 a_normal;
in vec2 a_texcoord;
out vec4 v_position;
out vec2 v_texCoord;
out vec3 v_normal;
out vec3 v_surfaceToLight;
out vec3 v_surfaceToView;
void main() {
v_texCoord = a_texcoord;
// v_position = (foo.u_world * u_viewProjection * a_position);
v_position = (u_viewProjection * foo.u_world * a_position);
v_normal = (foo.u_worldInverseTranspose * vec4(a_normal, 0)).xyz;
v_surfaceToLight = lights[0].u_lightWorldPos - (foo.u_world * a_position).xyz;
v_surfaceToView = (u_viewInverse[3] - (foo.u_world * a_position)).xyz;
gl_Position = v_position;
}
</script>
<script id="fs" type="notjs">
#version 300 es
precision mediump float;
in vec4 v_position;
in vec2 v_texCoord;
in vec3 v_normal;
in vec3 v_surfaceToLight;
in vec3 v_surfaceToView;
uniform Lights {
vec3 u_lightWorldPos;
vec4 u_lightColor;
} lights[2];
uniform sampler2D u_diffuse;
uniform Material {
vec4 u_ambient;
vec4 u_specular;
float u_shininess;
float u_specularFactor;
};
out vec4 theColor;
vec4 lit(float l ,float h, float m) {
return vec4(1.0,
max(abs(l), 0.0),
(l > 0.0) ? pow(max(0.0, h), m) : 0.0,
1.0);
}
void main() {
vec4 diffuseColor = texture(u_diffuse, v_texCoord);
vec3 a_normal = normalize(v_normal);
vec3 surfaceToLight = normalize(v_surfaceToLight);
vec3 surfaceToView = normalize(v_surfaceToView);
vec3 halfVector = normalize(surfaceToLight + surfaceToView);
vec4 litR = lit(dot(a_normal, surfaceToLight),
dot(a_normal, halfVector), u_shininess);
vec4 outColor = vec4((
lights[0].u_lightColor * (diffuseColor * litR.y + diffuseColor * u_ambient +
u_specular * litR.z * u_specularFactor)).rgb,
diffuseColor.a);
theColor = outColor;
}
</script>
<script src="../3rdparty/chroma.min.js"></script>
<script type="module">
import * as twgl from '../dist/6.x/twgl-full.module.js';
function main() {
const m4 = twgl.m4;
twgl.setDefaults({attribPrefix: "a_"});
const gl = document.getElementById("c").getContext('webgl2');
if (!twgl.isWebGL2(gl)) {
alert("Sorry, this example requires WebGL 2.0"); // eslint-disable-line
return;
}
const programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
const bufferInfo = twgl.primitives.createCubeBufferInfo(gl, 2);
const tex = twgl.createTexture(gl, {
min: gl.NEAREST,
mag: gl.NEAREST,
src: [
255, 255, 255, 255,
192, 192, 192, 255,
192, 192, 192, 255,
255, 255, 255, 255,
],
});
function rand(min, max) {
if (max === undefined) {
max = min;
min = 0;
}
return min + Math.random() * (max - min);
}
function randElement(array) {
return array[rand(array.length) | 0];
}
const uniforms = {
u_diffuse: tex,
};
// We pull out the Float32Array views for viewProjection and viewInverse (and world below)
// from the viewUboInfo but, if we're modifying the shaders it's possible they might
// get optimized away. So, the `|| Float32Array` basically just makes a dummy in that case
// so the rest of the code doesn't have to check for existence.
const viewUboInfo = twgl.createUniformBlockInfo(gl, programInfo, "View");
const viewProjection = viewUboInfo.uniforms.u_viewProjection || new Float32Array(16);
const viewInverse = viewUboInfo.uniforms.u_viewInverse || new Float32Array(16);
const lightUboInfos = [];
for (let ii = 0; ii < 10; ++ii) {
const lightUbo = twgl.createUniformBlockInfo(gl, programInfo, "Lights[0]");
twgl.setBlockUniforms(lightUbo, {
u_lightColor: chroma.hsv(rand(360), 0.6, 0.8).gl(),
u_lightWorldPos: [rand(-100, 100), rand(-100, 100), rand(-100, 100)],
});
twgl.setUniformBlock(gl, programInfo, lightUbo);
lightUboInfos.push(lightUbo);
}
const materialUboInfos = [];
for (let ii = 0; ii < 4; ++ii) {
const materialUbo = twgl.createUniformBlockInfo(gl, programInfo, "Material");
twgl.setBlockUniforms(materialUbo, {
u_ambient: [0, 0, 0, 1],
u_specular: chroma.hsv(rand(360), 1, 0.5).gl(),
u_shininess: rand(25, 250),
u_specularFactor: rand(0.5, 1),
});
twgl.setUniformBlock(gl, programInfo, materialUbo);
materialUboInfos.push(materialUbo);
}
const objects = [];
for (let ii = 0; ii < 300; ++ii) {
const modelUbo = twgl.createUniformBlockInfo(gl, programInfo, "Model");
const world = m4.rotateY(m4.rotateX(m4.translation([rand(-30, 30), rand(-30, 30), rand(-30, 30)]), rand(Math.PI * 2)), rand(Math.PI));
twgl.setBlockUniforms(modelUbo, {
u_world: world,
u_worldInverseTranspose: m4.transpose(m4.inverse(world)),
});
twgl.setUniformBlock(gl, programInfo, modelUbo);
const o = {
modelUboInfo: modelUbo,
materialUboInfo: randElement(materialUboInfos),
lightUboInfo: randElement(lightUboInfos),
world: modelUbo.uniforms.u_world || new Float32Array(16), // See above
};
objects.push(o);
}
function render(time) {
time *= 0.001;
twgl.resizeCanvasToDisplaySize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
const projection = m4.perspective(30 * Math.PI / 180, gl.canvas.clientWidth / gl.canvas.clientHeight, 0.5, 250);
const radius = 70;
const eye = [Math.sin(time) * radius, Math.sin(time * 0.3) * radius * 0.6, Math.cos(time) * radius];
const target = [0, 0, 0];
const up = [0, 1, 0];
const camera = m4.lookAt(eye, target, up, viewInverse);
const view = m4.inverse(camera);
m4.multiply(projection, view, viewProjection);
gl.useProgram(programInfo.program);
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
twgl.setUniforms(programInfo, uniforms);
twgl.setUniformBlock(gl, programInfo, viewUboInfo);
objects.forEach(function(o) {
twgl.bindUniformBlock(gl, programInfo, o.lightUboInfo);
twgl.bindUniformBlock(gl, programInfo, o.materialUboInfo);
twgl.bindUniformBlock(gl, programInfo, o.modelUboInfo);
twgl.drawBufferInfo(gl, bufferInfo);
});
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
main();
</script>
</html>