@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
1 lines • 106 kB
JavaScript
import{M as e,co as t,bi as n,bu as o,aV as r,F as s,aW as i,V as a,cp as l,i as c,m as u,c1 as f,aU as p,bs as d,aD as h,aR as m,bn as g}from"./index-BPURObVq.esm.min.js";import{S as y}from"./standardMaterial.pure-BRp0YAN-.esm.min.js";import{S as b}from"./skeleton-DinTjF3y.esm.min.js";import{B as v}from"./bone.pure-BcPT5kCP.esm.min.js";import{A as x}from"./animationGroup.pure-Bb9o5BkH.esm.min.js";import{M,a as I,F as w}from"./morphTargetManager-CdEb1BHc.esm.min.js";import{P as A}from"./pointLight.pure-BNtejoTr.esm.min.js";import{S as T}from"./spotLight.pure-5s2DDDIt.esm.min.js";import{A as S}from"./assetContainer-CAncviZo.esm.min.js";import"./prepass.defines-DBIG3K_p.esm.min.js";import"./material.detailMapConfiguration-FEC73qwc.esm.min.js";const C=15,_=[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258],O=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0],N=[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577],F=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13],B=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];class P{constructor(e,t,n){this.input=e,this.byteOffset=t,this.endOffset=n,this.bitBuffer=0,this.bitCount=0}readBits(e){this.ensureBits(e);const t=this.bitBuffer&(1<<e)-1;return this.bitBuffer>>>=e,this.bitCount-=e,t}readBit(){if(0===this.bitCount){if(this.byteOffset>=this.endOffset)throw new Error("zlib: unexpected end of input");this.bitBuffer=this.input[this.byteOffset++],this.bitCount=8}const e=1&this.bitBuffer;return this.bitBuffer>>>=1,this.bitCount--,e}alignToByte(){this.bitBuffer=0,this.bitCount=0}readUint16LE(){if(this.ensureByteAligned(),this.byteOffset+2>this.endOffset)throw new Error("zlib: unexpected end of input");const e=this.input[this.byteOffset]|this.input[this.byteOffset+1]<<8;return this.byteOffset+=2,e}readByte(){if(this.ensureByteAligned(),this.byteOffset>=this.endOffset)throw new Error("zlib: unexpected end of input");return this.input[this.byteOffset++]}ensureByteAligned(){if(0!==this.bitCount)throw new Error("deflate: expected byte alignment")}ensureBits(e){for(;this.bitCount<e;){if(this.byteOffset>=this.endOffset)throw new Error("zlib: unexpected end of input");this.bitBuffer|=this.input[this.byteOffset++]<<this.bitCount,this.bitCount+=8}}}class E{constructor(e){this.offset=0,this.adlerA=1,this.adlerB=0,this.bytes=new Uint8Array(e)}writeByte(e){if(this.offset>=this.bytes.byteLength)throw new Error("zlib: output length mismatch");const t=255&e;this.bytes[this.offset++]=t,this.adlerA+=t,this.adlerB+=this.adlerA,this.adlerA%=65521,this.adlerB%=65521}copy(e,t){if(e<=0||e>this.offset)throw new Error("deflate: distance out of range");for(let n=0;n<t;n++)this.writeByte(this.bytes[this.offset-e])}finish(){if(this.offset!==this.bytes.byteLength)throw new Error("zlib: output length mismatch")}adler32(){return(this.adlerB<<16|this.adlerA)>>>0}}class L{constructor(e,t={}){const n=new Array(16).fill(0);let o=0,r=0;for(const t of e){if(!Number.isInteger(t)||t<0||t>C)throw new Error("deflate: invalid huffman code lengths");t>0&&(n[t]++,o++,r=Math.max(r,t))}if(0===o){if(t.allowEmpty)return this.symbolsByLength=[],void(this.maxCodeLength=0);throw new Error("deflate: invalid huffman code lengths")}let s=1;for(let e=1;e<=C;e++)if(s=(s<<1)-n[e],s<0)throw new Error("deflate: invalid huffman code lengths");if(0!==s&&1!==o)throw new Error("deflate: invalid huffman code lengths");const i=new Array(16).fill(0);let a=0;for(let e=1;e<=C;e++)a=a+n[e-1]<<1,i[e]=a;this.symbolsByLength=Array.from({length:16},(e,t)=>{if(0===n[t])return;const o=new Int16Array(1<<t);return o.fill(-1),o});for(let t=0;t<e.length;t++){const n=e[t];0!==n&&(this.symbolsByLength[n][i[n]++]=t)}this.maxCodeLength=r}decode(e){if(0===this.maxCodeLength)throw new Error("deflate: invalid huffman code");let t=0;for(let n=1;n<=this.maxCodeLength;n++){t=t<<1|e.readBit();const o=this.symbolsByLength[n]?.[t]??-1;if(o>=0)return o}throw new Error("deflate: invalid huffman code")}}let k,R;function $(){if(!k){const e=new Array(288);for(let t=0;t<=143;t++)e[t]=8;for(let t=144;t<=255;t++)e[t]=9;for(let t=256;t<=279;t++)e[t]=7;for(let t=280;t<=287;t++)e[t]=8;k=new L(e)}return k}function D(){return R||(R=new L(new Array(32).fill(5))),R}function X(e,t){e.alignToByte();const n=e.readUint16LE();if(65535&(n^~e.readUint16LE()))throw new Error("deflate: invalid stored block length");for(let o=0;o<n;o++)t.writeByte(e.readByte())}function V(e,t,n,o){for(;;){const r=n.decode(e);if(r<256){t.writeByte(r);continue}if(256===r)return;if(r>285)throw new Error("deflate: invalid literal/length symbol");const s=r-257,i=_[s]+e.readBits(O[s]),a=o.decode(e);if(a>29)throw new Error("deflate: invalid distance symbol");const l=N[a]+e.readBits(F[a]);t.copy(l,i)}}function j(e){const t=e.readBits(5)+257,n=e.readBits(5)+1,o=e.readBits(4)+4,r=new Array(19).fill(0);for(let t=0;t<o;t++)r[B[t]]=e.readBits(3);const s=function(e,t,n){const o=[];for(;o.length<n;){const r=t.decode(e);if(r<=15){o.push(r);continue}let s,i;switch(r){case 16:if(0===o.length)throw new Error("deflate: invalid code length repeat");i=o[o.length-1],s=e.readBits(2)+3;break;case 17:i=0,s=e.readBits(3)+3;break;case 18:i=0,s=e.readBits(7)+11;break;default:throw new Error("deflate: invalid code length symbol")}if(o.length+s>n)throw new Error("deflate: invalid code length repeat");for(let e=0;e<s;e++)o.push(i)}return o}(e,new L(r),t+n),i=s.slice(0,t),a=s.slice(t);if(0===i[256])throw new Error("deflate: missing end-of-block code");return{literalLengthTree:new L(i),distanceTree:new L(a,{allowEmpty:!0})}}function U(e,t,n,o,r){let s,i,a,l;if(o?(K(t,n,25,r,"FBX node header"),s=z(e,n),i=z(e,n+8),a=z(e,n+16),l=25):(K(t,n,13,r,"FBX node header"),s=e.getUint32(n,!0),i=e.getUint32(n+4,!0),a=e.getUint32(n+8,!0),l=13),0===s)return null;if(s<=n||s>r)throw new Error(`Invalid FBX node end offset ${s} at offset ${n}`);const c=t[n+l-1];K(t,n+l,c,s,"FBX node name");const u=Z(t,n+l,c);let f=n+l+c;const p=f+a;if(p>s)throw new Error(`Invalid FBX property list length for node '${u}' at offset ${n}`);const d=[];for(let n=0;n<i;n++){const n=W(e,t,f,p);d.push(n.property),f=n.nextOffset}if(f!==p)throw new Error(`Invalid FBX property list length for node '${u}' at offset ${n}`);const h=[];if(f<s)for(;f<s;){const n=U(e,t,f,o,s);if(null===n)break;if(n.endOffset<=f||n.endOffset>s)throw new Error(`Invalid FBX child node end offset ${n.endOffset} at offset ${f}`);h.push(n.node),f=n.endOffset}return{node:{name:u,properties:d,children:h},endOffset:s}}function W(e,t,n,o){K(t,n,1,o,"FBX property type");const r=String.fromCharCode(t[n]);switch(n+=1,r){case"C":K(t,n,1,o,"FBX boolean property");return{property:{type:"boolean",value:0!==t[n]},nextOffset:n+1};case"Y":K(t,n,2,o,"FBX int16 property");return{property:{type:"int16",value:e.getInt16(n,!0)},nextOffset:n+2};case"I":K(t,n,4,o,"FBX int32 property");return{property:{type:"int32",value:e.getInt32(n,!0)},nextOffset:n+4};case"F":K(t,n,4,o,"FBX float32 property");return{property:{type:"float32",value:e.getFloat32(n,!0)},nextOffset:n+4};case"D":K(t,n,8,o,"FBX float64 property");return{property:{type:"float64",value:e.getFloat64(n,!0)},nextOffset:n+8};case"L":K(t,n,8,o,"FBX int64 property");return{property:{type:"int64",value:G(e,n)},nextOffset:n+8};case"S":{K(t,n,4,o,"FBX string property length");const r=e.getUint32(n,!0);K(t,n+4,r,o,"FBX string property data");const s=function(e,t,n){return new TextDecoder("utf-8").decode(e.subarray(t,t+n))}(t,n+4,r);return{property:{type:"string",value:s},nextOffset:n+4+r}}case"R":{K(t,n,4,o,"FBX raw property length");const r=e.getUint32(n,!0);K(t,n+4,r,o,"FBX raw property data");return{property:{type:"raw",value:t.slice(n+4,n+4+r)},nextOffset:n+4+r}}case"f":return Y(e,t,n,"float32[]",4,o);case"d":return Y(e,t,n,"float64[]",8,o);case"i":return Y(e,t,n,"int32[]",4,o);case"l":return Y(e,t,n,"int64[]",8,o);case"b":return Y(e,t,n,"boolean[]",1,o);default:throw new Error(`Unknown FBX property type: '${r}' at offset ${n-1}`)}}function Y(e,t,n,o,r,s){K(t,n,12,s,`FBX array property header for ${o}`);const i=e.getUint32(n,!0),a=e.getUint32(n+4,!0),l=e.getUint32(n+8,!0),c=i*r;let u;if(K(t,n+=12,l,s,`FBX array property data for ${o}`),1===a){u=function(e,t){if(!Number.isInteger(t)||t<0)throw new Error("zlib: invalid expected length");if(e.byteLength<6)throw new Error("zlib: unexpected end of input");const n=e[0],o=e[1];if(8!=(15&n)||n>>4>7||((n<<8)+o)%31!=0)throw new Error("zlib: invalid header");if(32&o)throw new Error("zlib: preset dictionary not supported");const r=new P(e,2,e.byteLength-4),s=new E(t);let i=!1;for(;!i;)switch(i=1===r.readBits(1),r.readBits(2)){case 0:X(r,s);break;case 1:V(r,s,$(),D());break;case 2:{const{literalLengthTree:e,distanceTree:t}=j(r);V(r,s,e,t);break}default:throw new Error("deflate: invalid block type")}if(r.byteOffset<e.byteLength-4)throw new Error("zlib: trailing deflate data");s.finish();const a=(e[e.byteLength-4]<<24|e[e.byteLength-3]<<16|e[e.byteLength-2]<<8|e[e.byteLength-1])>>>0;if(s.adler32()!==a)throw new Error("zlib: adler32 mismatch");return s.bytes}(t.subarray(n,n+l),c)}else{if(0!==a)throw new Error(`Unsupported FBX array encoding: ${a}`);if(l!==c)throw new Error(`Invalid FBX array byte length for ${o}`);u=t.slice(n,n+l)}const f=u.buffer.slice(u.byteOffset,u.byteOffset+u.byteLength);let p;switch(o){case"float32[]":p=new Float32Array(f);break;case"float64[]":p=new Float64Array(f);break;case"int32[]":p=new Int32Array(f);break;case"boolean[]":p=u;break;case"int64[]":p=function(e){const t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=new Float64Array(e.byteLength/8);for(let e=0;e<n.length;e++)n[e]=G(t,8*e);return n}(u);break;default:throw new Error(`Unexpected array type: ${o}`)}return{property:{type:o,value:p},nextOffset:n+l}}function K(e,t,n,o,r){if(t<0||n<0||t+n>o||t+n>e.byteLength)throw new Error(`${r}: unexpected end of input`)}function z(e,t){const n=e.getUint32(t,!0);return 4294967296*e.getUint32(t+4,!0)+n}function G(e,t){const n=e.getUint32(t,!0);return 4294967296*e.getInt32(t+4,!0)+n}function Z(e,t,n){let o="";for(let r=0;r<n;r++)o+=String.fromCharCode(e[t+r]);return o}function H(e){const t=new q(e),n=function(e){const t=e.match(/;\s*FBX\s+(\d+)\.(\d+)\.(\d+)/);if(!t)throw new Error("Cannot determine FBX version from ASCII header");return 1e3*parseInt(t[1])+100*parseInt(t[2])+parseInt(t[3])}(e),o=[];for(;!t.isEOF()&&(t.skipWhitespaceAndComments(),!t.isEOF());){const e=J(t);e&&o.push(e)}return{version:n,nodes:o}}var Q;!function(e){e[e.Identifier=0]="Identifier",e[e.Number=1]="Number",e[e.String=2]="String",e[e.OpenBrace=3]="OpenBrace",e[e.CloseBrace=4]="CloseBrace",e[e.Colon=5]="Colon",e[e.Comma=6]="Comma",e[e.Star=7]="Star",e[e.EOF=8]="EOF"}(Q||(Q={}));class q{constructor(e){this.text=e,this.pos=0,this.len=e.length}isEOF(){return this.skipWhitespaceAndComments(),this.pos>=this.len}peek(){const e=this.pos,t=this.next();return this.pos=e,t}next(){if(this.skipWhitespaceAndComments(),this.pos>=this.len)return{type:8,value:"",pos:this.pos};const e=this.text[this.pos],t=this.pos;switch(e){case"{":return this.pos++,{type:3,value:"{",pos:t};case"}":return this.pos++,{type:4,value:"}",pos:t};case":":return this.pos++,{type:5,value:":",pos:t};case",":return this.pos++,{type:6,value:",",pos:t};case"*":return this.pos++,{type:7,value:"*",pos:t};case'"':return this.readString();default:if(this.isNumberStart(e))return this.readNumber();if(this.isIdentStart(e))return this.readIdentifier();throw new Error(`Unexpected character '${e}' at position ${this.pos}`)}}expect(e){const t=this.next();if(t.type!==e)throw new Error(`Expected token type ${e} but got ${t.type} ('${t.value}') at pos ${t.pos}`);return t}isNextNodeStart(){const e=this.pos;if(this.skipWhitespaceAndComments(),this.pos<this.len&&this.isIdentStart(this.text[this.pos])){for(;this.pos<this.len&&this.isIdentChar(this.text[this.pos]);)this.pos++;for(;this.pos<this.len&&(" "===this.text[this.pos]||"\t"===this.text[this.pos]);)this.pos++;const t=this.pos<this.len&&":"===this.text[this.pos];return this.pos=e,t}return this.pos=e,!1}skipWhitespaceAndComments(){for(;this.pos<this.len;){const e=this.text[this.pos];if(" "===e||"\t"===e||"\r"===e||"\n"===e)this.pos++;else{if(";"!==e)break;for(;this.pos<this.len&&"\n"!==this.text[this.pos];)this.pos++}}}readString(){const e=this.pos;this.pos++;let t="";for(;this.pos<this.len&&'"'!==this.text[this.pos];)"\\"===this.text[this.pos]&&this.pos+1<this.len?(this.pos++,t+=this.text[this.pos]):t+=this.text[this.pos],this.pos++;return this.pos<this.len&&this.pos++,{type:2,value:t,pos:e}}readNumber(){const e=this.pos;for("-"!==this.text[this.pos]&&"+"!==this.text[this.pos]||this.pos++;this.pos<this.len&&this.isDigit(this.text[this.pos]);)this.pos++;if(this.pos<this.len&&"."===this.text[this.pos])for(this.pos++;this.pos<this.len&&this.isDigit(this.text[this.pos]);)this.pos++;if(this.pos<this.len&&("e"===this.text[this.pos]||"E"===this.text[this.pos]))for(this.pos++,this.pos<this.len&&("+"===this.text[this.pos]||"-"===this.text[this.pos])&&this.pos++;this.pos<this.len&&this.isDigit(this.text[this.pos]);)this.pos++;return{type:1,value:this.text.substring(e,this.pos),pos:e}}readIdentifier(){const e=this.pos;for(;this.pos<this.len&&this.isIdentChar(this.text[this.pos]);)this.pos++;return{type:0,value:this.text.substring(e,this.pos),pos:e}}isDigit(e){return e>="0"&&e<="9"}isNumberStart(e){return!!this.isDigit(e)||("-"===e||"+"===e)&&this.pos+1<this.len&&(this.isDigit(this.text[this.pos+1])||"."===this.text[this.pos+1])}isIdentStart(e){return e>="a"&&e<="z"||e>="A"&&e<="Z"||"_"===e}isIdentChar(e){return this.isIdentStart(e)||this.isDigit(e)||"|"===e}}function J(e){const t=e.peek();if(4===t.type||8===t.type)return null;const n=e.next();if(0!==n.type)throw new Error(`Expected identifier for node name, got '${n.value}' at pos ${n.pos}`);const o=n.value;e.expect(5);const r=[],s=[];let i=e.peek();if(7===i.type){e.next();const t=e.expect(1),n=parseInt(t.value);e.expect(3);const i=e.next();if(0===i.type&&"a"===i.value){e.expect(5);const t=function(e,t){const n=[];for(;;){const t=e.peek();if(4===t.type||8===t.type)break;if(6!==t.type){if(1!==t.type)break;{const t=e.next();n.push(Number(t.value))}}else e.next()}if(n.length!==t)throw new Error(`ASCII FBX array declared ${t} values but parsed ${n.length}`);return n}(e,n);r.push({type:"float64[]",value:new Float64Array(t)})}return e.expect(4),{name:o,properties:r,children:s}}for(i=e.peek();3!==i.type&&4!==i.type&&8!==i.type;){if(1===i.type){const t=e.next(),n=ee(t.value);!Number.isInteger(n)||t.value.includes(".")||t.value.includes("e")||t.value.includes("E")?r.push({type:"float64",value:n}):r.push({type:te(n)?"int32":"int64",value:n})}else if(2===i.type){const t=e.next();r.push({type:"string",value:t.value})}else if(0===i.type){if(e.isNextNodeStart())break;const t=e.next();r.push({type:"string",value:t.value})}else{if(6!==i.type)break;e.next()}i=e.peek()}if(i=e.peek(),3===i.type){for(e.next();i=e.peek(),4!==i.type&&8!==i.type;){const t=J(e);if(!t)break;s.push(t)}e.expect(4)}return{name:o,properties:r,children:s}}function ee(e){return Number(e)}function te(e){return e>=-2147483648&&e<=2147483647}function ne(e,t){return e.children.find(e=>e.name===t)}function oe(e,t){return e.nodes.find(e=>e.name===t)}function re(e,t){if(t<e.properties.length)return e.properties[t].value}function se(e){const t=e.indexOf("\0");t>=0&&(e=e.substring(0,t));const n=e.indexOf("::");return n>=0&&(e=e.substring(n+2)),e}function ie(e,t,n){const o=e.childrenOf.get(t)??[],r=[];for(const t of o){const o=e.objects.get(t.id);!o||n&&o.name!==n||r.push({id:t.id,node:o,propertyName:t.propertyName})}return r}function ae(e){return function(e){if("number"==typeof e){if(!Number.isSafeInteger(e))throw new Error(`Unsafe FBX object ID ${e.toString()}: object IDs must be safe integers.`);return e}}(e)}function le(e,t){const n=ae(e);if(void 0!==n)return n;if("string"!=typeof e)return;const o=se(e);return"Scene"===o?0:t.get(o)}function ce(e,t,n,o,r,s,i,a,l){e.push({type:r,childId:s,parentId:i,propertyName:a}),t.has(i)||t.set(i,[]),t.get(i).push({id:s,propertyName:a});n.get(s)&&o.push({reason:"duplicate-parent",message:"FBX object has multiple parents; preserving the existing last-parent behavior.",connectionIndex:l,type:r,childId:s,parentId:i,propertyName:a}),n.set(s,{id:i,propertyName:a})}function ue(e,t){const n=se(re(e,0)??e.name),o=re(e,1)??"";return{...e,properties:[{type:"int64",value:t},{type:"string",value:n},{type:"string",value:o}]}}function fe(e,t){return{name:"Geometry",properties:[{type:"int64",value:t},{type:"string",value:se(re(e,0)??"Geometry")},{type:"string",value:"Mesh"}],children:e.children}}function pe(e,t){const n=se(re(e,1)??"Geometry"),o=ne(e,"Vertices");if(!o)throw new Error(`Geometry '${n}' has no Vertices node`);const r=Me(we(o)),s=ne(e,"PolygonVertexIndex");if(!s)throw new Error(`Geometry '${n}' has no PolygonVertexIndex node`);const i=[],a=function(e){const t=[];let n=[],o=0;for(let r=0;r<e.length;r++){const s=e[r];s<0?(n.push(-(s+1)),t.push({indices:n,startIndex:o}),n=[],o=r+1):n.push(s)}return t}(Ie(we(s))),l=function(e,t,n){const o=[];for(let r=0;r<e.length;r++){const s=e[r];o.push(...de(s,r,t,n))}return o}(a,r,i),c=function(e){const t=[];for(let n=0;n<e.length;n++){const o=e[n];for(let e=0;e<o.indices.length;e++)t.push({polyIndex:n,vertexInPoly:e,controlPointIndex:o.indices[e],globalIndex:o.startIndex+e})}return t}(a),u=ne(e,"LayerElementNormal");let f=null;u&&(f=ve(u,"Normals","NormalsIndex",c,r.length/3,3,i));const p=function(e,t){return e.children.filter(e=>e.name===t)}(e,"LayerElementUV"),d=[];for(const e of p){const t=ne(e,"Name"),n=t?re(t,0)??`UVSet${d.length}`:`UVSet${d.length}`,o=ve(e,"UV","UVIndex",c,r.length/3,2,i);o&&d.push({name:n,data:o})}const h=d.length>0?d[0].data:null,m=ne(e,"LayerElementColor");let g=null;if(m){const e=ve(m,"Colors","ColorIndex",c,r.length/3,4,i);if(e){g=new Float32Array(e.length);for(let t=0;t<e.length;t++)g[t]=e[t]}}const y=ne(e,"LayerElementTangent"),b=ne(e,"LayerElementBinormal"),v=b?ve(b,"Binormals","BinormalsIndex",c,r.length/3,3,i):null,x=y?function(e,t,n,o,r,s){const i=function(e,t,n,o,r,s){const i=ne(e,t);if(!i)return 3;const a=Me(we(i)),l=re(ne(e,"MappingInformationType")??{properties:[]},0)??"",c=re(ne(e,"ReferenceInformationType")??{properties:[]},0)??"",u=ne(e,n),f=u?Ie(we(u)):null,p="IndexToDirect"===c&&f?Math.max(...Array.from(f),0)+1:"ByControlPoint"===l||"ByVertice"===l?r:"AllSame"===l?1:o.length;if(p>0&&a.length%p===0){const e=a.length/p;if(3===e||4===e)return e}return s.push({type:"layer-data-too-short",message:`Could not infer stride for layer '${e.name}', defaulting to 3.`,layerName:e.name}),3}(e,"Tangents","TangentsIndex",t,n,s),a=ve(e,"Tangents","TangentsIndex",t,n,i,s);if(!a)return null;const l=new Float64Array(4*t.length);for(let e=0;e<t.length;e++){const t=e*i,n=4*e;l[n]=a[t],l[n+1]=a[t+1],l[n+2]=a[t+2],l[n+3]=i>=4?a[t+3]:xe(e,l,o,r)}return l}(y,c,r.length/3,f,v,i):null,M=ne(e,"LayerElementMaterial");let I=null;M&&(I=function(e,t){const n=ne(e,"MappingInformationType"),o=ne(e,"ReferenceInformationType");if(!n||!o)return null;const r=re(n,0)??"",s=re(o,0)??"";if("AllSame"===r){const n=ne(e,"Materials"),o=n?Ie(we(n)):null,r=o&&o.length>0?o[0]:0,s=new Int32Array(t);return 0!==r&&s.fill(r),s}if("ByPolygon"===r){const t=ne(e,"Materials");if(!t)return null;const n=Ie(we(t));if("Direct"===s||"IndexToDirect"===s)return n}return null}(M,a.length));const w=function(e,t,n,o,r,s,i,a,l){const c=n.length,u=new Float64Array(3*c),f=new Uint32Array(c);for(let t=0;t<n.length;t++){const o=n[t].controlPointIndex;u[3*t]=e[3*o],u[3*t+1]=e[3*o+1],u[3*t+2]=e[3*o+2],f[t]=o}const p=3*t.length,d=new Uint32Array(p);for(let e=0;e<t.length;e++)d[3*e]=t[e].vertices[0],d[3*e+1]=t[e].vertices[1],d[3*e+2]=t[e].vertices[2];return{positions:u,indices:d,normals:o,uvs:r,uvSets:s,colors:i,tangents:a,binormals:l,controlPointIndices:f}}(r,l,c,f,h,d,g,x,v);let A=null;if(I){let e=!0;const t=I[0];for(let n=1;n<I.length;n++)if(I[n]!==t){e=!1;break}if(!e||0!==t){const e=w.indices.length/3;A=new Int32Array(e);for(let e=0;e<l.length;e++)A[e]=I[l[e].polyIndex]??0}}return{id:t,name:n,positions:w.positions,indices:w.indices,normals:w.normals,uvs:w.uvs,uvSets:w.uvSets,colors:w.colors,tangents:w.tangents,binormals:w.binormals,controlPointIndices:w.controlPointIndices,materialIndices:A,diagnostics:i}}function de(e,t,n,o){if(e.indices.length<3)return o.push({type:"degenerate-polygon",message:`Polygon ${t} has fewer than three vertices.`,polygonIndex:t}),[];if(3===e.indices.length)return[{vertices:[e.startIndex,e.startIndex+1,e.startIndex+2],polyIndex:t}];const r=function(e,t){const n=function(e,t){let n=0,o=0,r=0;for(let s=0;s<e.indices.length;s++){const i=3*e.indices[s],a=3*e.indices[(s+1)%e.indices.length],l=t[i],c=t[i+1],u=t[i+2],f=t[a],p=t[a+1],d=t[a+2];n+=(c-p)*(u+d),o+=(u-d)*(l+f),r+=(l-f)*(c+p)}return[n,o,r]}(e,t),o=Math.abs(n[0]),r=Math.abs(n[1]),s=Math.abs(n[2]);if(o+r+s<1e-12)return null;const i=o>r&&o>s?0:r>s?1:2;return e.indices.map(e=>{const o=t[3*e],r=t[3*e+1],s=t[3*e+2];return 0===i?n[0]>=0?[r,s]:[s,r]:1===i?n[1]>=0?[s,o]:[o,s]:n[2]>=0?[o,r]:[r,o]})}(e,n);if(!r)return o.push({type:"degenerate-polygon",message:`Polygon ${t} has a near-zero normal; using fan triangulation.`,polygonIndex:t}),he(e,t);const s=function(e){let t=0;for(let n=0;n<e.length;n++){const o=e[n],r=e[(n+1)%e.length];t+=o[0]*r[1]-r[0]*o[1]}return t/2}(r);if(Math.abs(s)<1e-12)return o.push({type:"degenerate-polygon",message:`Polygon ${t} projects to near-zero area; using fan triangulation.`,polygonIndex:t}),he(e,t);const i=s>0,a=e.indices.map((e,t)=>t),l=[];let c=0;for(;a.length>3&&c++<e.indices.length*e.indices.length;){let n=!1;for(let o=0;o<a.length;o++){const s=a[(o+a.length-1)%a.length],c=a[o],u=a[(o+1)%a.length];if(me(r[s],r[c],r[u],i)&&!ge(r,a,s,c,u)){l.push({vertices:[e.startIndex+s,e.startIndex+c,e.startIndex+u],polyIndex:t}),a.splice(o,1),n=!0;break}}if(!n)return o.push({type:"triangulation-fallback",message:`Polygon ${t} could not be fully ear-clipped; using fan triangulation.`,polygonIndex:t}),he(e,t)}return l.push({vertices:[e.startIndex+a[0],e.startIndex+a[1],e.startIndex+a[2]],polyIndex:t}),l}function he(e,t){const n=[];for(let o=1;o<e.indices.length-1;o++)n.push({vertices:[e.startIndex,e.startIndex+o,e.startIndex+o+1],polyIndex:t});return n}function me(e,t,n,o){const r=(t[0]-e[0])*(n[1]-e[1])-(t[1]-e[1])*(n[0]-e[0]);return o?r>1e-12:r<-1e-12}function ge(e,t,n,o,r){for(const s of t)if(s!==n&&s!==o&&s!==r&&ye(e[s],e[n],e[o],e[r]))return!0;return!1}function ye(e,t,n,o){const r=Math.abs(be(t,n,o)),s=Math.abs(be(e,t,n)),i=Math.abs(be(e,n,o)),a=Math.abs(be(e,o,t));return Math.abs(r-(s+i+a))<1e-10}function be(e,t,n){return(t[0]-e[0])*(n[1]-e[1])-(t[1]-e[1])*(n[0]-e[0])}function ve(e,t,n,o,r,s,i){const a=ne(e,"MappingInformationType"),l=ne(e,"ReferenceInformationType");if(!a||!l)return null;const c=re(a,0)??"",u=re(l,0)??"",f=ne(e,t);if(!f)return null;const p=Me(we(f));let d=null;if("IndexToDirect"===u){const t=ne(e,n);t&&(d=Ie(we(t)))}const h=new Float64Array(o.length*s);for(let t=0;t<o.length;t++){const n=o[t];let r;r="ByPolygonVertex"===c?"IndexToDirect"===u&&d?d[n.globalIndex]:n.globalIndex:"ByControlPoint"===c||"ByVertice"===c?"IndexToDirect"===u&&d?d[n.controlPointIndex]:n.controlPointIndex:"ByPolygon"===c?"IndexToDirect"===u&&d?d[n.polyIndex]:n.polyIndex:"AllSame"===c?0:n.globalIndex;for(let n=0;n<s;n++){const o=r*s+n;r<0||o>=p.length?(i.push({type:o>=p.length?"layer-data-too-short":"layer-index-out-of-bounds",message:`Layer '${e.name}' references unavailable element ${r}.`,layerName:e.name,index:r}),h[t*s+n]=0):h[t*s+n]=p[o]}}return h}function xe(e,t,n,o){if(!n||!o)return 1;const r=4*e,s=3*e,i=n[s],a=n[s+1],l=n[s+2],c=t[r],u=t[r+1],f=t[r+2];return(a*f-l*u)*o[s]+(l*c-i*f)*o[s+1]+(i*u-a*c)*o[s+2]<0?-1:1}function Me(e){if(e instanceof Float64Array)return e;if(e instanceof Float32Array)return new Float64Array(e);if(e instanceof Int32Array)return new Float64Array(e);if(Array.isArray(e)){const t=new Float64Array(e.length);for(let n=0;n<e.length;n++)t[n]=Number(e[n]);return t}throw new Error(`Cannot convert ${typeof e} to Float64Array`)}function Ie(e){if(e instanceof Int32Array)return e;if(e instanceof Float64Array){const t=new Int32Array(e.length);for(let n=0;n<e.length;n++)t[n]=Math.round(e[n]);return t}if(e instanceof Float32Array){const t=new Int32Array(e.length);for(let n=0;n<e.length;n++)t[n]=Math.round(e[n]);return t}if(Array.isArray(e)){const t=new Int32Array(e.length);for(let n=0;n<e.length;n++)t[n]=Math.round(Number(e[n]));return t}throw new Error(`Cannot convert ${typeof e} to Int32Array`)}function we(e){return 1===e.properties.length?e.properties[0].value:e.properties.map(e=>e.value)}function Ae(e,t,n){const o=e.get(t);if(o)return n?o.get(n):o.values().next().value}function Te(e,t,n,o=0){return _e(e,t,n)?.[o]}function Se(e,t,n,o){return Oe(Te(e,t,n))??o}function Ce(e,t,n,o){const r=_e(e,t,n);if(!r)return o;const s=Oe(r[0]),i=Oe(r[1]),a=Oe(r[2]);return void 0!==s&&void 0!==i&&void 0!==a?[s,i,a]:o}function _e(e,t,n){return function(e,t){const n=[ne(e,"Properties70"),ne(e,"Properties60")].filter(e=>void 0!==e);for(const e of n)for(const n of e.children)if(("P"===n.name||"Property"===n.name)&&re(n,0)===t)return n.properties.slice("Property"===n.name?3:4).map(e=>e.value);return}(e,n)??t?.properties.get(n)?.values}function Oe(e){if("number"==typeof e)return e}function Ne(e,t,n){const o=new Map,r=ne(n,"Properties70");for(const e of r?.children??[]){if("P"!==e.name)continue;const t=Fe(e);t&&o.set(t.name,t)}return{objectType:e,templateName:t,properties:o}}function Fe(e){const t=re(e,0);return t?{name:t,propertyType:re(e,1)??"",label:re(e,2)??"",flags:re(e,3)??"",values:e.properties.slice(4).map(e=>e.value)}:null}function Be(e,t,n,o){const r=se(re(e,1)??"Material"),s=function(e,t){if(!t)return;const n=ne(e,"ShadingModel"),o=n?re(n,0):void 0;if("phong"===o?.toLowerCase())return Ae(t,"Material","FbxSurfacePhong")??Ae(t,"Material");if("lambert"===o?.toLowerCase())return Ae(t,"Material","FbxSurfaceLambert")??Ae(t,"Material");return Ae(t,"Material")}(e,o),i=ne(e,"ShadingModel"),a="phong"===(i?re(i,0)??"Lambert":Te(e,s,"ShadingModel")??"Lambert").toLowerCase()?"Phong":"Lambert",l=function(e,t){const n={};return n.diffuseColor=Pe(e,t,"DiffuseColor")??Pe(e,t,"Diffuse"),n.diffuseFactor=Ee(e,t,"DiffuseFactor"),n.ambientColor=Pe(e,t,"AmbientColor")??Pe(e,t,"Ambient"),n.ambientFactor=Ee(e,t,"AmbientFactor"),n.specularColor=Pe(e,t,"SpecularColor")??Pe(e,t,"Specular"),n.specularFactor=Ee(e,t,"SpecularFactor"),n.shininess=Ee(e,t,"Shininess")??Ee(e,t,"ShininessExponent"),n.emissiveColor=Pe(e,t,"EmissiveColor")??Pe(e,t,"Emissive"),n.emissiveFactor=Ee(e,t,"EmissiveFactor"),n.opacity=Ee(e,t,"Opacity"),n.transparencyFactor=Ee(e,t,"TransparencyFactor"),n}(e,s),c=function(e,t,n){const o=[],r=ie(t,e,"Texture");for(const{id:e,node:s,propertyName:i}of r){const r=ne(s,"FileName"),a=ne(s,"RelativeFilename"),l=r?re(r,0)??"":"",c=a?re(a,0)??"":"";let u,f;const p=Ee(s,n,"UVRotation")??Ee(s,n,"Rotation");let d;u=Le(s,n,"UVTranslation")??Le(s,n,"Translation"),f=Le(s,n,"UVScaling")??Le(s,n,"Scaling");const h=Te(s,n,"UVSet");h&&h.length>0&&(d=h),u??=Re(s,"ModelUVTranslation"),f??=Re(s,"ModelUVScaling");let m=null;const g=ie(t,e,"Video");for(const{node:e}of g){const t=ne(e,"Content");if(t&&t.properties.length>0){const e=t.properties[0].value;e instanceof Uint8Array&&e.length>0?m=e:e instanceof ArrayBuffer&&e.byteLength>0&&(m=new Uint8Array(e))}}o.push({propertyName:i??"DiffuseColor",fileName:l,relativeFileName:c,id:e,embeddedData:m,uvTranslation:u,uvScaling:f,uvRotation:p,uvSetName:d})}return o}(t,n,o?Ae(o,"Texture","FbxFileTexture")??Ae(o,"Texture"):void 0);return{id:t,name:r,type:a,properties:l,textures:c}}function Pe(e,t,n){const o=_e(e,t,n);if(!o||o.length<3)return;const r=ke(o[0]),s=ke(o[1]),i=ke(o[2]);return void 0!==r&&void 0!==s&&void 0!==i?[r,s,i]:void 0}function Ee(e,t,n){return ke(Te(e,t,n))}function Le(e,t,n){const o=_e(e,t,n);if(!o)return;const r=ke(o[0]),s=ke(o[1]);return void 0!==r&&void 0!==s?[r,s]:void 0}function ke(e){if("number"==typeof e)return e}function Re(e,t){const n=ne(e,t);if(!n)return;const o=ke(n.properties[0]?.value),r=ke(n.properties[1]?.value);return void 0!==o&&void 0!==r?[o,r]:void 0}function $e(e,t,n){const o=n.parentOf.get(e);if(!o)return null;const r=o.id,s=n.objects.get(r);if(!s||"Geometry"!==s.name)return null;const i=n.parentOf.get(r),a=i?n.objects.get(i.id):void 0,l="Model"===a?.name?i.id:void 0,c=ie(n,e,"Deformer");if(0===c.length)return null;const u=new Map;for(const{id:e,node:t}of c){if("Cluster"!==re(t,2))continue;const o=ie(n,e,"Model");o.length>0&&u.set(o[0].id,{clusterId:e,clusterNode:t})}const f=function(e,t){const n=t.parentOf.get(e),o=n?t.objects.get(n.id):void 0,r="Model"===o?.name?n.id:void 0;if(void 0===r)return new Map;for(const[,e]of Array.from(t.objects)){if("Pose"!==e.name||"BindPose"!==re(e,2))continue;const t=new Map;for(const n of e.children){if("PoseNode"!==n.name)continue;const e=ne(n,"Node"),o=ne(n,"Matrix"),r=e?.properties[0]?.value,s=o?.properties[0]?.value;if("number"!=typeof r)continue;const i=Ke(s);16===i?.length&&t.set(r,i)}if(t.has(r))return t}return new Map}(r,n),p=[],d=function(e,t,n,o){const r=[],s=new Set,i=function(e,t){const n=new Set(Array.from(e.keys()));for(const o of Array.from(e.keys())){let e=De(o,t);for(;void 0!==e;){const o=t.objects.get(e);if(!o||"Model"!==o.name)break;n.add(e),e=De(e,t)}}return n}(e,n),a=function(e,t){const n=new Map;for(const o of Array.from(e)){let r=De(o,t);for(;void 0!==r;){if(e.has(r)){n.set(o,r);break}r=De(r,t)}}return n}(i,n),l=function(e,t){const n=new Map;for(const o of Array.from(e)){const e=t.get(o);void 0!==e&&(n.has(e)||n.set(e,[]),n.get(e).push(o))}return n}(i,a),c=Array.from(i).filter(e=>!a.has(e)).map(e=>({modelId:e,parentIndex:-1}));for(;c.length>0;){const{modelId:i,parentIndex:a}=c.shift();if(s.has(i))continue;s.add(i);const u=n.objects.get(i);if(!u)continue;const f=r.length,p=e.get(i),d=Ve(u),{bindPoseMatrix:h,transformLinkMatrix:m,transformAssociateModelMatrix:g,clusterMode:y}=p?je(p.clusterNode):{bindPoseMatrix:null,transformLinkMatrix:null,transformAssociateModelMatrix:null,clusterMode:"Unknown"},b=Ue(i,se(re(u,1)??`Bone${f}`),void 0!==p,y,h,m,g,t.get(i)??null);o.push(...b),r.push({modelId:i,name:se(re(u,1)??`Bone${f}`),index:f,parentIndex:a,isCluster:void 0!==p,translation:d.translation,rotation:d.rotation,preRotation:d.preRotation,postRotation:d.postRotation,rotationPivot:d.rotationPivot,scalingPivot:d.scalingPivot,rotationOffset:d.rotationOffset,scalingOffset:d.scalingOffset,scale:d.scale,rotationOrder:d.rotationOrder,inheritType:d.inheritType,clusterMode:y,bindPoseMatrix:h,transformLinkMatrix:m,transformAssociateModelMatrix:g,modelBindPoseMatrix:t.get(i)??null,diagnostics:b});for(const e of l.get(i)??[])s.has(e)||c.push({modelId:e,parentIndex:f})}return r}(u,f,n,p);if(0===d.length)return null;const{boneIndices:h,boneWeights:m}=function(e,t){let n=0;for(const o of e){const e=t.get(o.modelId);if(!e)continue;const r=ne(e.clusterNode,"Indexes");if(!r)continue;const s=Ye(r.properties[0]?.value);if(s)for(let e=0;e<s.length;e++)s[e]>n&&(n=s[e])}const o=n+1,r=new Array(o),s=new Array(o);for(let e=0;e<o;e++)r[e]=[],s[e]=[];for(const n of e){const e=t.get(n.modelId);if(!e)continue;const o=ne(e.clusterNode,"Indexes"),i=ne(e.clusterNode,"Weights");if(!o||!i)continue;const a=Ye(o.properties[0]?.value),l=Ke(i.properties[0]?.value);if(a&&l)for(let e=0;e<a.length;e++){const t=a[e];r[t].push(n.index),s[t].push(l[e])}}for(let e=0;e<o;e++){if(0===r[e].length)continue;const t=r[e].map((t,n)=>({index:t,weight:s[e][n]}));t.sort((e,t)=>t.weight-e.weight);const n=t.slice(0,8);r[e]=n.map(e=>e.index),s[e]=n.map(e=>e.weight)}for(let e=0;e<o;e++){const t=s[e].reduce((e,t)=>e+t,0);if(t>0)for(let n=0;n<s[e].length;n++)s[e][n]/=t}return{boneIndices:r,boneWeights:s}}(d,u);return{id:e,geometryId:r,meshBindPoseMatrix:void 0!==l?f.get(l)??null:null,bones:d,boneIndices:h,boneWeights:m,diagnostics:p}}function De(e,t){const n=t.connections.find(n=>"OO"===n.type&&n.childId===e&&"Model"===t.objects.get(n.parentId)?.name);return n?.parentId}function Xe(e){const t=re(e,2);return"Root"===t||"LimbNode"===t}function Ve(e){const t=[0,0,0],n=[0,0,0],o=[0,0,0],r=[0,0,0],s=[0,0,0],i=[0,0,0],a=[0,0,0],l=[0,0,0],c=[1,1,1];let u=0,f=1;const p=ne(e,"Properties70");if(!p)return{translation:t,rotation:n,preRotation:o,postRotation:r,rotationPivot:s,scalingPivot:i,rotationOffset:a,scalingOffset:l,scale:c,rotationOrder:u,inheritType:f};for(const e of p.children){if("P"!==e.name)continue;const p=re(e,0);if(p)switch(p){case"Lcl Translation":t[0]=We(e.properties[4]?.value)??0,t[1]=We(e.properties[5]?.value)??0,t[2]=We(e.properties[6]?.value)??0;break;case"Lcl Rotation":n[0]=We(e.properties[4]?.value)??0,n[1]=We(e.properties[5]?.value)??0,n[2]=We(e.properties[6]?.value)??0;break;case"PreRotation":o[0]=We(e.properties[4]?.value)??0,o[1]=We(e.properties[5]?.value)??0,o[2]=We(e.properties[6]?.value)??0;break;case"PostRotation":r[0]=We(e.properties[4]?.value)??0,r[1]=We(e.properties[5]?.value)??0,r[2]=We(e.properties[6]?.value)??0;break;case"RotationPivot":s[0]=We(e.properties[4]?.value)??0,s[1]=We(e.properties[5]?.value)??0,s[2]=We(e.properties[6]?.value)??0;break;case"ScalingPivot":i[0]=We(e.properties[4]?.value)??0,i[1]=We(e.properties[5]?.value)??0,i[2]=We(e.properties[6]?.value)??0;break;case"RotationOffset":a[0]=We(e.properties[4]?.value)??0,a[1]=We(e.properties[5]?.value)??0,a[2]=We(e.properties[6]?.value)??0;break;case"ScalingOffset":l[0]=We(e.properties[4]?.value)??0,l[1]=We(e.properties[5]?.value)??0,l[2]=We(e.properties[6]?.value)??0;break;case"Lcl Scaling":c[0]=We(e.properties[4]?.value)??1,c[1]=We(e.properties[5]?.value)??1,c[2]=We(e.properties[6]?.value)??1;break;case"RotationOrder":u=We(e.properties[4]?.value)??0;break;case"InheritType":f=We(e.properties[4]?.value)??1}}return{translation:t,rotation:n,preRotation:o,postRotation:r,rotationPivot:s,scalingPivot:i,rotationOffset:a,scalingOffset:l,scale:c,rotationOrder:u,inheritType:f}}function je(e){let t=null,n=null,o=null,r="Normalize";const s=ne(e,"Transform");if(s&&s.properties[0]){const e=s.properties[0].value;e instanceof Float64Array&&16===e.length?t=e:e instanceof Float32Array&&16===e.length&&(t=new Float64Array(e))}const i=ne(e,"TransformLink");if(i&&i.properties[0]){const e=i.properties[0].value;e instanceof Float64Array&&16===e.length?n=e:e instanceof Float32Array&&16===e.length&&(n=new Float64Array(e))}const a=ne(e,"TransformAssociateModel");if(a&&a.properties[0]){const e=a.properties[0].value;e instanceof Float64Array&&16===e.length?o=e:e instanceof Float32Array&&16===e.length&&(o=new Float64Array(e))}const l=ne(e,"Mode"),c=l?re(l,0):void 0;return"Normalize"===c||"Additive"===c||"TotalOne"===c?r=c:c&&(r="Unknown"),{bindPoseMatrix:t,transformLinkMatrix:n,transformAssociateModelMatrix:o,clusterMode:r}}function Ue(e,t,n,o,r,s,i,a){if(!n)return[];const l=[];return"Additive"!==o&&"TotalOne"!==o||l.push({type:"cluster-mode-runtime-unsupported",message:`Cluster mode '${o}' is preserved but not applied by Babylon linear blend skinning.`,boneModelId:e,boneName:t,clusterMode:o}),r||l.push({type:"missing-cluster-transform",message:"Cluster is missing Transform matrix; falling back to rest/bind-pose data.",boneModelId:e,boneName:t,clusterMode:o}),s||l.push({type:"missing-cluster-transform-link",message:"Cluster is missing TransformLink matrix; falling back to model bind pose or rest transform.",boneModelId:e,boneName:t,clusterMode:o}),a||l.push({type:"missing-bind-pose-matrix",message:"No BindPose matrix was found for this bone model.",boneModelId:e,boneName:t,clusterMode:o}),i&&l.push({type:"associate-model-present",message:"TransformAssociateModel is preserved for future associate-model skinning semantics.",boneModelId:e,boneName:t,clusterMode:o}),l}function We(e){if("number"==typeof e)return e}function Ye(e){if(e instanceof Int32Array)return e;if(e instanceof Float64Array){const t=new Int32Array(e.length);for(let n=0;n<e.length;n++)t[n]=Math.round(e[n]);return t}return null}function Ke(e){return e instanceof Float64Array?e:e instanceof Float32Array?new Float64Array(e):null}function ze(e,t){if(0===t.length)return[];const n=new Map;for(const o of t){const t=o.bones.filter(e=>e.isCluster).map(e=>e.modelId);if(0===t.length)continue;const r=Ge(t,e),s=n.get(r);s?s.push(o):n.set(r,[o])}return Array.from(n.entries()).sort(([e],[t])=>et(e,t)).map(([t,n])=>function(e,t,n){const o=new Set,r=new Set,s=new Map,i=new Map;for(const e of t)for(const t of e.bones){i.has(t.modelId)||i.set(t.modelId,i.size);let e=s.get(t.modelId);if(e||(e=[],s.set(t.modelId,e)),e.push(t),t.isCluster){o.add(t.modelId);for(const e of Ze(t.modelId,n))r.add(e)}}const a=function(e){const t=[];for(const[n,o]of Array.from(e)){const e=o.filter(e=>e.isCluster&&e.transformLinkMatrix).map(e=>e.transformLinkMatrix);if(e.length<2)continue;const r=e[0];e.some(e=>!qe(r,e,1e-5))&&t.push(`Model ${n.toString()} has differing Cluster.TransformLink matrices across skins`)}return t}(s),l=new Map;for(const[e,t]of Array.from(s))l.set(e,Qe(t));const c=function(e,t){const n=new Map;for(const o of Array.from(e)){const r=He(o,t);void 0!==r&&e.has(r)&&n.set(o,r)}return n}(r,n),u=function(e,t,n){const o=new Map;for(const n of Array.from(e)){const e=t.get(n);if(void 0===e)continue;let r=o.get(e);r||(r=[],o.set(e,r)),r.push(n)}for(const e of Array.from(o.values()))e.sort((e,t)=>tt(e,t,n));const r=Array.from(e).filter(e=>!t.has(e)).sort((e,t)=>tt(e,t,n)),s=[],i=[...r];for(;i.length>0;){const e=i.shift();s.push(e),i.push(...o.get(e)??[])}return s}(r,c,i),f=[],p=new Map;for(const e of u){const t=l.get(e)??Je(e,n);if(!t)continue;const r=c.get(e),s=void 0===r?-1:p.get(r)??-1,i=f.length,a={...t,index:i,parentIndex:s,isCluster:o.has(e)};f.push(a),p.set(e,i)}const d=t.map(t=>function(e,t,n){const o=e.bones.map(e=>{const o=n.get(e.modelId);if(void 0===o&&e.isCluster)throw new Error(`FBX rig resolver: cluster bone ${e.name} is missing from resolved rig ${t}`);return o??-1});return{skinId:e.id,geometryId:e.geometryId,rigId:t,skinBoneIndexToRigBoneIndex:o,clusterModelIds:new Set(e.bones.filter(e=>e.isCluster).map(e=>e.modelId))}}(t,`rig_${e.toString()}`,p));return{id:`rig_${e.toString()}`,rootModelIds:f.filter(e=>e.parentIndex<0).map(e=>e.modelId),bones:f,modelIdToBoneIndex:p,clusterModelIds:o,skinBindings:d,warnings:a}}(t,n,e))}function Ge(e,t){const n=function(e,t){if(0===e.length)return;const n=e.map(e=>Ze(e,t)),o=new Set(n[0]);for(const e of n.slice(1))for(const t of Array.from(o))e.includes(t)||o.delete(t);return n[0].find(e=>o.has(e))}(e,t)??e[0];let o=n,r=He(o,t);for(;void 0!==r;){const e=t.objects.get(r);if(!e||"Model"!==e.name||!Xe(e))break;o=r,r=He(r,t)}return o}function Ze(e,t){const n=[];let o=e;for(;void 0!==o;){const e=t.objects.get(o);if(!e||"Model"!==e.name)break;n.push(o),o=He(o,t)}return n}function He(e,t){const n=t.connections.find(n=>"OO"===n.type&&n.childId===e&&"Model"===t.objects.get(n.parentId)?.name);return n?.parentId}function Qe(e){return e.find(e=>e.isCluster&&e.transformLinkMatrix)??e.find(e=>e.isCluster)??e.find(e=>e.modelBindPoseMatrix)??e[0]}function qe(e,t,n){if(e.length!==t.length)return!1;for(let o=0;o<e.length;o++)if(Math.abs(e[o]-t[o])>n)return!1;return!0}function Je(e,t){const n=t.objects.get(e);if(!n||"Model"!==n.name)return null;const o=Ve(n);return{modelId:e,name:se(re(n,1)??`Bone${e.toString()}`),index:-1,parentIndex:-1,isCluster:!1,translation:o.translation,rotation:o.rotation,preRotation:o.preRotation,postRotation:o.postRotation,rotationPivot:o.rotationPivot,scalingPivot:o.scalingPivot,rotationOffset:o.rotationOffset,scalingOffset:o.scalingOffset,scale:o.scale,rotationOrder:o.rotationOrder,inheritType:o.inheritType,clusterMode:"Unknown",bindPoseMatrix:null,transformLinkMatrix:null,transformAssociateModelMatrix:null,modelBindPoseMatrix:null,diagnostics:[]}}function et(e,t){return e<t?-1:e>t?1:0}function tt(e,t,n){return(n.get(e)??Number.MAX_SAFE_INTEGER)-(n.get(t)??Number.MAX_SAFE_INTEGER)||et(e,t)}const nt=46186158e3,ot=1/23,rt=1e-4,st=1e-5,it=[24,25,30,48,50,60,100,120];function at(e,t,n){const o=se(re(t,1)??"Animation"),r=function(e){const t=ne(e,"Properties70");if(!t)return null;let n=0,o=null;for(const e of t.children){if("P"!==e.name)continue;const t=re(e,0);"LocalStart"===t||"ReferenceStart"===t?n=yt(e.properties[4]?.value)??n:"LocalStop"!==t&&"ReferenceStop"!==t||(o=yt(e.properties[4]?.value)??o)}return null!==o?{start:n,stop:o}:null}(t),s=ie(n,e,"AnimationLayer");if(0===s.length)return null;const i=[],a=[],l=[],c=[];let u=1/0,f=0;for(const{id:e,node:t}of s){const o=se(re(t,1)??"Layer");let r=100,s=0;const p=ne(t,"Properties70");if(p)for(const e of p.children){if("P"!==e.name)continue;const t=re(e,0);if("Weight"===t){const t=e.properties[4]?.value;"number"==typeof t&&(r=t)}else if("BlendMode"===t){const t=e.properties[4]?.value;"number"==typeof t&&(s=t)}}const d=ie(n,e,"AnimationCurveNode"),h=[],m=[],g=[];for(const{id:e,node:t}of d){const r=lt(e,t,n);if(!r){const r=ct(e,t,n);if(r){ut(r.curves,e=>{e<u&&(u=e),e>f&&(f=e)}),m.push(r),a.push(r);const t={type:"unsupported-curve-node",message:`AnimationCurveNode '${r.type}' is preserved as diagnostic data but not evaluated at runtime.`,layerName:o,curveNodeId:e,curveNodeType:r.type,targetId:r.targetId,propertyName:r.propertyName};g.push(t),c.push(t)}continue}for(const e of r.curves)for(const t of e.keys)t.time<u&&(u=t.time),t.time>f&&(f=t.time);h.push(r),i.push(r)}l.push({name:o,weight:r,normalizedWeight:r/100,blendMode:s,curveNodes:h,unsupportedCurveNodes:m,diagnostics:g})}if(0===i.length&&0===a.length)return null;l.length>1&&c.push({type:"multiple-animation-layers",message:"Multiple animation layers are preserved, but runtime blending is not yet evaluated."});for(const e of l){if(0!==e.blendMode){const t={type:"unsupported-layer-blend-mode",message:`Animation layer blend mode ${e.blendMode} is preserved but not yet blended at runtime.`,layerName:e.name};e.diagnostics.push(t),c.push(t)}if(100!==e.weight){const t={type:"partial-layer-weight",message:`Animation layer weight ${e.weight} is preserved but not yet applied at runtime.`,layerName:e.name};e.diagnostics.push(t),c.push(t)}}const p=u>0&&isFinite(u)?u:0;if(p>0){for(const e of i)for(const t of e.curves)for(const e of t.keys)e.time-=p;for(const e of a)for(const t of e.curves)for(const e of t.keys)e.time-=p;f-=p}const d=r?Math.max(r.start-p,0):0,h=r?Math.max(r.stop-p,d):0,m=h>d,g=m?d:0,y=m?h:f;return{name:o,startTime:g,stopTime:y,duration:Math.max(y-g,0),curveNodes:i,layers:l,unsupportedCurveNodes:a,diagnostics:c}}function lt(e,t,n){const o=se(re(t,1)??"");if("T"===o||"R"===o||"S"===o){const t=function(e,t){for(const n of t.connections)if(n.childId===e&&"OP"===n.type){const e=t.objects.get(n.parentId);if(e&&"Model"===e.name)return n.parentId}return null}(e,n);if(null===t)return null;const r=ft(e,n);return 0===r.length?null:{type:o,targetModelId:t,curves:r}}if("DeformPercent"===o){const t=function(e,t){for(const n of t.connections)if(n.childId===e&&"OP"===n.type){const e=t.objects.get(n.parentId);if(e&&"Deformer"===e.name){if("BlendShapeChannel"===re(e,2))return n.parentId}}for(const n of t.connections)if(n.childId===e&&"OO"===n.type){const e=t.objects.get(n.parentId);if(e&&"Deformer"===e.name){if("BlendShapeChannel"===re(e,2))return n.parentId}}return null}(e,n);if(null===t)return null;const o=ft(e,n);return 0===o.length?null:{type:"DeformPercent",targetModelId:t,curves:o}}return null}function ct(e,t,n){const o=se(re(t,1)??""),r=ft(e,n),s=function(e){const t={},n=ne(e,"Properties70");for(const e of n?.children??[]){if("P"!==e.name)continue;const n=re(e,0);if(!n?.startsWith("d|"))continue;const o=bt(e.properties[4]?.value);null!==o&&(t[n]=o)}return t}(t);if(0===r.length&&0===Object.keys(s).length)return null;let i,a=null;for(const t of n.connections)if(t.childId===e&&"OP"===t.type){a=t.parentId,i=t.propertyName;break}return{type:o,id:e,targetId:a,propertyName:i,curveCount:r.length,curves:r,defaultValues:s}}function ut(e,t){for(const n of e)for(const e of n.keys)t(e.time)}function ft(e,t){const n=[];for(const o of t.connections)if(o.parentId===e&&"OP"===o.type){const e=t.objects.get(o.childId);if(!e||"AnimationCurve"!==e.name)continue;const r=o.propertyName??"d|X",s=pt(e);if(s.length>0){const t=dt(e,s);n.push({channel:r,keys:t?ht(s):s,isSampled:t})}}if(0===n.length){const o=ie(t,e,"AnimationCurve"),r=["d|X","d|Y","d|Z"];for(let e=0;e<o.length&&e<3;e++){const t=pt(o[e].node);if(t.length>0){const s=dt(o[e].node,t);n.push({channel:r[e],keys:s?ht(t):t,isSampled:s})}}}return n}function pt(e){const t=ne(e,"KeyTime"),n=ne(e,"KeyValueFloat");if(!t||!n)return[];const o=function(e){if(e instanceof Float64Array)return e;return null}(t.properties[0]?.value),r=vt(n.properties[0]?.value),s=gt(ne(e,"KeyAttrFlags")?.properties[0]?.value),i=vt(ne(e,"KeyAttrDataFloat")?.properties[0]?.value),a=gt(ne(e,"KeyAttrRefCount")?.properties[0]?.value);if(!o||!r)return[];if(o.length!==r.length)return[];const l=function(e,t,n){if(!t||0===t.length)return new Array(e).fill(-1);if(n&&n.length>0){let t=0;for(const e of n)t+=e;if(t===e){const e=[];for(let t=0;t<n.length;t++){const o=n[t];for(let n=0;n<o;n++)e.push(t)}return e}}if(t.length===e)return Array.from({length:e},(e,t)=>t);if(1===t.length)return new Array(e).fill(0);return Array.from({length:e},(e,n)=>Math.min(n,t.length-1))}(o.length,s,a),c=[];for(let e=0;e<o.length;e++){const t=l[e],n=t>=0?s?.[t]??0:0,a=4*t;c.push({time:Number(o[e])/nt,value:r[e],interpolation:xt(n),constantMode:256&n?"next":"standard",rightSlope:Mt(i,a),nextLeftSlope:Mt(i,a+1)})}return c}function dt(e,t){return"FbxMayaSample Curve"===se(re(e,1)??"")||function(e){if(e.length<8)return!1;const t=[];for(let n=1;n<e.length;n++){const o=e[n].time-e[n-1].time;if(!(o>0))return!1;t.push(o)}const n=t.reduce((e,t)=>e+t,0)/t.length;if(n>ot)return!1;const o=Math.max(1e-6,.05*n);if(t.some(e=>Math.abs(e-n)>o))return!1;const r=1/n;if(!it.some(e=>Math.abs(r-e)<=Math.max(.25,.02*e)))return!1;return!function(e){let t=!1,n=!0,o=!0,r=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=0;for(const t of e)r=Math.min(r,t.value),s=Math.max(s,t.value);for(let r=0;r<e.length-1;r++){const s=e[r],a=e[r+1];if("cubic"!==s.interpolation)continue;t=!0;const l=a.time-s.time;if(!(l>0))continue;const c=(a.value-s.value)/l,u=s.rightSlope,f=s.nextLeftSlope;if(void 0!==u&&void 0!==f){(Math.abs(u)>st||Math.abs(f)>st)&&(o=!1);for(const e of[.25,.5,.75]){const t=It(s.value,a.value,u,f,l,e),n=s.value+e*l*c;i=Math.max(i,Math.abs(t-n))}}else n=!1}if(!t||!n||o)return!1;const a=s-r,l=Math.max(rt,.01*a);return i>l}(e)}(t)}function ht(e){return e.map(e=>({time:e.time,value:e.value,interpolation:"linear"}))}function mt(e,t){if(!e||0===e.keys.length)return null;const n=e.keys;if(t<=n[0].time)return n[0].value;if(t>=n[n.length-1].time)return n[n.length-1].value;for(let o=0;o<n.length-1;o++){const r=n[o],s=n[o+1];if(t<r.time||t>s.time)continue;if(s.time===r.time)return r.value;if("constant"===r.interpolation)return"next"===r.constantMode?s.value:r.value;const i=s.time-r.time,a=(t-r.time)/i;if("cubic"===r.interpolation&&!e.isSampled){const e=(s.value-r.value)/i,t=r.rightSlope??e,n=r.nextLeftSlope??e;return It(r.value,s.value,t,n,i,a)}return r.value+a*(s.value-r.value)}return n[n.length-1].value}function gt(e){if(e instanceof Int32Array)return e;if(e instanceof Float32Array||e instanceof Float64Array){const t=new Int32Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[n];return t}return null}function yt(e){return"number"==typeof e?e/nt:null}function bt(e){return"number"==typeof e?e:null}function vt(e){if(e instanceof Float32Array)return e;if(e instanceof Float64Array){const t=new Float32Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[n];return t}return null}function xt(e){return 8&e?"cubic":4&e?"linear":2&e?"constant":"linear"}function Mt(e,t){if(!e||t<0||t>=e.length)return;const n=e[t];return Number.isFinite(n)?n:void 0}function It(e,t,n,o,r,s){const i=s*s,a=i*s;return(2*a-3*i+1)*e+(a-2*i+s)*r*n+(-2*a+3*i)*t+(a-i)*r*o}function wt(e,t,n){const o=n.parentOf.get(e);if(!o)return null;const r=n.objects.get(o.id);if(!r||"Geometry"!==r.name)return null;const s=o.id,i=[],a=ie(n,e,"Deformer");for(const{id:e,node:t}of a){if("BlendShapeChannel"!==re(t,2))continue;const o=se(re(t,1)??"MorphTarget");let r=0;const s=ne(t,"Properties70");if(s)for(const e of s.children){if("P"!==e.name)continue;if("DeformPercent"===re(e,0)){const t=e.properties[4]?.value;"number"==typeof t&&(r=t)}}const a=At(t),l=[],c=ie(n,e,"Geometry");for(const{node:e}of c){if("Shape"!==re(e,2))continue;const t=Ct(e);t&&l.push(t)}if(l.length>0){const t=[],n=Tt(a,l,e,o,t);i.push({name:o,id:e,deformPercent:r,shapes:St(l,n),fullWeights:n?[...n].sort((e,t)=>e-t):null,diagnostics:t})}}return 0===i.length?null:{id:e,geometryId:s,channels:i}}function At(e){const t=ne(e,"FullWeights"),n=t?.properties[0]?.value;return n&&(n instanceof Float64Array||n instanceof Float32Array||n instanceof Int32Array)?Array.from(n,e=>Number(e)):null}function Tt(e,t,n,o,r){return e?e.length!==t.length?(1===t.length||r.push({type:"full-weights-mismatch",message:`FullWeights length ${e.length} does not match shape count ${t.length}; using the first shape for compatibility.`,channelId:n,channelName:o}),null):e:(t.length>1&&r.push({type:"missing-full-weights",message:"Blend shape channel has multiple shapes but no FullWeights; using the first shape for compatibility.",channelId:n,channelName:o}),null)}function St(e,t){return t&&t.length===e.length?e.map((e,n)=>({shape:e,weight:t[n]})).sort((e,t)=>e.weight-t.weight).map(e=>e.shape):e.length>1?[e[0]]:e}function Ct(e){const t=ne(e,"Indexes"),n=ne(e,"Vertices");if(!t||!n)return null;const o=t.properties[0]?.value,r=n.properties[0]?.value;if(!o||!r)return null;const s=function(e){if(e instanceof Uint32Array)return e;if(e instanceof Int32Array||e instanceof Float32Ar