-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmarchingcubes_3dtiles.html
216 lines (176 loc) · 6.71 KB
/
marchingcubes_3dtiles.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
<!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 { UtilGltf2, Gltf2 } from './lib/UtilGltf2.js';
import { Ray, Bvh, nearPoint, from3JSScreenProjection }
from './bvh/index.js';
import MarchingCubesGrid from './mc/MarchingCubesGrid.js';
import TileDictionary from './mc/TileDictionary.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 );
Ref.mat = new THREE.MeshPhongMaterial( { color:0x00ffff, flatShading:true } );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Setup our Grid Data
const mc = new MarchingCubesGrid( [4,3,4], 1, true ); //[5,3,5]
Ref.mc = mc;
//Ref.mc.togglePointByIdx( 4 );
mcRenderPoints();
// Initialize all the cells with some userData
mc.cells.forEach( (v,i,ary)=>{ ary[i].userData = { bitValue:0, mesh:null }; });
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const gltf = await Gltf2.fetch( './res/mc_tiles.gltf' );
Ref.dic = new TileDictionary();
tileLoading( gltf, Ref.dic );
mcRebuild();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 tileLoading( gltf, dic ){
const mCnt = gltf.json.meshes.length;
let m, geo, bits;
for( let i=0; i < mCnt; i++ ){
if( !gltf.json.meshes[ i ].name.startsWith( 'cliff' ) ) continue;
m = gltf.getMesh( i );
geo = UtilGltf2.primitiveGeo( m.primitives[ 0 ] );
bits = TileDictionary.parseBits( m.name );
dic.addUniqueTile( m.name, bits, geo );
}
}
function mcRebuild(){
Debug.ln.reset();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let bValue;
let tiles, tile, uTile;
let pos = [0,0,0];
for( let cell of Ref.mc.cells ){
// ------------------------------------
// If cell value hasn't changed, then skip
bValue = cell.getBitValue();
if( cell.userData.bitValue === bValue ) continue;
cell.userData.bitValue = bValue;
// ------------------------------------
// If setting empty space, then hide mesh
if( bValue === 0 || bValue === 255 ){
cell.userData.mesh.visible = false;
continue;
}
// ------------------------------------
// If tile doesn't exist, then hide any mesh with an error message.
tiles = Ref.dic.tiles[ bValue ];
if( !tiles ){
console.log( 'no tiles found for: ', bValue );
cell.userData.mesh.visible = false;
Debug.ln.box( cell.corners[0].pos, cell.corners[6].pos, 0xff0000 );
continue;
}
// ------------------------------------
// Generate mesh tile or replace geometry
tile = tiles[ 0 ];
uTile = Ref.dic.uniqueTiles[ tile.tileId ];
if( cell.userData.mesh ){
cell.userData.mesh.geometry = uTile.geo;
}else{
cell.userData.mesh = new THREE.Mesh( uTile.geo, Ref.mat );
cell.userData.mesh.position.fromArray( cell.getMidPos( pos ) );
cell.userData.mesh.scale.fromArray( [0.5,0.5,0.5] );
App.add( cell.userData.mesh );
}
cell.userData.mesh.visible = true;
cell.userData.mesh.quaternion.fromArray( tile.rotation );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>