@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.
3 lines (2 loc) • 6.08 kB
JavaScript
import{aR as e,V as t,M as o,aY as n,aD as r,bG as s,bn as i}from"./index-BPURObVq.esm.min.js";import{A as a}from"./assetContainer-CAncviZo.esm.min.js";import"./animation-BA-814pW.esm.min.js";import{B as l}from"./bone.pure-BcPT5kCP.esm.min.js";import{S as f}from"./skeleton-DinTjF3y.esm.min.js";import"./tools-BGvNItez.esm.min.js";const c="Xposition",p="Yposition",m="Zposition",h="Xrotation",d="Yrotation",u="Zrotation";class E{constructor(t){this.loopMode=e.ANIMATIONLOOPMODE_CYCLE,this.list=[],this.root=w(),this.numFrames=0,this.frameRate=0,this.skeleton=t}}function w(){return{name:"",type:"",offset:new t,channels:[],children:[],frames:[],parent:null}}function N(t,n,r){const s=function(e){const t=e.offset.x,n=e.offset.y,r=e.offset.z;return o.Translation(t,n,r)}(t),i=new l(t.name,r.skeleton,n,s),a=function(t,o){if(0===t.frames.length)return[];const n=[],r=t.channels.some(e=>e===c||e===p||e===m),s=t.channels.some(e=>e===h||e===d||e===u),i=new e(`${t.name}_pos`,"position",o.frameRate,e.ANIMATIONTYPE_VECTOR3,o.loopMode),a=new e(`${t.name}_rot`,"rotationQuaternion",o.frameRate,e.ANIMATIONTYPE_QUATERNION,o.loopMode),l=[],f=[];for(let e=0;e<t.frames.length;e++){const o=t.frames[e];r&&o.position&&l.push({frame:o.frame,value:o.position.clone()}),s&&f.push({frame:o.frame,value:o.rotation.clone()})}return l.length>0&&(i.setKeys(l),n.push(i)),f.length>0&&(a.setKeys(f),n.push(a)),n}(t,r);for(const e of a)e.getKeys()&&e.getKeys().length>0&&i.animations.push(e);for(const e of t.children)N(e,i,r)}function g(e,s,i,a){if("ENDSITE"===i.type)return;const l={frame:0,position:new t,rotation:new r};l.frame=s,l.position=new t,l.rotation=new r,i.frames.push(l);let f=o.Identity();for(let t=0;t<i.channels.length;++t){const r=i.channels[t],s=e[a.i++];if(!s)continue;const E=parseFloat(s.trim());if(r.endsWith("position"))switch(r){case c:l.position.x=E;break;case p:l.position.y=E;break;case m:l.position.z=E}else if(r.endsWith("rotation")){const e=n.ToRadians(E);let t;switch(r){case h:t=o.RotationX(e);break;case d:t=o.RotationY(e);break;case u:t=o.RotationZ(e)}f=t.multiply(f)}}r.FromRotationMatrixToRef(f,l.rotation);for(const t of i.children)g(e,s,t,a)}function I(e,o,n,r){const s=w();s.parent=n,r.list.push(s);let i=o.trim().split(/\s+/);if("END"===i[0].toUpperCase()&&"SITE"===i[1].toUpperCase()?(s.type="ENDSITE",s.name="ENDSITE"):(s.name=i[1],s.type=i[0].toUpperCase()),"{"!=e.shift()?.trim())throw new Error("Expected opening { after type & name");const a=e.shift()?.trim().split(/\s+/);if(!a)throw new Error("Unexpected end of file: missing OFFSET");if(i=a,"OFFSET"!=i[0].toUpperCase())throw new Error("Expected OFFSET, but got: "+i[0]);if(4!=i.length)throw new Error("OFFSET: Invalid number of values");const l=new t(parseFloat(i[1]),parseFloat(i[2]),parseFloat(i[3]));if(isNaN(l.x)||isNaN(l.y)||isNaN(l.z))throw new Error("OFFSET: Invalid values");if(s.offset=l,"ENDSITE"!=s.type){if(i=e.shift()?.trim().split(/\s+/),!i)throw new Error("Unexpected end of file: missing CHANNELS");if("CHANNELS"!=i[0].toUpperCase())throw new Error("Expected CHANNELS definition");const t=parseInt(i[1]);s.channels=i.splice(2,t),s.children=[]}for(;e.length>0;){const t=e.shift()?.trim();if("}"===t)return s;t&&s.children.push(I(e,t,s,r))}throw new Error("Unexpected end of file: missing closing brace")}function H(e,t,o,n){const r=e.split("\n"),{loopMode:s}=n;t._blockEntityCollection=!!o;const i=new f("","",t);i._parentContainer=o,t._blockEntityCollection=!1;const a=new E(i);a.loopMode=s;const l=r.shift();if(!l||"HIERARCHY"!==l.trim().toUpperCase())throw new Error("HIERARCHY expected");const c=r.shift();if(!c)throw new Error("Unexpected end of file after HIERARCHY");const p=I(r,c.trim(),null,a),m=r.shift();if(!m||"MOTION"!==m.trim().toUpperCase())throw new Error("MOTION expected");const h=r.shift();if(!h)throw new Error("Unexpected end of file before frame count");const d=h.trim().split(/[\s]+/);if(d.length<2)throw new Error("Invalid frame count line");const u=parseInt(d[1]);if(isNaN(u))throw new Error("Failed to read number of frames.");a.numFrames=u;const w=r.shift();if(!w)throw new Error("Unexpected end of file before frame time");const H=w.trim().split(/[\s]+/);if(H.length<3)throw new Error("Invalid frame time line");const R=parseFloat(H[2]);if(isNaN(R))throw new Error("Failed to read frame time.");if(R<=0)throw new Error("Failed to read frame time. Invalid value "+R);a.frameRate=1/R;for(let e=0;e<u;++e){const t=r.shift();if(!t)continue;g(t.trim().split(/[\s]+/)||[],e,p,{i:0})}return a.root=p,N(a.root,null,a),a.skeleton.returnToRest(),a.skeleton}class R{constructor(e){this.name=s.name,this.extensions=s.extensions,this._loadingOptions={...R._DefaultLoadingOptions,...e??{}}}static get _DefaultLoadingOptions(){return{loopMode:e.ANIMATIONLOOPMODE_CYCLE}}createPlugin(e){return new R(e[s.name])}canDirectLoad(e){return this.isBvhHeader(e)}isBvhHeader(e){return"HIERARCHY"==e.split("\n")[0]}isNotBvhHeader(e){return!this.isBvhHeader(e)}importMeshAsync(e,t,o){if("string"!=typeof o)return Promise.reject("BVH loader expects string data.");if(this.isNotBvhHeader(o))return Promise.reject("BVH loader expects HIERARCHY header.");try{const e=H(o,t,null,this._loadingOptions);return Promise.resolve({meshes:[],particleSystems:[],skeletons:[e],animationGroups:[],transformNodes:[],geometries:[],lights:[],spriteManagers:[]})}catch(e){return Promise.reject(e)}}loadAsync(e,t){return"string"!=typeof t?Promise.reject("BVH loader expects string data."):this.isNotBvhHeader(t)?Promise.reject("BVH loader expects HIERARCHY header."):this.importMeshAsync(null,e,t).then(()=>{})}loadAssetContainerAsync(e,t){if("string"!=typeof t)return Promise.reject("BVH loader expects string data.");if(this.isNotBvhHeader(t))return Promise.reject("BVH loader expects HIERARCHY header.");const o=new a(e);try{const n=H(t,e,o,this._loadingOptions);return o.skeletons.push(n),Promise.resolve(o)}catch(e){return Promise.reject(e)}}}let y=!1;function C(){y||(y=!0,i(new R))}C();export{R as BVHFileLoader,C as RegisterBVHFileLoader};
//# sourceMappingURL=bvhFileLoader-B2Zfzin1.esm.min.js.map