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) 12 kB
import{B as e}from"./bone.pure-BcPT5kCP.esm.min.js";import{M as t,O as i,a as n,aF as s,y as r,aG as a,T as o,aH as h,C as m,aI as l,V as u,aJ as d}from"./index-BPURObVq.esm.min.js";class g{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,s,r){this.name=e,this.id=s,this.bones=[],this.needInitialSkinMatrix=!1,this._isDirty=!0,this._meshesWithPoseMatrix=new Array,this._identity=t.Identity(),this._currentRenderId=-1,this._textureWidth=0,this._textureHeight=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 i,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(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 i in this._ranges)e&&(t+=", ",e=!1),t+=i;t+="}"}return t}getBoneIndexByName(e){for(let t=0,i=this.bones.length;t<i;t++)if(this.bones[t].name===e)return t;return-1}findBoneFromLinkedTransformNode(e){for(const t of this.bones)if(t._linkedTransformNode===e)return t;return null}findBoneFromLinkedTransformNodeName(e){for(const t of this.bones)if(t._linkedTransformNode&&t._linkedTransformNode.name===e)return t;return null}createAnimationRange(e,t,i){if(!this._ranges[e]){this._ranges[e]=new s(e,t,i);for(let n=0,s=this.bones.length;n<s;n++)this.bones[n].animations[0]&&this.bones[n].animations[0].createRange(e,t,i)}}deleteAnimationRange(e,t=!0){for(let i=0,n=this.bones.length;i<n;i++)this.bones[i].animations[0]&&this.bones[i].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,i=!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 u=i&&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,s=o[e];s?n=n&&this.bones[l].copyAnimationRange(s,t,a,i,u):(r.Warn("copyAnimationRange: not same rig, missing source bone "+e),n=!1)}const d=e.getAnimationRange(t);return d&&(this._ranges[t]=new s(t,d.from+a,d.to+a)),n}returnToRest(){for(const e of this.bones)-1!==e._index&&e.returnToRest()}_getHighestAnimationFrame(){let e=0;for(let t=0,i=this.bones.length;t<i;t++)if(this.bones[t].animations[0]){const i=this.bones[t].animations[0].getHighestFrame();e<i&&(e=i)}return e}beginAnimation(e,t,i,n){const s=this.getAnimationRange(e);return s?this._scene.beginAnimation(this,s.from,s.to,t,i,n):null}static MakeAnimationAdditive(e,t=0,i){const n=e.getAnimationRange(i);if(!n)return null;const s=e._scene.getAllAnimatablesByTarget(e);let r=null;for(let e=0;e<s.length;e++){const t=s[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(n[e],t,i)}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 i=0;i<this.bones.length;i++){const n=this.bones[i];n._childUpdateId++;const s=n.getParent();if(s?n.getLocalMatrix().multiplyToRef(s.getFinalMatrix(),n.getFinalMatrix()):t?n.getLocalMatrix().multiplyToRef(t,n.getFinalMatrix()):n.getFinalMatrix().copyFrom(n.getLocalMatrix()),-1!==n._index){const t=null===n._index?i:n._index;n.getAbsoluteInverseBindMatrix().multiplyToArray(n.getFinalMatrix(),e,16*t)}}this._identity.copyToArray(e,16*this.bones.length)}_computeTextureSize(){const e=4*(this.bones.length+1);let t=e,i=1;if(this.isUsingTextureForMatrices){const n=-4&this.getScene().getEngine().getCaps().maxTextureSize;n<t&&(t=n,i=Math.ceil(e/n))}return this._textureWidth=t,this._textureHeight=i,this._textureWidth*this._textureHeight*4}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}let t=null;if(this.needInitialSkinMatrix)for(const e of this._meshesWithPoseMatrix){const i=e.getPoseMatrix();let n=this._isDirty;if(null===t&&(t=this._computeTextureSize()),e._bonesTransformMatrices&&e._bonesTransformMatrices.length===t||(e._bonesTransformMatrices=new Float32Array(t),n=!0),n){if(this._synchronizedWithMesh!==e){this._synchronizedWithMesh=e;for(const e of this.bones)if(!e.getParent()){e.getBindMatrix().multiplyToRef(i,o.Matrix[1]),e._updateAbsoluteBindMatrices(o.Matrix[1])}if(this.isUsingTextureForMatrices){const i=e._transformMatrixTexture?.getSize(),n=i?i.width*i.height*4:0;e._transformMatrixTexture&&n===t||(e._transformMatrixTexture&&e._transformMatrixTexture.dispose(),e._transformMatrixTexture=h.CreateRGBATexture(e._bonesTransformMatrices,this._textureWidth,this._textureHeight,this._scene,!1,!1,m.TEXTURE_NEAREST_SAMPLINGMODE,m.TEXTURETYPE_FLOAT))}}this._computeTransformMatrices(e._bonesTransformMatrices,i),this.isUsingTextureForMatrices&&e._transformMatrixTexture&&e._transformMatrixTexture.update(e._bonesTransformMatrices)}}else{if(!this._isDirty)return;null===t&&(t=this._computeTextureSize()),this._transformMatrices&&this._transformMatrices.length===t||(this._transformMatrices=new Float32Array(t),this.isUsingTextureForMatrices&&(this._transformMatrixTexture&&this._transformMatrixTexture.dispose(),this._transformMatrixTexture=h.CreateRGBATexture(this._transformMatrices,this._textureWidth,this._textureHeight,this._scene,!1,!1,m.TEXTURE_NEAREST_SAMPLINGMODE,m.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,i){const n=new g(t,i||t,this._scene);n.needInitialSkinMatrix=this.needInitialSkinMatrix,n.metadata=this.metadata;for(let t=0;t<this.bones.length;t++){const i=this.bones[t];let s=null;const r=i.getParent();if(r){const e=this.bones.indexOf(r);s=n.bones[e]}const a=new e(i.name,n,s,i.getBindMatrix().clone(),i.getRestMatrix().clone());a._index=i._index,i._linkedTransformNode&&a.linkTransformNode(i._linkedTransformNode),l.DeepCopy(i.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 i of t.animations)i.enableBlending=!0,i.blendingSpeed=e}dispose(){if(this._meshesWithPoseMatrix.length=0,this.metadata=null,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,e.uniqueId=this.uniqueId,this.dimensionsAtRest&&(e.dimensionsAtRest=this.dimensionsAtRest.asArray()),e.bones=[],e.needInitialSkinMatrix=this.needInitialSkinMatrix,this.metadata&&(e.metadata=this.metadata);for(let t=0;t<this.bones.length;t++){const i=this.bones[t],n=i.getParent(),s={parentBoneIndex:n?this.bones.indexOf(n):-1,index:i.getIndex(),name:i.name,id:i.id,uniqueId:i.uniqueId,matrix:i.getBindMatrix().asArray(),rest:i.getRestMatrix().asArray(),linkedTransformNodeId:i.getTransformNode()?.id,linkedTransformNodeUniqueId:i.getTransformNode()?.uniqueId};e.bones.push(s),i.length&&(s.length=i.length),i.metadata&&(s.metadata=i.metadata),i.animations&&i.animations.length>0&&(s.animation=i.animations[0].serialize()),e.ranges=[];for(const t in this._ranges){const i=this._ranges[t];if(!i)continue;const n={};n.name=t,n.from=i.from,n.to=i.to,e.ranges.push(n)}}return e}static Parse(i,n){const s=new g(i.name,i.id,n);let r;for(i.dimensionsAtRest&&(s.dimensionsAtRest=u.FromArray(i.dimensionsAtRest)),s.needInitialSkinMatrix=i.needInitialSkinMatrix,i.metadata&&(s.metadata=i.metadata),r=0;r<i.bones.length;r++){const n=i.bones[r],a=i.bones[r].index;let o=null;n.parentBoneIndex>-1&&(o=s.bones[n.parentBoneIndex]);const h=n.rest?t.FromArray(n.rest):null,m=new e(n.name,s,o,t.FromArray(n.matrix),h,null,a);void 0!==n.id&&null!==n.id&&(m.id=n.id),n.length&&(m.length=n.length),n.metadata&&(m.metadata=n.metadata),n.animation&&m.animations.push(d(n.animation)),void 0!==n.linkedTransformNodeId&&null!==n.linkedTransformNodeId&&(s._hasWaitingData=!0,m._waitingTransformNodeId=n.linkedTransformNodeId),void 0!==n.linkedTransformNodeUniqueId&&null!==n.linkedTransformNodeUniqueId&&(s._hasWaitingData=!0,m._waitingTransformNodeUniqueId=n.linkedTransformNodeUniqueId)}if(i.ranges)for(r=0;r<i.ranges.length;r++){const e=i.ranges[r];s.createAnimationRange(e.name,e.from,e.to)}return s}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 i=0;i<this.bones.length;i++)this._sortBones(i,e,t);this.bones=e}_sortBones(e,t,i){if(i[e])return;i[e]=!0;const n=this.bones[e];if(!n)return;void 0===n._index&&(n._index=e);const s=n.getParent();s&&this._sortBones(this.bones.indexOf(s),t,i),t.push(n)}setCurrentPoseAsRest(){for(const e of this.bones)e.setCurrentPoseAsRest()}}export{g as S}; //# sourceMappingURL=skeleton-DinTjF3y.esm.min.js.map