|
|
@ -1,23 +1,32 @@ |
|
|
import * as THREE from 'three' |
|
|
import * as THREE from 'three' |
|
|
import BaseRenderer from '@/core/base/BaseRenderer.ts' |
|
|
import BaseRenderer from '@/core/base/BaseRenderer.ts' |
|
|
import { Text } from 'troika-three-text' |
|
|
|
|
|
import MoveLinePointPng from '@/assets/images/moveline_point.png' |
|
|
import MoveLinePointPng from '@/assets/images/moveline_point.png' |
|
|
import SimSunTTF from '@/assets/fonts/simsunb.ttf' |
|
|
import { getLineId, linkPlaneByPoint } from '@/core/ModelUtils.ts' |
|
|
import { getLineId } from '@/core/ModelUtils.ts' |
|
|
|
|
|
import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer' |
|
|
|
|
|
import { numberToString } from '@/utils/webutils.ts' |
|
|
|
|
|
import Constract from '@/core/Constract.ts' |
|
|
import Constract from '@/core/Constract.ts' |
|
|
import type { ExtrudeGeometryOptions } from 'three/src/geometries/ExtrudeGeometry' |
|
|
import InstancePointManager, { PointManageWrap } from '@/core/manager/InstancePointManager.ts' |
|
|
|
|
|
import type { Object3DLike } from '@/types/ModelTypes.ts' |
|
|
|
|
|
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial' |
|
|
|
|
|
import LineSegmentManager from '@/core/manager/LineSegmentManager.ts' |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 辅助测量工具渲染器 |
|
|
* AGV行走路线渲染器 point 是二维码站点 |
|
|
*/ |
|
|
*/ |
|
|
export default class WayRenderer extends BaseRenderer { |
|
|
export default class WayRenderer extends BaseRenderer { |
|
|
static LABEL_NAME = 'way_label' |
|
|
static LABEL_NAME = 'way_label' |
|
|
static POINT_NAME = 'way_point' |
|
|
static POINT_NAME = 'way_point' |
|
|
static LINE_NAME = 'way_line' |
|
|
static LINE_NAME = 'way_line' |
|
|
|
|
|
|
|
|
|
|
|
pointGeometry: THREE.BufferGeometry |
|
|
pointMaterial: THREE.Material |
|
|
pointMaterial: THREE.Material |
|
|
|
|
|
// lineMaterial: LineMaterial = new LineMaterial({
|
|
|
|
|
|
// color: 0xa0cfff,
|
|
|
|
|
|
// linewidth: 0.8,
|
|
|
|
|
|
// vertexColors: false,
|
|
|
|
|
|
// dashed: false,
|
|
|
|
|
|
// gapSize: 0,
|
|
|
|
|
|
// worldUnits: true
|
|
|
|
|
|
// })
|
|
|
|
|
|
lineGeometry: THREE.BufferGeometry = new THREE.PlaneGeometry(1, 1).rotateX(-Math.PI / 2) |
|
|
lineMaterial = new THREE.MeshBasicMaterial({ |
|
|
lineMaterial = new THREE.MeshBasicMaterial({ |
|
|
color: 0xa0cfff, |
|
|
color: 0xa0cfff, |
|
|
transparent: true, |
|
|
transparent: true, |
|
|
@ -29,149 +38,86 @@ export default class WayRenderer extends BaseRenderer { |
|
|
* 默认点的高度, 防止和地面重合 |
|
|
* 默认点的高度, 防止和地面重合 |
|
|
*/ |
|
|
*/ |
|
|
readonly defulePositionY: number = Constract.HEIGHT_WAY |
|
|
readonly defulePositionY: number = Constract.HEIGHT_WAY |
|
|
readonly defaultScale: THREE.Vector3 = new THREE.Vector3(0.25, 0.25, 0.1) |
|
|
readonly defaultScale: THREE.Vector3 = new THREE.Vector3(0.5, 0.1, 0.5) |
|
|
readonly defaultRotation: THREE.Vector3 = new THREE.Vector3(90, 0, 0) |
|
|
readonly defaultRotation: THREE.Vector3 = new THREE.Vector3(0, 0, 0) |
|
|
|
|
|
readonly rendererOption = { |
|
|
constructor(itemTypeName: string) { |
|
|
lineWidth: 0.8 |
|
|
super(itemTypeName) |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async init() { |
|
|
async init() { |
|
|
return Promise.all([ |
|
|
return Promise.all([ |
|
|
super.init(), |
|
|
super.init(), |
|
|
this.loadFont() |
|
|
new THREE.TextureLoader().loadAsync(MoveLinePointPng) |
|
|
]) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async loadFont() { |
|
|
|
|
|
return new Promise<void>((resolve, reject) => { |
|
|
|
|
|
new THREE.TextureLoader().load( |
|
|
|
|
|
MoveLinePointPng, |
|
|
|
|
|
(texture) => { |
|
|
|
|
|
this.pointMaterial = new THREE.SpriteMaterial({ |
|
|
|
|
|
map: texture, |
|
|
|
|
|
transparent: true, |
|
|
|
|
|
side: THREE.DoubleSide |
|
|
|
|
|
}) |
|
|
|
|
|
resolve() |
|
|
|
|
|
}, |
|
|
|
|
|
undefined, |
|
|
|
|
|
function(err) { |
|
|
|
|
|
reject(err) |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 所有的点,必须使用同一个尺寸, 改属性也无效 |
|
|
|
|
|
*/ |
|
|
|
|
|
override afterCreateOrUpdatePoint(item: ItemJson, option: RendererCudOption, object: THREE.Object3D) { |
|
|
|
|
|
super.afterCreateOrUpdatePoint(item, option, object) |
|
|
|
|
|
|
|
|
|
|
|
const point = object |
|
|
|
|
|
point.position.y = this.defulePositionY |
|
|
|
|
|
point.scale.set(this.defaultScale.x, this.defaultScale.y, this.defaultScale.z) |
|
|
|
|
|
point.rotation.set( |
|
|
|
|
|
THREE.MathUtils.degToRad(this.defaultRotation.x), |
|
|
|
|
|
THREE.MathUtils.degToRad(this.defaultRotation.y), |
|
|
|
|
|
THREE.MathUtils.degToRad(this.defaultRotation.z) |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private _createOrUpdateLine(startPosition: THREE.Vector3, endPosition: THREE.Vector3, type: LinkType) { |
|
|
|
|
|
const width = 1 |
|
|
|
|
|
const halfWidth = width / 2 |
|
|
|
|
|
|
|
|
|
|
|
const path = new THREE.LineCurve3( |
|
|
]).then(([_, texture]) => { |
|
|
new THREE.Vector3(startPosition.x, startPosition.z, halfWidth - Constract.HEIGHT_WAY_LINE), |
|
|
texture.flipY = false |
|
|
new THREE.Vector3(endPosition.x, endPosition.z, halfWidth - Constract.HEIGHT_WAY_LINE) |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const shape = new THREE.Shape() |
|
|
this.pointGeometry = new THREE.PlaneGeometry(1, 1).rotateX(-Math.PI / 2) |
|
|
shape.moveTo(halfWidth, -halfWidth) |
|
|
this.pointGeometry.center() |
|
|
shape.lineTo(halfWidth, halfWidth) |
|
|
|
|
|
|
|
|
|
|
|
const extrudeSettings: ExtrudeGeometryOptions = { |
|
|
|
|
|
steps: 2, // 沿路径的分段数
|
|
|
|
|
|
depth: 1, // 实际由路径长度决定
|
|
|
|
|
|
bevelEnabled: false, // 禁用倒角
|
|
|
|
|
|
extrudePath: path // 挤出路径
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const extrudedGeometry = new THREE.ExtrudeGeometry(shape, extrudeSettings) |
|
|
this.pointMaterial = new THREE.MeshBasicMaterial({ |
|
|
extrudedGeometry.rotateX(Math.PI / 2) |
|
|
map: texture, |
|
|
|
|
|
transparent: true, |
|
|
return new THREE.Mesh(extrudedGeometry, this.lineMaterial) |
|
|
depthWrite: false, |
|
|
|
|
|
side: THREE.DoubleSide |
|
|
|
|
|
}) |
|
|
|
|
|
this.pointMaterial.needsUpdate = true |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
createLineBasic(start: ItemJson, end: ItemJson, type: LinkType): THREE.Object3D { |
|
|
createPointBasic(item: ItemJson, option?: RendererCudOption): Object3DLike { |
|
|
const startPosition = new THREE.Vector3(start.tf[0][0], 0, start.tf[0][2]) |
|
|
// 不允许改变高度/角度/大小
|
|
|
const endPosition = new THREE.Vector3(end.tf[0][0], 0, end.tf[0][2]) |
|
|
item.tf = [ |
|
|
|
|
|
[item.tf[0][0], this.defulePositionY, item.tf[0][2]], |
|
|
const group = new THREE.Group() |
|
|
[this.defaultRotation.x, this.defaultRotation.y, this.defaultRotation.z], |
|
|
const lineMesh = this._createOrUpdateLine(startPosition, endPosition, type) |
|
|
[this.defaultScale.x, this.defaultScale.y, this.defaultScale.z] |
|
|
group.add(lineMesh) |
|
|
] |
|
|
|
|
|
return this.pointManager.createPoint(item) |
|
|
|
|
|
} |
|
|
const midPoint = new THREE.Vector3() |
|
|
|
|
|
.addVectors(startPosition, endPosition) |
|
|
|
|
|
.multiplyScalar(0.5) |
|
|
|
|
|
|
|
|
|
|
|
const distance = (startPosition.distanceTo(endPosition) * 1000).toFixed(0) |
|
|
|
|
|
|
|
|
|
|
|
const label = new Text() |
|
|
createLineBasic(start: ItemJson, end: ItemJson, type: LinkType): Object3DLike { |
|
|
label.text = distance |
|
|
const lineId = getLineId(start.id, end.id, type) |
|
|
label.font = SimSunTTF |
|
|
|
|
|
label.fontSize = 0.2 |
|
|
|
|
|
label.color = '#5f5f5f' |
|
|
|
|
|
label.opacity = 0.8 |
|
|
|
|
|
label.anchorX = 'center' |
|
|
|
|
|
label.anchorY = 'middle' |
|
|
|
|
|
label.depthOffset = 1 |
|
|
|
|
|
label.material.depthTest = false |
|
|
|
|
|
label.name = WayRenderer.LABEL_NAME |
|
|
|
|
|
label.quaternion.copy(this.tempViewport.camera.quaternion) |
|
|
|
|
|
label.sync() |
|
|
|
|
|
label.position.set(midPoint.x, Constract.HEIGHT_WAY_LABEL, midPoint.z) |
|
|
|
|
|
|
|
|
|
|
|
group.add(label) |
|
|
// 用 PlaneGeometry 替代线段
|
|
|
|
|
|
const startPosition = new THREE.Vector3(start.tf[0][0], this.defulePositionY, start.tf[0][2]) |
|
|
|
|
|
const endPosition = new THREE.Vector3(end.tf[0][0], this.defulePositionY, end.tf[0][2]) |
|
|
|
|
|
|
|
|
return group |
|
|
const wrap = this.lineManager.createPointSimple(lineId) |
|
|
|
|
|
const matrix = linkPlaneByPoint(startPosition, endPosition, this.rendererOption.lineWidth) |
|
|
|
|
|
wrap.applyMatrix4(matrix) |
|
|
|
|
|
return wrap |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
updateLine(start: ItemJson, end: ItemJson, type: LinkType, option?: RendererCudOption) { |
|
|
updateLine(start: ItemJson, end: ItemJson, type: LinkType, option?: RendererCudOption) { |
|
|
super.updateLine(start, end, type, option) |
|
|
|
|
|
|
|
|
|
|
|
const startPosition = new THREE.Vector3(start.tf[0][0], 0.01, start.tf[0][2]) |
|
|
|
|
|
const endPosition = new THREE.Vector3(end.tf[0][0], 0.01, end.tf[0][2]) |
|
|
|
|
|
|
|
|
|
|
|
const lineId = getLineId(start.id, end.id, type) |
|
|
const lineId = getLineId(start.id, end.id, type) |
|
|
const group = this.tempViewport.entityManager.findLineObjectById(lineId) |
|
|
|
|
|
|
|
|
|
|
|
// 清空group里的元素
|
|
|
const startPosition = new THREE.Vector3(start.tf[0][0], this.defulePositionY, start.tf[0][2]) |
|
|
const label: Text = group.children[1] |
|
|
const endPosition = new THREE.Vector3(end.tf[0][0], this.defulePositionY, end.tf[0][2]) |
|
|
group.clear() |
|
|
|
|
|
|
|
|
const wrap = this.tempViewport.entityManager.findLineObjectById(lineId) as PointManageWrap |
|
|
|
|
|
const matrix = linkPlaneByPoint(startPosition, endPosition, this.rendererOption.lineWidth) |
|
|
|
|
|
wrap.applyMatrix4(matrix) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const lineMesh = this._createOrUpdateLine(startPosition, endPosition, type) |
|
|
afterCreateOrUpdateLine(start: ItemJson, end: ItemJson, type: LinkType, option: RendererCudOption, object: Object3DLike) { |
|
|
group.add(lineMesh) |
|
|
super.afterCreateOrUpdateLine(start, end, type, option, object) |
|
|
|
|
|
|
|
|
const midPoint = new THREE.Vector3() |
|
|
const startPoint = this.tempViewport?.entityManager.findObjectById(start.id) |
|
|
.addVectors(startPosition, endPosition) |
|
|
const endPoint = this.tempViewport?.entityManager.findObjectById(end.id) |
|
|
.multiplyScalar(0.5) |
|
|
|
|
|
|
|
|
|
|
|
const distance = (startPosition.distanceTo(endPosition) * 1000).toFixed(0) |
|
|
this.tempViewport.labelManager.createOrUpdateLabelByDistance(object, startPoint.position, endPoint.position, { |
|
|
label.text = distance |
|
|
useHtmlLabel: false, |
|
|
label.quaternion.copy(this.tempViewport.camera.quaternion) |
|
|
fontSize: 0.2, |
|
|
label.sync() |
|
|
color: '#333333', |
|
|
label.position.set(midPoint.x, midPoint.y, midPoint.z) |
|
|
format: (distance) => { |
|
|
group.add(label) |
|
|
return (distance * 1000).toFixed(0) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
createPointBasic(item: ItemJson, option?: RendererCudOption): THREE.Object3D { |
|
|
afterDeleteLine(start: ItemJson, end: ItemJson, type: LinkType, option: RendererCudOption, object: Object3DLike) { |
|
|
const obj = new THREE.Sprite(this.pointMaterial as THREE.SpriteMaterial) |
|
|
super.afterDeleteLine(start, end, type, option, object) |
|
|
obj.name = WayRenderer.POINT_NAME |
|
|
this.tempViewport.labelManager.removeLabel(object) |
|
|
return obj |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
dispose() { |
|
|
dispose() { |
|
|
@ -179,4 +125,32 @@ export default class WayRenderer extends BaseRenderer { |
|
|
this.pointMaterial?.dispose() |
|
|
this.pointMaterial?.dispose() |
|
|
this.lineMaterial?.dispose() |
|
|
this.lineMaterial?.dispose() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get lineManager(): InstancePointManager { |
|
|
|
|
|
if (!this.tempViewport) { |
|
|
|
|
|
throw new Error('tempViewport is not set.') |
|
|
|
|
|
} |
|
|
|
|
|
return this.tempViewport.getOrCreatePointManager(this.itemTypeName + '_line', () => |
|
|
|
|
|
// 构建 LineSegment.points 代理对象
|
|
|
|
|
|
InstancePointManager.create(this.itemTypeName + '_line', |
|
|
|
|
|
this.tempViewport, |
|
|
|
|
|
this.lineGeometry, |
|
|
|
|
|
this.lineMaterial, |
|
|
|
|
|
Constract.MAX_WAY_LINE_INSTANCES) |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get pointManager(): InstancePointManager { |
|
|
|
|
|
if (!this.tempViewport) { |
|
|
|
|
|
throw new Error('tempViewport is not set.') |
|
|
|
|
|
} |
|
|
|
|
|
return this.tempViewport.getOrCreatePointManager(this.itemTypeName, () => |
|
|
|
|
|
// 构建 InstanceMesh 代理对象
|
|
|
|
|
|
InstancePointManager.create(this.itemTypeName, |
|
|
|
|
|
this.tempViewport, |
|
|
|
|
|
this.pointGeometry, |
|
|
|
|
|
this.pointMaterial, |
|
|
|
|
|
Constract.MAX_WAY_INSTANCES) |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|