加 QQ 1760723197 发完整源码
import * as Cesium from 'cesium'
import ArrowPolyline from './ArrowPolyline'
import EventConstant from './EventConstant'
import { cartesian3ToQuaternion, normalizingQuaternion, projectOnPlane, rayPlaneIntersection } from './Math'
import AxisSphere from './AxisSphere'
import EventManager from './EventManager'
export default class TranslationController {
/**
* 视图
* @type {Viewer}
*/
viewer = null
/**
* 模型
* @type {Cesium.Model}
*/
model = null
/**
* 模型位置
* @type {Cesium.Cartesian3}
*/
position = null
/**
* z轴
* @type {ArrowPolyline}
*/
axisZ = null
/**
* x轴
* @type {ArrowPolyline}
*/
axisX = null
/**
* y轴
* @type {ArrowPolyline}
*/
axisY = null
/**
* 操作杆集合
* @type {Cesium.PrimitiveCollection}
*/
primitives = null
/**
* 从摄像头发出与视窗上一点相交的射线
*/
pickRay = new Cesium.Ray()
/**
* 拾取到的位置
* @type {Cesium.Cartesian3}
*/
pickPoint = null
/**
* 当前操作轴
* @type {ArrowPolyline}
*/
axis = null
/**
* Z旋转轴
* @type {AxisSphere}
*/
axisSphereZ = null
/**
* X旋转轴
* @type {AxisSphere}
*/
axisSphereX = null
/**
* Y旋转轴
* @type {AxisSphere}
*/
axisSphereY = null
/**
* 辅助球
* @type {Cesium.Primitive}
*/
auxiliaryBall = null
//模型半径
radius = null
//模型原始矩阵
orgModelMatrix = null
//是否允许旋转
rotateEnable = null
//是否允许移动
moveEnable = null
constructor(viewer, model) {
this.viewer = viewer
this.model = model
this.rotateEnable = false
this.moveEnable = false
this.orgModelMatrix = this.model.root.transform.clone(new Cesium.Matrix4())
this.position = Cesium.Matrix4.getTranslation(model.root.transform, new Cesium.Cartesian3())
this.primitives = new Cesium.PrimitiveCollection()
this.viewer.scene.primitives.add(this.primitives)
// 创建平移轴
this._createRod()
// 旋转平移轴
this._rotationRod()
// 添加平移轴
this._addRod()
// 创建旋转轴
this._createSphereAxis()
// 旋转旋转轴
this._rotationSphereAxis()
// 添加旋转轴
this._addSphereAxis()
// 添加辅助球
this.auxiliaryBall = {
show: false
}
this.addAuxiliaryBall((this.radius * 2) / 3, Cesium.Color.RED.withAlpha(0.1))
this._initEventManager()
// 添加监听器
this._addListener()
}
//复原model
resetModel() {
this.model.root.transform = this.orgModelMatrix
this._resetMaterial()
}
//初始化监听
_initEventManager() {
this.viewer.eventManager = new EventManager(this.viewer)
}
// 添加监听器
_addListener() {
this.viewer.eventManager.addEventListener(EventConstant.LEFT_DOWN, this._clickListener)
this.viewer.eventManager.addEventListener(EventConstant.LEFT_UP, this._clickUpListener)
this.viewer.eventManager.addEventListener(EventConstant.MOUSE_MOVE, this._moveListener)
}
// 清除操纵杆, 监听器
destroy() {
if (this.primitives) {
this.primitives.removeA