FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
three.js/examples/webgl2_rendertarget_texture2darray.html at dev · XRSharp/three.js · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
XRSharp
/
three.js
Public
forked from
mrdoob/three.js
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
three.js
/
examples
/
webgl2_rendertarget_texture2darray.html
Copy path
More file actions
More file actions
Latest commit
History
History
History
288 lines (200 loc) · 7.24 KB
Breadcrumbs
three.js
/
examples
/
webgl2_rendertarget_texture2darray.html
Copy path
File metadata and controls
288 lines (200 loc) · 7.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<!DOCTYPE html
>
<
html
lang
="
en
"
>
<
head
>
<
title
>
three.js 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://threejs.org
"
target
="
_blank
"
rel
="
noopener
"
>
three.js
</
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
>
<
script
type
="
importmap
"
>
{
"imports"
:
{
"three"
:
"../build/three.module.js"
,
"three/addons/"
:
"./jsm/"
}
}
</
script
>
<
script
type
="
module
"
>
import
*
as
THREE
from
'three'
;
import
Stats
from
'three/addons/libs/stats.module.js'
;
import
{
unzipSync
}
from
'three/addons/libs/fflate.module.js'
;
import
{
GUI
}
from
'three/addons/libs/lil-gui.module.min.js'
;
import
WebGL
from
'three/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
THREE
.
Scene
(
)
;
const
postProcessCamera
=
new
THREE
.
OrthographicCamera
(
-
1
,
1
,
1
,
-
1
,
0
,
1
)
;
const
renderTarget
=
new
THREE
.
WebGLArrayRenderTarget
(
DIMENSIONS
.
width
,
DIMENSIONS
.
height
,
DIMENSIONS
.
depth
)
;
renderTarget
.
texture
.
format
=
THREE
.
RedFormat
;
const
postProcessMaterial
=
new
THREE
.
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
(
)
}
)
;
let
depthStep
=
0.4
;
let
camera
,
scene
,
mesh
,
renderer
,
stats
;
const
planeWidth
=
50
;
const
planeHeight
=
50
;
init
(
)
;
function
init
(
)
{
const
container
=
document
.
createElement
(
'div'
)
;
document
.
body
.
appendChild
(
container
)
;
camera
=
new
THREE
.
PerspectiveCamera
(
45
,
window
.
innerWidth
/
window
.
innerHeight
,
0.1
,
2000
)
;
camera
.
position
.
z
=
70
;
scene
=
new
THREE
.
Scene
(
)
;
/** Post-processing scene */
const
planeGeometry
=
new
THREE
.
PlaneGeometry
(
2
,
2
)
;
const
screenQuad
=
new
THREE
.
Mesh
(
planeGeometry
,
postProcessMaterial
)
;
postProcessScene
.
add
(
screenQuad
)
;
// 2D Texture array is available on WebGL 2.0
renderer
=
new
THREE
.
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
THREE
.
FileLoader
(
)
.
setResponseType
(
'arraybuffer'
)
.
load
(
'textures/3d/head256x256x109.zip'
,
function
(
data
)
{
const
zip
=
unzipSync
(
new
Uint8Array
(
data
)
)
;
const
array
=
new
Uint8Array
(
zip
[
'head256x256x109'
]
.
buffer
)
;
const
texture
=
new
THREE
.
DataArrayTexture
(
array
,
DIMENSIONS
.
width
,
DIMENSIONS
.
height
,
DIMENSIONS
.
depth
)
;
texture
.
format
=
THREE
.
RedFormat
;
texture
.
needsUpdate
=
true
;
const
material
=
new
THREE
.
ShaderMaterial
(
{
uniforms
:
{
diffuse
:
{
value
:
renderTarget
.
texture
}
,
depth
:
{
value
:
55
}
,
size
:
{
value
:
new
THREE
.
Vector2
(
planeWidth
,
planeHeight
)
}
}
,
vertexShader
:
document
.
getElementById
(
'vs'
)
.
textContent
.
trim
(
)
,
fragmentShader
:
document
.
getElementById
(
'fs'
)
.
textContent
.
trim
(
)
}
)
;
const
geometry
=
new
THREE
.
PlaneGeometry
(
planeWidth
,
planeHeight
)
;
mesh
=
new
THREE
.
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
)
;
let
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
>
Back
|
FazBrowse Home
|
New Git URL