axonis22 commited on
Commit
f509be8
·
verified ·
1 Parent(s): 5f8e9cb

Muse Engine 32 WebGL2 release candidate

Browse files
game/muse-authored/phase31/raw/PHASE31-NOTES-A.md ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ Phase 31 A – animated character import and hit detection.
2
+
3
+ Approach: clip-map auto-matches GLB/FBX animation names to idle/walk/run/shoot/death roles using token scoring and greedy assignment. Capsule-fit builds a robust body capsule from bind-pose vertices via percentile centre/radius, immune to T/A pose arms.
4
+
5
+ Limits: heuristic matching fails on exotic naming; capsule radius clamped to 12-35% height. Head sphere derived from bind vertices only.
6
+
7
+ Engine usage: body capsule is static shape transformed by hips bone each frame. Head sphere centre is bind-pose cluster centre transformed by current head bone skinning matrix, radius stays in object space scaled by height. Ray tests use raySphere for head, rayCapsule for body; head hits take priority over body.
game/muse-authored/phase31/raw/PHASE31-NOTES-B.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Approach
2
+ Skeleton pose is built from rest TRS then per-track sampling with binary search. World matrices are generated per node with T*R*S and parent multiplication. Skin palette is world * inverseBind written as 3x4 rows.
3
+
4
+ Limits
5
+ Max joints per mesh limited by MAX_VERTEX_UNIFORM_VECTORS. WebGL1 allows 256 vec4 uniforms typically. Reserve ~100 for matrices, leaving ~156 vec4 = 52 joints. Use MAX_JOINTS = floor((MAX_VERTEX_UNIFORM_VECTORS - 80)/4).
6
+
7
+ Per-frame cost
8
+ SampleClip O(Tracks * log K). WorldMatrices O(N). Skinning on GPU O(V). Bone budget reduction O(J^2+V) offline.
9
+
10
+ Shader feeding
11
+ Uniform vec4 uBones[3*MAX] holds palette rows. Vertex shader reconstructs mat3 from three vec4 per joint and computes vec3(dot(row0,pos),dot(row1,pos),dot(row2,pos)). Pick MAX from MAX_VERTEX_UNIFORM_VECTORS at init and clamp budget.
game/muse-authored/phase31/raw/bone-budget.js ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ window.MuseBoneBudget = (function(){
2
+ function reduce(opts){
3
+ var parents=opts.parents; var skinIndex=opts.skinIndex; var skinWeight=opts.skinWeight;
4
+ var budget=opts.budget; var protect=opts.protect||[];
5
+ var J=parents.length;
6
+ var V=skinIndex.length/4;
7
+ var totalWeight=new Float32Array(J);
8
+ for(var v=0;v<V;v++){
9
+ var base=v*4;
10
+ for(var k=0;k<4;k++){
11
+ var j=skinIndex[base+k]; var w=skinWeight[base+k];
12
+ if(j>=0 && j<J && w>0) totalWeight[j]+=w;
13
+ }
14
+ }
15
+ var protectSet=new Uint8Array(J);
16
+ for(var i=0;i<protect.length;i++) protectSet[protect[i]]=1;
17
+ var keep=new Uint8Array(J);
18
+ for(var i=0;i<J;i++) if(totalWeight[i]>1e-8 || protectSet[i]) keep[i]=1;
19
+ var children=[];
20
+ for(var i=0;i<J;i++) children[i]=[];
21
+ for(var i=0;i<J;i++){ var p=parents[i]; if(p>=0) children[p].push(i); }
22
+ var weightedCount=0;
23
+ for(var i=0;i<J;i++) if(keep[i] && totalWeight[i]>1e-8) weightedCount++;
24
+ var merged=0;
25
+ while(weightedCount>budget){
26
+ var bestIdx=-1; var bestW=Infinity;
27
+ for(var i=0;i<J;i++){
28
+ if(!keep[i] || totalWeight[i]<=1e-8 || protectSet[i]) continue;
29
+ var hasDesc=false;
30
+ var stack=children[i];
31
+ for(var s=0;s<stack.length;s++){
32
+ var d=stack[s];
33
+ if(keep[d] && totalWeight[d]>1e-8){ hasDesc=true; break; }
34
+ }
35
+ if(hasDesc) continue;
36
+ var w=totalWeight[i];
37
+ if(w<bestW || (w===bestW && i>bestIdx)){
38
+ bestW=w; bestIdx=i;
39
+ }
40
+ }
41
+ if(bestIdx===-1) break;
42
+ var target=-1;
43
+ var anc=parents[bestIdx];
44
+ while(anc>=0){
45
+ if(keep[anc]){ target=anc; break; }
46
+ anc=parents[anc];
47
+ }
48
+ if(target===-1){
49
+ var maxW=-1;
50
+ for(var i=0;i<J;i++) if(keep[i] && totalWeight[i]>1e-8){ if(totalWeight[i]>maxW){ maxW=totalWeight[i]; target=i; } }
51
+ }
52
+ if(target===-1 || target===bestIdx) break;
53
+ totalWeight[target]+=totalWeight[bestIdx];
54
+ totalWeight[bestIdx]=0;
55
+ keep[bestIdx]=0;
56
+ merged++;
57
+ weightedCount--;
58
+ }
59
+ var keepList=[];
60
+ for(var i=0;i<J;i++) if(keep[i]) keepList.push(i);
61
+ keepList.sort(function(a,b){return a-b;});
62
+ var slotOf=new Int32Array(J);
63
+ var slotMap=new Map();
64
+ for(var i=0;i<keepList.length;i++) slotMap.set(keepList[i],i);
65
+ for(var i=0;i<J;i++){
66
+ if(keep[i]){ slotOf[i]=slotMap.get(i); continue; }
67
+ var anc=i;
68
+ var found=-1;
69
+ while(anc>=0){
70
+ if(keep[anc]){ found=anc; break; }
71
+ anc=parents[anc];
72
+ }
73
+ if(found>=0) slotOf[i]=slotMap.get(found);
74
+ else slotOf[i]=0;
75
+ }
76
+ var newSkinIndex=new Uint16Array(skinIndex.length);
77
+ var newSkinWeight=new Float32Array(skinWeight.length);
78
+ for(var v=0;v<V;v++){
79
+ var acc=[];
80
+ var base=v*4;
81
+ for(var k=0;k<4;k++){
82
+ var j=skinIndex[base+k];
83
+ var w=skinWeight[base+k];
84
+ if(w<=0 || j<0 || j>=J) continue;
85
+ var slot=slotOf[j];
86
+ if(slot<0) slot=0;
87
+ acc.push({slot:slot,w:w});
88
+ }
89
+ var map=new Map();
90
+ for(var k=0;k<acc.length;k++){
91
+ var e=acc[k];
92
+ var cur=map.get(e.slot)||0;
93
+ map.set(e.slot,cur+e.w);
94
+ }
95
+ var pairs=[];
96
+ map.forEach(function(w,slot){ pairs.push({slot:slot,w:w}); });
97
+ pairs.sort(function(a,b){return b.w-a.w;});
98
+ var sum=0;
99
+ for(var k=0;k<pairs.length;k++) sum+=pairs[k].w;
100
+ var outIdx=base; var outW=base;
101
+ for(var k=0;k<4;k++){
102
+ if(k<pairs.length && sum>0){
103
+ newSkinIndex[outIdx+k]=pairs[k].slot;
104
+ newSkinWeight[outW+k]=pairs[k].w/sum;
105
+ }else{
106
+ newSkinIndex[outIdx+k]=0;
107
+ newSkinWeight[outW+k]=0;
108
+ }
109
+ }
110
+ }
111
+ return {keep:keepList,slotOf:slotOf,skinIndex:newSkinIndex,skinWeight:newSkinWeight,merged:merged};
112
+ }
113
+ return {reduce:reduce};
114
+ })();
game/muse-authored/phase31/raw/capsule-fit.js ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function(){
2
+ function percentile(arr,p){
3
+ arr.sort();
4
+ var idx=Math.floor(p*(arr.length-1));
5
+ return arr[idx];
6
+ }
7
+ function fit(positions,opts){
8
+ var n=positions.length/3;
9
+ var xs=new Float32Array(n);
10
+ var zs=new Float32Array(n);
11
+ var ys=new Float32Array(n);
12
+ var minY=Infinity,maxY=-Infinity;
13
+ for(var i=0;i<n;i++){
14
+ var x=positions[i*3], y=positions[i*3+1], z=positions[i*3+2];
15
+ xs[i]=x; zs[i]=z; ys[i]=y;
16
+ if(y<minY) minY=y;
17
+ if(y>maxY) maxY=y;
18
+ }
19
+ var height=maxY-minY;
20
+ var cx=(percentile(xs.slice(),0.05)+percentile(xs.slice(),0.95))*0.5;
21
+ var cz=(percentile(zs.slice(),0.05)+percentile(zs.slice(),0.95))*0.5;
22
+ var dists=new Float32Array(n);
23
+ for(var i=0;i<n;i++){
24
+ var dx=xs[i]-cx, dz=zs[i]-cz;
25
+ dists[i]=Math.sqrt(dx*dx+dz*dz);
26
+ }
27
+ var r80=percentile(dists.slice(),0.80);
28
+ var lo=0.12*height, hi=0.35*height;
29
+ var radius=Math.max(lo,Math.min(hi,r80));
30
+ var cnt=0;
31
+ for(var i=0;i<n;i++) if(dists[i]<=radius) cnt++;
32
+ var coverage=cnt/n;
33
+ return {cx:cx,cz:cz,radius:radius,yMin:minY,yMax:maxY,height:height,coverage:coverage};
34
+ }
35
+ function boneKey(name){
36
+ var s=name;
37
+ var cut=Math.max(s.lastIndexOf(':'),s.lastIndexOf('|'));
38
+ if(cut!==-1) s=s.slice(cut+1);
39
+ s=s.toLowerCase().trim();
40
+ s=s.replace(/^mixamorig/,'').replace(/^bip01/,'').replace(/^bip001/,'');
41
+ s=s.replace(/^character1_/,'');
42
+ s=s.replace(/[\s_\.]+/g,'');
43
+ return s;
44
+ }
45
+ function findRoles(boneNames){
46
+ var keys=boneNames.map(function(n,i){return {i:i,k:boneKey(n)};});
47
+ function findExact(target){
48
+ for(var e of keys){ if(e.k===target) return e.i; } return -1;
49
+ }
50
+ function findContains(substr,exclude){
51
+ var best=-1,bestLen=Infinity;
52
+ for(var e of keys){
53
+ if(e.k.indexOf(substr)!==-1){
54
+ var bad=false;
55
+ if(exclude) for(var ex of exclude){ if(e.k.indexOf(ex)!==-1){ bad=true; break; } }
56
+ if(!bad && e.k.length<bestLen){ bestLen=e.k.length; best=e.i; }
57
+ }
58
+ }
59
+ return best;
60
+ }
61
+ var head=findExact('head');
62
+ if(head===-1) head=findContains('head',['top','end','nub','tip','jaw','eye']);
63
+ var neck=findContains('neck',null);
64
+ var hips=findExact('hips');
65
+ if(hips===-1) hips=findExact('pelvis');
66
+ if(hips===-1) hips=findExact('hip');
67
+ if(hips===-1) hips=findExact('root');
68
+ var spine=-1;
69
+ var candidates=[];
70
+ for(var e of keys){ if(/spine|chest|torso|abdomen/.test(e.k)) candidates.push(e); }
71
+ if(candidates.length) candidates.sort(function(a,b){return b.i-a.i;}); // prefer later
72
+ if(candidates.length) spine=candidates[0].i;
73
+ function foot(side){
74
+ var markers=[];
75
+ if(side==='L') markers=['left','l'];
76
+ else markers=['right','r'];
77
+ var best=-1,bestLen=Infinity;
78
+ for(var e of keys){
79
+ var k=e.k;
80
+ if(k.indexOf('foot')!==-1||k.indexOf('ankle')!==-1){
81
+ var ok=false;
82
+ for(var m of markers){
83
+ if(k.indexOf(m)!==-1){ ok=true; break; }
84
+ if(k.endsWith(m)) ok=true;
85
+ }
86
+ if(ok && k.length<bestLen){ bestLen=k.length; best=e.i; }
87
+ }
88
+ }
89
+ return best;
90
+ }
91
+ return {head:head,neck:neck,hips:hips,spine:spine,footL:foot('L'),footR:foot('R')};
92
+ }
93
+ function headSphere(positions,headPos,height,opts){
94
+ var hY=headPos[1];
95
+ var limitY=hY-0.02*height;
96
+ var rad=0.2*height;
97
+ var xs=headPos[0], zs=headPos[2];
98
+ var pts=[];
99
+ var n=positions.length/3;
100
+ for(var i=0;i<n;i++){
101
+ var x=positions[i*3], y=positions[i*3+1], z=positions[i*3+2];
102
+ if(y<limitY) continue;
103
+ var dx=x-xs, dz=z-zs;
104
+ if(dx*dx+dz*dz>rad*rad) continue;
105
+ pts.push([x,y,z]);
106
+ }
107
+ if(pts.length<8){
108
+ return {c:[headPos[0],headPos[1],headPos[2]], r:0.09*height};
109
+ }
110
+ var minX=Infinity,maxX=-Infinity,minY=Infinity,maxY=-Infinity,minZ=Infinity,maxZ=-Infinity;
111
+ for(var p of pts){
112
+ if(p[0]<minX) minX=p[0]; if(p[0]>maxX) maxX=p[0];
113
+ if(p[1]<minY) minY=p[1]; if(p[1]>maxY) maxY=p[1];
114
+ if(p[2]<minZ) minZ=p[2]; if(p[2]>maxZ) maxZ=p[2];
115
+ }
116
+ var c=[(minX+maxX)*0.5,(minY+maxY)*0.5,(minZ+maxZ)*0.5];
117
+ var maxd=0;
118
+ for(var p of pts){
119
+ var dx=p[0]-c[0], dy=p[1]-c[1], dz=p[2]-c[2];
120
+ var d=Math.sqrt(dx*dx+dy*dy+dz*dz);
121
+ if(d>maxd) maxd=d;
122
+ }
123
+ var r=maxd*0.9;
124
+ var lo=0.05*height, hi=0.16*height;
125
+ r=Math.max(lo,Math.min(hi,r));
126
+ return {c:c,r:r};
127
+ }
128
+ function raySphere(o,d,c,r){
129
+ var ox=o[0],oy=o[1],oz=o[2];
130
+ var dx=d[0],dy=d[1],dz=d[2];
131
+ var cx=c[0],cy=c[1],cz=c[2];
132
+ var ocx=ox-cx, ocy=oy-cy, ocz=oz-cz;
133
+ var b=2*(ocx*dx+ocy*dy+ocz*dz);
134
+ var a=dx*dx+dy*dy+dz*dz;
135
+ var cc=ocx*ocx+ocy*ocy+ocz*ocz - r*r;
136
+ var disc=b*b-4*a*cc;
137
+ if(disc<0) return -1;
138
+ var s=Math.sqrt(disc);
139
+ var t0=(-b - s)/(2*a);
140
+ var t1=(-b + s)/(2*a);
141
+ if(t0>=0) return t0;
142
+ if(t1>=0) return 0;
143
+ return -1;
144
+ }
145
+ function rayCapsule(o,d,a,b,r){
146
+ var ox=o[0],oy=o[1],oz=o[2];
147
+ var dx=d[0],dy=d[1],dz=d[2];
148
+ var ax=a[0],ay=a[1],az=a[2];
149
+ var bx=b[0],by=b[1],bz=b[2];
150
+ var vx=bx-ax, vy=by-ay, vz=bz-az;
151
+ var wx=ox-ax, wy=oy-ay, wz=oz-az;
152
+ var len2=vx*vx+vy*vy+vz*vz;
153
+ var t;
154
+ if(len2===0){
155
+ t=raySphere(o,d,a,r);
156
+ return t;
157
+ }
158
+ var c1=wx*vx+wy*vy+wz*vz;
159
+ var c2=dx*vx+dy*vy+dz*vz;
160
+ var denom=1 - c2*c2/len2; // not correct, use proper projection
161
+ // Project point onto segment
162
+ var wdotv=c1;
163
+ var proj=wdotv/len2;
164
+ var closest;
165
+ if(proj<0) closest=[ax,ay,az];
166
+ else if(proj>1) closest=[bx,by,bz];
167
+ else closest=[ax+vx*proj, ay+vy*proj, az+vz*proj];
168
+ var ocx=ox-closest[0], ocy=oy-closest[1], ocz=oz-closest[2];
169
+ var bproj=2*(ocx*dx+ocy*dy+ocz*dz);
170
+ var aproj=dx*dx+dy*dy+dz*dz;
171
+ var cc=ocx*ocx+ocy*ocy+ocz*ocz - r*r;
172
+ if(cc<=0) return 0;
173
+ var disc=bproj*bproj-4*aproj*cc;
174
+ if(disc<0) return -1;
175
+ var s=Math.sqrt(disc);
176
+ var t0=(-bproj - s)/(2*aproj);
177
+ if(t0>=0) return t0;
178
+ return -1;
179
+ }
180
+ window.MuseCapsuleFit={fit:fit,findRoles:findRoles,headSphere:headSphere,raySphere:raySphere,rayCapsule:rayCapsule};
181
+ })();
game/muse-authored/phase31/raw/clip-map.js ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function(){
2
+ function splitCamel(s){return s.replace(/([a-z0-9])([A-Z])/g,'$1 $2');}
3
+ function tokenize(name){
4
+ var seg=name;
5
+ var parts=seg.split('|');
6
+ for(var i=parts.length-1;i>=0;i--){
7
+ var p=parts[i];
8
+ if(!p) continue;
9
+ var low=p.toLowerCase();
10
+ if(low==='mixamo.com'||low==='layer0'||low==='baselayer'||low==='default') continue;
11
+ if(/^take\s*\d+$/.test(low)||/^take\d+$/.test(low)) continue;
12
+ seg=p; break;
13
+ }
14
+ var colon=seg.lastIndexOf(':');
15
+ if(colon!==-1) seg=seg.slice(colon+1);
16
+ seg=splitCamel(seg);
17
+ var raw=seg.split(/[\s_\-\.]+/);
18
+ var out=[];
19
+ for(var t of raw){
20
+ if(!t) continue;
21
+ var tt=t.toLowerCase();
22
+ if(/^\d+$/.test(tt)) continue;
23
+ out.push(tt);
24
+ }
25
+ return out;
26
+ }
27
+ function stem(w){
28
+ if(w==='dying') return 'die';
29
+ if(w==='running') return 'run';
30
+ if(w==='firing') return 'fire';
31
+ if(w.endsWith('ing') && w.length>4) return w.slice(0,-3);
32
+ if(w.endsWith('ed') && w.length>3) return w.slice(0,-2);
33
+ if(w.endsWith('s') && w.length>3) return w.slice(0,-1);
34
+ return w;
35
+ }
36
+ var ROLES=['idle','walk','run','shoot','death'];
37
+ var SYN={
38
+ idle:['idle','stand','standing','breathe','breathing','rest'],
39
+ walk:['walk','stroll'],
40
+ run:['run','jog','sprint'],
41
+ shoot:['shoot','shot','fire','attack','gunshot'],
42
+ death:['death','die','dead','dying']
43
+ };
44
+ var DIR=['back','backward','backwards','left','right','strafe','turn','side'];
45
+ var PEN=['hit','react','reaction','hurt','start','end','stop','to'];
46
+ function scoreClip(tokens,role){
47
+ var syn=SYN[role];
48
+ var hasSyn=false;
49
+ for(var t of tokens){
50
+ var s=stem(t);
51
+ for(var k of syn){ if(s===k||t===k){ hasSyn=true; break; } }
52
+ if(hasSyn) break;
53
+ }
54
+ if(!hasSyn) return -Infinity;
55
+ var sc=10;
56
+ var other=0;
57
+ for(var r of ROLES){
58
+ if(r===role) continue;
59
+ for(var t of tokens){
60
+ var s=stem(t);
61
+ for(var k of SYN[r]){ if(s===k||t===k){ other++; break; } }
62
+ }
63
+ }
64
+ if(other>0) sc-=6;
65
+ for(var t of tokens){ if(DIR.includes(t)) sc-=4; }
66
+ for(var t of tokens){ if(PEN.includes(t)) sc-=3; }
67
+ if(tokens.length>1) sc-=(tokens.length-1);
68
+ if((role==='walk'||role==='run')){
69
+ for(var t of tokens){ if(t==='loop'||t==='forward'||t==='fwd') sc+=1; }
70
+ }
71
+ return sc;
72
+ }
73
+ function match(names,opts){
74
+ opts=opts||{};
75
+ var overrides=opts.overrides||{};
76
+ var n=names.length;
77
+ var tokenLists=names.map(tokenize);
78
+ var scores={};
79
+ for(var r of ROLES){ scores[r]=new Array(n).fill(-Infinity); }
80
+ for(var i=0;i<n;i++){
81
+ var toks=tokenLists[i];
82
+ for(var ri=0;ri<ROLES.length;ri++){
83
+ var r=ROLES[ri];
84
+ var sc=scoreClip(toks,r);
85
+ if(sc>=5) scores[r][i]=sc;
86
+ }
87
+ }
88
+ var roleIdx={};
89
+ for(var i=0;i<ROLES.length;i++) roleIdx[ROLES[i]]=i;
90
+ var assignedClip=new Array(n).fill(false);
91
+ var roleMap={};
92
+ for(var r of ROLES) roleMap[r]=-1;
93
+ for(var r of ROLES){
94
+ var idx=overrides[r];
95
+ if(idx!==undefined){
96
+ if(idx>=0 && idx<n){ roleMap[r]=idx; assignedClip[idx]=true; }
97
+ else { roleMap[r]=-1; }
98
+ }
99
+ }
100
+ var remainingRoles=ROLES.filter(function(r){ return roleMap[r]===-1; });
101
+ while(remainingRoles.length){
102
+ var best={score:-Infinity,ri:-1,ci:-1};
103
+ for(var ri=0;ri<remainingRoles.length;ri++){
104
+ var r=remainingRoles[ri];
105
+ for(var i=0;i<n;i++){
106
+ if(assignedClip[i]) continue;
107
+ var sc=scores[r][i];
108
+ if(sc>best.score || (sc===best.score && (i<best.ci || (i===best.ci && roleIdx[r]<roleIdx[ROLES[best.ri]])))){
109
+ best={score:sc,ri:ri,ci:i};
110
+ }
111
+ }
112
+ }
113
+ if(best.score===-Infinity || best.score<5) break;
114
+ var r=remainingRoles[best.ri];
115
+ roleMap[r]=best.ci;
116
+ assignedClip[best.ci]=true;
117
+ remainingRoles.splice(best.ri,1);
118
+ }
119
+ var used=new Set();
120
+ for(var r of ROLES){ if(roleMap[r]>=0) used.add(roleMap[r]); }
121
+ var unmatched=[];
122
+ for(var i=0;i<n;i++) if(!used.has(i)) unmatched.push(i);
123
+ return {roles:{idle:roleMap.idle,walk:roleMap.walk,run:roleMap.run,shoot:roleMap.shoot,death:roleMap.death}, scores:scores, unmatched:unmatched};
124
+ }
125
+ function boneKey(name){
126
+ var s=name;
127
+ var colon=s.lastIndexOf(':');
128
+ var pipe=s.lastIndexOf('|');
129
+ var cut=Math.max(colon,pipe);
130
+ if(cut!==-1) s=s.slice(cut+1);
131
+ s=s.toLowerCase().trim();
132
+ s=s.replace(/^mixamorig/,'').replace(/^bip01/,'').replace(/^bip001/,'');
133
+ s=s.replace(/^character1_/,'');
134
+ s=s.replace(/[\s_\.]+/g,'');
135
+ return s;
136
+ }
137
+ window.MuseClipMap={ROLES:ROLES,tokens:tokenize,match:match,boneKey:boneKey};
138
+ })();
game/muse-authored/phase31/raw/skeleton-pose.js ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ window.MuseSkeletonPose = (function(){
2
+ function createPose(N){
3
+ var t = new Float32Array(3*N);
4
+ var r = new Float32Array(4*N);
5
+ var s = new Float32Array(3*N);
6
+ for(var i=0;i<N;i++){ r[4*i+3]=1; s[3*i]=1; s[3*i+1]=1; s[3*i+2]=1; }
7
+ return {t:t,r:r,s:s};
8
+ }
9
+ function restPose(skeleton,out){
10
+ var n=skeleton.nodes; var N=n.length;
11
+ var t=out.t,r=out.r,s=out.s;
12
+ for(var i=0;i<N;i++){
13
+ var nd=n[i];
14
+ t[3*i]=nd.t[0]; t[3*i+1]=nd.t[1]; t[3*i+2]=nd.t[2];
15
+ r[4*i]=nd.r[0]; r[4*i+1]=nd.r[1]; r[4*i+2]=nd.r[2]; r[4*i+3]=nd.r[3];
16
+ s[3*i]=nd.s[0]; s[3*i+1]=nd.s[1]; s[3*i+2]=nd.s[2];
17
+ }
18
+ }
19
+ function lerp(a,b,u){return a+(b-a)*u;}
20
+ function slerp(qa,qb,u){
21
+ var x1=qa[0],y1=qa[1],z1=qa[2],w1=qa[3];
22
+ var x2=qb[0],y2=qb[1],z2=qb[2],w2=qb[3];
23
+ var dot=x1*x2+y1*y2+z1*z2+w1*w2;
24
+ if(dot<0){x2=-x2;y2=-y2;z2=-z2;w2=-w2;dot=-dot;}
25
+ if(dot>0.9995){
26
+ var ix=x1+(x2-x1)*u, iy=y1+(y2-y1)*u, iz=z1+(z2-z1)*u, iw=w1+(w2-w1)*u;
27
+ var len=Math.sqrt(ix*ix+iy*iy+iz*iz+iw*iw)||1;
28
+ return [ix/len,iy/len,iz/len,iw/len];
29
+ }
30
+ var theta=Math.acos(Math.min(1,Math.max(-1,dot)));
31
+ var sinTheta=Math.sin(theta);
32
+ var s0=Math.sin((1-u)*theta)/sinTheta;
33
+ var s1=Math.sin(u*theta)/sinTheta;
34
+ return [x1*s0+x2*s1, y1*s0+y2*s1, z1*s0+z2*s1, w1*s0+w2*s1];
35
+ }
36
+ function binarySearch(times,t){
37
+ var lo=0,hi=times.length-1;
38
+ while(lo<=hi){
39
+ var mid=(lo+hi)>>1;
40
+ if(times[mid]<=t) lo=mid+1; else hi=mid-1;
41
+ }
42
+ return hi;
43
+ }
44
+ function sampleClip(skeleton,clip,time,loop,out){
45
+ restPose(skeleton,out);
46
+ var dur=clip.duration;
47
+ if(loop){
48
+ time=((time%dur)+dur)%dur;
49
+ }else{
50
+ if(time<0)time=0; if(time>dur)time=dur;
51
+ }
52
+ var N=skeleton.nodes.length;
53
+ var t=out.t,r=out.r,s=out.s;
54
+ for(var i=0;i<clip.tracks.length;i++){
55
+ var tr=clip.tracks[i];
56
+ var node=tr.node;
57
+ if(node<0||node>=N) continue;
58
+ var times=tr.times; var vals=tr.values;
59
+ var path=tr.path; var interp=tr.interp;
60
+ var stride=path==='rotation'?4:3;
61
+ var k=times.length;
62
+ if(k===0) continue;
63
+ var idx=binarySearch(times,time);
64
+ if(idx<0){ idx=0; }
65
+ if(idx>=k-1){ idx=k-1; }
66
+ var t0=times[idx];
67
+ var t1=times[Math.min(idx+1,k-1)];
68
+ var u=0;
69
+ if(t1>t0) u=(time-t0)/(t1-t0);
70
+ var base=idx*stride;
71
+ var base1=(Math.min(idx+1,k-1))*stride;
72
+ var off=3*node;
73
+ if(path==='translation'){
74
+ if(interp==='STEP'){
75
+ t[off]=vals[base]; t[off+1]=vals[base+1]; t[off+2]=vals[base+2];
76
+ }else{
77
+ t[off]=lerp(vals[base],vals[base1],u);
78
+ t[off+1]=lerp(vals[base+1],vals[base1+1],u);
79
+ t[off+2]=lerp(vals[base+2],vals[base1+2],u);
80
+ }
81
+ }else if(path==='scale'){
82
+ if(interp==='STEP'){
83
+ s[off]=vals[base]; s[off+1]=vals[base+1]; s[off+2]=vals[base+2];
84
+ }else{
85
+ s[off]=lerp(vals[base],vals[base1],u);
86
+ s[off+1]=lerp(vals[base+1],vals[base1+1],u);
87
+ s[off+2]=lerp(vals[base+2],vals[base1+2],u);
88
+ }
89
+ }else{
90
+ var qa=[vals[base],vals[base+1],vals[base+2],vals[base+3]];
91
+ var qb=[vals[base1],vals[base1+1],vals[base1+2],vals[base1+3]];
92
+ if(interp==='STEP'){
93
+ r[4*node]=qa[0]; r[4*node+1]=qa[1]; r[4*node+2]=qa[2]; r[4*node+3]=qa[3];
94
+ }else{
95
+ var q=slerp(qa,qb,u);
96
+ r[4*node]=q[0]; r[4*node+1]=q[1]; r[4*node+2]=q[2]; r[4*node+3]=q[3];
97
+ }
98
+ }
99
+ }
100
+ }
101
+ function blendPoses(a,b,w,out){
102
+ var N=a.t.length/3;
103
+ var ta=a.t, tb=b.t, oa=out.t;
104
+ var sa=a.s, sb=b.s, os=out.s;
105
+ var ra=a.r, rb=b.r, or=out.r;
106
+ var iw=1-w;
107
+ for(var i=0;i<N;i++){
108
+ var i3=i*3, i4=i*4;
109
+ oa[i3]=ta[i3]*iw+tb[i3]*w;
110
+ oa[i3+1]=ta[i3+1]*iw+tb[i3+1]*w;
111
+ oa[i3+2]=ta[i3+2]*iw+tb[i3+2]*w;
112
+ os[i3]=sa[i3]*iw+sb[i3]*w;
113
+ os[i3+1]=sa[i3+1]*iw+sb[i3+1]*w;
114
+ os[i3+2]=sa[i3+2]*iw+sb[i3+2]*w;
115
+ var qa=[ra[i4],ra[i4+1],ra[i4+2],ra[i4+3]];
116
+ var qb=[rb[i4],rb[i4+1],rb[i4+2],rb[i4+3]];
117
+ var q=slerp(qa,qb,w);
118
+ or[i4]=q[0]; or[i4+1]=q[1]; or[i4+2]=q[2]; or[i4+3]=q[3];
119
+ }
120
+ }
121
+ function quatToMat3(q,s){
122
+ var x=q[0],y=q[1],z=q[2],w=q[3];
123
+ var xx=x*x, yy=y*y, zz=z*z;
124
+ var xy=x*y, xz=x*z, yz=y*z;
125
+ var wx=w*x, wy=w*y, wz=w*z;
126
+ var sx=s[0], sy=s[1], sz=s[2];
127
+ return [
128
+ (1-2*(yy+zz))*sx, 2*(xy-wz)*sy, 2*(xz+wy)*sz,
129
+ 2*(xy+wz)*sx, (1-2*(xx+zz))*sy, 2*(yz-wx)*sz,
130
+ 2*(xz-wy)*sx, 2*(yz+wx)*sy, (1-2*(xx+yy))*sz
131
+ ];
132
+ }
133
+ function worldMatrices(skeleton,pose,rootMatrix,out){
134
+ var N=skeleton.nodes.length;
135
+ var nodes=skeleton.nodes;
136
+ var t=pose.t,r=pose.r,s=pose.s;
137
+ for(var i=0;i<N;i++){
138
+ var parent=nodes[i].parent;
139
+ var m=new Float32Array(16);
140
+ var ti=t[3*i], tj=t[3*i+1], tk=t[3*i+2];
141
+ var si=s[3*i], sj=s[3*i+1], sk=s[3*i+2];
142
+ var q=[r[4*i],r[4*i+1],r[4*i+2],r[4*i+3]];
143
+ var m3=quatToMat3(q,[si,sj,sk]);
144
+ m[0]=m3[0]; m[1]=m3[3]; m[2]=m3[6]; m[3]=0;
145
+ m[4]=m3[1]; m[5]=m3[4]; m[6]=m3[7]; m[7]=0;
146
+ m[8]=m3[2]; m[9]=m3[5]; m[10]=m3[8]; m[11]=0;
147
+ m[12]=ti; m[13]=tj; m[14]=tk; m[15]=1;
148
+ var dst=out.subarray(i*16,i*16+16);
149
+ dst.set(m);
150
+ if(parent===-1){
151
+ var rm=rootMatrix;
152
+ var res=new Float32Array(16);
153
+ for(var row=0;row<4;row++) for(var col=0;col<4;col++){
154
+ var sum=0;
155
+ for(var k=0;k<4;k++) sum+=rm[k*4+row]*m[col*4+k];
156
+ res[col*4+row]=sum;
157
+ }
158
+ out.set(res,i*16);
159
+ }else{
160
+ var p=out.subarray(parent*16,parent*16+16);
161
+ var res=new Float32Array(16);
162
+ for(var row=0;row<4;row++) for(var col=0;col<4;col++){
163
+ var sum=0;
164
+ for(var k=0;k<4;k++) sum+=p[k*4+row]*m[col*4+k];
165
+ res[col*4+row]=sum;
166
+ }
167
+ out.set(res,i*16);
168
+ }
169
+ }
170
+ }
171
+ function skinPalette(world,boneNodes,inverseBind,bindShape,out){
172
+ var J=boneNodes.length;
173
+ var tmp=new Float32Array(16);
174
+ for(var j=0;j<J;j++){
175
+ var node=boneNodes[j];
176
+ var w=world.subarray(node*16,node*16+16);
177
+ var ib=inverseBind.subarray(j*16,j*16+16);
178
+ for(var row=0;row<4;row++){
179
+ for(var col=0;col<4;col++){
180
+ var sum=0;
181
+ for(var k=0;k<4;k++) sum+=w[k*4+row]*ib[col*4+k];
182
+ tmp[col*4+row]=sum;
183
+ }
184
+ }
185
+ var off=12*j;
186
+ out[off]=tmp[0]; out[off+1]=tmp[4]; out[off+2]=tmp[8]; out[off+3]=tmp[12];
187
+ out[off+4]=tmp[1]; out[off+5]=tmp[5]; out[off+6]=tmp[9]; out[off+7]=tmp[13];
188
+ out[off+8]=tmp[2]; out[off+9]=tmp[6]; out[off+10]=tmp[10]; out[off+11]=tmp[14];
189
+ }
190
+ }
191
+ function Fader(skeleton){
192
+ this.skeleton=skeleton;
193
+ this.cur={idx:-1,time:0,speed:1,loop:true};
194
+ this.nxt={idx:-1,time:0,speed:1,loop:true};
195
+ this.fadeDur=0; this.fadeEl=0;
196
+ }
197
+ Fader.prototype.play=function(clipIndex,fadeSeconds,opts){
198
+ opts=opts||{};
199
+ var loop=opts.loop!==undefined?opts.loop:true;
200
+ var speed=opts.speed!==undefined?opts.speed:1;
201
+ if(this.nxt.idx===clipIndex && this.fadeDur>0 && this.fadeEl<this.fadeDur) return;
202
+ if(fadeSeconds<=0){
203
+ this.cur.idx=clipIndex; this.cur.time=0; this.cur.speed=speed; this.cur.loop=loop;
204
+ this.nxt.idx=-1; this.fadeDur=0; this.fadeEl=0;
205
+ return;
206
+ }
207
+ this.cur.idx=this.nxt.idx>=0?this.nxt.idx:this.cur.idx;
208
+ this.cur.time=this.cur.time;
209
+ this.nxt.idx=clipIndex; this.nxt.time=0; this.nxt.speed=speed; this.nxt.loop=loop;
210
+ this.fadeDur=fadeSeconds; this.fadeEl=0;
211
+ };
212
+ Fader.prototype.update=function(dt,clips,outPose){
213
+ if(this.cur.idx<0 && this.nxt.idx<0) return;
214
+ var aPose={t:new Float32Array(this.skeleton.nodes.length*3),r:new Float32Array(this.skeleton.nodes.length*4),s:new Float32Array(this.skeleton.nodes.length*3)};
215
+ var bPose={t:new Float32Array(this.skeleton.nodes.length*3),r:new Float32Array(this.skeleton.nodes.length*4),s:new Float32Array(this.skeleton.nodes.length*3)};
216
+ if(this.cur.idx>=0){
217
+ var c=clips[this.cur.idx];
218
+ this.cur.time+=dt*this.cur.speed;
219
+ if(!this.cur.loop && this.cur.time>c.duration) this.cur.time=c.duration;
220
+ sampleClip(this.skeleton,c,this.cur.time,this.cur.loop,aPose);
221
+ }
222
+ if(this.nxt.idx>=0){
223
+ var c=clips[this.nxt.idx];
224
+ this.nxt.time+=dt*this.nxt.speed;
225
+ if(!this.nxt.loop && this.nxt.time>c.duration) this.nxt.time=c.duration;
226
+ sampleClip(this.skeleton,c,this.nxt.time,this.nxt.loop,bPose);
227
+ }
228
+ var w=0;
229
+ if(this.fadeDur>0){
230
+ this.fadeEl+=dt;
231
+ w=Math.min(1,this.fadeEl/this.fadeDur);
232
+ if(w>=1){
233
+ this.cur=this.nxt; this.nxt.idx=-1; this.fadeDur=0; this.fadeEl=0;
234
+ }
235
+ }
236
+ if(this.cur.idx>=0 && this.nxt.idx>=0){
237
+ blendPoses(aPose,bPose,w,outPose);
238
+ }else if(this.nxt.idx>=0){
239
+ outPose.t.set(bPose.t); outPose.r.set(bPose.r); outPose.s.set(bPose.s);
240
+ }else{
241
+ outPose.t.set(aPose.t); outPose.r.set(aPose.r); outPose.s.set(aPose.s);
242
+ }
243
+ };
244
+ Fader.prototype.state=function(){
245
+ var done=false;
246
+ if(this.nxt.idx>=0){
247
+ var c=this.nxt; done=!c.loop && c.time>= (c.clip?c.clip.duration:0);
248
+ }
249
+ return {clip:this.nxt.idx>=0?this.nxt.idx:this.cur.idx, time:this.nxt.idx>=0?this.nxt.time:this.cur.time, weight:this.fadeDur?Math.min(1,this.fadeEl/this.fadeDur):1, fading:this.fadeDur>0 && this.fadeEl<this.fadeDur, done:done};
250
+ };
251
+ return {createPose:createPose,restPose:restPose,sampleClip:sampleClip,blendPoses:blendPoses,worldMatrices:worldMatrices,skinPalette:skinPalette,Fader:Fader};
252
+ })();