-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBVH_SDFGen_Complete_Test.html
1119 lines (825 loc) · 41.2 KB
/
BVH_SDFGen_Complete_Test.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>BVH_SDFGen_Complete_Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<style type="text/css">
html,
body {
padding: 0;
margin: 0;
overflow: hidden;
font-family: monospace;
}
canvas {
width: 100%;
height: 100%;
}
#model-info {
color: white;
position: absolute;
left: 10px;
bottom: 10px;
opacity: 0.5;
}
#output {
white-space: pre;
margin-bottom: 10px;
}
#info {
top: 0;
width: 100%;
pointer-events: none;
position: absolute;
color: white;
font-family: monospace;
text-align: center;
padding: 5px 0;
}
</style>
</head>
<body>
<div id="model-info">
<div id="output"></div>
<div>Model by DailyArt on Sketchfab</div>
</div>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/",
"three/addonsmore/": "https://unpkg.com/[email protected]/examples/jsm/",
"three-mesh-bvh": "https://cdn.jsdelivr.net/npm/[email protected]/build/index.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/stats.module.js';
import { GLTFLoader } from 'three/addons/GLTFLoader.js';
import { OBJLoader } from 'three/addons/OBJLoader.js';
import { OrbitControls } from 'three/addons/OrbitControls.js';
import { GUI } from 'three/addons/lil-gui.module.min.js';
import { MeshoptDecoder } from 'three/addons/meshopt_decoder.module.js';
import { FullScreenQuad } from 'three/addonsmore/postprocessing/Pass.js';
import { GenerateMeshBVHWorker } from './bvh_core/src/workers/GenerateMeshBVHWorker.js';
import { StaticGeometryGenerator, MeshBVH, MeshBVHHelper, computeBoundsTree, getBVHExtremes } from './bvh_core/src/index.js';
import { GenerateSDFMaterial } from './bvh_gensdf/GenerateSDFMaterial.js';
import { RenderSDFLayerMaterial } from './bvh_gensdf/RenderSDFLayerMaterial.js';
import { RayMarchSDFMaterial } from './bvh_gensdf/RayMarchSDFMaterial.js';
import {calcSizeFitCamPerspective} from './utils/calcSizeFitCamPerspective.js'
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
const params = {
gpuGeneration: false, // worker chưa hoạt động
resolution: 16,
margin: 0.2,
regenerate: () => updateSDF(),
mode: 'grid layers',
layer: 0,
surface: 0.1,
skeletonHelper: false,
bvhHelper: true,
bvhHelperDepth: 10,
autoUpdate: true,
updateRate: 0,
pause: false,
u_hold :1,
u_slice:0
};
let dataTT
let countTriggle = 0
let renderTargetGenImg, meshCheckFbo ,sceneFbo,camFbo
const RENDERSDF = false
const isRenderSkinCheck = true
let renderer, camera, clock, scene, gui, stats, boxHelper;
let outputContainer, bvh, geometry, sdfTex, meshSampleSdf;
let generateSdfPass, layerPass, raymarchPass;
let bvhGenerationWorker, staticGeometryGenerator222, bvhHelper222, meshHelper222;
const inverseBoundsMatrix = new THREE.Matrix4();
let matInstanced,monitorTrack1
let saveTexLoop
clock = new THREE.Clock();
//for skinedmesh
let timeSinceUpdate = 0;
let initialExtremes = null;
let model, skeletonHelper, mixer, animationAction, staticGeometryGenerator, wireframeMaterial, meshHelper, bvhHelper
const textureSdfSample = new THREE.TextureLoader().load('/models/eisbar/sdf.png')
//config
const dim = params.resolution;
const pxWidth = 1 / dim;
const halfWidth = .5 * pxWidth;
let listM = []
initSDFtextureFBO()
function initSDFtextureFBO() {
sdfTex = new THREE.Data3DTexture(new Float32Array(dim ** 3), dim, dim, dim);
sdfTex.format = THREE.RedFormat;
sdfTex.type = THREE.FloatType;
sdfTex.minFilter = THREE.LinearFilter;
sdfTex.magFilter = THREE.LinearFilter;
sdfTex.needsUpdate = true;
console.log("sdfTex type", " THREE.RedFormat , THREE.FloatType","sdfTex config", sdfTex, "Total", sdfTex.source.data, "sizeXYZ", sdfTex.source.data.width)
}
init();
initMonitor()
function initMonitor() {
monitorTrack1 = new THREE.Mesh(
new THREE.PlaneGeometry(2, 2),
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 u_slice;
uniform float u_sdfScale;
uniform vec3 u_sdfOffset;
uniform vec2 resTex2d;
uniform float u_sdfVoxelSize;
uniform vec4 u_sdfSliceInfo;
uniform float u_sdfOutBoundForce;
uniform float u_sdfInBoundForce;
uniform float u_sdfThreshold;
uniform sampler2D tex;
uniform sampler3D tex3d;
vec2 computeSliceOffsetOrigin(float slice, float slicesPerRow, vec2 sliceSize) {
return sliceSize * vec2(mod(slice, slicesPerRow),
floor(slice / slicesPerRow));
}
vec4 sampleAs3DTextureOrigin(
sampler2D tex, vec3 texCoord, float size, float numRows, float slicesPerRow) {
float slice = texCoord.z * size;
float sliceZ = floor(slice); // slice we need
float zOffset = fract(slice); // dist between slices
vec2 sliceSize = vec2(1.0 / slicesPerRow, // u space of 1 slice
1.0 / numRows); // v space of 1 slice
vec2 slice0Offset = computeSliceOffsetOrigin(sliceZ, slicesPerRow, sliceSize);
vec2 slice1Offset = computeSliceOffsetOrigin(sliceZ + 1.0, slicesPerRow, sliceSize);
vec2 slicePixelSize = sliceSize / size; // space of 1 pixel
vec2 sliceInnerSize = slicePixelSize * (size - 1.0); // space of size pixels
vec2 uv = slicePixelSize * 0.5 + texCoord.xy * sliceInnerSize;
//uv = vec2(uv.x,1.-uv.y);
vec4 slice0Color = texture2D(tex, slice0Offset + uv);
vec4 slice1Color = texture2D(tex, slice1Offset + uv);
return mix(slice0Color, slice1Color, zOffset);
return slice0Color;
}
void main() {
vec3 pos = vec3(vUv - .5,u_slice);
vec3 voxelTextureCoordOri = pos / u_sdfScale + u_sdfOffset ;
vec3 voxelTextureCoord = clamp(voxelTextureCoordOri, vec3(0.5 / u_sdfVoxelSize), vec3(1.0 - 0.5 / u_sdfVoxelSize));
vec4 distanceInfo = sampleAs3DTextureOrigin(tex, vec3(voxelTextureCoord.xy,min(0.995,u_slice)) , resTex2d.x,resTex2d.y,resTex2d.y);
vec4 distanceInfoFormat = distanceInfo * 2. - 1.;
vec3 disSDF = normalize(distanceInfoFormat.xyz + pos) * (- distanceInfoFormat.w * .5);
gl_FragColor = vec4(distanceInfoFormat.xyz,1.-distanceInfoFormat.w);
// vec4 sdf3d = texture(tex3d, vec3(voxelTextureCoord.xy,min(0.995,u_slice)) );
// vec4 sdf3dFormat = sdf3d * 2. - 1.;
// gl_FragColor = sdf3dFormat;
}
`,
uniforms: {
u_slice : { value: 0 },
tex: { value: null },
tex3d: { value: null },
u_sdfOffset : { value: new THREE.Vector3(0.5000, 0.5000, 0.5000) },
u_sdfScale : { value: 1 },
u_sdfVoxelSize : { value: 128 },
u_sdfSliceInfo : { value: new THREE.Vector4(128, 16, 0.0625, 0.1250) },
u_sdfThreshold : { value: .05 },
u_sdfOutBoundForce : { value: .1 },
u_sdfInBoundForce : { value: .01 },
resTex2d : { value: new THREE.Vector2(params.resolution,Math.sqrt(params.resolution)) }
},
transparent: true,
side: 2
})
)
// monitorTrack1.position.x = 1
// monitorTrack1.position.y = -1
// scene.add(monitorTrack1)
}
render();
function init() {
dataTT = initDatasdfCOCO()
outputContainer = document.getElementById('output');
// renderer setup
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0, 0);
document.body.appendChild(renderer.domElement);
// scene setup
scene = new THREE.Scene();
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(1, 1, 1);
scene.add(light);
scene.add(new THREE.AmbientLight(0xffffff, 0.2));
// camera setup
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 50);
camera.position.set(1, 1, 2);
camera.far = 100;
camera.updateProjectionMatrix();
boxHelper = new THREE.Box3Helper(new THREE.Box3());
scene.add(boxHelper);
const axesHelper = new THREE.AxesHelper(500);
scene.add(axesHelper);
new OrbitControls(camera, renderer.domElement);
// stats setup
stats = new Stats();
document.body.appendChild(stats.dom);
// sdf pass to generate the 3d texture
generateSdfPass = new FullScreenQuad(new GenerateSDFMaterial());
const rayMatBvh =new RenderSDFLayerMaterial()
rayMatBvh.uniforms.uTexelSize.value.copy(new THREE.Vector2(pxWidth,pxWidth))
layerPass = new FullScreenQuad(rayMatBvh);
// screen pass to render the sdf ray marching
raymarchPass = new FullScreenQuad( new RayMarchSDFMaterial());
// load model and generate bvh
// bvhGenerationWorker = new GenerateMeshBVHWorker();
wireframeMaterial = new THREE.MeshBasicMaterial({
wireframe: true,
transparent: true,
opacity: 0.5,
depthWrite: false,
});
meshHelper = new THREE.Mesh(new THREE.BufferGeometry(), wireframeMaterial);
meshHelper222 = new THREE.Mesh(new THREE.BufferGeometry(), wireframeMaterial);
initFboToGenImg()
new GLTFLoader()
.load('models/mei/scene.gltf', gltf => {
gltf.scene.updateMatrixWorld(true);
// prep the model and add it to the scene
model = gltf.scene;
model.scale.set(.0042,.0042,.0042)
model.position.y = -.3
model.position.z = -.1
model.updateMatrixWorld(true);
model.traverse((child) => {
if (child.isMesh) {
if (child.type === 'Mesh') {
// child.removeFromParent()
child.visible = false
} else if (child.type === 'SkinnedMesh') {
let bfd = child.clone()
console.log(bfd)
listM.push(bfd)
}
}
});
if (!RENDERSDF) {
if (isRenderSkinCheck) {
const geometryTorus = new THREE.TorusGeometry(.2, .1, 16, 100);
bvh = new MeshBVH(listM[0].geometry);
meshSampleSdf = new THREE.Mesh(listM[0].geometry, new THREE.MeshStandardMaterial());
//updateSDF();
}
scene.add(model)
// skeleton helper
skeletonHelper = new THREE.SkeletonHelper(model);
skeletonHelper.visible = false;
scene.add(skeletonHelper);
// animations
const animations = gltf.animations;
mixer = new THREE.AnimationMixer(model);
animationAction = mixer.clipAction(animations[0]);
animationAction.timeScale = 0.5;
animationAction.play();
animationAction.paused = params.pause;
// prep the geometry
staticGeometryGenerator = new StaticGeometryGenerator(model);
// originalMaterials = staticGeometryGenerator.getMaterials();
// scene.add(meshHelper);
bvhHelper = new MeshBVHHelper(meshHelper, 10);
scene.add(bvhHelper);
}
});
// for (let i = 0; i < save.length; i+=3) {
// let a = new THREE.Mesh(new THREE.SphereGeometry(.01,5,5),new THREE.MeshBasicMaterial({color:"blue"}))
// a.position.set(save[i*3],save[i*3+1],save[i*3+2])
// scene.add(a)
// }
rebuildGUI();
initInstancedMesh(sdfTex)
window.addEventListener('resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}, false);
}
function initFboToGenImg() {
renderTargetGenImg = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat
});
sceneFbo = new THREE.Scene()
camFbo = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 50);
camFbo.position.z = 2
const [widthFit, heightFit] = calcSizeFitCamPerspective(camFbo)
meshCheckFbo = new THREE.Mesh(
new THREE.PlaneGeometry(heightFit, heightFit),
new THREE.ShaderMaterial({
uniforms: {
tex: {
value: null
}
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
varying vec2 vUv;
uniform sampler2D tex;
void main() {
vec4 texColor = texture2D(tex,vUv);
float gridSize = 20.0; // Kích thước ô vuông grid
vec2 grid = fract(vUv * gridSize);
float lineThickness = 0.05; // Độ dày của đường grid
// Tạo màu grid (trắng cho đường và xám cho nền)
vec3 gridColor = vec3(0.8); // Màu xám cho ô vuông
if (grid.x < lineThickness || grid.y < lineThickness) {
gridColor = vec3(1.0); // Màu trắng cho đường grid
}
// Pha trộn grid và texture dựa trên alpha của texture
vec3 finalColor = mix(gridColor, texColor.rgb, 1.);
// Xuất màu cuối cùng ra màn hình
gl_FragColor = texColor;
gl_FragColor = vec4(texColor.xyz, 1.);
}
`,
transparent:true,
side:2
}),
)
sceneFbo.add(meshCheckFbo)
}
function regenerateMesh()
{
// if(!listM[0]) return
// const matrix = new THREE.Matrix4();
// const center = new THREE.Vector3();
// const quat = new THREE.Quaternion();
// const scale = new THREE.Vector3();
// // compute the bounding box of the geometry including the margin which is used to
// // define the range of the SDF
// listM[0].geometry.boundingBox.getCenter(center);
// scale.subVectors(listM[0].geometry.boundingBox.max, listM[0].geometry.boundingBox.min);
// scale.x += 2 * params.margin;
// scale.y += 2 * params.margin;
// scale.z += 2 * params.margin;
// matrix.compose(center, quat, scale);
// inverseBoundsMatrix.copy(matrix).invert();
// // update the box helper
// boxHelper.box.copy(listM[0].geometry.boundingBox);
// boxHelper.box.min.x -= params.margin;
// boxHelper.box.min.y -= params.margin;
// boxHelper.box.min.z -= params.margin;
// boxHelper.box.max.x += params.margin;
// boxHelper.box.max.y += params.margin;
// boxHelper.box.max.z += params.margin;
if (meshHelper && staticGeometryGenerator) {
let generateTime, refitTime, startTime;
// time the geometry generation
startTime = window.performance.now();
staticGeometryGenerator.generate(meshHelper.geometry);
generateTime = window.performance.now() - startTime;
// time the bvh refitting
startTime = window.performance.now();
if (!meshHelper.geometry.boundsTree) {
meshHelper.geometry.computeBoundsTree();
refitTime = '-';
} else {
meshHelper.geometry.boundsTree.refit();
if( /* countTriggle % 100 === 0 || */ /* 1 === 2 */ countTriggle % 2 === 0 ) {
if (!params.gpuGeneration) {
// create a new 3d data texture
let point = new THREE.Vector3();
let ray = new THREE.Ray();
let target = {};
for (let x = 0; x < dim; x++) {
for (let y = 0; y < dim; y++) {
for (let z = 0; z < dim; z++) {
// adjust by half width of the pixel so we sample the pixel center
// and offset by half the box size.
point.set(
halfWidth + x * pxWidth - 0.5,
halfWidth + y * pxWidth - 0.5,
halfWidth + z * pxWidth - 0.5,
)/* .applyMatrix4(matrix); */
let index = x + y * dim + z * dim * dim;
let dist = meshHelper.geometry.boundsTree.closestPointToPoint(point, target).distance;
// raycast inside the mesh to determine if the distance should be positive or negative
ray.origin.copy(point);
ray.direction.set(0, 0, 1);
let hit = meshHelper.geometry.boundsTree.raycastFirst(ray, THREE.DoubleSide);
let isInside = hit && hit.face.normal.dot(ray.direction) > 0.1;
// set the distance in the texture data
sdfTex.image.data[index] = isInside ? - dist : dist*2;
sdfTex.needsUpdate = true
}
}
}
}
}
refitTime = (window.performance.now() - startTime).toFixed(2);
}
bvhHelper.update();
timeSinceUpdate = 0;
const extremes = getBVHExtremes(meshHelper.geometry.boundsTree);
if (initialExtremes === null) {
initialExtremes = extremes;
}
let score = 0;
let initialScore = 0;
for (const i in extremes) {
score += extremes[i].surfaceAreaScore;
initialScore += initialExtremes[i].surfaceAreaScore;
}
const degradation = (score / initialScore) - 1.0;
// update time display
// outputContainer.innerHTML =
// `mesh generation time: ${ generateTime.toFixed( 2 ) } ms\n` +
// `refit time: ${ refitTime } ms\n` +
// `bvh degradation: ${ ( 100 * degradation ).toFixed( 2 ) }%`;
}
}
// build the gui with parameters based on the selected display mode
function rebuildGUI() {
if (gui) {
gui.destroy();
}
params.layer = Math.min(params.resolution, params.layer);
gui = new GUI();
const generationFolder = gui.addFolder('generation');
generationFolder.add(params, 'gpuGeneration');
generationFolder.add(params, 'resolution', 10, 200, 1);
generationFolder.add(params, 'margin', 0, 1);
generationFolder.add(params, 'regenerate');
const displayFolder = gui.addFolder('display');
displayFolder.add(params, 'mode', ['geometry', 'raymarching', 'layer', 'grid layers']).onChange(() => {
rebuildGUI();
});
if (params.mode === 'layer') {
displayFolder.add(params, 'layer', 0, params.resolution, 1);
}
if (params.mode === 'raymarching') {
displayFolder.add(params, 'surface', - 0.2, 0.5);
}
function updatePropsPGPU() {
matInstanced.uniforms.u_hold.value = params.u_hold
}
gui.add(params, 'u_hold', 0, 4, .0001).onChange(updatePropsPGPU);
function check() {
monitorTrack1.material.uniforms.u_slice.value = params.u_slice
}
gui.add(params, 'u_slice', 0, 1, .0001).onChange(check);
}
// update the sdf texture based on the selected parameters
function updateSDF() {
const matrix = new THREE.Matrix4();
const center = new THREE.Vector3();
const quat = new THREE.Quaternion();
const scale = new THREE.Vector3();
// compute the bounding box of the geometry including the margin which is used to
// define the range of the SDF
geometry.boundingBox.getCenter(center);
scale.subVectors(geometry.boundingBox.max, geometry.boundingBox.min);
scale.x += 2 * params.margin;
scale.y += 2 * params.margin;
scale.z += 2 * params.margin;
matrix.compose(center, quat, scale);
inverseBoundsMatrix.copy(matrix).invert();
// update the box helper
boxHelper.box.copy(geometry.boundingBox);
boxHelper.box.min.x -= params.margin;
boxHelper.box.min.y -= params.margin;
boxHelper.box.min.z -= params.margin;
boxHelper.box.max.x += params.margin;
boxHelper.box.max.y += params.margin;
boxHelper.box.max.z += params.margin;
// dispose of the existing sdf
if (sdfTex) {
sdfTex.dispose();
}
const startTime = window.performance.now();
if (!params.gpuGeneration) {
// create a new 3d data texture
console.log("=>>>updateSDF")
const point = new THREE.Vector3();
const ray = new THREE.Ray();
const target = {};
// iterate over all pixels and check distance
console.time()
for (let x = 0; x < dim; x++) {
for (let y = 0; y < dim; y++) {
for (let z = 0; z < dim; z++) {
// adjust by half width of the pixel so we sample the pixel center
// and offset by half the box size.
point.set(
halfWidth + x * pxWidth - 0.5,
halfWidth + y * pxWidth - 0.5,
halfWidth + z * pxWidth - 0.5,
).applyMatrix4(matrix);
const index = x + y * dim + z * dim * dim;
const dist = bvh.closestPointToPoint(point, target).distance;
// raycast inside the mesh to determine if the distance should be positive or negative
ray.origin.copy(point);
ray.direction.set(0, 0, 1);
const hit = bvh.raycastFirst(ray, THREE.DoubleSide);
const isInside = hit && hit.face.normal.dot(ray.direction) > 0.0;
// set the distance in the texture data
sdfTex.image.data[index] = isInside ? - dist : dist;
}
}
}
}
console.log("Total time export sdf")
console.timeEnd()
// update the timing display
const delta = window.performance.now() - startTime;
outputContainer.innerText = `${delta.toFixed(2)}ms`;
rebuildGUI();
}
function initDatasdfCOCO() {
const width = Math.sqrt(params.resolution);
const height = Math.sqrt(params.resolution);
const size = width * height;
const data = new Float32Array(width * height * 4)
for ( let i = 0; i < size; i ++ ) {
const stride = i * 4;
data[ stride ] =0.;
data[ stride + 1 ] = 0.;
data[ stride + 2 ] =0.;
data[ stride + 3 ] = 1;
}
// used the buffer to create a DataTexture
const texture = new THREE.DataTexture( data, width, height,THREE.RGBAFormat,THREE.FloatType );
texture.needsUpdate = true;
return texture
}
function updatexx(texture) {
const arrT = texture;
const dataTTs = dataTT.image.data;
console.log(arrT)
// for (let k = 0, kl = arrT.length; k < kl; k += 4) {
// const stride = i * 4;
// data[ stride ] = arrT[ stride ];
// data[ stride + 1 ] = arrT[ stride ];
// data[ stride + 2 ] =arrT[ stride ];
// data[ stride + 3 ] =arrT[ stride ];
// }
}
function initInstancedMesh(data3d) {
console.log(data3d)
const count = 2000; // Số lượng instance
const geometry = new THREE.BoxGeometry(0.08, 0.08, 0.08);
// Tạo mảng lưu trữ vị trí các điểm
const positions = new Float32Array(count * 3);
const uvs = new Float32Array(count * 2); // Mảng UVs
const materialChunk = new THREE.MeshPhongMaterial({
side: 2,
color: 0xc5461b,
//emissive:0x3a2b82,
specular: 0x2c2a2a,
shininess: 100,
transparent:true
});
materialChunk.onBeforeCompile = (shader) => {
shader.uniforms.posGpu = { value: null };
shader.uniforms.velGpu = { value: null };
shader.uniforms.time = { value: 0 };
shader.uniforms.u_hold = { value: 1 };
shader.uniforms.texture2DSDF = { value: textureSdfSample };
// shader.uniforms.resTex2d = { value: new THREE.Vector2(params.resolution,Math.sqrt(params.resolution)) };
shader.uniforms.resTex2d = { value: new THREE.Vector2(128,16)};
shader.uniforms.texture3DSDF = { value: data3d };
shader.uniforms.tSize = { value: new THREE.Vector2(params.resolution, params.resolution) };
shader.uniforms.uTexelSize = { value: new THREE.Vector2(1 / 20, 1 / 20) }
// Sửa đổi vertex shader
shader.vertexShader = `
attribute vec3 offset;
varying vec2 vUv;
varying float distance;
varying vec4 sdf;
uniform float u_hold;
uniform float time;
uniform vec2 tSize;
uniform vec2 resTex2d;
uniform vec2 uTexelSize;
uniform sampler2D posGpu;
uniform sampler2D velGpu;
uniform sampler3D texture3DSDF;
uniform sampler2D texture2DSDF;
vec2 computeSliceOffsetOrigin(float slice, float slicesPerRow, vec2 sliceSize) {
return sliceSize * vec2(mod(slice, slicesPerRow),
floor(slice / slicesPerRow));
}
vec4 sampleAs3DTextureOrigin(
sampler2D tex, vec3 texCoord, float size, float numRows, float slicesPerRow) {
float slice = texCoord.z * size;
float sliceZ = floor(slice); // slice we need
float zOffset = fract(slice); // dist between slices
vec2 sliceSize = vec2(1.0 / slicesPerRow, // u space of 1 slice
1.0 / numRows); // v space of 1 slice
vec2 slice0Offset = computeSliceOffsetOrigin(sliceZ, slicesPerRow, sliceSize);
vec2 slice1Offset = computeSliceOffsetOrigin(sliceZ + 1.0, slicesPerRow, sliceSize);
vec2 slicePixelSize = sliceSize / size; // space of 1 pixel
vec2 sliceInnerSize = slicePixelSize * (size - 1.0); // space of size pixels
vec2 uv = slicePixelSize * 0.5 + texCoord.xy * sliceInnerSize;
//uv = vec2(uv.x,1.-uv.y);
vec4 slice0Color = texture2D(tex, slice0Offset + uv);
vec4 slice1Color = texture2D(tex, slice1Offset + uv);
return mix(slice0Color, slice1Color, zOffset);
//return slice0Color;
}
// Hàm tính gradient của SDF tại vị trí 'pos'
vec3 calculateSDFGradient(vec3 pos,float step) {
float sdfX1 = texture(texture3DSDF, pos + vec3(step, 0.0, 0.0)).r;
float sdfX2 = texture(texture3DSDF, pos - vec3(step, 0.0, 0.0)).r;
float sdfY1 = texture(texture3DSDF, pos + vec3(0.0, step, 0.0)).r;
float sdfY2 = texture(texture3DSDF, pos - vec3(0.0, step, 0.0)).r;
float sdfZ1 = texture(texture3DSDF, pos + vec3(0.0, 0.0, step)).r;
float sdfZ2 = texture(texture3DSDF, pos - vec3(0.0, 0.0, step)).r;
vec3 gradient = normalize(vec3(sdfX1 - sdfX2, sdfY1 - sdfY2, sdfZ1 - sdfZ2));
return gradient;
}
${shader.vertexShader}
`.replace(
`#include <fog_vertex>`,
`#include <fog_vertex>
float id = float(gl_InstanceID);
vec2 uvT = vec2(
mod(id, tSize.x) / tSize.x, // Tính chỉ số cột
floor(id / tSize.x) / tSize.y // Tính chỉ số hàng
);
vec3 posSelf = offset * 1.;
vec3 u_sdfOffset = vec3(0.5000, 0.5000, 0.5000);
vec3 voxelTextureCoordOri = posSelf.xyz / 1. + u_sdfOffset ;
vec3 voxelTextureCoord = clamp(voxelTextureCoordOri, vec3(0.5 /resTex2d.x), vec3(1.0 - 0.5 / resTex2d.x));
vec4 distanceInfo = sampleAs3DTextureOrigin(texture2DSDF, voxelTextureCoord , resTex2d.x,resTex2d.x/resTex2d.y,resTex2d.y);
vec4 distanceInfoFormat = distanceInfo * 2. - 1.;
vec3 disSDF = normalize(distanceInfoFormat.xyz + voxelTextureCoordOri) * ((1.-distanceInfoFormat.w)*u_hold);
sdf = distanceInfoFormat;
vec4 cc = texture2D(texture2DSDF,uvT);
/// sdf = cc;
distance = distanceInfoFormat.a+ .8;
float tt = texture(texture3DSDF, posSelf).r;
// Điều chỉnh vị trí điểm dựa trên khoảng cách tới bề mặt và gradient
// posSelf = disSDF;
// Tính toán vị trí cuối cùng của điểm trong không gian clip
gl_Position = projectionMatrix * modelViewMatrix * vec4(posSelf + position, 1.0);
vUv = uv; // Truyền UV đến fragment shader
`,
shader.fragmentShader = `
varying float distance;
varying vec4 sdf;
uniform float u_hold;
${shader.fragmentShader}
`.replace(`#include <dithering_fragment>`,
`
#include <dithering_fragment>
gl_FragColor = vec4(vec3(1.,0.,0.),step(.95,1.-distance));
if(1.-sdf.w > u_hold) {
gl_FragColor = vec4(sdf.xyz,1.-sdf.w);
}else {
discard;
}
`
)
);
matInstanced = shader;
};
const offsets = new Float32Array(count * 3);
for (let i = 0; i < count; i++) {
offsets[i * 3 + 0] = Math.random() * 2 - 1; // X
offsets[i * 3 + 1] = Math.random() * 2 - 1; // Y
offsets[i * 3 + 2] =Math.random() * 2 - 1; // Z
}
geometry.setAttribute('offset', new THREE.InstancedBufferAttribute(offsets, 3));
const cInstancedMesh = new THREE.InstancedMesh(geometry, materialChunk, count);
const colors = [];
for (let i = 0; i < count; i++) {
// Tạo ma trận transform cho mỗi instance
const matrix = new THREE.Matrix4();
// Tạo vị trí ngẫu nhiên trong phạm vi từ -50 đến 50
const position = new THREE.Vector3(
Math.random() * 2. - 1.,
Math.random() * 2. - 1.,
Math.random() * 2. - 1.,
);
// Tạo tỷ lệ ngẫu nhiên từ 0.5 đến 1.5
const scale = new THREE.Vector3(
Math.random() + 0.5,
Math.random() + 0.5,
Math.random() + 0.5
);
// Tạo rotation ngẫu nhiên
const rotation = new THREE.Euler(
Math.random() * 2 * Math.PI,
Math.random() * 2 * Math.PI,
Math.random() * 2 * Math.PI
);
// Áp dụng transform vào ma trận
matrix.makeRotationFromEuler(rotation);
matrix.setPosition(position);
matrix.scale(scale);
// Gán ma trận cho instance thứ i
cInstancedMesh.setMatrixAt(i, matrix);
// Tạo màu ngẫu nhiên cho mỗi instance
const color = new THREE.Color(Math.random(), Math.random(), Math.random());
colors.push(color.r, color.g, color.b);
}
scene.add(cInstancedMesh);
}
function render() {
stats.update();
requestAnimationFrame(render);
const delta = Math.min(clock.getDelta(), 30 * 0.001);
if(matInstanced) matInstanced.uniforms.time.value = clock.elapsedTime