-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_Voxel.html
309 lines (239 loc) · 8.3 KB
/
Test_Voxel.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - decal splatter</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<style>
body {
margin:0;
padding:0;
}
</style>
<body>
<div id="container"></div>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/stats.module.js';
import { OrbitControls } from 'three/addons/OrbitControls.js';
import { GLTFLoader } from 'three/addons/GLTFLoader.js';
let controlsMain
let textureFbo,dataTexture3D
let arrBinary,arrModel,Model
let arr
const n = 36; // Số lượng voxel trên mỗi chiều
const countVoxel = n ** 3;
// arrBinary = Array.from({ length: countVoxel }, () => checkCondition() ? 0 : 1);
arrBinary = Array.from({ length: countVoxel }, () => 0);
// Tạo scene, camera và renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
const directionalLight = new THREE.DirectionalLight( 0xffffff, 5.5 );
scene.add( directionalLight );
const light = new THREE.AmbientLight( 0xfffff,2. ); // soft white light
scene.add( light );
const renderer = new THREE.WebGLRenderer({});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
controlsMain = new OrbitControls(camera, renderer.domElement);
const stats = new Stats();
document.body.appendChild(stats.dom);
initMain()
// initTexture3D()
// initTexture2D()
initVoxel()
//initPlaneFake()
function initPlaneFake() {
const shaderMaterial = new THREE.ShaderMaterial({
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
varying vec2 vUv;
uniform float uTime;
void main() {
// Tạo hiệu ứng gradient động
vec3 color = vec3(0.5 + 0.5 * sin(uTime + vUv.x * 3.14),
0.5 + 0.5 * sin(uTime + vUv.y * 3.14),
0.5 + 0.5 * sin(uTime));
gl_FragColor = vec4(color, 1.0);
}
`,
uniforms: {
uTime: { value: 0.0 },
},
wireframe:true
});
const planeGeometry = new THREE.PlaneGeometry(10, 10, 10, 10);
const plane = new THREE.Mesh(planeGeometry, shaderMaterial);
plane.rotation.x = -Math.PI / 3;
scene.add(plane);
}
function initTexture2D() {
const sizeAll = n ** 3
const ww = Math.sqrt(sizeAll)
const size = ww ** 2;
console.log("==>SIZE FBO:",size)
const data = new Float32Array( 4 * size );
for ( let i = 0; i < size; i ++ ) {
const c = i * 4;
data[ c ] = Math.random();
data[ c + 1 ] = Math.random();
data[ c + 2 ] = Math.random();
data[ c + 3 ] = 1;
}
// used the buffer to create a DataTexture
textureFbo = new THREE.DataTexture( data, ww, ww );
textureFbo.needsUpdate = true;
}
function initTexture3D() {
const sizeVol = n
let textureData3dOut = new Int16Array(4 * sizeVol ** 3);
for (let i = 0; i < textureData3dOut.length; i+=4) {
textureData3dOut[i] = 1
textureData3dOut[i+1] = 1
textureData3dOut[i+2] = 1
}
console.log("==>SIZE Vol:",textureData3dOut.length/4)
console.log("==>SIZE Instanced:",n**3)
dataTexture3D = new THREE.Data3DTexture(textureData3dOut, sizeVol, sizeVol, sizeVol);
dataTexture3D.format = THREE.RGBAFormat;
dataTexture3D.type = THREE.Int16Array;
dataTexture3D.unpackAlignment = 1;
dataTexture3D.needsUpdate = true;
}
function initMain() {
const loader = new GLTFLoader();
loader.load("/models/mei/scene.gltf", (gltf) => {
gltf.scene.traverse((child) => {
if (child.isSkinnedMesh) {
console.log("Found SkinnedMesh:", child.geometry );
const minBox = child.geometry
arrModel = child.geometry.attributes.position.array
for (let i = 0; i < arrModel.length; i+=3) {
let xx = new THREE.Vector3(arrModel[i],arrModel[i+1],arrModel[i+2])
xx.normalize()
}
console.log(minBox)
}
});
}, undefined, (error) => {
console.error("Error loading model:", error);
});
const meshSample = new THREE.Mesh(
//new THREE.BoxGeometry(1,1,1,16,16,16),
new THREE.TorusKnotGeometry( .7, .5, 300, 300 ),
// new THREE.SphereGeometry(1,128,128),
new THREE.MeshBasicMaterial({color:"blue",wireframe:true})
)
console.log("vert model", meshSample.geometry.attributes.position.array.length,meshSample.geometry.attributes.position.array)
arrModel = meshSample.geometry.attributes.position.array
const unitVoxel = 1/n
for (let i = 0; i < arrModel.length; i+=3) {
const vec3 = new THREE.Vector3(arrModel[i],arrModel[i+1],arrModel[i+2])
vec3.multiplyScalar(.25) //scaleBase
vec3.addScalar(.5) //center
//vec3.normalize()
const x = Math.floor(vec3.x/unitVoxel)
const y = Math.floor(vec3.y/unitVoxel)
const z = Math.floor(vec3.z/unitVoxel)
if (x >= 0 && x < n && y >= 0 && y < n && z >= 0 && z < n) {
const index1D = x + y * n + z * n * n;
if(arrBinary[index1D] !== 1) {
arrBinary[index1D] = 1;
}
}
}
//scene.add(meshSample)
}
function checkCondition() {
let a = Math.random()
return a < .95 ? true : false
}
function initVoxel() {
const size = 0.1;
const spacing = 0.100;
const offset = (n - 1) * spacing / 2;
const geometry = new THREE.BoxGeometry(size, size, size);
const material = new THREE.MeshPhongMaterial({ color: 0x9c551a,side:0, transparent:true });
material.onBeforeCompile = (shader) => {
shader.vertexShader = `
attribute float state;
varying float vState;
varying float condition_dis;
${shader.vertexShader}
`.replace(
`#include <begin_vertex>`,
`
#include <begin_vertex>
vState = state;
`
);
shader.fragmentShader = `
varying float vState;
varying float condition_dis;
${shader.fragmentShader}
`.replace(
`#include <clipping_planes_fragment>`,
`
#include <clipping_planes_fragment>
if (vState < 0.5) {
discard;
}
`
);
console.log( shader.fragmentShader)
}
const instancedMesh = new THREE.InstancedMesh(geometry, material, countVoxel);
const states = new Float32Array(arrBinary);
instancedMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
const stateAttribute = new THREE.InstancedBufferAttribute(states, 1);
instancedMesh.geometry.setAttribute('state', stateAttribute);
instancedMesh.frustumCulled = true;
scene.add(instancedMesh);
let index = 0;
let count = 0
for (let x = 0; x < n; x++) {
for (let y = 0; y < n; y++) {
for (let z = 0; z < n; z++) {
const matrix = new THREE.Matrix4();
matrix.setPosition(
x * spacing - offset,
y * spacing - offset,
z * spacing - offset,
// arrBinary[count] === 1 ? z * spacing - offset : -5
);
if (arrBinary[count] === 0) {
// matrix.scale(new THREE.Vector3(0, 0, 0));
}
instancedMesh.setMatrixAt(index++, matrix);
count+=1
}
}
}
}
// Render loop
function animate() {
requestAnimationFrame(animate);
if(directionalLight) directionalLight.position.copy(controlsMain.object.position)
stats.update();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>