-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathtexture_3d.html
More file actions
295 lines (238 loc) · 9.59 KB
/
Copy pathtexture_3d.html
File metadata and controls
295 lines (238 loc) · 9.59 KB
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
<!DOCTYPE html>
<!-- Ported from the OpenGL Samples Pack https://github.com/g-truc/ogl-samples/blob/master/tests/gl-320-texture-3d.cpp -->
<html lang="en">
<head>
<title>WebGL 2 Samples - texture_3d</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="utility.js"></script>
<script src="third-party/gl-matrix-min.js"></script>
<script src="third-party/noise3D.js"></script>
</head>
<body>
<div id="info">WebGL 2 Samples - texture_3d</div>
<p id="description">
This sample demonstrates creating a custom 3D texture from simplex noise.
</p>
<!-- WebGL 2 shaders.
This section is adapted from Example 6.15 and 6.16,
OpenGL® Programming Guide: The Official Guide to Learning OpenGL®, Version 4.3,
Dave Shreiner, Graham Sellers
-->
<script id="vs" type="x-shader/x-vertex">
#version 300 es
#define POSITION_LOCATION 0
#define TEXCOORD_LOCATION 1
precision highp float;
precision highp int;
layout(location = POSITION_LOCATION) in vec2 position;
layout(location = TEXCOORD_LOCATION) in vec2 in_texcoord;
// Output 3D texture coordinate after transformation
out vec3 v_texcoord;
// Matrix to transform the texture coordinates into 3D space
uniform mat4 orientation;
void main()
{
// Multiply the texture coordinate by the transformation
// matrix to place it into 3D space
v_texcoord = (orientation * vec4(in_texcoord - vec2(0.5, 0.5), 0.5, 1.0)).stp;
gl_Position = vec4(position, 0.0, 1.0);
}
</script>
<script id="fs" type="x-shader/x-fragment">
#version 300 es
precision highp float;
precision highp int;
precision highp sampler3D;
uniform sampler3D diffuse;
in vec3 v_texcoord;
out vec4 color;
void main()
{
color = texture(diffuse, v_texcoord);
}
</script>
<script>
(function() {
'use strict';
var canvas = document.createElement('canvas');
canvas.width = Math.min(window.innerWidth, window.innerHeight);
canvas.height = canvas.width;
document.body.appendChild(canvas);
var gl = canvas.getContext( 'webgl2', { antialias: false } );
var isWebGL2 = !!gl;
if(!isWebGL2) {
document.getElementById('info').innerHTML = 'WebGL 2 is not available. See <a href="https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">How to get a WebGL 2 implementation</a>';
return;
}
// -- Divide viewport
var windowSize = {
x: canvas.width,
y: canvas.height
};
var Corners = {
TOP_LEFT: 0,
TOP_RIGHT: 1,
BOTTOM_RIGHT: 2,
BOTTOM_LEFT: 3,
MAX: 4
};
var viewport = new Array(Corners.MAX);
viewport[Corners.BOTTOM_LEFT] = {
x: 0,
y: 0,
z: windowSize.x / 2,
w: windowSize.y / 2
};
viewport[Corners.BOTTOM_RIGHT] = {
x: windowSize.x / 2,
y: 0,
z: windowSize.x / 2,
w: windowSize.y / 2
};
viewport[Corners.TOP_RIGHT] = {
x: windowSize.x / 2,
y: windowSize.y / 2,
z: windowSize.x / 2,
w: windowSize.y / 2
};
viewport[Corners.TOP_LEFT] = {
x: 0,
y: windowSize.y / 2,
z: windowSize.x / 2,
w: windowSize.y / 2
};
// -- Initialize texture
// Note By @kenrussel: The sample was changed from R32F to R8 for best portability.
// not all devices can render to floating-point textures
// (and, further, this functionality is in a WebGL extension: EXT_color_buffer_float),
// and renderability is a requirement for generating mipmaps.
var SIZE = 32;
var data = new Uint8Array(SIZE * SIZE * SIZE);
for (var k = 0; k < SIZE; ++k) {
for (var j = 0; j < SIZE; ++j) {
for (var i = 0; i < SIZE; ++i) {
data[i + j * SIZE + k * SIZE * SIZE] = snoise([i, j, k]) * 256;
}
}
}
var texture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_3D, texture);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_BASE_LEVEL, 0);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAX_LEVEL, Math.log2(SIZE));
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage3D(
gl.TEXTURE_3D, // target
0, // level
gl.R8, // internalformat
SIZE, // width
SIZE, // height
SIZE, // depth
0, // border
gl.RED, // format
gl.UNSIGNED_BYTE, // type
data // pixel
);
gl.generateMipmap(gl.TEXTURE_3D);
// -- Initialize program
var program = createProgram(gl, getShaderSource('vs'), getShaderSource('fs'));
var uniformTextureMatrixLocation = gl.getUniformLocation(program, 'orientation');
var uniformDiffuseLocation = gl.getUniformLocation(program, 'diffuse');
// -- Initialize buffer
var positions = new Float32Array([
-1.0, -1.0,
1.0, -1.0,
1.0, 1.0,
1.0, 1.0,
-1.0, 1.0,
-1.0, -1.0
]);
var vertexPosBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
var texCoords = new Float32Array([
0.0, 1.0,
1.0, 1.0,
1.0, 0.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0
]);
var vertexTexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, texCoords, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
// -- Initilize vertex array
var vertexArray = gl.createVertexArray();
gl.bindVertexArray(vertexArray);
var vertexPosLocation = 0; // set with GLSL layout qualifier
gl.enableVertexAttribArray(vertexPosLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
gl.vertexAttribPointer(vertexPosLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
var vertexTexLocation = 1; // set with GLSL layout qualifier
gl.enableVertexAttribArray(vertexTexLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexBuffer);
gl.vertexAttribPointer(vertexTexLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindVertexArray(null);
// -- Render
var orientation = [0.0, 0.0, 0.0];
requestAnimationFrame(render);
function yawPitchRoll(yaw, pitch, roll) {
var cosYaw = Math.cos(yaw);
var sinYaw = Math.sin(yaw);
var cosPitch = Math.cos(pitch);
var sinPitch = Math.sin(pitch);
var cosRoll = Math.cos(roll);
var sinRoll = Math.sin(roll);
return [
cosYaw * cosPitch,
cosYaw * sinPitch * sinRoll - sinYaw * cosRoll,
cosYaw * sinPitch * cosRoll + sinYaw * sinRoll,
0.0,
sinYaw * cosPitch,
sinYaw * sinPitch * sinRoll + cosYaw * cosRoll,
sinYaw * sinPitch * cosRoll - cosYaw * sinRoll,
0.0,
-sinPitch,
cosPitch * sinRoll,
cosPitch * cosRoll,
0.0,
0.0, 0.0, 0.0, 1.0
];
}
function render() {
orientation[0] += 0.020; // yaw
orientation[1] += 0.010; // pitch
orientation[2] += 0.005; // roll
var yawMatrix = new Float32Array(yawPitchRoll(orientation[0], 0.0, 0.0));
var pitchMatrix = new Float32Array(yawPitchRoll(0.0, orientation[1], 0.0));
var rollMatrix = new Float32Array(yawPitchRoll(0.0, 0.0, orientation[2]));
var yawPitchRollMatrix = new Float32Array(yawPitchRoll(orientation[0], orientation[1], orientation[2]));
var matrices = [yawMatrix, pitchMatrix, rollMatrix, yawPitchRollMatrix];
// Clear color buffer
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Bind program
gl.useProgram(program);
gl.uniform1i(uniformDiffuseLocation, 0);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_3D, texture);
gl.bindVertexArray(vertexArray);
for (var i = 0; i < Corners.MAX; ++i) {
gl.viewport(viewport[i].x, viewport[i].y, viewport[i].z, viewport[i].w);
gl.uniformMatrix4fv(uniformTextureMatrixLocation, false, matrices[i]);
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, 1);
}
requestAnimationFrame(render);
}
})();
</script>
<div id="highlightedLines" style="display: none">#L132-L170</div>
</body>
</html>