-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Expand file tree
/
Copy pathTextureHelperGPU.js
More file actions
210 lines (134 loc) · 4.58 KB
/
Copy pathTextureHelperGPU.js
File metadata and controls
210 lines (134 loc) · 4.58 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
import {
NodeMaterial,
BoxGeometry,
BufferAttribute,
Mesh,
PlaneGeometry,
DoubleSide,
Vector3,
} from 'three/webgpu';
import { texture as textureNode, cubeTexture, texture3D, float, vec4, attribute } from 'three/tsl';
import { mergeGeometries } from '../utils/BufferGeometryUtils.js';
/**
* A helper that can be used to display any type of texture for
* debugging purposes. Depending on the type of texture (2D, 3D, Array),
* the helper becomes a plane or box mesh.
*
* This helper can only be used with {@link WebGPURenderer}.
* When using {@link WebGLRenderer}, import from `TextureHelper.js`.
*
* @private
* @augments Mesh
* @three_import import { TextureHelper } from 'three/addons/helpers/TextureHelperGPU.js';
*/
class TextureHelper extends Mesh {
/**
* Constructs a new texture helper.
*
* @param {Texture} texture - The texture to visualize.
* @param {number} [width=1] - The helper's width.
* @param {number} [height=1] - The helper's height.
* @param {number} [depth=1] - The helper's depth.
*/
constructor( texture, width = 1, height = 1, depth = 1 ) {
const material = new NodeMaterial();
material.side = DoubleSide;
material.transparent = true;
material.name = 'TextureHelper';
let colorNode;
const uvw = attribute( 'uvw' );
if ( texture.isCubeTexture ) {
colorNode = cubeTexture( texture ).sample( uvw );
} else if ( texture.isData3DTexture || texture.isCompressed3DTexture ) {
colorNode = texture3D( texture ).sample( uvw );
} else if ( texture.isArrayTexture || texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {
colorNode = textureNode( texture ).sample( uvw.xy ).depth( uvw.z );
} else {
colorNode = textureNode( texture );
}
const alphaNode = float( getAlpha( texture ) );
material.colorNode = vec4( colorNode.rgb, alphaNode );
const geometry = texture.isCubeTexture
? createCubeGeometry( width, height, depth )
: createSliceGeometry( texture, width, height, depth );
super( geometry, material );
/**
* The texture to visualize.
*
* @type {Texture}
*/
this.texture = texture;
this.type = 'TextureHelper';
}
/**
* Frees the GPU-related resources allocated by this instance. Call this
* method whenever this instance is no longer used in your app.
*/
dispose() {
this.geometry.dispose();
this.material.dispose();
}
}
function getImageCount( texture ) {
if ( texture.isCubeTexture ) {
return 6;
} else if ( texture.isArrayTexture || texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {
return texture.image.depth;
} else if ( texture.isData3DTexture || texture.isCompressed3DTexture ) {
return texture.image.depth;
} else {
return 1;
}
}
function getAlpha( texture ) {
if ( texture.isCubeTexture ) {
return 1;
} else if ( texture.isArrayTexture || texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {
return Math.max( 1 / texture.image.depth, 0.25 );
} else if ( texture.isData3DTexture || texture.isCompressed3DTexture ) {
return Math.max( 1 / texture.image.depth, 0.25 );
} else {
return 1;
}
}
function createCubeGeometry( width, height, depth ) {
const geometry = new BoxGeometry( width, height, depth );
const position = geometry.attributes.position;
const uv = geometry.attributes.uv;
const uvw = new BufferAttribute( new Float32Array( uv.count * 3 ), 3 );
const _direction = new Vector3();
for ( let j = 0, jl = uv.count; j < jl; ++ j ) {
_direction.fromBufferAttribute( position, j ).normalize();
uvw.setFromVector3( j, _direction );
}
geometry.deleteAttribute( 'uv' );
geometry.setAttribute( 'uvw', uvw );
return geometry;
}
function createSliceGeometry( texture, width, height, depth ) {
const sliceCount = getImageCount( texture );
const geometries = [];
for ( let i = 0; i < sliceCount; ++ i ) {
const geometry = new PlaneGeometry( width, height );
if ( sliceCount > 1 ) {
geometry.translate( 0, 0, depth * ( i / ( sliceCount - 1 ) - 0.5 ) );
}
const uv = geometry.attributes.uv;
const uvw = new BufferAttribute( new Float32Array( uv.count * 3 ), 3 );
for ( let j = 0, jl = uv.count; j < jl; ++ j ) {
const u = uv.getX( j );
const v = texture.flipY ? uv.getY( j ) : 1 - uv.getY( j );
const w = sliceCount === 1
? 1
: texture.isArrayTexture || texture.isDataArrayTexture || texture.isCompressedArrayTexture
? i
: i / ( sliceCount - 1 );
uvw.setXYZ( j, u, v, w );
}
geometry.deleteAttribute( 'uv' );
geometry.setAttribute( 'uvw', uvw );
geometries.push( geometry );
}
return mergeGeometries( geometries );
}
export { TextureHelper };