UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 5.59 kB
import{Vec3 as s}from"../../core/math/vec3.js";import{Quat as e}from"../../core/math/quat.js";import{GIZMOAXIS_XYZ 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{BoxShape as n}from"./shape/box-shape.js";import{PlaneShape as l}from"./shape/plane-shape.js";import{BoxLineShape as p}from"./shape/boxline-shape.js";const _=new s,c=new s,d=new s,x=new e,y=.98;class m extends a{constructor(e,_){super(e,_),this._shapes={xyz:new n(this._device,{axis:t,layers:[this._layer.id],shading:this._shading,defaultColor:this._meshColors.axis.xyz,hoverColor:this._meshColors.hover.xyz}),yz: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}),xz: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}),xy: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}),x:new p(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 p(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 p(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._coordSpace=r,this._nodeScales=new Map,this._forward=new s,this._useUniformScaling=!1,this.snapIncrement=1,this.flipShapes=!0,this.lowerBoundScale=new s(-1/0,-1/0,-1/0),this._createTransform(),this.on(a.EVENT_TRANSFORMSTART,(()=>{this._storeNodeScales()})),this.on(a.EVENT_TRANSFORMMOVE,(e=>{const t=d.copy(e).sub(this._selectionStartPoint);this.snap&&(t.mulScalar(1/this.snapIncrement),t.round(),t.mulScalar(this.snapIncrement)),t.mulScalar(1/this._scale),this._setNodeScales(t.add(s.ONE))})),this.on(a.EVENT_NODESDETACH,(()=>{this._nodeScales.clear()})),this._app.on("prerender",(()=>{this._shapesLookAtCamera()}))}set coordSpace(s){}get coordSpace(){return this._coordSpace}set uniform(s){this._useUniformScaling=null==s||s}get uniform(){return this._useUniformScaling}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 axisBoxSize(s){this._setArrowProp("boxSize",s)}get axisBoxSize(){return this._shapes.x.boxSize}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.xyz.size=s}get axisCenterSize(){return this._shapes.xyz.size}set axisCenterTolerance(s){this._shapes.xyz.tolerance=s}get axisCenterTolerance(){return this._shapes.xyz.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)<y,this.flipShapes&&(this._shapes.x.flipped=e<0),e=s.dot(this.root.up),this._shapes.y.entity.enabled=Math.abs(e)<y,this.flipShapes&&(this._shapes.y.flipped=e<0),e=s.dot(this.root.forward),this._shapes.z.entity.enabled=Math.abs(e)<y,this.flipShapes&&(this._shapes.z.flipped=e>0),_.cross(s,this.root.right),this._shapes.yz.entity.enabled=_.length()<y,this.flipShapes&&(this._shapes.yz.flipped=c.set(0,+(_.dot(this.root.forward)<0),+(_.dot(this.root.up)<0))),_.cross(s,this.root.forward),this._shapes.xy.entity.enabled=_.length()<y,this.flipShapes&&(this._shapes.xy.flipped=c.set(+(_.dot(this.root.up)<0),+(_.dot(this.root.right)>0),0)),_.cross(s,this.root.up),this._shapes.xz.entity.enabled=_.length()<y,this.flipShapes&&(this._shapes.xz.flipped=c.set(+(_.dot(this.root.forward)>0),0,+(_.dot(this.root.right)>0)))}_storeNodeScales(){for(let s=0;s<this.nodes.length;s++){const e=this.nodes[s];this._nodeScales.set(e,e.getLocalScale().clone())}}_setNodeScales(s){for(let e=0;e<this.nodes.length;e++){const t=this.nodes[e],i=this._nodeScales.get(t);i&&t.setLocalScale(_.copy(i).mul(s).max(this.lowerBoundScale))}}_screenToPoint(e,r){const a=this.root.getPosition(),n=this._camera.screenToWorld(e,r,1),l=this._selectedAxis,p=this._selectedIsPlane,d=this._useUniformScaling&&p||l===t,y=this._createRay(n),m=this._createPlane(l,d,!p),g=new s;if(m.intersectsRay(y,g),d){switch(l){case i:_.copy(this.root.up),c.copy(this.root.forward).mulScalar(-1);break;case o:_.copy(this.root.right),c.copy(this.root.forward).mulScalar(-1);break;case h:_.copy(this.root.up),c.copy(this.root.right);break;default:_.copy(this._camera.entity.up),c.copy(this._camera.entity.right)}c.add(_).normalize(),_.sub2(g,a);const s=_.length()*_.normalize().dot(c);return g.set(s,s,s),l!==t&&(g[l]=1),g}return x.copy(this._rootStartRot).invert().transformVector(g,g),p||this._projectToAxis(g,l),g}}export{m as ScaleGizmo};