-
Notifications
You must be signed in to change notification settings - Fork 3
/
marchingcubes.html
142 lines (117 loc) · 4.35 KB
/
marchingcubes.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
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title></head><body>
<script type="module">
// #region IMPORTS
import Starter, { THREE } from './lib/starter.js';
import DynLineMesh from './lib/DynLineMesh.js';
import ShapePointsMesh from './lib/ShapePointsMesh.js';
import DynamicMesh from './lib/DynamicMesh.js';
import { Ray, Bvh, nearPoint, from3JSScreenProjection }
from './bvh/index.js';
import MarchingCubesGrid from './mc/MarchingCubesGrid.js';
import MarchingCubesMesher from './mc/MarchingCubesMesher.js';
// #endregion
// #region MAIN
let App;
let Debug = {};
let Ref = {};
window.addEventListener( "load", async _=>{
App = new Starter( { webgl2:true, grid:true } );
App.setCamera( 30, 30, 10, [0,0.5,0] );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App.add( ( Debug.ln = new DynLineMesh() ) );
App.add( ( Debug.pnt = new ShapePointsMesh().disableDepth() ) );
Ref.dmesh = new DynamicMesh();
App.add( Ref.dmesh.mesh );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Setup our Grid Data
const mc = new MarchingCubesGrid( [5,3,5], 1, true );
Ref.mc = mc;
mcRenderPoints();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Setup what we need to do ray intersection on Grid Points
const bvh = new Bvh();
bvh.maxItemsPerNode = 20;
bvh.getItemPosition = ( data, idx )=>{ return data[ idx ].pos; };
bvh.setData( mc.points );
Ref.bvh = bvh;
// debugBVH();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App.render();
});
// #region BVH INTERSECTION
window.addEventListener( "pointerdown", e=>{
if( e.button != 2 ) return;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Compute the Ray and visually draw a line
const ray = from3JSScreenProjection( new Ray(), e.layerX, e.layerY, App );
ray.forAABB();
//Debug.ln.add( ray.posStart, ray.posEnd, 0x00ffff );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ref.bvh.rayIntersect( ray, onIntersect );
} );
function onIntersect( ray, slice ){
const RNG = 0.2;
let min = Infinity;
let iMin = -1;
let t;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for( let i of slice ){
// Loop all the points in the slice & test which
// one is the closest to the ray
t = nearPoint( ray, Ref.bvh.data[ i ].pos, RNG );
if( t !== null && t < min ){
min = t;
iMin = i;
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Point interesected
if( iMin != -1 ){
Ref.mc.togglePointByIdx( iMin ); // Toggle it's state
mcRebuild(); // Rebuild Mesh
return true;
}
return false;
}
// #endregion
// #region BUILDING / RENDERING
function mcRebuild(){
const mesher = new MarchingCubesMesher(); // Mesher
mesher.build( Ref.mc ); // Convert Active Points to Voxel Triangles
Ref.dmesh.rebuild( mesher ); // Update renderer with new vertices/indices
mesher.dispose(); // Clean up
mcRenderPoints(); // ReRender MC Points
}
function mcRenderPoints(){
Debug.pnt.reset();
for( let p of Ref.mc.points ){
Debug.pnt.add( p.pos, (!p.enabled)?0x707070:0xffffff, 3 );
}
}
// #endregion
// #region DEBUGGING
function debugBVH(){
const bvh = Ref.bvh;
const color = [ 0x81D773, 0x6DA9EA, 0xF7716A, 0x00ff00, 0x00ffff, 0xffff00 ];
let n, c = 0;
for( let i=0; i < bvh.nodes.length; i++ ){
n = bvh.nodes[ i ];
if( n.isLeaf ){
Debug.ln.box( n.minBound, n.maxBound, color[c] );
for( let i=0; i < n.sliceLength; i++ ){
Debug.pnt.add( bvh.getItemPosition( bvh.data, bvh.partitioned[ i+n.sliceIndex ] ), color[c], 2 );
}
c++;
}else{
Debug.ln.box( n.minBound, n.maxBound, 0x707070 );
}
}
}
// #endregion
</script>
<div style="position:fixed; top:0px; left:0px; padding:5px; background-color:#ffffff20">
Right click to select or deselect a point on the grid.
</div>
</body></html>