feat: v1.5 3D 门窗开合动画
- 扇推断逻辑抽取为共享模块 inferSashGlyphs(2D符号与3D动画共用) - WindoorThreed 重写: 扇面板独立网格组 平开扇绕铰链旋转(od定铰链侧, 门75°/平开32°/双向门40°) 推拉扇沿轨道平移(双扇前后轨交替) 区域归属扇的不再重复渲染静态玻璃 - 开合控制条: 开合度滑杆+自动演示+全开/全关, 平滑插值动画 - 组合画布 3D 预览自动获得开合能力
This commit is contained in:
@@ -5,23 +5,36 @@
|
||||
<span v-if="info">{{ info }}</span>
|
||||
<span class="hint">左键旋转 · 滚轮缩放 · 右键平移</span>
|
||||
</div>
|
||||
<div class="ctrl" v-if="hasOpenable">
|
||||
<span>开合</span>
|
||||
<el-slider v-model="openPct" style="width: 160px" :min="0" :max="100" :format-tooltip="v => v + '%'" />
|
||||
<el-checkbox v-model="autoPlay" size="small">自动演示</el-checkbox>
|
||||
<el-button size="small" text @click="openPct = openPct > 0 ? 0 : 100">{{ openPct > 0 ? '全关' : '全开' }}</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* 3D 窗型查看器(自研 Three.js 实现)
|
||||
* 从 drawing_data 重建:外框/中梃 → 型材盒体;玻璃区域 → 半透明玻璃板;
|
||||
* ArcBall 轨道控制(自实现,不依赖 three examples 以控制体积)。
|
||||
* 3D 窗型查看器 + 门窗开合动画(自研 Three.js 实现)
|
||||
* - 玻璃区域/中梃/外框静态重建(分割树重放)
|
||||
* - 扇面板独立成组: 平开扇绕铰链旋转(门开角更大), 推拉扇沿轨道平移, 双向门两向开
|
||||
* - 开合度滑杆 / 自动演示; 组合画布的多形状直接支持
|
||||
*/
|
||||
import { onMounted, onBeforeUnmount, ref, watch } from 'vue'
|
||||
import { onMounted, onBeforeUnmount, ref, watch, computed } from 'vue'
|
||||
import * as THREE from 'three'
|
||||
import { bboxOf, parseDrawing, regionPoints, replayShape, type DrawingData, type Pt } from '../types/drawing'
|
||||
import {
|
||||
bboxOf, inferSashGlyphs, parseDrawing, regionPoints, replayShape,
|
||||
type DrawingData, type Pt,
|
||||
} from '../types/drawing'
|
||||
|
||||
const props = defineProps<{ drawingData: string | object }>()
|
||||
const props = withDefaults(defineProps<{ drawingData: string | object; controls?: boolean }>(), { controls: true })
|
||||
const cvRef = ref<HTMLCanvasElement>()
|
||||
const wrapRef = ref<HTMLDivElement>()
|
||||
const info = ref('')
|
||||
const openPct = ref(0)
|
||||
const autoPlay = ref(false)
|
||||
const hasOpenable = ref(false)
|
||||
|
||||
let renderer: THREE.WebGLRenderer | null = null
|
||||
let scene: THREE.Scene | null = null
|
||||
@@ -29,6 +42,16 @@ let camera: THREE.PerspectiveCamera | null = null
|
||||
let raf = 0
|
||||
let orbit = { theta: -0.7, phi: 1.1, dist: 1.6, target: new THREE.Vector3(0, 0, 0) }
|
||||
let dragging: { x: number; y: number; button: number } | null = null
|
||||
let sashAnims: {
|
||||
group: THREE.Group
|
||||
kind: 'sliding' | 'casement'
|
||||
baseX: number
|
||||
slideDir: number
|
||||
hingeSign: number
|
||||
maxAng: number
|
||||
w: number
|
||||
}[] = []
|
||||
let curT = 0
|
||||
|
||||
function dispose() {
|
||||
if (scene) {
|
||||
@@ -51,67 +74,134 @@ function build(drawing: DrawingData) {
|
||||
const dir = new THREE.DirectionalLight(0xffffff, 0.9)
|
||||
dir.position.set(1, 2, 3)
|
||||
scene.add(dir)
|
||||
sashAnims = []
|
||||
|
||||
// 全局包围盒(取全部基底区域)
|
||||
const pts: Pt[] = []
|
||||
for (const s of drawing.sm) {
|
||||
for (const r of replayShape(s).regions) {
|
||||
for (const seg of r) { pts.push(seg.ps); pts.push(seg.pe) }
|
||||
}
|
||||
}
|
||||
if (!pts.length) { info.value = '无几何数据'; return }
|
||||
if (!pts.length) { info.value = '无几何数据'; hasOpenable.value = false; return }
|
||||
const gb = bboxOf(pts)
|
||||
const gw = gb.x1 - gb.x0, gh = gb.y1 - gb.y0
|
||||
const maxDim = Math.max(gw, gh)
|
||||
const S = 1 / maxDim // 归一化到 1 单位
|
||||
const S = 1 / maxDim
|
||||
const cx = (gb.x0 + gb.x1) / 2, cy = (gb.y0 + gb.y1) / 2
|
||||
const DEPTH = Math.max(0.04, (drawing.s?.cs ?? 60) * S * 1.2) // 型材厚度
|
||||
const GLASS_T = 0.008
|
||||
const DEPTH = Math.max(0.04, (drawing.s?.cs ?? 60) * S * 1.2)
|
||||
|
||||
const frameMat = new THREE.MeshStandardMaterial({ color: 0x636568, roughness: 0.55, metalness: 0.35 })
|
||||
const sashMat = new THREE.MeshStandardMaterial({ color: 0x777c82, roughness: 0.5, metalness: 0.35 })
|
||||
const glassMat = new THREE.MeshPhysicalMaterial({
|
||||
color: 0xdfeef5, transparent: true, opacity: 0.42, roughness: 0.08,
|
||||
metalness: 0.05, side: THREE.DoubleSide,
|
||||
})
|
||||
|
||||
const frameGroup = new THREE.Group()
|
||||
const sashRoot = new THREE.Group()
|
||||
scene.add(frameGroup)
|
||||
scene.add(sashRoot)
|
||||
|
||||
const fwOf = (shape: any) => {
|
||||
const ew = shape.fm?.ew ?? [60, 60, 60, 60]
|
||||
return Math.max(0.02, (ew.reduce((a: number, b: number) => a + b, 0) / ew.length) * S)
|
||||
}
|
||||
|
||||
for (const shape of drawing.sm) {
|
||||
const { regions } = replayShape(shape)
|
||||
const { regions, sashes } = inferSashGlyphs(shape)
|
||||
if (!regions.length) continue
|
||||
const cm = shape.cm ?? {}
|
||||
const gm = cm.g
|
||||
? new THREE.MeshPhysicalMaterial({
|
||||
color: new THREE.Color(cm.g.replace(/rgba?\(([^)]+)\)/, (_, v: string) => {
|
||||
color: new THREE.Color(cm.g.replace(/rgba?\(([^)]+)\)/, (_m: string, v: string) => {
|
||||
const parts = v.split(',').map(x => parseFloat(x))
|
||||
return `rgb(${parts[0]},${parts[1]},${parts[2]})`
|
||||
})),
|
||||
transparent: true, opacity: 0.45, roughness: 0.1, side: THREE.DoubleSide,
|
||||
})
|
||||
: glassMat
|
||||
const fw = fwOf(shape)
|
||||
|
||||
// 判断每个区域是否属于某扇(区域中心在扇矩形内 → 由扇面板渲染)
|
||||
const sashRects = sashes.map(s => ({ ...s, cx: (s.x0 + s.x1) / 2, cy: (s.y0 + s.y1) / 2 }))
|
||||
|
||||
for (const region of regions) {
|
||||
const rpts = regionPoints(region)
|
||||
if (rpts.length < 3) continue
|
||||
const bb = bboxOf(rpts)
|
||||
const w = (bb.x1 - bb.x0) * S, h = (bb.y1 - bb.y0) * S
|
||||
const mx = ((bb.x0 + bb.x1) / 2 - cx) * S, my = ((bb.y0 + bb.y1) / 2 - cy) * S
|
||||
// 玻璃板
|
||||
const glass = new THREE.Mesh(new THREE.BoxGeometry(w, h, GLASS_T), gm)
|
||||
const mx = ((bb.x0 + bb.x1) / 2 - cx) * S
|
||||
const my = ((bb.y0 + bb.y1) / 2 - cy) * S
|
||||
const inSash = sashRects.some(r => {
|
||||
const cxr = (r.x0 + r.x1) / 2, cyr = (r.y0 + r.y1) / 2
|
||||
return Math.abs(cxr - (bb.x0 + bb.x1) / 2) < w / 2 && Math.abs(cyr - (bb.y0 + bb.y1) / 2) < h / 2
|
||||
})
|
||||
if (inSash) continue
|
||||
const glass = new THREE.Mesh(new THREE.BoxGeometry(w, h, 0.008), gm)
|
||||
glass.position.set(mx, my, 0)
|
||||
scene!.add(glass)
|
||||
// 四周型材框(盒体,宽度=料宽 8% 视觉比例)
|
||||
const fw = Math.max(0.02, (shape.fm?.ew?.[0] ?? 60) * S)
|
||||
frameGroup.add(glass)
|
||||
}
|
||||
|
||||
// 扇面板(可开合):玻璃 + 四周扇框,装入铰链/滑动组
|
||||
sashes.forEach((s, k) => {
|
||||
const w = (s.x1 - s.x0) * S, h = (s.y1 - s.y0) * S
|
||||
if (w < 0.02 || h < 0.02) return
|
||||
const cxr = ((s.x0 + s.x1) / 2 - cx) * S
|
||||
const cyr = ((s.y0 + s.y1) / 2 - cy) * S
|
||||
const group = new THREE.Group()
|
||||
const panel = new THREE.Group()
|
||||
const glass = new THREE.Mesh(new THREE.BoxGeometry(w, h, 0.006), gm)
|
||||
panel.add(glass)
|
||||
const t = Math.max(0.018, fw)
|
||||
const mkBar = (bw: number, bh: number, ox: number, oy: number) => {
|
||||
const bar = new THREE.Mesh(new THREE.BoxGeometry(bw, bh, DEPTH * 0.8), sashMat)
|
||||
bar.position.set(ox, oy, 0)
|
||||
panel.add(bar)
|
||||
}
|
||||
mkBar(w, t, 0, h / 2 - t / 2)
|
||||
mkBar(w, t, 0, -h / 2 + t / 2)
|
||||
mkBar(t, h, -w / 2 + t / 2, 0)
|
||||
mkBar(t, h, w / 2 - t / 2, 0)
|
||||
group.add(panel)
|
||||
sashRoot.add(group)
|
||||
|
||||
if (s.kind === 'sliding') {
|
||||
// 推拉: 双扇前后轨交替, 沿 X 平移
|
||||
group.position.set(cxr, cyr, k % 2 === 0 ? 0.012 : -0.012)
|
||||
sashAnims.push({ group, kind: 'sliding', baseX: cxr, slideDir: k % 2 === 0 ? 1 : -1, hingeSign: 1, maxAng: 0, w })
|
||||
} else {
|
||||
// 平开/门: 铰链在 od 指向的对侧(箭头指向开启方向), 绕 Y 轴旋转
|
||||
const odRad = ((s.od ?? 90) * Math.PI) / 180
|
||||
const hingeLeft = Math.cos(odRad) >= 0 ? false : true // 箭头偏右→铰链在左
|
||||
const hingeX = hingeLeft ? cxr - w / 2 : cxr + w / 2
|
||||
group.position.set(hingeX, cyr, 0)
|
||||
panel.position.set(cxr - hingeX, 0, 0)
|
||||
const maxAng = (s.isDoor ? 75 : s.wtf ? 40 : 32) * Math.PI / 180
|
||||
const dirSign = s.openToward === 'inward' ? 1 : -1
|
||||
sashAnims.push({ group, kind: 'casement', baseX: hingeX, slideDir: 0, hingeSign: dirSign, maxAng, w })
|
||||
}
|
||||
})
|
||||
|
||||
// 静态外框(整体)
|
||||
if (regions.length) {
|
||||
const all = regions.map(r => regionPoints(r).length ? r : r).flat()
|
||||
const bb = bboxOf(all.flatMap(seg => [seg.ps, seg.pe]))
|
||||
const x0 = ((bb.x0) - cx) * S, x1 = ((bb.x1) - cx) * S
|
||||
const y0 = ((bb.y0) - cy) * S, y1 = ((bb.y1) - cy) * S
|
||||
const w = x1 - x0, h = y1 - y0
|
||||
const mk = (bw: number, bh: number, ox: number, oy: number) => {
|
||||
const bar = new THREE.Mesh(new THREE.BoxGeometry(bw, bh, DEPTH), frameMat)
|
||||
bar.position.set(mx + ox, my + oy, 0)
|
||||
scene!.add(bar)
|
||||
bar.position.set((x0 + x1) / 2 + ox, (y0 + y1) / 2 + oy, 0)
|
||||
frameGroup.add(bar)
|
||||
}
|
||||
mk(w + fw * 2, fw, 0, h / 2 + fw / 2) // 下(屏幕y翻转后为底)
|
||||
mk(w + fw * 2, fw, 0, -h / 2 - fw / 2) // 上
|
||||
mk(fw, h, -w / 2 - fw / 2, 0) // 左
|
||||
mk(fw, h, w / 2 + fw / 2, 0) // 右
|
||||
mk(w + fw * 2, fw, 0, y1 + fw / 2)
|
||||
mk(w + fw * 2, fw, 0, y0 - fw / 2)
|
||||
mk(fw, h, x0 - fw / 2, 0)
|
||||
mk(fw, h, x1 + fw / 2, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// 地面参考网格(模型已归一化到 1 单位,网格取 2 单位)
|
||||
hasOpenable.value = sashAnims.some(a => a.kind === 'sliding' || a.maxAng > 0)
|
||||
const grid = new THREE.GridHelper(2, 10, 0xc8cdd2, 0xdfe3e8)
|
||||
grid.position.y = -gh * S / 2 - DEPTH
|
||||
scene.add(grid)
|
||||
@@ -124,9 +214,23 @@ function build(drawing: DrawingData) {
|
||||
info.value = `${Math.round(gw)} × ${Math.round(gh)} mm`
|
||||
}
|
||||
|
||||
function applyOpen(t: number) {
|
||||
curT = t
|
||||
for (const a of sashAnims) {
|
||||
if (a.kind === 'sliding') {
|
||||
a.group.position.x = a.baseX + a.slideDir * a.w * 0.42 * t
|
||||
} else {
|
||||
a.group.rotation.y = a.hingeSign * a.maxAng * t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function loop() {
|
||||
raf = requestAnimationFrame(loop)
|
||||
if (!renderer || !scene || !camera) return
|
||||
// 自动演示: 开合度按正弦摆动
|
||||
const target = (autoPlay.value ? 50 + 50 * Math.sin(Date.now() / 1200) : openPct.value) / 100
|
||||
applyOpen(curT + (target - curT) * 0.12)
|
||||
camera.position.set(
|
||||
orbit.target.x + orbit.dist * Math.sin(orbit.phi) * Math.sin(orbit.theta),
|
||||
orbit.target.y + orbit.dist * Math.cos(orbit.phi),
|
||||
@@ -149,7 +253,6 @@ function onMove(e: MouseEvent) {
|
||||
const dx = e.clientX - dragging.x, dy = e.clientY - dragging.y
|
||||
dragging = { x: e.clientX, y: e.clientY, button: dragging.button }
|
||||
if (dragging.button === 2) {
|
||||
// 平移
|
||||
const pan = new THREE.Vector3().crossVectors(camera!.up, camera!.getWorldDirection(new THREE.Vector3()))
|
||||
orbit.target.addScaledVector(pan, -dx * orbit.dist * 0.001)
|
||||
orbit.target.y += dy * orbit.dist * 0.001
|
||||
@@ -193,4 +296,8 @@ canvas { width: 100%; height: 100%; display: block; }
|
||||
.hud3d { position: absolute; right: 10px; top: 10px; background: rgba(255,255,255,0.9);
|
||||
border-radius: 6px; padding: 4px 10px; font-size: 12px; color: #606266; display: flex; gap: 12px; }
|
||||
.hint { color: #b0b5bc; }
|
||||
.ctrl { position: absolute; left: 50%; transform: translateX(-50%); bottom: 12px;
|
||||
background: rgba(255,255,255,0.92); border-radius: 10px; padding: 8px 14px;
|
||||
display: flex; gap: 12px; align-items: center; font-size: 12px; color: #606266;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.12); }
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user