UNPKG

cesium-gizmo

Version:

A Simple Cesium Gizmo using Cesium's Public API.

2 lines (1 loc) 15.1 kB
(function(T,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("cesium")):typeof define=="function"&&define.amd?define(["exports","cesium"],t):(T=typeof globalThis<"u"?globalThis:T||self,t(T.CesiumGizmo={},T.Cesium))})(this,function(T,t){"use strict";function F(r){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const a in r)if(a!=="default"){const n=Object.getOwnPropertyDescriptor(r,a);Object.defineProperty(e,a,n.get?n:{enumerable:!0,get:()=>r[a]})}}return e.default=r,Object.freeze(e)}const G=F(t);function Y(r,e,a){const n=new G.BoundingSphere;return n.center=r,n.radius=e,a.camera.getPixelSize(n,a.context.drawingBufferWidth,a.context.drawingBufferHeight)}function Z(r,e){const a=new G.Cartesian3,n=new G.Cartographic,s=e.context,o=Math.max(s.drawingBufferWidth,s.drawingBufferHeight),i=r.modelMatrix;if(a.x=i[12],a.y=i[13],a.z=i[14],e.camera._scene.mode!==G.SceneMode.SCENE3D){const x=e.mapProjection,d=x.ellipsoid.cartesianToCartographic(a,n);x.project(d,a),G.Cartesian3.fromElements(a.z,a.x,a.y,a)}const c=1,p=Y(a,c,e),f=1/p,P=Math.min(f*2*c,o);let h=1;return P<r.length&&(h=r.length*p/(2*c)),h}const l={xAxis:"xAxis",yAxis:"yAxis",zAxis:"zAxis"},M={translate:"translate",rotate:"rotate",scale:"scale"},N={local:"local",surface:"surface"};class U{constructor(e,a){this._gizmo=e,this._part=[],this._show=!0,this._scale=1,this._scaleMatrix=new t.Matrix4,this._mode=a}update(e){if(this._show){if(this._scale=Z(this._gizmo,e),this._mode===M.translate){const a=t.Transforms.eastNorthUpToFixedFrame(t.Matrix4.getTranslation(this._gizmo.modelMatrix,new t.Cartesian3));this._scaleMatrix=t.Matrix4.multiplyByUniformScale(a,this._scale,a)}else this._scaleMatrix=t.Matrix4.multiplyByUniformScale(this._gizmo.modelMatrix,this._scale,new t.Matrix4);for(const a of this._part)a.modelMatrix=this._scaleMatrix,a.update(e)}}isDestroyed(){return!1}destroy(){for(const e of this._part)e.destroy();return this._part=[],t.destroyObject(this)}}class V{constructor(e){e=t.defaultValue(e,{}),this.mode=null,this.applyTransformationToMountedPrimitive=!0,this.modelMatrix=t.Matrix4.IDENTITY,this.length=250,this.transMode=null,this._viewer=null,this._mountedPrimitive=null,this._transPrimitives=null,this._rotatePrimitives=null,this._scalePrimitives=null,this._xMaterial=t.Material.fromType("Color",{color:new t.Color(1,0,0,.99)}),this._yMaterial=t.Material.fromType("Color",{color:new t.Color(0,1,0,.99)}),this._zMaterial=t.Material.fromType("Color",{color:new t.Color(0,0,1,.99)}),this._highlightMaterial=t.Material.fromType("Color",{color:new t.Color(1,1,0,.99)}),this.onGizmoPointerDown=e.onGizmoPointerDown,this.onGizmoPointerUp=e.onGizmoPointerUp,this.onGizmoPointerMove=e.onGizmoPointerMove,this.createGizmoPrimitive()}createGizmoPrimitive(){const o=new t.CylinderGeometry({length:.2,topRadius:0,bottomRadius:.06}),i=new t.CylinderGeometry({length:.8,topRadius:.01,bottomRadius:.01}),c=y(t.Cartesian3.UNIT_X,.2/2+.8),p=y(t.Cartesian3.UNIT_X,.8/2),f=y(t.Cartesian3.UNIT_Y,.2/2+.8),P=y(t.Cartesian3.UNIT_Y,.8/2),h=y(t.Cartesian3.UNIT_Z,.2/2+.8),x=y(t.Cartesian3.UNIT_Z,.8/2),d=new t.GeometryInstance({id:l.xAxis,geometry:o,modelMatrix:c}),S=new t.GeometryInstance({id:l.xAxis,geometry:i,modelMatrix:p}),g=new t.GeometryInstance({id:l.yAxis,geometry:o,modelMatrix:f}),B=new t.GeometryInstance({id:l.yAxis,geometry:i,modelMatrix:P}),C=new t.GeometryInstance({id:l.zAxis,geometry:o,modelMatrix:h}),D=new t.GeometryInstance({id:l.zAxis,geometry:i,modelMatrix:x}),A={depthTest:{enabled:!1},depthMask:!1,blending:t.BlendingState.ALPHA_BLEND},I=t.Matrix4.IDENTITY,tt=new t.Primitive({geometryInstances:[d,S],appearance:new t.MaterialAppearance({material:this._xMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),et=new t.Primitive({geometryInstances:[g,B],appearance:new t.MaterialAppearance({material:this._yMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),at=new t.Primitive({geometryInstances:[C,D],appearance:new t.MaterialAppearance({material:this._zMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),k=new U(this,M.translate);k._part.push(tt,et,at),this._transPrimitives=k;const nt=X(1),L=new t.PolylineGeometry({positions:nt,width:5,arcType:t.ArcType.NONE,vertexFormat:t.PolylineMaterialAppearance.VERTEX_FORMAT}),rt=y(t.Cartesian3.UNIT_X,0),it=y(t.Cartesian3.UNIT_Y,0),ot=y(t.Cartesian3.UNIT_Z,0),st=new t.GeometryInstance({id:l.xAxis,geometry:L,modelMatrix:rt}),ct=new t.GeometryInstance({id:l.yAxis,geometry:L,modelMatrix:it}),lt=new t.GeometryInstance({id:l.zAxis,geometry:L,modelMatrix:ot}),dt=new t.Primitive({geometryInstances:st,appearance:new t.PolylineMaterialAppearance({material:this._xMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),xt=new t.Primitive({geometryInstances:ct,appearance:new t.PolylineMaterialAppearance({material:this._yMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),Mt=new t.Primitive({geometryInstances:lt,appearance:new t.PolylineMaterialAppearance({material:this._zMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),W=new U(this,M.rotate);W._part.push(dt,xt,Mt),this._rotatePrimitives=W;const R=t.BoxGeometry.fromDimensions({dimensions:new t.Cartesian3(.2/2,.2/2,.2/2)}),ht=y(t.Cartesian3.UNIT_X,.8),pt=y(t.Cartesian3.UNIT_Y,.8),ft=y(t.Cartesian3.UNIT_Z,.8),yt=new t.GeometryInstance({id:l.xAxis,geometry:R,modelMatrix:ht}),_t=new t.GeometryInstance({id:l.yAxis,geometry:R,modelMatrix:pt}),wt=new t.GeometryInstance({id:l.zAxis,geometry:R,modelMatrix:ft}),Pt=new t.Primitive({geometryInstances:[yt,S],appearance:new t.MaterialAppearance({material:this._xMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),vt=new t.Primitive({geometryInstances:[_t,B],appearance:new t.MaterialAppearance({material:this._yMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),Tt=new t.Primitive({geometryInstances:[wt,D],appearance:new t.MaterialAppearance({material:this._zMaterial,renderState:A}),modelMatrix:I,asynchronous:!1}),z=new U(this,M.scale);z._part.push(Pt,vt,Tt),this._scalePrimitives=z}attach(e){this._viewer=e,this._viewer.scene.primitives.add(this._transPrimitives),this._viewer.scene.primitives.add(this._rotatePrimitives),this._viewer.scene.primitives.add(this._scalePrimitives),this.setMode(M.translate),this.transMode=N.local,K(this._viewer,this)}detach(){this._viewer.scene.primitives.remove(this._transPrimitives),this._viewer.scene.primitives.remove(this._rotatePrimitives),this._viewer.scene.primitives.remove(this._scalePrimitives),this._viewer=null,J()}isGizmoPrimitive(e){return this._transPrimitives._part.indexOf(e)!==-1||this._rotatePrimitives._part.indexOf(e)!==-1||this._scalePrimitives._part.indexOf(e)!==-1}setMode(e){e===M.translate?(this.mode=M.translate,this._transPrimitives._show=!0,this._rotatePrimitives._show=!1,this._scalePrimitives._show=!1):e===M.rotate?(this.mode=M.rotate,this._transPrimitives._show=!1,this._rotatePrimitives._show=!0,this._scalePrimitives._show=!1):e===M.scale&&(this.mode=M.scale,this._transPrimitives._show=!1,this._rotatePrimitives._show=!1,this._scalePrimitives._show=!0)}}function y(r,e){const a=t.Matrix4.IDENTITY.clone();if(t.Cartesian3.equals(r,t.Cartesian3.UNIT_Y)){const n=t.Matrix3.fromRotationX(t.Math.toRadians(-90)),s=t.Cartesian3.fromElements(0,e,0);t.Matrix4.setTranslation(a,s,a),t.Matrix4.setRotation(a,n,a)}else if(t.Cartesian3.equals(r,t.Cartesian3.UNIT_X)){const n=t.Matrix3.fromRotationY(t.Math.toRadians(90)),s=t.Cartesian3.fromElements(e,0,0);t.Matrix4.setTranslation(a,s,a),t.Matrix4.setRotation(a,n,a)}else if(t.Cartesian3.equals(r,t.Cartesian3.UNIT_Z)){const n=t.Matrix3.IDENTITY,s=t.Cartesian3.fromElements(0,0,e);t.Matrix4.setTranslation(a,s,a),t.Matrix4.setRotation(a,n,a)}return a}function X(r){const e=[];for(let a=0;a<=360;a++){const n=t.Math.toRadians(a),s=Math.cos(n)*r,o=Math.sin(n)*r;e.push(new t.Cartesian3(s,o,0))}return e}let b=new t.Cartesian2,v=new t.Cartesian3,_=new t.Matrix4,E=new t.Matrix4,m=null,w;const j=new t.Matrix4,H=new t.Cartesian3,q=new t.Matrix4,O=new t.Matrix4;function K(r,e){w=new t.ScreenSpaceEventHandler(r.canvas),w.setInputAction(a=>{const n=r.scene.pick(a.position);t.defined(n)&&(e.isGizmoPrimitive(n.primitive)||n.primitive.modelMatrix instanceof t.Matrix4&&(e._mountedPrimitive=n.primitive,e.modelMatrix=n.primitive.modelMatrix.clone()))},t.ScreenSpaceEventType.LEFT_CLICK),w.setInputAction(a=>{const n=r.scene.pick(a.position);t.defined(n)&&(n.id===l.xAxis||n.id===l.yAxis||n.id===l.zAxis?(console.log(n.id),m=n.id,r.scene.screenSpaceCameraController.enableRotate=!1,r.scene.screenSpaceCameraController.enableTranslate=!1,b=a.position,v=new t.Cartesian3(e.modelMatrix[12],e.modelMatrix[13],e.modelMatrix[14]),_=e.modelMatrix.clone(),E=e._mountedPrimitive.modelMatrix.clone(),typeof e.onGizmoPointerDown=="function"&&e.onGizmoPointerDown()):(m=null,r.scene.screenSpaceCameraController.enableRotate=!0,r.scene.screenSpaceCameraController.enableTranslate=!0))},t.ScreenSpaceEventType.LEFT_DOWN),w.setInputAction(a=>{m&&(m=null,b=new t.Cartesian2,v=new t.Cartesian3,_=new t.Matrix4,E=new t.Matrix4,typeof e.onGizmoPointerUp=="function"&&e.onGizmoPointerDown()),r.scene.screenSpaceCameraController.enableRotate=!0,r.scene.screenSpaceCameraController.enableTranslate=!0},t.ScreenSpaceEventType.LEFT_UP),w.setInputAction(a=>{if(!m){const s=r.scene.pick(a.endPosition),o=t.defined(s)&&s.id===l.xAxis?e._highlightMaterial:e._xMaterial,i=t.defined(s)&&s.id===l.yAxis?e._highlightMaterial:e._yMaterial,c=t.defined(s)&&s.id===l.zAxis?e._highlightMaterial:e._zMaterial;e._transPrimitives._part[0].appearance.material=o,e._transPrimitives._part[1].appearance.material=i,e._transPrimitives._part[2].appearance.material=c,e._rotatePrimitives._part[0].appearance.material=o,e._rotatePrimitives._part[1].appearance.material=i,e._rotatePrimitives._part[2].appearance.material=c,e._scalePrimitives._part[0].appearance.material=o,e._scalePrimitives._part[1].appearance.material=i,e._scalePrimitives._part[2].appearance.material=c;return}const n=new t.Cartesian2(a.endPosition.x-b.x,a.endPosition.y-b.y);if(!(n.x===0&&n.y===0)){if(e.mode===M.translate){const s=Q(m,r,n);if(e.transMode===N.local){const o=t.Matrix4.fromTranslation(s,j),i=t.Matrix4.multiply(o,_,o);if(isNaN(i[12])||isNaN(i[13])||isNaN(i[14]))return;if(e.modelMatrix[12]=i[12],e.modelMatrix[13]=i[13],e.modelMatrix[14]=i[14],typeof e.onGizmoPointerMove=="function"&&e.onGizmoPointerMove({mode:M.translate,transMode:N.local,result:t.Matrix4.getTranslation(i,new t.Cartesian3)}),e.applyTransformationToMountedPrimitive){const c=e._mountedPrimitive;c.modelMatrix[12]=i[12],c.modelMatrix[13]=i[13],c.modelMatrix[14]=i[14]}}else if(e.transMode===N.surface){const o=t.Cartographic.fromCartesian(v),i=new t.Cartographic,c=new t.Cartesian3;switch(m){case l.xAxis:{const d=t.Matrix4.fromTranslation(s,new t.Matrix4);t.Matrix4.multiply(d,_,d),c.x=d[12],c.y=d[13],c.z=d[14],t.Cartographic.fromCartesian(c,void 0,i),i.height=o.height,i.latitude=o.latitude;break}case l.yAxis:{const d=t.Matrix4.fromTranslation(s,new t.Matrix4);t.Matrix4.multiply(d,_,d),c.x=d[12],c.y=d[13],c.z=d[14],t.Cartographic.fromCartesian(c,void 0,i),i.height=o.height;break}case l.zAxis:{o.height+=s.z,t.Cartographic.clone(o,i);break}}t.Cartographic.toCartesian(i,void 0,c);const p=t.Transforms.eastNorthUpToFixedFrame(v),f=t.Matrix4.inverseTransformation(p,new t.Matrix4),P=t.Matrix4.multiply(f,_,new t.Matrix4),h=t.Transforms.eastNorthUpToFixedFrame(c),x=t.Matrix4.multiply(h,P,new t.Matrix4);if(isNaN(x[12])||isNaN(x[13])||isNaN(x[14]))return;if(t.Matrix4.clone(x,e.modelMatrix),typeof e.onGizmoPointerMove=="function"&&e.onGizmoPointerMove({mode:M.translate,transMode:N.surface,result:x.clone()}),e.applyTransformationToMountedPrimitive){const d=e._mountedPrimitive;t.Matrix4.clone(x,d.modelMatrix)}}}else if(e.mode===M.rotate){const s=$(m,r,v,_,a.startPosition,a.endPosition);t.Matrix4.multiplyByMatrix3(_,s,_),t.Matrix4.clone(_,e.modelMatrix);const o=e._mountedPrimitive,i=t.Matrix4.getScale(o.modelMatrix,H),c=t.Matrix4.multiplyByScale(_,i,q);typeof e.onGizmoPointerMove=="function"&&e.onGizmoPointerMove({mode:M.rotate,result:t.Transforms.fixedFrameToHeadingPitchRoll(c)}),e.applyTransformationToMountedPrimitive&&t.Matrix4.clone(c,o.modelMatrix)}else if(e.mode===M.scale){const s=u(m,r,v,_,n);if(typeof e.onGizmoPointerMove=="function"&&e.onGizmoPointerMove({mode:M.scale,result:t.Matrix4.fromScale(s,O)}),e.applyTransformationToMountedPrimitive){const o=e._mountedPrimitive,i=t.Matrix4.multiplyByScale(E,s,O);t.Matrix4.clone(i,o.modelMatrix)}}}},t.ScreenSpaceEventType.MOUSE_MOVE)}function J(){w.removeInputAction(t.ScreenSpaceEventType.LEFT_CLICK),w.removeInputAction(t.ScreenSpaceEventType.LEFT_DOWN),w.removeInputAction(t.ScreenSpaceEventType.LEFT_UP),w.removeInputAction(t.ScreenSpaceEventType.MOUSE_MOVE),w=w&&w.destroy()}function Q(r,e,a){let n;switch(r){case l.xAxis:n=t.Cartesian3.UNIT_X;break;case l.yAxis:n=t.Cartesian3.UNIT_Y;break;case l.zAxis:n=t.Cartesian3.UNIT_Z;break}const s=t.Transforms.eastNorthUpToFixedFrame(v),o=t.Matrix4.multiplyByPointAsVector(s,n,new t.Cartesian3),i=t.SceneTransforms.worldToWindowCoordinates(e.scene,v),c=t.Cartesian3.add(v,o,new t.Cartesian3),p=t.SceneTransforms.worldToWindowCoordinates(e.scene,c),f=t.Cartesian2.subtract(p,i,new t.Cartesian2),P=t.Cartesian2.dot(f,a)/t.Cartesian2.magnitude(f),h=e.camera.getPixelSize(new t.BoundingSphere(v,t.Cartesian3.magnitude(o)),e.canvas.width,e.canvas.height);return t.Cartesian3.multiplyByScalar(o,P*h,new t.Cartesian3)}function $(r,e,a,n,s,o){const i=t.SceneTransforms.worldToWindowCoordinates(e.scene,a),c=t.Cartesian2.subtract(s,i,new t.Cartesian2),p=t.Cartesian2.subtract(o,i,new t.Cartesian2),f=t.Math.signNotZero(t.Cartesian2.cross(c,p)),P=t.Cartesian2.angleBetween(c,p),h=-f,x=t.Cartesian3.subtract(e.scene.camera.positionWC,a,new t.Cartesian3);let d;const S=new t.Matrix3,g=new t.Cartesian3;switch(r){case l.xAxis:t.Matrix4.multiplyByPointAsVector(n,t.Cartesian3.UNIT_X,g),d=t.Math.signNotZero(t.Cartesian3.dot(g,x)),t.Matrix3.fromRotationX(P*h*d,S);break;case l.yAxis:t.Matrix4.multiplyByPointAsVector(n,t.Cartesian3.UNIT_Y,g),d=t.Math.signNotZero(t.Cartesian3.dot(g,x)),t.Matrix3.fromRotationY(P*h*d,S);break;case l.zAxis:t.Matrix4.multiplyByPointAsVector(n,t.Cartesian3.UNIT_Z,g),d=t.Math.signNotZero(t.Cartesian3.dot(g,x)),t.Matrix3.fromRotationZ(P*h*d,S);break}return S}function u(r,e,a,n,s){const o=new t.Cartesian3;switch(r){case l.xAxis:t.Matrix4.multiplyByPointAsVector(n,t.Cartesian3.UNIT_X,o);break;case l.yAxis:t.Matrix4.multiplyByPointAsVector(n,t.Cartesian3.UNIT_Y,o);break;case l.zAxis:t.Matrix4.multiplyByPointAsVector(n,t.Cartesian3.UNIT_Z,o);break}const i=t.SceneTransforms.worldToWindowCoordinates(e.scene,a),c=t.Cartesian3.add(a,o,new t.Cartesian3),p=t.SceneTransforms.worldToWindowCoordinates(e.scene,c),f=t.Cartesian2.subtract(p,i,new t.Cartesian2),h=t.Cartesian2.dot(f,s)/t.Cartesian2.magnitude(f)/10+1;let x;switch(r){case l.xAxis:x=new t.Cartesian3(h,1,1);break;case l.yAxis:x=new t.Cartesian3(1,h,1);break;case l.zAxis:x=new t.Cartesian3(1,1,h);break}return x}T.Gizmo=V,T.GizmoMode=M,T.TranslateMode=N,Object.defineProperty(T,Symbol.toStringTag,{value:"Module"})});