From 1e32ffde768bd79513cce5edd6968b715888aafd Mon Sep 17 00:00:00 2001 From: Guillaume Fradin Date: Wed, 13 Sep 2023 00:03:33 +0100 Subject: [PATCH] refactor quadpoint --- src/core/geometry/entities/point/CorePoint.ts | 2 +- src/core/geometry/modules/quad/QuadPoint.ts | 38 +++++++++++++++---- .../modules/quad/QuadPointAttribute.ts | 8 +--- .../modules/three/CoreThreejsPoint.ts | 2 +- src/core/wfc/WFCSolver.ts | 2 +- src/engine/nodes/sop/QuadPlane.ts | 7 ++-- src/engine/nodes/sop/QuadSmooth.ts | 2 +- src/engine/nodes/sop/Quadrangulate.ts | 12 ++++-- 8 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/core/geometry/entities/point/CorePoint.ts b/src/core/geometry/entities/point/CorePoint.ts index b885c099b..0a029a710 100644 --- a/src/core/geometry/entities/point/CorePoint.ts +++ b/src/core/geometry/entities/point/CorePoint.ts @@ -35,7 +35,7 @@ export abstract class TypedCorePoint extends CoreEntit console.warn('CorePoint.addAttribute needs to be overloaded'); } - static pointsCount(object: ObjectContent) { + static pointsCount(object: ObjectContent): number { return 0; } diff --git a/src/core/geometry/modules/quad/QuadPoint.ts b/src/core/geometry/modules/quad/QuadPoint.ts index 64cd6bded..be6623bf4 100644 --- a/src/core/geometry/modules/quad/QuadPoint.ts +++ b/src/core/geometry/modules/quad/QuadPoint.ts @@ -3,13 +3,14 @@ import {ObjectGeometryMap, CoreObjectType, ObjectContent} from '../../ObjectCont import {TypedCorePoint} from '../../entities/point/CorePoint'; import {PointAttributesDict} from '../../entities/point/Common'; import {QuadObject} from './QuadObject'; +import {Attribute} from '../../Attribute'; export class QuadPoint extends TypedCorePoint { protected _geometry?: ObjectGeometryMap[CoreObjectType.QUAD]; - protected override _object: QuadObject + protected override _object: QuadObject; constructor(object: QuadObject, index: number) { super(object, index); - this._object = object + this._object = object; this._updateGeometry(); } override object() { @@ -38,16 +39,39 @@ export class QuadPoint extends TypedCorePoint { attribute: BufferAttribute ) {} static override attributes(object: ObjectContent): PointAttributesDict | undefined { - return undefined; + const geometry = (object as any as QuadObject).geometry; + if (!geometry) { + return; + } + return geometry.attributes; } - static override pointsCount(object: ObjectContent) { - return 0; + static override pointsCount(object: ObjectContent): number { + const positionAttribute = this.attribute(object, Attribute.POSITION); + if (!positionAttribute) { + return 0; + } + return positionAttribute.count; } override position(target: Vector3) { - return target; + if (!this._geometry) { + return target; + } + const {array} = this.attribute(Attribute.POSITION) as BufferAttribute; + return target.fromArray(array, this._index * 3); } override normal(target: Vector3) { - return target; + if (!this._geometry) { + return target; + } + const {array} = this.attribute(Attribute.NORMAL) as BufferAttribute; + return target.fromArray(array, this._index * 3); + } + static override computeNormals(object: ObjectContent) { + // const geometry = (object as QuadObject).geometry as BufferGeometry | undefined; + // if (!geometry) { + // return null; + // } + // geometry.computeVertexNormals(); } // diff --git a/src/core/geometry/modules/quad/QuadPointAttribute.ts b/src/core/geometry/modules/quad/QuadPointAttribute.ts index bf663f1df..d9b646a6a 100644 --- a/src/core/geometry/modules/quad/QuadPointAttribute.ts +++ b/src/core/geometry/modules/quad/QuadPointAttribute.ts @@ -1,7 +1,3 @@ -export class QuadPointAttribute { - constructor(public array: number[], public itemSize: number) {} +import {BufferAttribute} from 'three'; - clone() { - return new (this.constructor as typeof QuadPointAttribute)([...this.array], this.itemSize); - } -} +export type QuadPointAttribute = BufferAttribute; diff --git a/src/core/geometry/modules/three/CoreThreejsPoint.ts b/src/core/geometry/modules/three/CoreThreejsPoint.ts index 643885582..410b4d55e 100644 --- a/src/core/geometry/modules/three/CoreThreejsPoint.ts +++ b/src/core/geometry/modules/three/CoreThreejsPoint.ts @@ -60,7 +60,7 @@ export class CoreThreejsPoint extends TypedCorePoint { } return geometry.attributes; } - static override pointsCount(object: ObjectContent) { + static override pointsCount(object: ObjectContent): number { const geometry = (object as any as Mesh).geometry as BufferGeometry | undefined; if (!geometry) { return 0; diff --git a/src/core/wfc/WFCSolver.ts b/src/core/wfc/WFCSolver.ts index d2714c66a..65252d046 100644 --- a/src/core/wfc/WFCSolver.ts +++ b/src/core/wfc/WFCSolver.ts @@ -63,7 +63,7 @@ export class WFCSolver { } } - this._quadPositionArray = this.quadObject.geometry.attributes[Attribute.POSITION].array; + this._quadPositionArray = this.quadObject.geometry.attributes[Attribute.POSITION].array as number[]; const quadsCount = this.quadObject.geometry.quadsCount(); const quadPrimitive = new QuadPrimitive(this.quadObject, 0); for (let i = 0; i < quadsCount; i++) { diff --git a/src/engine/nodes/sop/QuadPlane.ts b/src/engine/nodes/sop/QuadPlane.ts index e50ba4418..ae699a1d7 100644 --- a/src/engine/nodes/sop/QuadPlane.ts +++ b/src/engine/nodes/sop/QuadPlane.ts @@ -4,12 +4,11 @@ * */ import {QuadSopNode} from './_BaseQuad'; -import {Vector3, Matrix4} from 'three'; +import {Vector3, Matrix4, BufferAttribute} from 'three'; import {NodeParamsConfig, ParamConfig} from '../utils/params/ParamsConfig'; import {CoreGroup} from '../../../core/geometry/Group'; import {SopType} from '../../poly/registers/nodes/types/Sop'; import {QuadGeometry} from '../../../core/geometry/modules/quad/QuadGeometry'; -import {QuadPointAttribute} from '../../../core/geometry/modules/quad/QuadPointAttribute'; import {Attribute} from '../../../core/geometry/Attribute'; import {rotationMatrix} from '../../../core/Transform'; @@ -77,7 +76,7 @@ export class QuadPlaneSopNode extends QuadSopNode { const pointsCountY = quadsCountY + 1; const quadsCount = quadsCountX * quadsCountY; const positionsCount = pointsCountX * pointsCountY; - const positions = new Array(positionsCount * 3); + const positions = new Float32Array(positionsCount * 3); const indices = new Array(4); for (let x = 0; x < pointsCountX; x++) { @@ -101,7 +100,7 @@ export class QuadPlaneSopNode extends QuadSopNode { indices[quadIndex + 3] = i2; } - const position = new QuadPointAttribute(positions, 3); + const position = new BufferAttribute(positions, 3); geometry.addPointAttribute(Attribute.POSITION, position); geometry.setIndex(indices); diff --git a/src/engine/nodes/sop/QuadSmooth.ts b/src/engine/nodes/sop/QuadSmooth.ts index e64be71f9..1f6c92c7a 100644 --- a/src/engine/nodes/sop/QuadSmooth.ts +++ b/src/engine/nodes/sop/QuadSmooth.ts @@ -106,7 +106,7 @@ export class QuadSmoothSopNode extends QuadSopNode { if (!position) { return; } - const tmpPositionArray0 = [...position.array]; + const tmpPositionArray0 = [...(position.array as number[])]; const tmpPositionArray1 = [...tmpPositionArray0]; const index = geometry.index; diff --git a/src/engine/nodes/sop/Quadrangulate.ts b/src/engine/nodes/sop/Quadrangulate.ts index c042e6864..2d3eacb6c 100644 --- a/src/engine/nodes/sop/Quadrangulate.ts +++ b/src/engine/nodes/sop/Quadrangulate.ts @@ -8,8 +8,7 @@ import {NodeParamsConfig, ParamConfig} from '../utils/params/ParamsConfig'; import {CoreGroup} from '../../../core/geometry/Group'; import {SopType} from '../../poly/registers/nodes/types/Sop'; import {QuadGeometry} from '../../../core/geometry/modules/quad/QuadGeometry'; -import {Vector3, BufferGeometry, Mesh} from 'three'; -import {QuadPointAttribute} from '../../../core/geometry/modules/quad/QuadPointAttribute'; +import {Vector3, BufferGeometry, BufferAttribute, Mesh} from 'three'; import {Attribute} from '../../../core/geometry/Attribute'; import {InputCloneMode} from '../../poly/InputCloneMode'; import {QuadObject} from '../../../core/geometry/modules/quad/QuadObject'; @@ -88,14 +87,16 @@ export class QuadrangulateSopNode extends QuadSopNode 0 /*&& quadsCount < expectedQuadsCount*/) { i++; @@ -196,6 +199,7 @@ export class QuadrangulateSopNode extends QuadSopNode