-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtile_view.html
274 lines (234 loc) · 8.39 KB
/
tile_view.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
<!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 { UtilGltf2, Gltf2 } from './lib/UtilGltf2.js';
import { vec3_add, quat_axisAngle, quat_mul }
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, 30, 14, [0,0.5,0] );
App.add( ( Debug.ln = new DynLineMesh() ) );
App.add( ( Debug.pnt = new ShapePointsMesh() ) );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// LOAD IN MESH
const gltf = await Gltf2.fetch( './res/mc_tiles.gltf' );
const dic = new TileDictionary();
const m = gltf.getMesh( 9 ); //6
const geo = UtilGltf2.primitiveGeo( m.primitives[ 0 ] );
const c = TileDictionary.parseBits( m.name );
dic.addUniqueTile( m.name, c, geo );
let mat = new THREE.MeshPhongMaterial( {color:0x00ffff, flatShading:true, });
let mesh;
const zStart = -5;
let x = 0, z;
let pos = [0,1,0];
let p = [0,0,0];
let i = 0;
for( let t of dic.uniqueTiles ){
pos[ 2 ] = zStart;
console.log( t );
for( let sh of t.subTiles ){
mesh = new THREE.Mesh( t.geo, mat );
mesh.position.fromArray( pos );
mesh.quaternion.fromArray( sh.rotation );
App.add( mesh );
for( let c=0; c < 8; c++ ){
vec3_add( p, pos, TileDictionary.cornerOffsets[c] );
Debug.pnt.add( p, ( sh.bits[c])?0x00ff00: 0x707070, 12 );
}
pos[ 2 ] += 3.5;
if( i++ == 3 ){
pos[ 0 ] += 3.5;
pos[ 2 ] = zStart;
}
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App.render();
});
//#endregion
// #region DEBUGGING
function renderTile( tile ){
const mat = new THREE.MeshPhongMaterial( {color:0x00ffff, flatShading:true, });
}
// #endregion
// #region PROCESS TILE
class Tile{
constructor( name, bits, geo, id=-1 ){
this.id = id;
this.name = name;
this.geo = geo;
this.bits = bits;
this.bitValue = TileDictionary.computeBitValue( bits );
this.subTiles = [];
}
genSubTile( hRot=0, vRot=0 ){
const st = new SubTile( this.id );
let bits = ( vRot )? TileDictionary.bitRotateFlip( this.bits ) : this.bits;
switch( hRot ){
case 0:
st.setBits( bits );
st.rotation = ( vRot )? TileDictionary.urot : TileDictionary.rot;
break;
case 1:
st.setBits( TileDictionary.bitRotateRight( bits, 1 ) );
st.rotation = ( vRot )? TileDictionary.urot90 : TileDictionary.rot90;
break;
case 2:
st.setBits( TileDictionary.bitRotateRight( bits, 2 ) );
st.rotation = ( vRot )? TileDictionary.urot180 : TileDictionary.rot180;
break;
case 3:
st.setBits( TileDictionary.bitRotateRight( bits, 3 ) );
st.rotation = ( vRot )? TileDictionary.urot270 : TileDictionary.rot270;
break;
}
this.subTiles.push( st );
return st;
}
}
class SubTile{
constructor( tId ){
this.tileId = tId;
this.rotation = TileDictionary.rot;
this.bits = null;
this.bitValue = 0
}
setBits( bits ){
this.bits = bits;
this.bitValue = TileDictionary.computeBitValue( bits );
}
}
class TileDictionary{
// #region MAIN
tiles = new Array( 255 ).fill( null );
uniqueTiles = new Array();
constructor(){}
// #endregion
// #region METHODS
addUniqueTile( name, bits, geo ){
const t = new Tile( name, bits, geo, this.uniqueTiles.length );
this.uniqueTiles.push( t );
const onlyFlip = TileDictionary.isOnlyFlip( t.bitValue );
const noFlip = TileDictionary.isNoFlip( t.bitValue );
const isOnlyRot180 = TileDictionary.isOnlyRot180( t.bitValue );
const isOnlyRot90 = TileDictionary.isOnlyRot90( t.bitValue );
this.addTile( t.genSubTile( 0 ) );
if( onlyFlip ){
this.addTile( t.genSubTile( 0, 1 ) ); // Flip to top only
}else{
if( isOnlyRot90 ){
this.addTile( t.genSubTile( 1 ) );
}else{
this.addTile( t.genSubTile( 2 ) );
if( !isOnlyRot180 ){
this.addTile( t.genSubTile( 1 ) );
this.addTile( t.genSubTile( 3 ) );
}
}
if( !noFlip ){
this.addTile( t.genSubTile( 0, 1 ) );
this.addTile( t.genSubTile( 3, 1 ) );
if( !isOnlyRot180 && !isOnlyRot90 ){
this.addTile( t.genSubTile( 1, 1 ) );
this.addTile( t.genSubTile( 2, 1 ) );
}
}
}
return this;
}
addTile( t ){
let ary = this.tiles[ t.bitValue ];
if( !ary ) this.tiles[ t.bitValue ] = ary = new Array();
ary.push( t );
return this;
}
// #endregion
// #region BITS FUNCTIONS
static cornerOffsets = [
[-1, -1, -1],
[ 1, -1, -1],
[ 1, -1, 1],
[-1, -1, 1],
[-1, 1, -1],
[ 1, 1, -1],
[ 1, 1, 1],
[-1, 1, 1],
];
static rot = [0,0,0,1];
static rot90 = quat_axisAngle( [0,0,0,1], [0,1,0], -Math.PI * 0.5 );
static rot180 = quat_axisAngle( [0,0,0,1], [0,1,0], Math.PI );
static rot270 = quat_axisAngle( [0,0,0,1], [0,1,0], -Math.PI * 1.5 );
static urot = quat_axisAngle( [0,0,0,1], [1,0,0], Math.PI );
static urot90 = quat_mul( [0,0,0,0], this.rot90, this.urot );
static urot180 = quat_mul( [0,0,0,0], this.rot180, this.urot ) ;
static urot270 = quat_mul( [0,0,0,0], this.rot270, this.urot );
static cornerBits = [ 1, 2, 4, 8, 16, 32, 64, 128 ];
static computeBitValue( bits ){
let rtn = 0;
for( let i=0; i < 8; i++ ){
if( bits[ i ] === 1 ) rtn += this.cornerBits[ i ];
}
return rtn;
}
static bitRotateRight( bits, steps ){
const rtn = new Array( 8 );
let ii;
for( let i=0; i < 4; i++ ){
ii = ( i + steps ) % 4; // Loop Around
rtn[ ii ] = bits[ i ]; // Bottom
rtn[ ii + 4 ] = bits[ i + 4 ]; // Top
}
return rtn;
}
static bitRotateFlip( bits ){
return [
bits[ 7 ],
bits[ 6 ],
bits[ 5 ],
bits[ 4 ],
bits[ 3 ],
bits[ 2 ],
bits[ 1 ],
bits[ 0 ],
];
}
static onlyFlipBits = [ 15, 240 ];
static noFlipBits = [ 17, 34, 51, 68, 102, 119, 136, 153, 187, 204, 221, 238 ];
static onlyRot180Bits = [ 95, 175 ];
static onlyRot90Bits = [ 5, 10, 80, 160 ];
static isOnlyFlip( bitVal ){ return ( this.onlyFlipBits.indexOf( bitVal ) !== -1 ) }
static isNoFlip( bitVal ){ return ( this.noFlipBits.indexOf( bitVal ) !== -1 ); }
static isOnlyRot180( bitVal ){ return ( this.onlyRot180Bits.indexOf( bitVal ) !== -1 ); }
static isOnlyRot90( bitVal ){ return ( this.onlyRot90Bits.indexOf( bitVal ) !== -1 ); }
static regBits = /([01]{4})\-([01]{4})/;
static parseBits( str ){
const results = this.regBits.exec( str );
if( !results ) return null;
return [
parseInt( results[1][0] ),
parseInt( results[1][1] ),
parseInt( results[1][2] ),
parseInt( results[1][3] ),
parseInt( results[2][0] ),
parseInt( results[2][1] ),
parseInt( results[2][2] ),
parseInt( results[2][3] ),
];
}
// #endregion
}
// #endregion
</script>
</body></html>