Skip to content

Commit c460b28

Browse files
committed
VOXLoader: Added VOXDataTexture3D.
1 parent e35432a commit c460b28

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

examples/jsm/loaders/VOXLoader.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import {
22
BufferGeometry,
3+
DataTexture3D,
34
FileLoader,
45
Float32BufferAttribute,
56
Loader,
7+
LinearFilter,
68
Mesh,
7-
MeshStandardMaterial
9+
MeshStandardMaterial,
10+
NearestFilter,
11+
RedFormat
812
} from '../../../build/three.module.js';
913

1014
class VOXLoader extends Loader {
@@ -257,4 +261,39 @@ class VOXMesh extends Mesh {
257261

258262
}
259263

260-
export { VOXLoader, VOXMesh };
264+
class VOXDataTexture3D extends DataTexture3D {
265+
266+
constructor( chunk ) {
267+
268+
const data = chunk.data;
269+
const size = chunk.size;
270+
271+
const offsety = size.x;
272+
const offsetz = size.x * size.y;
273+
274+
const array = new Uint8Array( size.x * size.y * size.z );
275+
276+
for ( let j = 0, k = 0; j < data.length; j += 4, k ++ ) {
277+
278+
const x = data[ j + 0 ];
279+
const y = data[ j + 1 ];
280+
const z = data[ j + 2 ];
281+
282+
const index = x + ( y * offsety ) + ( z * offsetz );
283+
284+
array[ index ] = 255.0;
285+
286+
}
287+
288+
super( array, size.x, size.y, size.z );
289+
290+
this.format = RedFormat;
291+
this.minFilter = NearestFilter;
292+
this.magFilter = LinearFilter;
293+
this.unpackAlignment = 1;
294+
295+
}
296+
297+
}
298+
299+
export { VOXLoader, VOXMesh, VOXDataTexture3D };

examples/webgl2_volume_instancing.html

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<script type="module">
1616
import * as THREE from '../build/three.module.js';
1717
import { OrbitControls } from './jsm/controls/OrbitControls.js';
18-
import { VOXLoader } from './jsm/loaders/VOXLoader.js';
18+
import { VOXLoader, VOXDataTexture3D } from './jsm/loaders/VOXLoader.js';
1919

2020
import { WEBGL } from './jsm/WebGL.js';
2121

@@ -165,42 +165,20 @@
165165
for ( var i = 0; i < chunks.length; i ++ ) {
166166

167167
const chunk = chunks[ i ];
168-
const data = chunk.data;
169-
const size = chunk.size;
170-
171-
const array = new Uint8Array( size.x * size.y * size.z );
172-
173-
for ( var j = 0, k = 0; j < data.length; j += 4, k ++ ) {
174-
175-
const x = data[ j + 0 ];
176-
const y = data[ j + 1 ];
177-
const z = data[ j + 2 ];
178-
179-
const index = x + ( y * size.x ) + ( z * size.x * size.y );
180-
181-
array[ index ] = 255.0;
182-
183-
}
184-
185-
const texture = new THREE.DataTexture3D( array, size.x, size.y, size.z );
186-
texture.format = THREE.RedFormat;
187-
texture.minFilter = THREE.NearestFilter;
188-
texture.magFilter = THREE.LinearFilter;
189-
texture.unpackAlignment = 1;
190168

191169
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
192170
const material = new THREE.RawShaderMaterial( {
193171
glslVersion: THREE.GLSL3,
194172
uniforms: {
195-
map: { value: texture },
173+
map: { value: new VOXDataTexture3D( chunk ) },
196174
cameraPos: { value: new THREE.Vector3() }
197175
},
198176
vertexShader,
199177
fragmentShader,
200178
side: THREE.BackSide
201179
} );
202180

203-
const mesh = new THREE.InstancedMesh( geometry, material, 100000 );
181+
const mesh = new THREE.InstancedMesh( geometry, material, 50000 );
204182
mesh.onBeforeRender = function () {
205183

206184
this.material.uniforms.cameraPos.value.copy( camera.position );

0 commit comments

Comments
 (0)