@@ -9,16 +9,27 @@ import type { ModelData } from './types.ts';
99import { VertexData } from './types.ts' ;
1010import { setupOrbitCamera } from './setup-orbit-camera.ts' ;
1111
12- const MODELS = {
13- LongBoi : '/TypeGPU/assets/mesh-skinning/LongBoi.glb' ,
14- DancingBot : '/TypeGPU/assets/mesh-skinning/DancingBot.glb' ,
12+ const MODELS : Record <
13+ string ,
14+ { path : string ; scale : number ; offset : [ number , number , number ] }
15+ > = {
16+ LongBoi : {
17+ path : '/TypeGPU/assets/mesh-skinning/LongBoi.glb' ,
18+ scale : 1 ,
19+ offset : [ 0 , 0 , 0 ] ,
20+ } ,
21+ DancingBot : {
22+ path : '/TypeGPU/assets/mesh-skinning/DancingBot.glb' ,
23+ scale : 8 ,
24+ offset : [ 0 , - 8 , 0 ] ,
25+ } ,
1526} ;
1627type ModelName = keyof typeof MODELS ;
1728
1829const MAX_JOINTS = 64 ;
1930
2031let currentModelName : ModelName = 'LongBoi' ;
21- let modelData : ModelData = await loadGLBModel ( MODELS [ currentModelName ] ) ;
32+ let modelData : ModelData = await loadGLBModel ( MODELS [ currentModelName ] . path ) ;
2233let twistEnabled = false ;
2334let bendEnabled = false ;
2435let animationPlaying = true ;
@@ -127,11 +138,15 @@ const getJointMatrices = (): d.m4x4f[] => {
127138 const useLongBoi = currentModelName === 'LongBoi' &&
128139 ( twistEnabled || bendEnabled ) ;
129140 const animTransforms =
130- currentModelName === 'DancingBot' && modelData . animations . length > 0 &&
131- animationPlaying
141+ currentModelName === 'DancingBot' && modelData . animations . length > 0
132142 ? sampleAnimation ( modelData . animations [ 0 ] , animationTime )
133143 : undefined ;
134144
145+ const { scale, offset } = MODELS [ currentModelName ] ;
146+ const modelTransform = mat4 . identity ( ) ;
147+ mat4 . translate ( modelTransform , offset , modelTransform ) ;
148+ mat4 . scale ( modelTransform , [ scale , scale , scale ] , modelTransform ) ;
149+
135150 const matrices = modelData . jointNodes . map ( ( jointNode : number , i : number ) => {
136151 const world = getWorldTransform (
137152 jointNode ,
@@ -140,7 +155,8 @@ const getJointMatrices = (): d.m4x4f[] => {
140155 useLongBoi ,
141156 ) ;
142157 const invBind = modelData . inverseBindMatrices . slice ( i * 16 , ( i + 1 ) * 16 ) ;
143- return mat4 . mul ( world , invBind , d . mat4x4f ( ) ) ;
158+ const jointMatrix = mat4 . mul ( world , invBind , d . mat4x4f ( ) ) ;
159+ return mat4 . mul ( modelTransform , jointMatrix , d . mat4x4f ( ) ) ;
144160 } ) ;
145161
146162 while ( matrices . length < MAX_JOINTS ) {
@@ -241,14 +257,14 @@ async function switchModel(name: ModelName) {
241257 return ;
242258 }
243259 currentModelName = name ;
244- modelData = await loadGLBModel ( MODELS [ name ] ) ;
260+ modelData = await loadGLBModel ( MODELS [ name ] . path ) ;
245261 vertexBuffer = root . createBuffer (
246262 d . arrayOf ( VertexData , modelData . vertexCount ) ,
247263 createVertexData ( ) ,
248264 ) . $usage ( 'vertex' ) ;
249265 indexBuffer = root . createBuffer (
250266 d . arrayOf ( d . u16 , modelData . indices . length ) ,
251- Array . from ( modelData . indices ) as number [ ] ,
267+ Array . from ( modelData . indices ) ,
252268 ) . $usage ( 'index' ) ;
253269 currentIndexCount = modelData . indices . length ;
254270 animationTime = 0 ;
@@ -319,6 +335,13 @@ export const controls = {
319335 animationPlaying = v ;
320336 } ,
321337 } ,
338+ 'Reset Animation' : {
339+ onButtonClick : ( ) => {
340+ animationTime = 0 ;
341+ bendTime = 0 ;
342+ twistTime = 0 ;
343+ } ,
344+ } ,
322345} ;
323346
324347export function onCleanup ( ) {
0 commit comments