From 0e272ab527020305adad63d5dba4f21b401f24cf Mon Sep 17 00:00:00 2001 From: luoyifan Date: Thu, 12 Jun 2025 16:30:50 +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/core/manager/InstanceMeshManager.ts | 5 +- src/example/example1.js | 2 +- src/modules/rack/RackRenderer.ts | 236 ++++++++++++++++++-------------- 3 files changed, 140 insertions(+), 103 deletions(-) diff --git a/src/core/manager/InstanceMeshManager.ts b/src/core/manager/InstanceMeshManager.ts index aaf0acf..839ff2d 100644 --- a/src/core/manager/InstanceMeshManager.ts +++ b/src/core/manager/InstanceMeshManager.ts @@ -10,7 +10,7 @@ export default class InstanceMeshManager { public readonly viewport: Viewport public readonly allowSelect: boolean public readonly allowDrag: boolean - public readonly blockCapacity: number = 1000 // 每个 block 的容量 + public readonly blockCapacity: number public readonly blocks: InstanceMeshBlock[] = [] private readonly geometry: THREE.BufferGeometry @@ -20,13 +20,14 @@ export default class InstanceMeshManager { constructor(name: string, viewport: Viewport, geometry: THREE.BufferGeometry, material: THREE.Material, - allowSelect: boolean, allowDrag: boolean) { + allowSelect: boolean, allowDrag: boolean, blockCapacity: number = 1000) { this.name = name this.viewport = viewport this.allowSelect = allowSelect this.allowDrag = allowDrag this.geometry = geometry this.material = material + this.blockCapacity = blockCapacity } /** diff --git a/src/example/example1.js b/src/example/example1.js index 53b83d5..aa4ce8f 100644 --- a/src/example/example1.js +++ b/src/example/example1.js @@ -399,7 +399,7 @@ export default { }, { catalogCode: '__f3', t: 'floor', - items: buildRackPerformanceData(50, 200) + items: buildRackPerformanceData(10, 200) } ], elevator: [], // 电梯 diff --git a/src/modules/rack/RackRenderer.ts b/src/modules/rack/RackRenderer.ts index 9279bb4..af4c789 100644 --- a/src/modules/rack/RackRenderer.ts +++ b/src/modules/rack/RackRenderer.ts @@ -9,6 +9,9 @@ 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' /** * 货架货位渲染器 @@ -26,6 +29,64 @@ 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) } @@ -57,7 +118,7 @@ export default class RackRenderer extends BaseRenderer { } - createPoint(item: ItemJson, option?: RendererCudOption): THREE.Object3D { + createPoint(item: ItemJson, option?: RendererCudOption): Object3DLike { // 创建平面几何体 if (!item.dt.bays || !item.dt.rackDepth) { system.showErrorDialog('RackRenderer field bays / rackDepth is null!') @@ -127,16 +188,12 @@ 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 group + return meshes } dispose() { @@ -148,67 +205,7 @@ export default class RackRenderer extends BaseRenderer { throw new Error('Rack createPointBasic not allow!') } - 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 { - + createVerticalBar(length: number): THREE.BufferGeometry { // 创建一个形状 柱子的截面形状 const shape = new THREE.Shape() shape.moveTo(this.barSectionPoints[0].x, this.barSectionPoints[0].y) @@ -267,7 +264,7 @@ export default class RackRenderer extends BaseRenderer { return material } - createLinkBar(x, y, z, vBarLength, depth, bottomDistance, topDistance): THREE.BufferGeometry { + createLinkBar(vBarLength: number, depth: number, bottomDistance: number, topDistance: number): THREE.BufferGeometry { const bgs: BufferGeometry[] = [] const top = vBarLength - topDistance @@ -358,8 +355,7 @@ export default class RackRenderer extends BaseRenderer { return material } - createHorizontalBar(x, y, z, length): THREE.BufferGeometry { - + createHorizontalBar(length: number): THREE.BufferGeometry { // 创建一个形状 柱子的截面形状 const shape = new THREE.Shape() shape.moveTo(this.barSectionPoints[0].x, this.barSectionPoints[0].y) @@ -430,7 +426,7 @@ export default class RackRenderer extends BaseRenderer { return material } - createRack(item: ItemJson, option?: RendererCudOption): InstancedMesh[] { + createRack(item: ItemJson, option?: RendererCudOption): Object3DLike { if (!item.dt.bays || !item.dt.rackDepth) { system.showErrorDialog('RackRenderer field bays / rackDepth is null!') return null @@ -541,68 +537,59 @@ 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() - vBarMesh.setMatrixAt(i, dummy.matrix) + wrap.setMatrix4(dummy.matrix) } - meshes.push(vBarMesh) } if (linkBarMatrix.length > 0) { - 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() - } + // 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) 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) + // linkBarMesh.setMatrixAt(i, dummy.matrix) + wrap.setMatrix4(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() - hBarMesh.setMatrixAt(i, dummy.matrix) + wrap.setMatrix4(dummy.matrix) } - meshes.push(hBarMesh) } - return meshes + + return new THREE.Group() } @@ -638,4 +625,53 @@ 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) + }) + } }