@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) • 11.1 kB
JavaScript
import{B as e}from"./bone-BwMp7T0B.esm.min.js";import{M as t,O as s,H as n,bv as i,L as r,aE as a,aS as o,C as h,bs as m,V as l}from"./index-Bw2N6w1y.esm.min.js";import{R as g}from"./rawTexture-CPAqHIA7.esm.min.js";class u{get useTextureToStoreBoneMatrices(){return this._useTextureToStoreBoneMatrices}set useTextureToStoreBoneMatrices(e){this._useTextureToStoreBoneMatrices=e,this._markAsDirty()}get animationPropertiesOverride(){return this._animationPropertiesOverride?this._animationPropertiesOverride:this._scene.animationPropertiesOverride}set animationPropertiesOverride(e){this._animationPropertiesOverride=e}get isUsingTextureForMatrices(){return this.useTextureToStoreBoneMatrices&&this._canUseTextureForBones}get uniqueId(){return this._uniqueId}constructor(e,i,r){this.name=e,this.id=i,this.bones=[],this.needInitialSkinMatrix=!1,this._isDirty=!0,this._meshesWithPoseMatrix=new Array,this._identity=t.Identity(),this._currentRenderId=-1,this._ranges={},this._absoluteTransformIsDirty=!0,this._canUseTextureForBones=!1,this._uniqueId=0,this._numBonesWithLinkedTransformNode=0,this._hasWaitingData=null,this._parentContainer=null,this.doNotSerialize=!1,this._useTextureToStoreBoneMatrices=!0,this._animationPropertiesOverride=null,this.onBeforeComputeObservable=new s,this.bones=[],this._scene=r||n.LastCreatedScene,this._uniqueId=this._scene.getUniqueId(),this._scene.addSkeleton(this),this._isDirty=!0;const a=this._scene.getEngine().getCaps();this._canUseTextureForBones=a.textureFloat&&a.maxVertexTextureImageUnits>0}getClassName(){return"Skeleton"}getChildren(){return this.bones.filter((e=>!e.getParent()))}getTransformMatrices(e){if(this.needInitialSkinMatrix){if(!e)throw new Error("getTransformMatrices: When using the needInitialSkinMatrix flag, a mesh must be provided");return e._bonesTransformMatrices||this.prepare(!0),e._bonesTransformMatrices}return this._transformMatrices&&!this._isDirty||this.prepare(!this._transformMatrices),this._transformMatrices}getTransformMatrixTexture(e){return this.needInitialSkinMatrix&&e._transformMatrixTexture?e._transformMatrixTexture:this._transformMatrixTexture}getScene(){return this._scene}toString(e){let t=`Name: ${this.name}, nBones: ${this.bones.length}`;if(t+=`, nAnimationRanges: ${this._ranges?Object.keys(this._ranges).length:"none"}`,e){t+=", Ranges: {";let e=!0;for(const s in this._ranges)e&&(t+=", ",e=!1),t+=s;t+="}"}return t}getBoneIndexByName(e){for(let t=0,s=this.bones.length;t<s;t++)if(this.bones[t].name===e)return t;return-1}createAnimationRange(e,t,s){if(!this._ranges[e]){this._ranges[e]=new i(e,t,s);for(let n=0,i=this.bones.length;n<i;n++)this.bones[n].animations[0]&&this.bones[n].animations[0].createRange(e,t,s)}}deleteAnimationRange(e,t=!0){for(let s=0,n=this.bones.length;s<n;s++)this.bones[s].animations[0]&&this.bones[s].animations[0].deleteRange(e,t);this._ranges[e]=null}getAnimationRange(e){return this._ranges[e]||null}getAnimationRanges(){const e=[];let t;for(t in this._ranges)e.push(this._ranges[t]);return e}copyAnimationRange(e,t,s=!1){if(this._ranges[t]||!e.getAnimationRange(t))return!1;let n=!0;const a=this._getHighestAnimationFrame()+1,o={},h=e.bones;let m,l;for(l=0,m=h.length;l<m;l++)o[h[l].name]=h[l];this.bones.length!==h.length&&(r.Warn(`copyAnimationRange: this rig has ${this.bones.length} bones, while source as ${h.length}`),n=!1);const g=s&&this.dimensionsAtRest&&e.dimensionsAtRest?this.dimensionsAtRest.divide(e.dimensionsAtRest):null;for(l=0,m=this.bones.length;l<m;l++){const e=this.bones[l].name,i=o[e];i?n=n&&this.bones[l].copyAnimationRange(i,t,a,s,g):(r.Warn("copyAnimationRange: not same rig, missing source bone "+e),n=!1)}const u=e.getAnimationRange(t);return u&&(this._ranges[t]=new i(t,u.from+a,u.to+a)),n}returnToRest(){for(const e of this.bones)-1!==e._index&&e.returnToRest()}_getHighestAnimationFrame(){let e=0;for(let t=0,s=this.bones.length;t<s;t++)if(this.bones[t].animations[0]){const s=this.bones[t].animations[0].getHighestFrame();e<s&&(e=s)}return e}beginAnimation(e,t,s,n){const i=this.getAnimationRange(e);return i?this._scene.beginAnimation(this,i.from,i.to,t,s,n):null}static MakeAnimationAdditive(e,t=0,s){const n=e.getAnimationRange(s);if(!n)return null;const i=e._scene.getAllAnimatablesByTarget(e);let r=null;for(let e=0;e<i.length;e++){const t=i[e];if(t.fromFrame===n?.from&&t.toFrame===n?.to){r=t;break}}const o=e.getAnimatables();for(let e=0;e<o.length;e++){const n=o[e].animations;if(n)for(let e=0;e<n.length;e++)a.MakeAnimationAdditive(n[e],t,s)}return r&&(r.isAdditive=!0),e}_markAsDirty(){this._isDirty=!0,this._absoluteTransformIsDirty=!0}_registerMeshWithPoseMatrix(e){this._meshesWithPoseMatrix.push(e)}_unregisterMeshWithPoseMatrix(e){const t=this._meshesWithPoseMatrix.indexOf(e);t>-1&&this._meshesWithPoseMatrix.splice(t,1)}_computeTransformMatrices(e,t){this.onBeforeComputeObservable.notifyObservers(this);for(let s=0;s<this.bones.length;s++){const n=this.bones[s];n._childUpdateId++;const i=n.getParent();if(i?n.getLocalMatrix().multiplyToRef(i.getFinalMatrix(),n.getFinalMatrix()):t?n.getLocalMatrix().multiplyToRef(t,n.getFinalMatrix()):n.getFinalMatrix().copyFrom(n.getLocalMatrix()),-1!==n._index){const t=null===n._index?s:n._index;n.getAbsoluteInverseBindMatrix().multiplyToArray(n.getFinalMatrix(),e,16*t)}}this._identity.copyToArray(e,16*this.bones.length)}prepare(e=!1){if(!e){const e=this.getScene().getRenderId();if(this._currentRenderId===e)return;this._currentRenderId=e}if(this._numBonesWithLinkedTransformNode>0)for(const e of this.bones)if(e._linkedTransformNode){const t=e._linkedTransformNode;e.position=t.position,t.rotationQuaternion?e.rotationQuaternion=t.rotationQuaternion:e.rotation=t.rotation,e.scaling=t.scaling}if(this.needInitialSkinMatrix)for(const e of this._meshesWithPoseMatrix){const t=e.getPoseMatrix();let s=this._isDirty;if(e._bonesTransformMatrices&&e._bonesTransformMatrices.length===16*(this.bones.length+1)||(e._bonesTransformMatrices=new Float32Array(16*(this.bones.length+1)),s=!0),s){if(this._synchronizedWithMesh!==e){this._synchronizedWithMesh=e;for(const e of this.bones)if(!e.getParent()){e.getBindMatrix().multiplyToRef(t,o.Matrix[1]),e._updateAbsoluteBindMatrices(o.Matrix[1])}if(this.isUsingTextureForMatrices){const t=4*(this.bones.length+1);e._transformMatrixTexture&&e._transformMatrixTexture.getSize().width===t||(e._transformMatrixTexture&&e._transformMatrixTexture.dispose(),e._transformMatrixTexture=g.CreateRGBATexture(e._bonesTransformMatrices,4*(this.bones.length+1),1,this._scene,!1,!1,h.TEXTURE_NEAREST_SAMPLINGMODE,h.TEXTURETYPE_FLOAT))}}this._computeTransformMatrices(e._bonesTransformMatrices,t),this.isUsingTextureForMatrices&&e._transformMatrixTexture&&e._transformMatrixTexture.update(e._bonesTransformMatrices)}}else{if(!this._isDirty)return;this._transformMatrices&&this._transformMatrices.length===16*(this.bones.length+1)||(this._transformMatrices=new Float32Array(16*(this.bones.length+1)),this.isUsingTextureForMatrices&&(this._transformMatrixTexture&&this._transformMatrixTexture.dispose(),this._transformMatrixTexture=g.CreateRGBATexture(this._transformMatrices,4*(this.bones.length+1),1,this._scene,!1,!1,h.TEXTURE_NEAREST_SAMPLINGMODE,h.TEXTURETYPE_FLOAT))),this._computeTransformMatrices(this._transformMatrices,null),this.isUsingTextureForMatrices&&this._transformMatrixTexture&&this._transformMatrixTexture.update(this._transformMatrices)}this._isDirty=!1}getAnimatables(){if(!this._animatables||this._animatables.length!==this.bones.length){this._animatables=[];for(let e=0;e<this.bones.length;e++)this._animatables.push(this.bones[e])}return this._animatables}clone(t,s){const n=new u(t,s||t,this._scene);n.needInitialSkinMatrix=this.needInitialSkinMatrix;for(let t=0;t<this.bones.length;t++){const s=this.bones[t];let i=null;const r=s.getParent();if(r){const e=this.bones.indexOf(r);i=n.bones[e]}const a=new e(s.name,n,i,s.getBindMatrix().clone(),s.getRestMatrix().clone());a._index=s._index,s._linkedTransformNode&&a.linkTransformNode(s._linkedTransformNode),m.DeepCopy(s.animations,a.animations)}if(this._ranges){n._ranges={};for(const e in this._ranges){const t=this._ranges[e];t&&(n._ranges[e]=t.clone())}}return this._isDirty=!0,n.prepare(!0),n}enableBlending(e=.01){for(const t of this.bones)for(const s of t.animations)s.enableBlending=!0,s.blendingSpeed=e}dispose(){if(this._meshesWithPoseMatrix.length=0,this.getScene().stopAnimation(this),this.getScene().removeSkeleton(this),this._parentContainer){const e=this._parentContainer.skeletons.indexOf(this);e>-1&&this._parentContainer.skeletons.splice(e,1),this._parentContainer=null}this._transformMatrixTexture&&(this._transformMatrixTexture.dispose(),this._transformMatrixTexture=null)}serialize(){const e={};e.name=this.name,e.id=this.id,this.dimensionsAtRest&&(e.dimensionsAtRest=this.dimensionsAtRest.asArray()),e.bones=[],e.needInitialSkinMatrix=this.needInitialSkinMatrix;for(let t=0;t<this.bones.length;t++){const s=this.bones[t],n=s.getParent(),i={parentBoneIndex:n?this.bones.indexOf(n):-1,index:s.getIndex(),name:s.name,id:s.id,matrix:s.getBindMatrix().asArray(),rest:s.getRestMatrix().asArray(),linkedTransformNodeId:s.getTransformNode()?.id};e.bones.push(i),s.length&&(i.length=s.length),s.metadata&&(i.metadata=s.metadata),s.animations&&s.animations.length>0&&(i.animation=s.animations[0].serialize()),e.ranges=[];for(const t in this._ranges){const s=this._ranges[t];if(!s)continue;const n={};n.name=t,n.from=s.from,n.to=s.to,e.ranges.push(n)}}return e}static Parse(s,n){const i=new u(s.name,s.id,n);let r;for(s.dimensionsAtRest&&(i.dimensionsAtRest=l.FromArray(s.dimensionsAtRest)),i.needInitialSkinMatrix=s.needInitialSkinMatrix,r=0;r<s.bones.length;r++){const n=s.bones[r],o=s.bones[r].index;let h=null;n.parentBoneIndex>-1&&(h=i.bones[n.parentBoneIndex]);const m=n.rest?t.FromArray(n.rest):null,l=new e(n.name,i,h,t.FromArray(n.matrix),m,null,o);void 0!==n.id&&null!==n.id&&(l.id=n.id),n.length&&(l.length=n.length),n.metadata&&(l.metadata=n.metadata),n.animation&&l.animations.push(a.Parse(n.animation)),void 0!==n.linkedTransformNodeId&&null!==n.linkedTransformNodeId&&(i._hasWaitingData=!0,l._waitingTransformNodeId=n.linkedTransformNodeId)}if(s.ranges)for(r=0;r<s.ranges.length;r++){const e=s.ranges[r];i.createAnimationRange(e.name,e.from,e.to)}return i}computeAbsoluteMatrices(e=!1){(this._absoluteTransformIsDirty||e)&&(this.bones[0].computeAbsoluteMatrices(),this._absoluteTransformIsDirty=!1)}computeAbsoluteTransforms(e=!1){this.computeAbsoluteMatrices(e)}getPoseMatrix(){let e=null;return this._meshesWithPoseMatrix.length>0&&(e=this._meshesWithPoseMatrix[0].getPoseMatrix()),e}sortBones(){const e=[],t=new Array(this.bones.length);for(let s=0;s<this.bones.length;s++)this._sortBones(s,e,t);this.bones=e}_sortBones(e,t,s){if(s[e])return;s[e]=!0;const n=this.bones[e];if(!n)return;void 0===n._index&&(n._index=e);const i=n.getParent();i&&this._sortBones(this.bones.indexOf(i),t,s),t.push(n)}setCurrentPoseAsRest(){for(const e of this.bones)e.setCurrentPoseAsRest()}}export{u as S};
//# sourceMappingURL=skeleton-CkTZwWbM.esm.min.js.map