UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 5.44 kB
import{Vec3 as s}from"../../core/math/vec3.js";import{Quat as e}from"../../core/math/quat.js";import{GIZMOAXIS_FACE as t,GIZMOAXIS_X as i,GIZMOAXIS_Y as o,GIZMOAXIS_Z as h,GIZMOSPACE_LOCAL as r}from"./constants.js";import{TransformGizmo as a}from"./transform-gizmo.js";import{PlaneShape as n}from"./shape/plane-shape.js";import{ArrowShape as l}from"./shape/arrow-shape.js";import{SphereShape as p}from"./shape/sphere-shape.js";const _=new s,d=new s,c=new s,x=new e,g=.98;class y extends a{constructor(e,r){super(e,r),this._shapes={face:new p(this._device,{axis:t,layers:[this._layer.id],shading:this._shading,defaultColor:this._meshColors.axis.xyz,hoverColor:this._meshColors.hover.xyz}),yz:new n(this._device,{axis:i,layers:[this._layer.id],shading:this._shading,rotation:new s(0,0,-90),defaultColor:this._meshColors.axis.x,hoverColor:this._meshColors.hover.x}),xz:new n(this._device,{axis:o,layers:[this._layer.id],shading:this._shading,rotation:new s(0,0,0),defaultColor:this._meshColors.axis.y,hoverColor:this._meshColors.hover.y}),xy:new n(this._device,{axis:h,layers:[this._layer.id],shading:this._shading,rotation:new s(90,0,0),defaultColor:this._meshColors.axis.z,hoverColor:this._meshColors.hover.z}),x:new l(this._device,{axis:i,layers:[this._layer.id],shading:this._shading,rotation:new s(0,0,-90),defaultColor:this._meshColors.axis.x,hoverColor:this._meshColors.hover.x}),y:new l(this._device,{axis:o,layers:[this._layer.id],shading:this._shading,rotation:new s(0,0,0),defaultColor:this._meshColors.axis.y,hoverColor:this._meshColors.hover.y}),z:new l(this._device,{axis:h,layers:[this._layer.id],shading:this._shading,rotation:new s(90,0,0),defaultColor:this._meshColors.axis.z,hoverColor:this._meshColors.hover.z})},this._nodeLocalPositions=new Map,this._nodePositions=new Map,this._forward=new s,this.snapIncrement=1,this.flipShapes=!0,this._createTransform(),this.on(a.EVENT_TRANSFORMSTART,(()=>{this._storeNodePositions()})),this.on(a.EVENT_TRANSFORMMOVE,(s=>{const e=c.copy(s).sub(this._selectionStartPoint);this.snap&&(e.mulScalar(1/this.snapIncrement),e.round(),e.mulScalar(this.snapIncrement)),this._setNodePositions(e)})),this.on(a.EVENT_NODESDETACH,(()=>{this._nodeLocalPositions.clear(),this._nodePositions.clear()})),this._app.on("prerender",(()=>{this._shapesLookAtCamera()}))}set axisGap(s){this._setArrowProp("gap",s)}get axisGap(){return this._shapes.x.gap}set axisLineThickness(s){this._setArrowProp("lineThickness",s)}get axisLineThickness(){return this._shapes.x.lineThickness}set axisLineLength(s){this._setArrowProp("lineLength",s)}get axisLineLength(){return this._shapes.x.lineLength}set axisLineTolerance(s){this._setArrowProp("tolerance",s)}get axisLineTolerance(){return this._shapes.x.tolerance}set axisArrowThickness(s){this._setArrowProp("arrowThickness",s)}get axisArrowThickness(){return this._shapes.x.arrowThickness}set axisArrowLength(s){this._setArrowProp("arrowLength",s)}get axisArrowLength(){return this._shapes.x.arrowLength}set axisPlaneSize(s){this._setPlaneProp("size",s)}get axisPlaneSize(){return this._shapes.yz.size}set axisPlaneGap(s){this._setPlaneProp("gap",s)}get axisPlaneGap(){return this._shapes.yz.gap}set axisCenterSize(s){this._shapes.face.size=s}get axisCenterSize(){return this._shapes.face.size}set axisCenterTolerance(s){this._shapes.face.tolerance=s}get axisCenterTolerance(){return this._shapes.face.tolerance}_setArrowProp(s,e){this._shapes.x[s]=e,this._shapes.y[s]=e,this._shapes.z[s]=e}_setPlaneProp(s,e){this._shapes.yz[s]=e,this._shapes.xz[s]=e,this._shapes.xy[s]=e}_shapesLookAtCamera(){const s=this.facing;let e=s.dot(this.root.right);this._shapes.x.entity.enabled=Math.abs(e)<g,this.flipShapes&&(this._shapes.x.flipped=e<0),e=s.dot(this.root.up),this._shapes.y.entity.enabled=Math.abs(e)<g,this.flipShapes&&(this._shapes.y.flipped=e<0),e=s.dot(this.root.forward),this._shapes.z.entity.enabled=Math.abs(e)<g,this.flipShapes&&(this._shapes.z.flipped=e>0),_.cross(s,this.root.right),this._shapes.yz.entity.enabled=_.length()<g,this.flipShapes&&(this._shapes.yz.flipped=d.set(0,+(_.dot(this.root.forward)<0),+(_.dot(this.root.up)<0))),_.cross(s,this.root.forward),this._shapes.xy.entity.enabled=_.length()<g,this.flipShapes&&(this._shapes.xy.flipped=d.set(+(_.dot(this.root.up)<0),+(_.dot(this.root.right)>0),0)),_.cross(s,this.root.up),this._shapes.xz.entity.enabled=_.length()<g,this.flipShapes&&(this._shapes.xz.flipped=d.set(+(_.dot(this.root.forward)>0),0,+(_.dot(this.root.right)>0)))}_storeNodePositions(){for(let s=0;s<this.nodes.length;s++){const e=this.nodes[s];this._nodeLocalPositions.set(e,e.getLocalPosition().clone()),this._nodePositions.set(e,e.getPosition().clone())}}_setNodePositions(s){for(let t=0;t<this.nodes.length;t++){const i=this.nodes[t];if(this._coordSpace===r){var e;const t=this._nodeLocalPositions.get(i);if(!t)continue;_.copy(s),null==(e=i.parent)||e.getWorldTransform().getScale(d),d.x=1/d.x,d.y=1/d.y,d.z=1/d.z,x.copy(i.getLocalRotation()).transformVector(_,_),_.mul(d),i.setLocalPosition(_.add(t))}else{const e=this._nodePositions.get(i);if(!e)continue;i.setPosition(_.copy(s).add(e))}}this._updatePosition()}_screenToPoint(e,i){const o=this._camera.screenToWorld(e,i,1),h=this._selectedAxis,r=this._selectedIsPlane,a=this._createRay(o),n=this._createPlane(h,h===t,!r),l=new s;return n.intersectsRay(a,l),x.copy(this._rootStartRot).invert().transformVector(l,l),r||h===t||this._projectToAxis(l,h),l}}export{y as TranslateGizmo};