-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdemo.html
142 lines (111 loc) · 4.54 KB
/
demo.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 Util from './lib/Util.js';
import DynLineMesh from './lib/DynLineMesh.js';
import ShapePointsMesh from './lib/ShapePointsMesh.js';
import PixelFontMesh from './lib/PixelFontMesh.js';
import IrregularSphericalGrid from './grid/IrregularSphericalGrid.js';
import IrregularCircleGrid from './grid/IrregularCircleGrid.js';
import IrregularHexGrid from './grid/IrregularHexGrid.js';
import{ vec3_norm_scale } from './lib/Maths.js';
// #endregion
// #region MAIN
let App;
let Debug = {};
let Ref = {};
window.addEventListener( "load", async _=>{
App = new Starter( { webgl2:true, grid:true } );
App.setCamera( 0, 40, 11, [0,0.0,-1] );
// App.add( ( Debug.ln = new DynLineMesh() ) );
// App.add( ( Debug.pnt = new ShapePointsMesh() ) );
App.add( ( Debug.px = new PixelFontMesh() ) );
Debug.px.setRes( 2, 1 );
Debug.px.position.y = 0.25;
Debug.px.scale.fromArray( [0.5,0.5,0.5] );
Debug.px.reset().add( "00" );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const sphere = IrregularSphericalGrid.build( 2, 4, 0, 0.1, 100 );
const sphereRend = new RenderIrrGrid();
setRadius( sphere, 1.95 );
sphereRend.genMesh( sphere );
sphereRend.wireFrame( sphere );
sphereRend.grp.position.fromArray( [0,2,-2] );
// --------------------------------------
const circle = IrregularCircleGrid.build( 2, 4, 0, 0.4, 100, false );
const circleRend = new RenderIrrGrid( [0,0.001,0]);
circleRend.genMesh( circle );
circleRend.wireFrame( circle );
circleRend.grp.position.fromArray( [-2.5,0,0] );
// --------------------------------------
const hex = IrregularHexGrid.build( 2, 5, 0, 0.1, 100, 0 );
const hexRend = new RenderIrrGrid( [0,0.001,0]);
hexRend.genMesh( hex );
hexRend.wireFrame( hex );
hexRend.grp.position.fromArray( [2.5,0,0] );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let loop = 0;
setInterval( ()=>{
IrregularSphericalGrid._relaxFaces( sphere, 2, 1, 0.1 );
sphereRend.wireFrame( sphere );
IrregularCircleGrid._relaxFaces( circle, 1, 0.2 );
circleRend.wireFrame( circle );
IrregularHexGrid._relax_Forces( hex, 1, 0.2 );
hexRend.wireFrame( hex );
Debug.px.reset().add( (++loop).toString() );
}, 1000 );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App.render();
});
//#endregion
// #region DEBUG
function setRadius( grid, radius ){
for( let v of grid.vertices ) vec3_norm_scale( v.pos, v.pos, radius );
}
// #endregion
class RenderIrrGrid{
constructor( offset=[0,0,0] ){
this.ln = new DynLineMesh();
this.pnt = new ShapePointsMesh();
this.grp = new THREE.Group();
this.mesh = null;
this.pnt.position.fromArray( offset );
this.ln.position.fromArray( offset );
this.grp.add( this.ln );
this.grp.add( this.pnt );
App.add( this.grp );
}
wireFrame( top ){
this.ln.reset();
this.pnt.reset();
for( const e of top.vertices ) this.pnt.add( e.pos, 0x00ff00, 2 );
for( const f of top.faces ){
let edg = top.halfEdges[ f.halfEdges[0] ];
let len = f.halfEdges.length;
for( let i=1; i <= len; i++ ){
let ii = i % len;
let nex = top.halfEdges[ f.halfEdges[ ii ] ];
this.ln.add( top.getVertPos( edg.vertex ), top.getVertPos( nex.vertex ), 0x707070 );
edg = nex;
}
}
}
genMesh( top ){
const verts = top.flattenVertices();
const ind = top.flattenIndices();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const bGeo = new THREE.BufferGeometry();
bGeo.setAttribute( "position", new THREE.BufferAttribute( new Float32Array( verts ), 3 ) );
if( ind && ind.length > 0 ) bGeo.setIndex( ind );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const mat = new THREE.MeshPhongMaterial( { color:0x009999 } ); // ,side:THREE.DoubleSide
const mesh = new THREE.Mesh( bGeo, mat );
this.mesh = mesh;
this.grp.add( mesh );
}
}
</script>
</body></html>