import { Geometry, GeometryLogic } from '../geometry/Geometry'; import { registerLogic, reactive, computed } from '@feng3d/reactivity'; import { VertexAttributes } from '@feng3d/webgpu'; declare module '@feng3d/reactivity' { interface LogicMap { CylinderGeometry: CylinderGeometryLogic; } } declare module '../geometry/Geometry' { export interface GeometryMap { CylinderGeometry: CylinderGeometry; } } /** * 圆柱体几何体(纯数据接口)。 */ export interface CylinderGeometry extends Geometry { readonly __type__: 'CylinderGeometry'; /** 顶部半径(缺失时由工厂填充默认值) */ readonly topRadius?: number; /** 底部半径(缺失时由工厂填充默认值) */ readonly bottomRadius?: number; /** 高度(缺失时由工厂填充默认值) */ readonly height?: number; /** 横向分割数(缺失时由工厂填充默认值) */ readonly segmentsW?: number; /** 纵向分割数(缺失时由工厂填充默认值) */ readonly segmentsH?: number; /** 顶部是否封口(缺失时由工厂填充默认值) */ readonly topClosed?: boolean; /** 底部是否封口(缺失时由工厂填充默认值) */ readonly bottomClosed?: boolean; /** 侧面是否封口(缺失时由工厂填充默认值) */ readonly surfaceClosed?: boolean; /** 是否朝上(缺失时由工厂填充默认值) */ readonly yUp?: boolean; } // CylinderGeometry 默认值由 CylinderGeometryLogic 内参数访问器处理(见下) /** * CylinderGeometryLogic 逻辑类(ConeGeometry 亦复用本类)。 * * 继承 {@link GeometryLogic},每个顶点属性用 computed 独立懒计算, * 依赖 topRadius/bottomRadius/height/segmentsW/segmentsH/topClosed/bottomClosed/surfaceClosed/yUp。 * 不使用 effect/invalidateGeometry — 参数变化时 computed 自动失效重算。 */ export class CylinderGeometryLogic extends GeometryLogic { // 响应式参数(不修改原始数据,缺失字段通过 ?? 提供默认值;ConeGeometry 按 __type__ 区分) readonly #isCone = (this._data as { __type__: string }).__type__ === 'ConeGeometry'; readonly #topRadius = (): number => reactive(this._data as CylinderGeometry).topRadius ?? (this.#isCone ? 0 : 0.5); readonly #bottomRadius = (): number => reactive(this._data as CylinderGeometry).bottomRadius ?? 0.5; readonly #height = (): number => reactive(this._data as CylinderGeometry).height ?? 2; readonly #segmentsW = (): number => reactive(this._data as CylinderGeometry).segmentsW ?? 16; readonly #segmentsH = (): number => reactive(this._data as CylinderGeometry).segmentsH ?? 1; readonly #topClosed = (): boolean => reactive(this._data as CylinderGeometry).topClosed ?? !this.#isCone; readonly #bottomClosed = (): boolean => reactive(this._data as CylinderGeometry).bottomClosed ?? true; readonly #surfaceClosed = (): boolean => reactive(this._data as CylinderGeometry).surfaceClosed ?? true; readonly #yUp = (): boolean => reactive(this._data as CylinderGeometry).yUp ?? true; // 每个属性独立 computed,仅在实际被读取时计算 readonly #_positions = computed(() => this.#buildPositions()); readonly #_normals = computed(() => this.#buildNormals()); readonly #_tangents = computed(() => this.#buildTangents()); readonly #_uvs = computed(() => this.#buildUVs()); readonly #_colors = computed(() => { const pos = this.#_positions.value; if (pos.length === 0) return new Float32Array(0); const count = pos.length / 3; return new Float32Array(count * 4).fill(1); }); readonly #_indicesComputed = computed(() => this.#buildIndices()); // attributes: data 由 computed getter 驱动 readonly #_attrTable: VertexAttributes = { a_position: this.computedAttr(this.#_positions, 'float32x3'), a_color: this.computedAttr(this.#_colors, 'float32x4'), a_uv: this.computedAttr(this.#_uvs, 'float32x2'), a_normal: this.computedAttr(this.#_normals, 'float32x3'), a_tangent: this.computedAttr(this.#_tangents, 'float32x3'), }; protected constructor(data: CylinderGeometry) { super(data); } /** 内部创建入口(protected constructor 的唯一出口) */ static create(data: CylinderGeometry): CylinderGeometryLogic { return new CylinderGeometryLogic(data); } override get vertices(): VertexAttributes { return this.#_attrTable; } /** indices 由 computed 驱动(覆写基类 getter) */ override get vertexIndices(): number[] { return this.#_indicesComputed.value; } // ---- 顶点构建(直接返回 Float32Array,内部 reactive 建立依赖) ---- // 三个方法独立运行同样的迭代结构,各自只填充一种属性;index/startIndex 计数必须保持一致。 #buildPositions(): Float32Array { let i: number; let j: number; let index = 0; let x: number; let y: number; let z: number; let radius: number; let revolutionAngle = 0; let comp1: number; let comp2: number; let startIndex = 0; const data: number[] = []; const revolutionAngleDelta = 2 * Math.PI / this.#segmentsW(); const addVertex = (px: number, py: number, pz: number) => { data[index] = px; data[index + 1] = py; data[index + 2] = pz; index += 3; }; // 顶部 if (this.#topClosed() && this.#topRadius() > 0) { z = -0.5 * this.#height(); for (i = 0; i <= this.#segmentsW(); ++i) { comp1 = this.#yUp() ? -z : 0; comp2 = this.#yUp() ? 0 : z; addVertex(0, comp1, comp2); revolutionAngle = i * revolutionAngleDelta; x = this.#topRadius() * Math.cos(revolutionAngle); y = this.#topRadius() * Math.sin(revolutionAngle); if (this.#yUp()) { comp1 = -z; comp2 = y; } else { comp1 = y; comp2 = z; } if (i === this.#segmentsW()) { addVertex(data[startIndex + 3], data[startIndex + 4], data[startIndex + 5]); } else { addVertex(x, comp1, comp2); } } } // 底部 if (this.#bottomClosed() && this.#bottomRadius() > 0) { z = 0.5 * this.#height(); startIndex = index; for (i = 0; i <= this.#segmentsW(); ++i) { comp1 = this.#yUp() ? -z : 0; comp2 = this.#yUp() ? 0 : z; addVertex(0, comp1, comp2); revolutionAngle = i * revolutionAngleDelta; x = this.#bottomRadius() * Math.cos(revolutionAngle); y = this.#bottomRadius() * Math.sin(revolutionAngle); if (this.#yUp()) { comp1 = -z; comp2 = y; } else { comp1 = y; comp2 = z; } if (i === this.#segmentsW()) { addVertex(x, data[startIndex + 1], data[startIndex + 2]); } else { addVertex(x, comp1, comp2); } } } // 侧面 if (this.#surfaceClosed()) { for (j = 0; j <= this.#segmentsH(); ++j) { radius = this.#topRadius() - ((j / this.#segmentsH()) * (this.#topRadius() - this.#bottomRadius())); z = -(this.#height() / 2) + (j / this.#segmentsH() * this.#height()); startIndex = index; for (i = 0; i <= this.#segmentsW(); ++i) { revolutionAngle = i * revolutionAngleDelta; x = radius * Math.cos(revolutionAngle); y = radius * Math.sin(revolutionAngle); if (this.#yUp()) { comp1 = -z; comp2 = y; } else { comp1 = y; comp2 = z; } if (i === this.#segmentsW()) { addVertex(data[startIndex], data[startIndex + 1], data[startIndex + 2]); } else { addVertex(x, comp1, comp2); } } } } return new Float32Array(data); } #buildNormals(): Float32Array { let i: number; let j: number; let index = 0; let revolutionAngle = 0; let t1: number; let t2: number; const data: number[] = []; const revolutionAngleDelta = 2 * Math.PI / this.#segmentsW(); const dr = this.#bottomRadius() - this.#topRadius(); const latNormElev = dr / this.#height(); const latNormBase = (latNormElev === 0) ? 1 : this.#height() / dr; const addVertex = (nx: number, ny: number, nz: number) => { data[index] = nx; data[index + 1] = ny; data[index + 2] = nz; index += 3; }; // 顶部 if (this.#topClosed() && this.#topRadius() > 0) { for (i = 0; i <= this.#segmentsW(); ++i) { if (this.#yUp()) { t1 = 1; t2 = 0; } else { t1 = 0; t2 = -1; } addVertex(0, t1, t2); if (i === this.#segmentsW()) { addVertex(0, t1, t2); } else { addVertex(0, t1, t2); } } } // 底部 if (this.#bottomClosed() && this.#bottomRadius() > 0) { for (i = 0; i <= this.#segmentsW(); ++i) { if (this.#yUp()) { t1 = -1; t2 = 0; } else { t1 = 0; t2 = 1; } addVertex(0, t1, t2); if (i === this.#segmentsW()) { addVertex(0, t1, t2); } else { addVertex(0, t1, t2); } } } // 侧面 if (this.#surfaceClosed()) { let na0: number; let na1: number; let naComp1: number; let naComp2: number; for (j = 0; j <= this.#segmentsH(); ++j) { for (i = 0; i <= this.#segmentsW(); ++i) { revolutionAngle = i * revolutionAngleDelta; na0 = latNormBase * Math.cos(revolutionAngle); na1 = latNormBase * Math.sin(revolutionAngle); if (this.#yUp()) { naComp1 = latNormElev; naComp2 = na1; } else { naComp1 = na1; naComp2 = latNormElev; } if (i === this.#segmentsW()) { addVertex(na0, latNormElev, na1); } else { addVertex(na0, naComp1, naComp2); } } } } return new Float32Array(data); } #buildTangents(): Float32Array { let i: number; let j: number; let index = 0; let revolutionAngle = 0; let t1: number; let t2: number; const data: number[] = []; const revolutionAngleDelta = 2 * Math.PI / this.#segmentsW(); const dr = this.#bottomRadius() - this.#topRadius(); const latNormElev = dr / this.#height(); const latNormBase = (latNormElev === 0) ? 1 : this.#height() / dr; const addVertex = (tx: number, ty: number, tz: number) => { data[index] = tx; data[index + 1] = ty; data[index + 2] = tz; index += 3; }; // 顶部 if (this.#topClosed() && this.#topRadius() > 0) { for (i = 0; i <= this.#segmentsW(); ++i) { addVertex(1, 0, 0); if (i === this.#segmentsW()) { addVertex(1, 0, 0); } else { addVertex(1, 0, 0); } } } // 底部 if (this.#bottomClosed() && this.#bottomRadius() > 0) { for (i = 0; i <= this.#segmentsW(); ++i) { addVertex(1, 0, 0); if (i === this.#segmentsW()) { addVertex(1, 0, 0); } else { addVertex(1, 0, 0); } } } // 侧面 if (this.#surfaceClosed()) { let na0: number; let na1: number; for (j = 0; j <= this.#segmentsH(); ++j) { for (i = 0; i <= this.#segmentsW(); ++i) { revolutionAngle = i * revolutionAngleDelta; na0 = latNormBase * Math.cos(revolutionAngle); na1 = latNormBase * Math.sin(revolutionAngle); if (this.#yUp()) { t1 = 0; t2 = -na0; } else { t1 = -na0; t2 = 0; } if (i === this.#segmentsW()) { addVertex(na1, t1, t2); } else { addVertex(-na1, t1, t2); } } } } return new Float32Array(data); } #buildUVs(): Float32Array { let i: number; let j: number; let x: number; let y: number; let revolutionAngle: number; const data: number[] = []; const revolutionAngleDelta = 2 * Math.PI / this.#segmentsW(); let index = 0; if (this.#topClosed()) { for (i = 0; i <= this.#segmentsW(); ++i) { revolutionAngle = i * revolutionAngleDelta; x = 0.5 + 0.5 * -Math.cos(revolutionAngle); y = 0.5 + 0.5 * Math.sin(revolutionAngle); data[index++] = 0.5; data[index++] = 0.5; data[index++] = x; data[index++] = y; } } if (this.#bottomClosed()) { for (i = 0; i <= this.#segmentsW(); ++i) { revolutionAngle = i * revolutionAngleDelta; x = 0.5 + 0.5 * Math.cos(revolutionAngle); y = 0.5 + 0.5 * Math.sin(revolutionAngle); data[index++] = 0.5; data[index++] = 0.5; data[index++] = x; data[index++] = y; } } if (this.#surfaceClosed()) { for (j = 0; j <= this.#segmentsH(); ++j) for (i = 0; i <= this.#segmentsW(); ++i) { data[index++] = (i / this.#segmentsW()); data[index++] = (j / this.#segmentsH()); } } return new Float32Array(data); } #buildIndices(): number[] { let i: number; let j: number; let index = 0; const indices: number[] = []; let n = 0; // 环绕方向:顶点法线朝外,三角形须按 (0,1,2) 顺序写入才是从外侧观察的逆时针。 // // 旧实现交换了后两个顶点(0,2,1),实测与顶点法线相反:面法线·质心方向为负, // 说明绕序反了(正面朝内、被 `cullFace: 'back'` 剔除)。ConeGeometry 复用本 Logic, // 因此圆锥箭头同样受影响。 const addTriangle = (vertexIndex0: number, vertexIndex1: number, vertexIndex2: number) => { indices[n++] = vertexIndex0; indices[n++] = vertexIndex1; indices[n++] = vertexIndex2; }; if (this.#topClosed() && this.#topRadius() > 0) { for (i = 0; i <= this.#segmentsW(); ++i) { index += 2; if (i > 0) addTriangle(index - 1, index - 3, index - 2); } } if (this.#bottomClosed() && this.#bottomRadius() > 0) { for (i = 0; i <= this.#segmentsW(); ++i) { index += 2; if (i > 0) addTriangle(index - 2, index - 3, index - 1); } } if (this.#surfaceClosed()) { let a: number; let b: number; let c: number; let d: number; for (j = 0; j <= this.#segmentsH(); ++j) for (i = 0; i <= this.#segmentsW(); ++i) { index++; if (i > 0 && j > 0) { a = index - 1; b = index - 2; c = b - this.#segmentsW() - 1; d = a - this.#segmentsW() - 1; addTriangle(a, b, c); addTriangle(a, c, d); } } } return indices; } } registerLogic('CylinderGeometry', CylinderGeometryLogic as unknown as new (data: CylinderGeometry) => CylinderGeometryLogic);