import { Color4 as Color4Math, Vector3, Vector3Like } from '@feng3d/math'; import type { Color4 } from '../core/Color4'; import { Geometry, GeometryLogic } from './Geometry'; import { registerLogic, reactive, computed } from '@feng3d/reactivity'; import { VertexAttributes } from '@feng3d/webgpu'; declare module './Geometry' { export interface GeometryMap { SegmentGeometry: SegmentGeometry; } } /** * 线段 */ export interface Segment { /** 起点坐标 */ readonly start: Vector3Like; /** 终点坐标 */ readonly end: Vector3Like; /** 起点颜色 */ readonly startColor: Color4; /** 终点颜色 */ readonly endColor: Color4; } /** * 创建线段数据。 */ export function createSegment(): Segment { return { start: new Vector3(), end: new Vector3(), startColor: { __type__: 'Color4', r: 1, g: 1, b: 1, a: 1 }, endColor: { __type__: 'Color4', r: 1, g: 1, b: 1, a: 1 }, }; } /** * 线段几何体(纯数据接口)。 * * 通过 {@link segments} 列表声明线段,SegmentGeometryLogic 用 computed 按 segments 懒生成 * positions/colors/indices。 */ export interface SegmentGeometry extends Geometry { readonly __type__: 'SegmentGeometry'; /** 线段列表 */ readonly segments: Segment[]; } /** * SegmentGeometryLogic 逻辑类。 * * 继承 {@link GeometryLogic},用 computed 按 segments 懒生成 * positions/colors/indices。segments 变化时 computed 自动失效重算。 */ export class SegmentGeometryLogic extends GeometryLogic { // 响应式参数(不修改原始数据,缺失字段通过 ?? 提供默认值) readonly #segments = (): Segment[] => reactive(this._data as SegmentGeometry).segments ?? []; readonly #_positions = computed(() => this.#buildPositions()); readonly #_colors = computed(() => this.#buildColors()); readonly #_indicesComputed = computed(() => this.#buildIndices()); readonly #_attrTable: VertexAttributes = { a_position: this.computedAttr(this.#_positions, 'float32x3'), a_color: this.computedAttr(this.#_colors, 'float32x4'), a_uv: { data: new Float32Array(), format: 'float32x2' }, a_normal: { data: new Float32Array(), format: 'float32x3' }, a_tangent: { data: new Float32Array(), format: 'float32x3' }, }; protected constructor(data: SegmentGeometry) { super(data); } /** 内部创建入口(protected constructor 的唯一出口) */ static create(data: SegmentGeometry): SegmentGeometryLogic { return new SegmentGeometryLogic(data); } override get vertices(): VertexAttributes { return this.#_attrTable; } /** indices 由 computed 驱动(覆写基类 getter) */ override get vertexIndices(): number[] { return this.#_indicesComputed.value; } #buildPositions(): Float32Array { const numSegments = Math.max(1, this.#segments().length); const data: number[] = []; for (let i = 0; i < numSegments; i++) { const element = this.#segments()[i]; const start = (element && element.start) || new Vector3(); const end = (element && element.end) || new Vector3(); data.push(start.x, start.y, start.z, end.x, end.y, end.z); } return new Float32Array(data); } #buildColors(): Float32Array { const numSegments = Math.max(1, this.#segments().length); const data: number[] = []; for (let i = 0; i < numSegments; i++) { const element = this.#segments()[i]; const startColor = (element && element.startColor) || new Color4Math(); const endColor = (element && element.endColor) || new Color4Math(); data.push(startColor.r, startColor.g, startColor.b, startColor.a, endColor.r, endColor.g, endColor.b, endColor.a); } return new Float32Array(data); } #buildIndices(): number[] { const numSegments = Math.max(1, this.#segments().length); const indices: number[] = []; for (let i = 0; i < numSegments; i++) { indices.push(i * 2, i * 2 + 1); } return indices; } } registerLogic('SegmentGeometry', SegmentGeometryLogic as unknown as new (data: SegmentGeometry) => SegmentGeometryLogic);