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_rendertarget_texture2darray.html
292 lines (203 loc) · 7.89 KB
/
webgl2_rendertarget_texture2darray.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webgl - 2D texture array framebuffer attachment</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>
<script id="vertex-postprocess" type="x-shader/x-vertex">
out vec2 vUv;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<!--
Fragment shader processing an input 2d texture array and writing the output
into a framebuffer. The framebuffer should have a 2d texture array bound
as color attachment.
-->
<script id="fragment-postprocess" type="x-shader/x-fragment">
precision highp sampler2DArray;
precision mediump float;
in vec2 vUv;
uniform sampler2DArray uTexture;
uniform int uDepth;
uniform float uIntensity;
void main()
{
float voxel = texture(uTexture, vec3(vUv, uDepth)).r;
gl_FragColor.r = voxel * uIntensity;
}
</script>
<script id="vs" type="x-shader/x-vertex">
uniform vec2 size;
out vec2 vUv;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
// Convert position.xy to 1.0-0.0
vUv.xy = position.xy / size + 0.5;
vUv.y = 1.0 - vUv.y; // original data is upside down
}
</script>
<script id="fs" type="x-shader/x-fragment">
precision highp float;
precision highp int;
precision highp sampler2DArray;
uniform sampler2DArray diffuse;
in vec2 vUv;
uniform int depth;
void main() {
vec4 color = texture(diffuse, vec3(vUv, depth));
// lighten a bit
gl_FragColor = vec4(color.rrr * 1.5, 1.0);
}
</script>
<body>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">
Verge3D
</a>
-2D Texture array framebuffer color attachment
<br />
<p>
This example shows how to render to an array of 2D texture.</br>
WebGL2 allows to render to specific "layers" in 3D texture and array of textures.
</p>
Scanned head data by
<a href="https://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering" target="_blank" rel="noopener">Divine Augustine</a><br />
licensed under
<a href="https://www.codeproject.com/info/cpol10.aspx" target="_blank" rel="noopener">CPOL</a>
</div>
<!-- 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 Stats from 'v3d/addons/libs/stats.module.js';
import { unzipSync } from 'v3d/addons/libs/fflate.module.js';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
import WebGL from 'v3d/addons/capabilities/WebGL.js';
if (WebGL.isWebGL2Available() === false) {
document.body.appendChild(WebGL.getWebGL2ErrorMessage());
}
const DIMENSIONS = {
width: 256,
height: 256,
depth: 109
};
const params = {
intensity: 1
};
/** Post-processing objects */
const postProcessScene = new v3d.Scene();
const postProcessCamera = new v3d.OrthographicCamera(- 1, 1, 1, -1, 0, 1);
const renderTarget = new v3d.WebGLArrayRenderTarget(DIMENSIONS.width, DIMENSIONS.height, DIMENSIONS.depth);
renderTarget.texture.format = v3d.RedFormat;
const postProcessMaterial = new v3d.ShaderMaterial({
uniforms: {
uTexture: { value: null },
uDepth: { value: 55 },
uIntensity: { value: 1.0 }
},
vertexShader: document.getElementById('vertex-postprocess').textContent.trim(),
fragmentShader: document.getElementById('fragment-postprocess').textContent.trim()
});
var depthStep = 0.4;
var camera, scene, mesh, renderer, stats;
var planeWidth = 50;
var planeHeight = 50;
init();
function init() {
var container = document.createElement('div');
document.body.appendChild(container);
camera = new v3d.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.z = 70;
scene = new v3d.Scene();
/** Post-processing scene */
const planeGeometry = new v3d.PlaneGeometry(2, 2);
const screenQuad = new v3d.Mesh(planeGeometry, postProcessMaterial);
postProcessScene.add(screenQuad);
// 2D Texture array is available on WebGL 2.0
renderer = new v3d.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
stats = new Stats();
container.appendChild(stats.dom);
window.addEventListener('resize', onWindowResize);
const gui = new GUI();
gui.add(params, 'intensity', 0, 1).step(0.01).onChange(value => postProcessMaterial.uniforms.uIntensity.value = value);
gui.open();
// width 256, height 256, depth 109, 8-bit, zip archived raw data
new v3d.FileLoader()
.setResponseType('arraybuffer')
.load('textures/3d/head256x256x109.zip', function(data) {
var zip = unzipSync(new Uint8Array(data));
const array = new Uint8Array(zip['head256x256x109'].buffer);
const texture = new v3d.DataArrayTexture(array, DIMENSIONS.width, DIMENSIONS.height, DIMENSIONS.depth);
texture.format = v3d.RedFormat;
texture.needsUpdate = true;
var material = new v3d.ShaderMaterial({
uniforms: {
diffuse: { value: renderTarget.texture },
depth: { value: 55 },
size: { value: new v3d.Vector2(planeWidth, planeHeight) }
},
vertexShader: document.getElementById('vs').textContent.trim(),
fragmentShader: document.getElementById('fs').textContent.trim()
});
var geometry = new v3d.PlaneGeometry(planeWidth, planeHeight);
mesh = new v3d.Mesh(geometry, material);
scene.add(mesh);
postProcessMaterial.uniforms.uTexture.value = texture;
animate();
});
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
var value = mesh.material.uniforms['depth'].value;
value += depthStep;
if (value > 109.0 || value < 0.0) {
if (value > 1.0) value = 109.0 * 2.0 - value;
if (value < 0.0) value = -value;
depthStep = -depthStep;
}
mesh.material.uniforms['depth'].value = value;
render();
}
/**
* Renders the 2D array into the render target `renderTarget`.
*/
function renderTo2DArray() {
const layer = Math.floor(mesh.material.uniforms['depth'].value);
postProcessMaterial.uniforms.uDepth.value = layer;
renderer.setRenderTarget(renderTarget, layer);
renderer.render(postProcessScene, postProcessCamera);
renderer.setRenderTarget(null);
}
function render() {
// Step 1 - Render the input DataArrayTexture into render target
renderTo2DArray();
// Step 2 - Renders the scene containing the plane with a material
// sampling the render target texture.
renderer.render(scene, camera);
}
</script>
</body>
</html>