From a30b2ae5dba27635d93ad2649fb854b9f1047eef Mon Sep 17 00:00:00 2001 From: luoyifan Date: Thu, 12 Jun 2025 16:34:31 +0800 Subject: [PATCH] =?UTF-8?q?Rack=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/rack/RackRenderer.ts | 236 +++++++++++++++++---------------------- 1 file changed, 100 insertions(+), 136 deletions(-) diff --git a/src/modules/rack/RackRenderer.ts b/src/modules/rack/RackRenderer.ts index af4c789..9279bb4 100644 --- a/src/modules/rack/RackRenderer.ts +++ b/src/modules/rack/RackRenderer.ts @@ -9,9 +9,6 @@ import { Material } from 'three/src/materials/Material' import { InstancedMesh } from 'three/src/objects/InstancedMesh' //@ts-ignore import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js' -import InstancePointManager from '@/core/manager/InstancePointManager.ts' -import InstanceMeshManager from '@/core/manager/InstanceMeshManager.ts' -import type { Object3DLike } from '@/types/ModelTypes.ts' /** * 货架货位渲染器 @@ -29,64 +26,6 @@ export default class RackRenderer extends BaseRenderer { readonly defaultRotation: THREE.Vector3 = new THREE.Vector3(0, 0, 0) readonly defaultLineWidth: number = 0.05 - - bottomBarHeight = 0.2 - bottomLinkHeight = 0.2 - - barSectionPoints = [ - { x: -0.05, y: -0.05 }, - { x: -0.025, y: -0.05 }, - { x: -0.01, y: -0.045 }, - { x: 0.05, y: -0.045 }, - { x: 0.025, y: -0.05 }, - { x: 0.05, y: -0.05 }, - { x: 0.05, y: 0.042 }, - { x: 0.042, y: 0.05 }, - { x: 0.025, y: 0.05 }, - { x: 0.025, y: 0.042 }, - { x: 0.042, y: 0.042 }, - { x: 0.042, y: -0.042 }, - { x: -0.042, y: -0.042 }, - { x: -0.042, y: 0.042 }, - { x: -0.025, y: 0.042 }, - { x: -0.025, y: 0.05 }, - { x: -0.042, y: 0.05 }, - { x: -0.05, y: 0.042 }, - { x: -0.05, y: -0.05 } - ] - - linkSectionPoints = [ - { x: -0.05, y: -0.05 }, - { x: -0.05, y: 0.05 }, - { x: 0, y: 0.05 }, - { x: 0, y: 0.06 }, - { x: -0.06, y: 0.06 }, - { x: -0.06, y: -0.05 }, - { x: -0.05, y: -0.05 } - ] - - linkBarSectionPoints = [ - { x: -0.025, y: -0.025 }, - { x: 0.025, y: -0.025 }, - { x: 0.025, y: 0.025 }, - { x: -0.025, y: 0.025 }, - { x: -0.025, y: -0.025 } - ] - rackVerticalBarWidth = 0.1 - rackVerticalBarDepth = 0.08 - rackVerticalBarColor = 0xFF35499C - rackVerticalBarMaterial: Material = this.createVerticalBarMaterial() - - rackLinkBarColor = 0xFF35499C - rackLinkBarGeometry: BufferGeometry = null - rackLinkBarMaterial: Material = this.createLinkBarMaterial() - - rackHorizontalBarWidth = 0.1 - rackHorizontalBarDepth = 0.08 - rackHorizontalBarColor = 0xFFF97F27 - rackHorizontalBarMaterial: Material = this.createHorizontalBarMaterial() - - constructor(itemTypeName: string) { super(itemTypeName) } @@ -118,7 +57,7 @@ export default class RackRenderer extends BaseRenderer { } - createPoint(item: ItemJson, option?: RendererCudOption): Object3DLike { + createPoint(item: ItemJson, option?: RendererCudOption): THREE.Object3D { // 创建平面几何体 if (!item.dt.bays || !item.dt.rackDepth) { system.showErrorDialog('RackRenderer field bays / rackDepth is null!') @@ -188,12 +127,16 @@ export default class RackRenderer extends BaseRenderer { const meshes = this.createRack(item, option) + meshes.forEach(mesh => { + group.add(mesh) + }) + // 设置位置 // group.position.set(item.tf[0][0], item.tf[0][1], item.tf[0][2]) // // item.dt.rackWidth = rackWidth // item.dt.rackHeight = rackHeight - return meshes + return group } dispose() { @@ -205,7 +148,67 @@ export default class RackRenderer extends BaseRenderer { throw new Error('Rack createPointBasic not allow!') } - createVerticalBar(length: number): THREE.BufferGeometry { + rackVerticalBarWidth = 0.1 + rackVerticalBarDepth = 0.08 + rackVerticalBarColor = 0xFF35499C + rackVerticalBarGeometry: BufferGeometry = null + rackVerticalBarMaterial: Material = null + + rackLinkBarColor = 0xFF35499C + rackLinkBarGeometry: BufferGeometry = null + rackLinkBarMaterial: Material = null + + rackHorizontalBarWidth = 0.1 + rackHorizontalBarDepth = 0.08 + rackHorizontalBarColor = 0xFFF97F27 + rackHorizontalBarGeometry: BufferGeometry = null + rackHorizontalBarMaterial: Material = null + + bottomBarHeight = 0.2 + bottomLinkHeight = 0.2 + + barSectionPoints = [ + { x: -0.05, y: -0.05 }, + { x: -0.025, y: -0.05 }, + { x: -0.01, y: -0.045 }, + { x: 0.05, y: -0.045 }, + { x: 0.025, y: -0.05 }, + { x: 0.05, y: -0.05 }, + { x: 0.05, y: 0.042 }, + { x: 0.042, y: 0.05 }, + { x: 0.025, y: 0.05 }, + { x: 0.025, y: 0.042 }, + { x: 0.042, y: 0.042 }, + { x: 0.042, y: -0.042 }, + { x: -0.042, y: -0.042 }, + { x: -0.042, y: 0.042 }, + { x: -0.025, y: 0.042 }, + { x: -0.025, y: 0.05 }, + { x: -0.042, y: 0.05 }, + { x: -0.05, y: 0.042 }, + { x: -0.05, y: -0.05 } + ] + + linkSectionPoints = [ + { x: -0.05, y: -0.05 }, + { x: -0.05, y: 0.05 }, + { x: 0, y: 0.05 }, + { x: 0, y: 0.06 }, + { x: -0.06, y: 0.06 }, + { x: -0.06, y: -0.05 }, + { x: -0.05, y: -0.05 } + ] + + linkBarSectionPoints = [ + { x: -0.025, y: -0.025 }, + { x: 0.025, y: -0.025 }, + { x: 0.025, y: 0.025 }, + { x: -0.025, y: 0.025 }, + { x: -0.025, y: -0.025 } + ] + + createVerticalBar(x, y, z, length): THREE.BufferGeometry { + // 创建一个形状 柱子的截面形状 const shape = new THREE.Shape() shape.moveTo(this.barSectionPoints[0].x, this.barSectionPoints[0].y) @@ -264,7 +267,7 @@ export default class RackRenderer extends BaseRenderer { return material } - createLinkBar(vBarLength: number, depth: number, bottomDistance: number, topDistance: number): THREE.BufferGeometry { + createLinkBar(x, y, z, vBarLength, depth, bottomDistance, topDistance): THREE.BufferGeometry { const bgs: BufferGeometry[] = [] const top = vBarLength - topDistance @@ -355,7 +358,8 @@ export default class RackRenderer extends BaseRenderer { return material } - createHorizontalBar(length: number): THREE.BufferGeometry { + createHorizontalBar(x, y, z, length): THREE.BufferGeometry { + // 创建一个形状 柱子的截面形状 const shape = new THREE.Shape() shape.moveTo(this.barSectionPoints[0].x, this.barSectionPoints[0].y) @@ -426,7 +430,7 @@ export default class RackRenderer extends BaseRenderer { return material } - createRack(item: ItemJson, option?: RendererCudOption): Object3DLike { + createRack(item: ItemJson, option?: RendererCudOption): InstancedMesh[] { if (!item.dt.bays || !item.dt.rackDepth) { system.showErrorDialog('RackRenderer field bays / rackDepth is null!') return null @@ -537,59 +541,68 @@ export default class RackRenderer extends BaseRenderer { distanceX += bay.bayWidth } + const meshes: InstancedMesh[] = [] + if (vBarMatrix.length > 0) { + if (!this.rackVerticalBarGeometry) { + this.rackVerticalBarGeometry = this.createVerticalBar(vBarMatrix[0].x, vBarMatrix[0].y, vBarMatrix[0].z, vBarMatrix[0].l) + } + if (!this.rackVerticalBarMaterial) { + this.rackVerticalBarMaterial = this.createVerticalBarMaterial() + } const dummy = new THREE.Object3D() - - // const vBarMesh = new THREE.InstancedMesh(this.rackVerticalBarGeometry, this.rackVerticalBarMaterial, vBarMatrix.length) + const vBarMesh = new THREE.InstancedMesh(this.rackVerticalBarGeometry, this.rackVerticalBarMaterial, vBarMatrix.length) for (let i = 0; i < vBarMatrix.length; i++) { const vp = vBarMatrix[i] - const wrap = this.getVBarMesh(vp.l).create(item.id) - dummy.position.set(vp.x, vp.y, vp.z) dummy.rotation.set(vp.rx, vp.ry, vp.rz) dummy.scale.set(vp.sx, vp.sy, vp.sz) dummy.updateMatrix() - wrap.setMatrix4(dummy.matrix) + vBarMesh.setMatrixAt(i, dummy.matrix) } + meshes.push(vBarMesh) } if (linkBarMatrix.length > 0) { - // if (!this.rackLinkBarGeometry) { - // this.rackLinkBarGeometry = this.createLinkBar(rackHeight, item.dt.rackDepth, this.bottomLinkHeight, 0.2) - // } - const meshInstance = this.getLinkBarMesh(rackHeight, item.dt.rackDepth, this.bottomLinkHeight, 0.2) + if (!this.rackLinkBarGeometry) { + this.rackLinkBarGeometry = this.createLinkBar(linkBarMatrix[0].x, linkBarMatrix[0].y, linkBarMatrix[0].z, rackHeight, item.dt.rackDepth, this.bottomLinkHeight, 0.2) + } + if (!this.rackLinkBarMaterial) { + this.rackLinkBarMaterial = this.createLinkBarMaterial() + } const dummy = new THREE.Object3D() const linkBarMesh = new THREE.InstancedMesh(this.rackLinkBarGeometry, this.rackLinkBarMaterial, linkBarMatrix.length) for (let i = 0; i < linkBarMatrix.length; i++) { - - const wrap = meshInstance.create(item.id) - const lp = linkBarMatrix[i] dummy.position.set(lp.x, lp.y, lp.z) dummy.rotation.set(lp.rx, lp.ry, lp.rz) dummy.scale.set(lp.sx, lp.sy, lp.sz) dummy.updateMatrix() - // linkBarMesh.setMatrixAt(i, dummy.matrix) - wrap.setMatrix4(dummy.matrix) + linkBarMesh.setMatrixAt(i, dummy.matrix) } + meshes.push(linkBarMesh) } if (hBarMatrix.length > 0) { + if (!this.rackHorizontalBarGeometry) { + this.rackHorizontalBarGeometry = this.createHorizontalBar(hBarMatrix[0].x, hBarMatrix[0].y, hBarMatrix[0].z, hBarMatrix[0].l) + } + if (!this.rackHorizontalBarMaterial) { + this.rackHorizontalBarMaterial = this.createHorizontalBarMaterial() + } const dummy = new THREE.Object3D() - // const hBarMesh = new THREE.InstancedMesh(this.rackHorizontalBarGeometry, this.rackHorizontalBarMaterial, hBarMatrix.length) + const hBarMesh = new THREE.InstancedMesh(this.rackHorizontalBarGeometry, this.rackHorizontalBarMaterial, hBarMatrix.length) for (let i = 0; i < hBarMatrix.length; i++) { const hp = hBarMatrix[i] - const wrap = this.getHBarMesh(hp.l).create(item.id) - dummy.position.set(hp.x, hp.y, hp.z) dummy.rotation.set(hp.rx, hp.ry, hp.rz) dummy.scale.set(hp.sx, hp.sy, hp.sz) dummy.updateMatrix() - wrap.setMatrix4(dummy.matrix) + hBarMesh.setMatrixAt(i, dummy.matrix) } + meshes.push(hBarMesh) } - - return new THREE.Group() + return meshes } @@ -625,53 +638,4 @@ export default class RackRenderer extends BaseRenderer { } } - getLinkBarMesh(vBarLength: number, depth: number, bottomDistance: number, topDistance: number): InstanceMeshManager { - if (!this.tempViewport) { - throw new Error('tempViewport is not set.') - } - - // rackHeight, item.dt.rackDepth, this.bottomLinkHeight, 0.2 - const geometry = this.createLinkBar(vBarLength, depth, bottomDistance, topDistance) - const name = 'rack_linkbar_' + vBarLength + '_' + depth + '_' + bottomDistance + '_' + topDistance - - return this.tempViewport.getOrCreateMeshManager(name, () => - // 构建 InstanceMesh 代理对象 - new InstanceMeshManager(name, - this.tempViewport, - geometry, this.rackLinkBarMaterial, - false, false, 100000) - ) - } - - getVBarMesh(length: number): InstanceMeshManager { - if (!this.tempViewport) { - throw new Error('tempViewport is not set.') - } - - const name = 'rack_vbar' + length - - return this.tempViewport.getOrCreateMeshManager(name, () => { - const geometry = this.createVerticalBar(length) - return new InstanceMeshManager(name, - this.tempViewport, - geometry, this.rackVerticalBarMaterial, - false, false, 100000) - }) - } - - getHBarMesh(length: number): InstanceMeshManager { - if (!this.tempViewport) { - throw new Error('tempViewport is not set.') - } - - const name = 'rack_hbar' + length - - return this.tempViewport.getOrCreateMeshManager(name, () => { - const geometry = this.createHorizontalBar(length) - return new InstanceMeshManager(name, - this.tempViewport, - geometry, this.rackHorizontalBarMaterial, - false, false, 100000) - }) - } }