UNPKG

@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.2 kB
import{B as t}from"./bone-WlJTsCYz.esm.min.js";import{M as e,O as s,G as n,bt as i,L as r,ay as a,aO as o,C as h,bp as m,V as l}from"./index-yNtOWHHe.esm.min.js";import{R as g}from"./rawTexture-BOCcQuA1.esm.min.js";class d{get useTextureToStoreBoneMatrices(){return this._useTextureToStoreBoneMatrices}set useTextureToStoreBoneMatrices(t){this._useTextureToStoreBoneMatrices=t,this._markAsDirty()}get animationPropertiesOverride(){return this._animationPropertiesOverride?this._animationPropertiesOverride:this._scene.animationPropertiesOverride}set animationPropertiesOverride(t){this._animationPropertiesOverride=t}get isUsingTextureForMatrices(){return this.useTextureToStoreBoneMatrices&&this._canUseTextureForBones}get uniqueId(){return this._uniqueId}constructor(t,i,r){this.name=t,this.id=i,this.bones=[],this.needInitialSkinMatrix=!1,this._isDirty=!0,this._meshesWithPoseMatrix=new Array,this._identity=e.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.metadata=null,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((t=>!t.getParent()))}getTransformMatrices(t){if(this.needInitialSkinMatrix){if(!t)throw new Error("getTransformMatrices: When using the needInitialSkinMatrix flag, a mesh must be provided");return t._bonesTransformMatrices||this.prepare(!0),t._bonesTransformMatrices}return this._transformMatrices&&!this._isDirty||this.prepare(!this._transformMatrices),this._transformMatrices}getTransformMatrixTexture(t){return this.needInitialSkinMatrix&&t._transformMatrixTexture?t._transformMatrixTexture:this._transformMatrixTexture}getScene(){return this._scene}toString(t){let e=`Name: ${this.name}, nBones: ${this.bones.length}`;if(e+=`, nAnimationRanges: ${this._ranges?Object.keys(this._ranges).length:"none"}`,t){e+=", Ranges: {";let t=!0;for(const s in this._ranges)t&&(e+=", ",t=!1),e+=s;e+="}"}return e}getBoneIndexByName(t){for(let e=0,s=this.bones.length;e<s;e++)if(this.bones[e].name===t)return e;return-1}createAnimationRange(t,e,s){if(!this._ranges[t]){this._ranges[t]=new i(t,e,s);for(let n=0,i=this.bones.length;n<i;n++)this.bones[n].animations[0]&&this.bones[n].animations[0].createRange(t,e,s)}}deleteAnimationRange(t,e=!0){for(let s=0,n=this.bones.length;s<n;s++)this.bones[s].animations[0]&&this.bones[s].animations[0].deleteRange(t,e);this._ranges[t]=null}getAnimationRange(t){return this._ranges[t]||null}getAnimationRanges(){const t=[];let e;for(e in this._ranges)t.push(this._ranges[e]);return t}copyAnimationRange(t,e,s=!1){if(this._ranges[e]||!t.getAnimationRange(e))return!1;let n=!0;const a=this._getHighestAnimationFrame()+1,o={},h=t.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&&t.dimensionsAtRest?this.dimensionsAtRest.divide(t.dimensionsAtRest):null;for(l=0,m=this.bones.length;l<m;l++){const t=this.bones[l].name,i=o[t];i?n=n&&this.bones[l].copyAnimationRange(i,e,a,s,g):(r.Warn("copyAnimationRange: not same rig, missing source bone "+t),n=!1)}const d=t.getAnimationRange(e);return d&&(this._ranges[e]=new i(e,d.from+a,d.to+a)),n}returnToRest(){for(const t of this.bones)-1!==t._index&&t.returnToRest()}_getHighestAnimationFrame(){let t=0;for(let e=0,s=this.bones.length;e<s;e++)if(this.bones[e].animations[0]){const s=this.bones[e].animations[0].getHighestFrame();t<s&&(t=s)}return t}beginAnimation(t,e,s,n){const i=this.getAnimationRange(t);return i?this._scene.beginAnimation(this,i.from,i.to,e,s,n):null}static MakeAnimationAdditive(t,e=0,s){const n=t.getAnimationRange(s);if(!n)return null;const i=t._scene.getAllAnimatablesByTarget(t);let r=null;for(let t=0;t<i.length;t++){const e=i[t];if(e.fromFrame===n?.from&&e.toFrame===n?.to){r=e;break}}const o=t.getAnimatables();for(let t=0;t<o.length;t++){const n=o[t].animations;if(n)for(let t=0;t<n.length;t++)a.MakeAnimationAdditive(n[t],e,s)}return r&&(r.isAdditive=!0),t}_markAsDirty(){this._isDirty=!0,this._absoluteTransformIsDirty=!0}_registerMeshWithPoseMatrix(t){this._meshesWithPoseMatrix.push(t)}_unregisterMeshWithPoseMatrix(t){const e=this._meshesWithPoseMatrix.indexOf(t);e>-1&&this._meshesWithPoseMatrix.splice(e,1)}_computeTransformMatrices(t,e){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()):e?n.getLocalMatrix().multiplyToRef(e,n.getFinalMatrix()):n.getFinalMatrix().copyFrom(n.getLocalMatrix()),-1!==n._index){const e=null===n._index?s:n._index;n.getAbsoluteInverseBindMatrix().multiplyToArray(n.getFinalMatrix(),t,16*e)}}this._identity.copyToArray(t,16*this.bones.length)}prepare(t=!1){if(!t){const t=this.getScene().getRenderId();if(this._currentRenderId===t)return;this._currentRenderId=t}if(this._numBonesWithLinkedTransformNode>0)for(const t of this.bones)if(t._linkedTransformNode){const e=t._linkedTransformNode;t.position=e.position,e.rotationQuaternion?t.rotationQuaternion=e.rotationQuaternion:t.rotation=e.rotation,t.scaling=e.scaling}if(this.needInitialSkinMatrix)for(const t of this._meshesWithPoseMatrix){const e=t.getPoseMatrix();let s=this._isDirty;if(t._bonesTransformMatrices&&t._bonesTransformMatrices.length===16*(this.bones.length+1)||(t._bonesTransformMatrices=new Float32Array(16*(this.bones.length+1)),s=!0),s){if(this._synchronizedWithMesh!==t){this._synchronizedWithMesh=t;for(const t of this.bones)if(!t.getParent()){t.getBindMatrix().multiplyToRef(e,o.Matrix[1]),t._updateAbsoluteBindMatrices(o.Matrix[1])}if(this.isUsingTextureForMatrices){const e=4*(this.bones.length+1);t._transformMatrixTexture&&t._transformMatrixTexture.getSize().width===e||(t._transformMatrixTexture&&t._transformMatrixTexture.dispose(),t._transformMatrixTexture=g.CreateRGBATexture(t._bonesTransformMatrices,4*(this.bones.length+1),1,this._scene,!1,!1,h.TEXTURE_NEAREST_SAMPLINGMODE,h.TEXTURETYPE_FLOAT))}}this._computeTransformMatrices(t._bonesTransformMatrices,e),this.isUsingTextureForMatrices&&t._transformMatrixTexture&&t._transformMatrixTexture.update(t._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 t=0;t<this.bones.length;t++)this._animatables.push(this.bones[t])}return this._animatables}clone(e,s){const n=new d(e,s||e,this._scene);n.needInitialSkinMatrix=this.needInitialSkinMatrix,n.metadata=this.metadata;for(let e=0;e<this.bones.length;e++){const s=this.bones[e];let i=null;const r=s.getParent();if(r){const t=this.bones.indexOf(r);i=n.bones[t]}const a=new t(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 t in this._ranges){const e=this._ranges[t];e&&(n._ranges[t]=e.clone())}}return this._isDirty=!0,n.prepare(!0),n}enableBlending(t=.01){for(const e of this.bones)for(const s of e.animations)s.enableBlending=!0,s.blendingSpeed=t}dispose(){if(this._meshesWithPoseMatrix.length=0,this.metadata=null,this.getScene().stopAnimation(this),this.getScene().removeSkeleton(this),this._parentContainer){const t=this._parentContainer.skeletons.indexOf(this);t>-1&&this._parentContainer.skeletons.splice(t,1),this._parentContainer=null}this._transformMatrixTexture&&(this._transformMatrixTexture.dispose(),this._transformMatrixTexture=null)}serialize(){const t={};t.name=this.name,t.id=this.id,this.dimensionsAtRest&&(t.dimensionsAtRest=this.dimensionsAtRest.asArray()),t.bones=[],t.needInitialSkinMatrix=this.needInitialSkinMatrix,this.metadata&&(t.metadata=this.metadata);for(let e=0;e<this.bones.length;e++){const s=this.bones[e],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};t.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()),t.ranges=[];for(const e in this._ranges){const s=this._ranges[e];if(!s)continue;const n={};n.name=e,n.from=s.from,n.to=s.to,t.ranges.push(n)}}return t}static Parse(s,n){const i=new d(s.name,s.id,n);let r;for(s.dimensionsAtRest&&(i.dimensionsAtRest=l.FromArray(s.dimensionsAtRest)),i.needInitialSkinMatrix=s.needInitialSkinMatrix,s.metadata&&(i.metadata=s.metadata),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?e.FromArray(n.rest):null,l=new t(n.name,i,h,e.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 t=s.ranges[r];i.createAnimationRange(t.name,t.from,t.to)}return i}computeAbsoluteMatrices(t=!1){(this._absoluteTransformIsDirty||t)&&(this.bones[0].computeAbsoluteMatrices(),this._absoluteTransformIsDirty=!1)}computeAbsoluteTransforms(t=!1){this.computeAbsoluteMatrices(t)}getPoseMatrix(){let t=null;return this._meshesWithPoseMatrix.length>0&&(t=this._meshesWithPoseMatrix[0].getPoseMatrix()),t}sortBones(){const t=[],e=new Array(this.bones.length);for(let s=0;s<this.bones.length;s++)this._sortBones(s,t,e);this.bones=t}_sortBones(t,e,s){if(s[t])return;s[t]=!0;const n=this.bones[t];if(!n)return;void 0===n._index&&(n._index=t);const i=n.getParent();i&&this._sortBones(this.bones.indexOf(i),e,s),e.push(n)}setCurrentPoseAsRest(){for(const t of this.bones)t.setCurrentPoseAsRest()}}export{d as S}; //# sourceMappingURL=skeleton-DrI_Z0uE.esm.min.js.map