-
Notifications
You must be signed in to change notification settings - Fork 3
/
hex_raycast.html
208 lines (173 loc) · 6.32 KB
/
hex_raycast.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
<!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 Util from './lib/Util.js';
import DynLineMesh from './lib/DynLineMesh.js';
import ShapePointsMesh from './lib/ShapePointsMesh.js';
import IrregularHexGrid from './grid/IrregularHexGrid.js';
import { vec3_add, vec3_scale } from './lib/Maths.js';
import { Ray, Bvh, nearPoint, from3JSScreenProjection }
from './bvh/index.js';
import op_GetVertFaces from './halfedge/ops/op_GetVertFaces.js';
import op_FaceCentroid from './halfedge/ops/op_FaceCentroid.js';
// #endregion
// #region MAIN
let App;
let Debug = {};
let Ref = {};
window.addEventListener( "load", async _=>{
App = new Starter( { webgl2:true, grid:true } );
App.setCamera( 0, 50, 9, [0,0.0,0] );
App.add( ( Debug.ln = new DynLineMesh() ) );
App.add( ( Debug.pnt = new ShapePointsMesh() ) );
App.add( ( Debug.ln2 = new DynLineMesh() ) );
App.add( ( Debug.pnt2 = new ShapePointsMesh() ) );
Debug.ln.position.y = 0.005;
Debug.pnt.position.y = 0.005;
Debug.ln2.position.y = 0.1;
Debug.pnt2.position.y = 0.1;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const grid = IrregularHexGrid.build( 3, 3, 50, 0.3, 101, 0 );
debugVertices( grid );
debugFaces( grid );
debugMesh( grid );
Ref.grid = grid;
Ref.bvh = loadBVH( grid );
//debugBVH( Ref.bvh );
Ref.ray = new Ray();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App.render();
});
//#endregion
// #region DEBUG
function debugBVH( 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], 8 );
}
c++;
}else{
Debug.ln.box( n.minBound, n.maxBound, 0x707070 );
}
}
}
function debugVertices( grid ){
for( const e of grid.vertices ) Debug.pnt.add( e.pos, 0x00ff00, 3 );
}
function debugFaces( grid ){
for( const f of grid.faces ){
let edg = grid.halfEdges[ f.halfEdges[0] ];
let len = f.halfEdges.length;
for( let i=1; i <= len; i++ ){
let ii = i % len;
let nex = grid.halfEdges[ f.halfEdges[ ii ] ];
Debug.ln.add( grid.getVertPos( edg.vertex ), grid.getVertPos( nex.vertex ), 0x707070 );
edg = nex;
}
}
}
function debugMesh( grid ){
const verts = grid.flattenVertices();
const ind = grid.flattenIndices();
const mesh = toMesh( verts, ind );
mesh.position.y = 0.001;
App.add( mesh );
}
function toMesh( verts, idx ){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const bGeo = new THREE.BufferGeometry();
bGeo.setAttribute( "position", new THREE.BufferAttribute( new Float32Array( verts ), 3 ) );
if( idx && idx.length > 0 ) bGeo.setIndex( idx );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const mat = new THREE.MeshPhongMaterial( { color:0x009999 } ); // ,side:THREE.DoubleSide
const mesh = new THREE.Mesh( bGeo, mat );
return mesh;
}
// #endregion
// #region RAY CASTING
window.addEventListener( "pointermove", e=>{
//if( e.button != 2 ) return;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Compute the Ray and visually draw a line
const ray = from3JSScreenProjection( Ref.ray, e.layerX, e.layerY, App );
Ref.ray.forAABB();
Debug.ln2.reset();
Debug.pnt2.reset();
//Debug.ln2.add( Ref.ray.posStart, Ref.ray.posEnd, 0x00ffff );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ref.bvh.rayIntersect( Ref.ray, onIntersect );
} );
function loadBVH( grid ){
const bvh = new Bvh();
bvh.getItemPosition = ( data, idx )=>{ return data[ idx ].pos; };
bvh.setData( grid.vertices );
return bvh;
}
function onIntersect( ray, slice ){
const rng = 0.3;
const verts = Ref.grid.vertices;
let min = Infinity;
let iMin = -1;
let t;
for( let i of slice ){
t = nearPoint( ray, verts[ i ].pos, rng );
if( t !== null && t < min ){
min = t;
iMin = i;
}
}
if( iMin != -1 ){
selectVertex( iMin );
return true;
}
return false;
}
function selectVertex( idx ){
const vPos = Ref.grid.vertices[ idx ].pos;
const faces = op_GetVertFaces( Ref.grid, idx );
let pos, prev;
Debug.pnt2.add( vPos, 0xffff00, 4 );
switch( faces.length ){
// ------------------------------------------------------
case 1: console.log( 'one');
const aa = op_FaceCentroid( Ref.grid, faces[0] );
Debug.ln2.add( vPos, aa, 0x00ffff );
Debug.pnt2.add( aa, 0x00ffff, 5 );
break;
// ------------------------------------------------------
case 2: console.log( 'two');
const a = op_FaceCentroid( Ref.grid, faces[0] );
const b = op_FaceCentroid( Ref.grid, faces[1] );
Debug.ln2.add( a, b, 0x00ffff );
Debug.ln2.add( vPos, b, 0x00ffff );
Debug.ln2.add( vPos, a, 0x00ffff );
Debug.pnt2.add( a, 0x00ffff, 3 );
Debug.pnt2.add( b, 0x00ffff, 3 );
break;
// ------------------------------------------------------
default:
const first = op_FaceCentroid( Ref.grid, faces[0] );
let prev = first;
let pos = null;
for( let i=1; i < faces.length; i++ ){
pos = op_FaceCentroid( Ref.grid, faces[i] );
Debug.ln2.add( prev, pos, 0x00ffff );
Debug.pnt2.add( pos, 0x00ffff, 3 );
prev = pos;
}
Debug.pnt2.add( first, 0x00ffff, 3 );
Debug.ln2.add( prev, first, 0x00ffff );
break;
}
}
// #endregion
</script>
</body></html>