-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
/
Copy paththreejs-voxel-geometry.js
77 lines (59 loc) · 1.7 KB
/
threejs-voxel-geometry.js
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
import * as THREE from 'three';
import * as BufferGeometryUtils from '../../examples/jsm/utils/BufferGeometryUtils.js';
import { threejsLessonUtils } from './threejs-lesson-utils.js';
{
const darkMatcher = window.matchMedia( '(prefers-color-scheme: dark)' );
const isDarkMode = darkMatcher.matches;
const darkColors = {
wire: '#DDD',
};
const lightColors = {
wire: '#000',
};
const colors = isDarkMode ? darkColors : lightColors;
threejsLessonUtils.addDiagrams( {
mergedCubes: {
create() {
const geometries = [];
const width = 3;
const height = 2;
const depth = 2;
for ( let y = 0; y < height; ++ y ) {
for ( let z = 0; z < depth; ++ z ) {
for ( let x = 0; x < width; ++ x ) {
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
geometry.applyMatrix4( ( new THREE.Matrix4() ).makeTranslation( x, y, z ) );
geometries.push( geometry );
}
}
}
const mergedGeometry = BufferGeometryUtils.mergeGeometries( geometries, false );
const material = new THREE.MeshBasicMaterial( {
color: colors.wire,
wireframe: true,
} );
const mesh = new THREE.Mesh( mergedGeometry, material );
mesh.position.set(
0.5 - width / 2,
0.5 - height / 2,
0.5 - depth / 2 );
const base = new THREE.Object3D();
base.add( mesh );
base.scale.setScalar( 3.5 );
return base;
},
},
culledCubes: {
create() {
const geometry = new THREE.BoxGeometry( 3, 2, 2, 3, 2, 2 );
const material = new THREE.MeshBasicMaterial( {
color: colors.wire,
wireframe: true,
} );
const mesh = new THREE.Mesh( geometry, material );
mesh.scale.setScalar( 3.5 );
return mesh;
},
},
} );
}