-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
377 lines (303 loc) · 13.2 KB
/
index.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title></title></head>
<style>canvas{ display:block; } body, html { padding:0px; margin:0px; width:100%; height:100%; }</style>
<body><script src="./import-map.js"></script><script type="module">
// #region IMPORTS
import useThreeWebGL2, { THREE, useDarkScene, useVisualDebug }
from './_lib/useThreeWebGL2.js';
import { GLTFLoader } from './_thirdparty/GLTFLoader.js';
import { Pane } from './_thirdparty/tweakpane-4.0.4.min.js';
import * as TweakpaneEssentials from './_thirdparty/tweakpane-plugin-essentials-0.2.1.min.js';
import useTransformControl from './_lib/useTransformControl.js';
// #endregion
// #region MAIN
let Debug;
let App = useDarkScene( useThreeWebGL2() );
let Ref = {
pause : false,
gizmo : useTransformControl( App ),
action : null,
char : { mesh: null, skel:null, handl:null, handr:null, head:null },
props : {
Shield : { mesh:null, pos:[-0.1,0,0.05], rot:[0,Math.PI * -0.5,0] },
Axe_2handed : { mesh:null, pos:[-0.022537977402425205, 0.14825856797100856, 0.035714842191911636], quat:[0.24249606553513545, -0.3183588941038431, 0.01461333299132493, 0.9163131141939649] },
Crossbow_1handed : { mesh:null, pos:[-0.10687772450783956, 0.049749252628277, 0.01802268378453304], quat:[0, -0.5147333453308135, 0, 0.857350326998567] },
Crossbow_2handed : { mesh:null, pos:[-0.055489156674837635, 0.07315769473973252, -0.0036172203874524833], quat:[-0.0022801307010711642, -0.5110644303109522, 0.0001895087240134682, 0.8595393610308788] },
Mug_empty : { mesh:null, pos:[-0.30155191381336904, -0.004978531268737184, -0.03889880826609325] },
Chair : { mesh:null, pos:[0.006739486923674987, -0.0485037582431323, -0.5772039579972961], quat:[0, 0.7029668210948672, 0, 0.7112226433682894] },
},
};
Ref.gizmo.onMove = v=>{ console.log( 'Pos', v ) };
Ref.gizmo.onRotate = v=>{ console.log( 'Rot', v ) };
// https://www.youtube.com/watch?v=T1KNCtAqJ7A
// https://tweakpane.github.io/docs/getting-started/
window.addEventListener( 'load', async ()=>{
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Setup
App.sphericalLook( 0, 20, 4, [ 0, 0.9, 0 ] );
Debug = await useVisualDebug( App );
// const PARAMS = {
// factor : 123,
// title : 'hello',
// color : '#ff0055',
// num : 5,
// theme : 'dark',
// };
// const pane = new Pane();
// pane.addBinding(PARAMS, 'factor');
// pane.addBinding(PARAMS, 'title', { label:'titles' } );
// pane.addBinding(PARAMS, 'color');
// pane.addBinding(PARAMS, 'num').on( 'change', e=>console.log(e.value ) );
// pane.addBinding(PARAMS, 'theme', {options: {Dark: 'dark', Light: 'light'}} ).on( 'change', e=>console.log(e) );
// const f = pane.addFolder({ title: 'Title', expanded: true });
// f.addButton( {title: 'Increment', label: 'counter'}).on( 'click', ()=>console.log('click') );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// const url = './_res/models/mannequin/mannequin.gltf';
// const gltf = await GLTFLoaderAsync( url );
// const grp = gltf.scene;
// App.scene.add( grp );
// const skeleton = new THREE.SkeletonHelper( grp.children[0].children[1] );
// skeleton.visible = true;
// App.scene.add( skeleton );
// grp.traverse( o=>{
// console.log( o instanceof THREE.SkinnedMesh );
// });
// console.log( grp );
// Object3D : grp.children[0]
// SkinnedMesh : grp.children[0].children[0]
// Bone : grp.children[0].children[1]
// const url2 = './_res/anim/kaykit_char_animations.gltf';
// const gltf2 = await GLTFLoaderAsync( url2 );
// const ary = gltf2.animations;
// // console.log( ary );
// Ref.mixer = new THREE.AnimationMixer( grp.children[0].children[0] );
// Ref.action = Ref.mixer.clipAction( ary[23] );
// Ref.action.play();
// // ary.forEach( o=>console.log(o.name) );
// console.log( ary.length );
Promise.allSettled([
loadCharacter( './_res/models/mannequin/mannequin.gltf' ),
loadAnimations( './_res/anim/kaykit_char_animations.gltf' ),
loadProps(),
]).then( dataLoaded );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App.createRenderLoop( onPreRender ).start();
// App.renderLoop();
});
function onPreRender( dt, et ){
if( Ref.mixer && !Ref.pause ) Ref.mixer.update( dt );
}
// #endregion
// #region HELPERS
/** Wrap ThreeJS's GLTFLoader in a promise to use Async/Await functionality */
function GLTFLoaderAsync( url ){
return new Promise( ( resolve, reject )=>{
new GLTFLoader().load( url, resolve, null, reject );
});
}
function wsAlignSocket( bone, toUp=[0,0,1], toFwd=[0,-1,0], toPos=[0.0, 0.1, 0.0] ){
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Data & Worldspace rotation of bone
const vUP = new THREE.Vector3().fromArray( toUp );
const vFWD = new THREE.Vector3().fromArray( toFwd );
const rot = new THREE.Quaternion();
bone.getWorldQuaternion( rot );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Compute Swing Rotation
const v = new THREE.Vector3( 0, 1, 0 );
v.applyQuaternion( rot ); // Compute UP Vector for bone
const swing = new THREE.Quaternion();
swing .setFromUnitVectors( v, vUP ) // Rotation between current & target vectors
.multiply( rot ) // PreMul to current rotation
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Compute Twist Rotation
v.set( 0, 0, 1 ).applyQuaternion( swing ); // Compute FORWARD Vector for bone
const twist = new THREE.Quaternion();
twist .setFromUnitVectors( v, vFWD ) // Rotation between current & target vectors
.multiply( swing ); // PreMul to swing rotation
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Update Rotating
twist.premultiply( rot.invert() ); // to Local space
const socket = new THREE.Group();
socket.quaternion.copy( twist ); // Save LS to socket's offset
socket.position.fromArray( toPos ); // Copy position offset
return socket;
}
// #endregion
// #region LOADERS
async function loadCharacter( url ){
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const gltf = await GLTFLoaderAsync( url );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Find skinned mesh to animate. Need to add it & its first bone to
// the scene to make it render correctly.
let mesh;
gltf.scene.traverse( o=>{ if( !mesh && o instanceof THREE.SkinnedMesh ) mesh = o; });
App.scene.add( mesh, mesh.skeleton.bones[0] );
Ref.char.mesh = mesh;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Add skeleton viewer
const skel = new THREE.SkeletonHelper( mesh.skeleton.bones[0] );
skel.visible = false;
App.scene.add( skel );
Ref.char.skel = skel;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Find bones to place props
for( const b of mesh.skeleton.bones ){
switch( b.name ){
// ------------------------
case 'handl' :{
Ref.char.handl = wsAlignSocket( b, [0,0,1], [0,1,0] );
b.add( Ref.char.handl );
break; }
// ------------------------
case 'handr' :{
Ref.char.handr = wsAlignSocket( b, [0,0,1], [0,-1,0] );
b.add( Ref.char.handr );
break; }
// ------------------------
case 'head' : Ref.char.head = b; break;
}
}
}
async function loadAnimations( url ){
const gltf = await GLTFLoaderAsync( url );
Ref.animations = gltf.animations;
await buildUI( gltf.animations );
}
async function loadProps(){
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const ary = await Promise.allSettled([
GLTFLoaderAsync( './_res/models/pete/pete_gear.gltf' ),
GLTFLoaderAsync( './_res/models/props/random_props.gltf' ),
]);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Save references for prop meshes
ary.forEach( i=>{
i.value.scene.traverse( o=>{
if( !o.isMesh ) return;
const n = o.name;
if( Ref.props[ n ] ){
// PreExisting object means custom transform is needed
const p = Ref.props[ n ];
if( p.pos ) o.position.fromArray( p.pos );
if( p.rot ) o.rotation.fromArray( p.rot );
if( p.quat ) o.quaternion.fromArray( p.quat );
p.mesh = o;
}else{
// Render mesh as is
Ref.props[ n ] = { mesh:o };
}
});
});
}
async function dataLoaded( e ){
Ref.mixer = new THREE.AnimationMixer( Ref.char.mesh );
playAnimation( Ref.animations[0].name );
// playAnimation( 'Block' );
// Ref.char.handr.add( Ref.props.Skeleton_Staff.mesh );
// App.scene.add( Ref.props.Chair.mesh );
// Ref.gizmo.attach( Ref.props.Skeleton_Staff.mesh );
}
// #endregion
// #region UI
async function buildUI( anim ){
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Build up data
const clipOpt = anim.reduce( (p,c,i)=>{ p[c.name] = c.name; return p; }, {} );
const s = {
clips : anim[0].name,
Skeleton : false,
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Build up ui
const p = new Pane();
p.registerPlugin( TweakpaneEssentials );
const f = p.addFolder({ title: 'Control Panel', expanded: true });
f .addBinding( s, 'clips', { label:'Clips', options: clipOpt } )
.on( 'change', onAnimationChange );
f .addBlade({ view: 'buttongrid', size: [3, 1], label: '',
cells: (x, y) => ({ title: [ ['Start', 'Pause', 'Stop'] ][y][x] }),
}).on('click', (ev) => {
switch( ev.index[0] ){
case 0: Ref.action.play(); Ref.pause = false; break;
case 1: Ref.pause = true; break;
case 2: Ref.action.stop(); Ref.pause = false; break;
}
});
f .addBinding( s, 'Skeleton' ).on( 'change', onSkeleton );
const d = p.addFolder({ title: 'Debug', expanded: false });
d.addButton({ title: 'Top View' }).on( 'click', ()=>App.sphericalLook( 0, 90, 5 ) );
d.addButton({ title: 'Right View' }).on( 'click', ()=>App.sphericalLook( 90, 0, 4 ) );
d.addButton({ title: 'Front View' }).on( 'click', ()=>App.sphericalLook( 0, 0, 4 ) );
d.addButton({ title: 'Rotate' }).on( 'click', ()=>Ref.gizmo.toRotate() );
d.addButton({ title: 'Translate' }).on( 'click', ()=>Ref.gizmo.toTranslate() );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ref.pane = p;
}
function onAnimationChange( e ){ playAnimation( e.value ); }
function onSkeleton( e ){ Ref.char.skel.visible = e.value; }
function playAnimation( id ){
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const clip = Ref.animations.find( i=>( i.name === id ) );
if( !clip ){ console.error( 'Clip not found:', id ); return; }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const act = Ref.mixer.clipAction( clip );
if( Ref.action ) Ref.action.stop();
act.play();
Ref.action = act;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
showClipProps( clip.name );
}
function showClipProps( id ){
console.log( 'props', id );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Hide all props
// TODO - Find better way to sync or swop active props
for( const p of Object.values( Ref.props ) ) p.mesh.visible = false;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( id.includes( '1H_Melee' ) ){
toRightHand( Ref.props.Sword );
}else if( id.includes( '2H_Melee' ) ){
toRightHand( Ref.props.Axe_2handed );
}else if( id.includes( 'Spellcast' ) ){
toRightHand( Ref.props.Skeleton_Staff );
// --------------------------------
}else if( id.includes( '1H_Ranged' ) ){
toRightHand( Ref.props.Crossbow_1handed );
}else if( id.includes( '2H_Ranged' ) ){
toRightHand( Ref.props.Crossbow_2handed );
// --------------------------------
}else if( id.includes( 'Block' ) ){
toRightHand( Ref.props.Sword );
toLeftHand( Ref.props.Shield );
// --------------------------------
}else if( id.includes( 'Dualwield' ) ){
toRightHand( Ref.props.Sword );
toLeftHand( Ref.props.Axe_1handed );
}else if( id.includes( 'Use_Item' ) || id.includes( 'Throw' ) ){
toRightHand( Ref.props.Mug_empty );
}else if( id.includes( 'Chair' ) ){
toScene( Ref.props.Chair );
// --------------------------------
}else if( id.includes( 'Jump' ) ){
toHead( Ref.props.Helmet );
}
}
function toRightHand( prop ){
prop.mesh.visible = true;
Ref.char.handr.add( prop.mesh );
}
function toLeftHand( prop ){
prop.mesh.visible = true;
Ref.char.handl.add( prop.mesh );
}
function toHead( prop ){
prop.mesh.visible = true;
Ref.char.head.add( prop.mesh );
}
function toScene( prop ){
prop.mesh.visible = true;
App.scene.add( prop.mesh );
}
// #endregion
</script></body></html>