Files
moon-gaming/games/New folder/Extensions/3D/Model3DRuntimeObject3DRenderer.js
T
2023-11-23 20:37:33 +00:00

3 lines
4.2 KiB
JavaScript

var gdjs;(function(o){const E=n=>{n.metalness&&(n.metalness=0)},M=n=>{const t=n;if(!!t.material)if(Array.isArray(t.material))for(let e=0;e<t.material.length;e++)E(t.material[e]);else E(t.material)},T=n=>n.traverse(M),p=n=>{const t=new THREE.MeshBasicMaterial;return n.color&&(t.color=n.color),n.map&&(t.map=n.map),t},f=n=>{const t=n;if(!!t.material)if(Array.isArray(t.material))for(let e=0;e<t.material.length;e++)t.material[e]=p(t.material[e]);else t.material=p(t.material)},D=n=>n.traverse(f);class O extends o.RuntimeObject3DRenderer{constructor(t,e){const a=e.getGame().getModel3DManager().getModel(t._modelResourceName),s=new THREE.Group,l=new THREE.Group;l.rotation.order="ZYX",l.add(s);super(t,e,l);this._model3DRuntimeObject=t,this._threeObject=s,this._originalModel=a,this._modelOriginPoint=[0,0,0],this.updateSize(),this.updatePosition(),this.updateRotation(),this._animationMixer=new THREE.AnimationMixer(s),this._action=null}updateAnimation(t){this._animationMixer.update(t)}updatePosition(){const t=this.getOriginPoint(),e=this.getCenterPoint();this.get3DRendererObject().position.set(this._object.getX()-this._object.getWidth()*(t[0]-e[0]),this._object.getY()-this._object.getHeight()*(t[1]-e[1]),this._object.getZ()-this._object.getDepth()*(t[2]-e[2]))}getOriginPoint(){return this._model3DRuntimeObject._originPoint||this._modelOriginPoint}getCenterPoint(){return this._model3DRuntimeObject._centerPoint||this._modelOriginPoint}_updateDefaultTransformation(t,e,a,s,l,_,u,r){t.rotation.set(o.toRad(e),o.toRad(a),o.toRad(s)),t.updateMatrixWorld(!0);const i=new THREE.Box3().setFromObject(t),m=i.max.x-i.min.x,c=i.max.y-i.min.y,d=i.max.z-i.min.z;this._modelOriginPoint[0]=-i.min.x/m,this._modelOriginPoint[1]=-i.min.y/c,this._modelOriginPoint[2]=-i.min.z/d,this._modelOriginPoint[1]=1-this._modelOriginPoint[1];const h=this._model3DRuntimeObject._centerPoint;h&&t.position.set(-(i.min.x+(i.max.x-i.min.x)*h[0]),-(i.min.y+(i.max.y-i.min.y)*(1-h[1])),-(i.min.z+(i.max.z-i.min.z)*h[2])),t.scale.set(1,1,1),t.rotation.set(o.toRad(e),o.toRad(a),o.toRad(s));const b=1/m,j=1/c,x=1/d,g=new THREE.Matrix4;if(g.makeScale(b,-j,x),t.updateMatrix(),t.applyMatrix4(g),r){const H=l/m,A=_/c,P=u/d,R=Math.min(H,A,P);this._object._setOriginalWidth(R*m),this._object._setOriginalHeight(R*c),this._object._setOriginalDepth(R*d)}}_updateModel(t,e,a,s,l,_,u){const r=new THREE.Group;r.rotation.order="ZYX";const i=THREE_ADDONS.SkeletonUtils.clone(this._originalModel.scene);r.add(i),this._replaceMaterials(r),this._updateDefaultTransformation(r,t,e,a,s,l,_,u),this.get3DRendererObject().remove(this._threeObject),this.get3DRendererObject().add(r),this._threeObject=r,this._animationMixer=new THREE.AnimationMixer(i);const m=this._model3DRuntimeObject.isAnimationPaused();this._model3DRuntimeObject.setAnimationIndex(this._model3DRuntimeObject.getAnimationIndex()),m&&this.pauseAnimation()}_replaceMaterials(t){this._model3DRuntimeObject._materialType===o.Model3DRuntimeObject.MaterialType.StandardWithoutMetalness?T(t):this._model3DRuntimeObject._materialType===o.Model3DRuntimeObject.MaterialType.Basic&&D(t)}getAnimationCount(){return this._originalModel.animations.length}getAnimationName(t){return this._originalModel.animations[t].name}hasAnimationEnded(){return this._action?!this._action.isRunning():!0}animationPaused(){if(!!this._action)return this._action.paused}pauseAnimation(){!this._action||(this._action.paused=!0)}resumeAnimation(){!this._action||(this._action.paused=!1)}playAnimation(t,e){this._animationMixer.stopAllAction();const a=THREE.AnimationClip.findByName(this._originalModel.animations,t);if(!a){console.error(`The GLB file: ${this._model3DRuntimeObject._modelResourceName} doesn't have any animation named: ${t}`);return}this._action=this._animationMixer.clipAction(a),this._action.setLoop(e?THREE.LoopRepeat:THREE.LoopOnce,Number.POSITIVE_INFINITY),this._action.clampWhenFinished=!0,this._action.play(),this._animationMixer.update(0)}getAnimationElapsedTime(){return this._action?this._action.time:0}setAnimationElapsedTime(t){this._action&&(this._action.time=t)}getAnimationDuration(t){const e=THREE.AnimationClip.findByName(this._originalModel.animations,t);return e?e.duration:0}}o.Model3DRuntimeObjectRenderer=O})(gdjs||(gdjs={}));
//# sourceMappingURL=Model3DRuntimeObject3DRenderer.js.map