From fc4389a371f9c930e21994569e975d52663f703c Mon Sep 17 00:00:00 2001 From: Paras Sanghavi Date: Sun, 21 Mar 2021 10:42:14 -0700 Subject: [PATCH] fix: move LineGeometry methods from instance to prototype methods (#42) Co-authored-by: Josh Ellis --- src/lines/LineGeometry.ts | 28 +++------------------------- src/lines/LineSegmentsGeometry.ts | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 35 deletions(-) diff --git a/src/lines/LineGeometry.ts b/src/lines/LineGeometry.ts index d1ad2639..ce558487 100644 --- a/src/lines/LineGeometry.ts +++ b/src/lines/LineGeometry.ts @@ -9,7 +9,7 @@ class LineGeometry extends LineSegmentsGeometry { super() } - public setPositions = (array: number[] | Float32Array): this => { + public setPositions(array: number[] | Float32Array): this { // converts [ x1, y1, z1, x2, y2, z2, ... ] to pairs format const length = array.length - 3 @@ -25,26 +25,7 @@ class LineGeometry extends LineSegmentsGeometry { points[2 * i + 5] = array[i + 5] } - let lineSegments - - if (array instanceof Float32Array) { - lineSegments = array - } else if (Array.isArray(array)) { - lineSegments = new Float32Array(array) - } else { - console.error('LineSegmentsGeometry.setPosition requires either a Float32Array or regular array of numbers') - return this - } - - const instanceBuffer = new InstancedInterleavedBuffer(lineSegments, 6, 1) // xyz, xyz - - this.setAttribute('instanceStart', new InterleavedBufferAttribute(instanceBuffer, 3, 0)) // xyz - this.setAttribute('instanceEnd', new InterleavedBufferAttribute(instanceBuffer, 3, 3)) // xyz - - // - - this.computeBoundingBox() - this.computeBoundingSphere() + super.setPositions(points) return this } @@ -65,10 +46,7 @@ class LineGeometry extends LineSegmentsGeometry { colors[2 * i + 5] = array[i + 5] } - const instanceColorBuffer = new InstancedInterleavedBuffer(colors, 6, 1) // rgb, rgb - - this.setAttribute('instanceColorStart', new InterleavedBufferAttribute(instanceColorBuffer, 3, 0)) // rgb - this.setAttribute('instanceColorEnd', new InterleavedBufferAttribute(instanceColorBuffer, 3, 3)) // rgb + super.setColors(colors) return this } diff --git a/src/lines/LineSegmentsGeometry.ts b/src/lines/LineSegmentsGeometry.ts index 3bea3b2c..a9965d94 100644 --- a/src/lines/LineSegmentsGeometry.ts +++ b/src/lines/LineSegmentsGeometry.ts @@ -33,7 +33,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { this.setAttribute('uv', new Float32BufferAttribute(uvs, 2)) } - public applyMatrix4 = (matrix: Matrix4): this => { + public applyMatrix4(matrix: Matrix4): this { const start = this.attributes.instanceStart const end = this.attributes.instanceEnd @@ -56,7 +56,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { return this } - public setPositions = (array: number[] | Float32Array): this => { + public setPositions(array: number[] | Float32Array): this { let lineSegments if (array instanceof Float32Array) { @@ -81,7 +81,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { return this } - public setColors = (array: number[] | Float32Array): this => { + public setColors(array: number[] | Float32Array): this { let colors if (array instanceof Float32Array) { @@ -101,25 +101,25 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { return this } - public fromWireframeGeometry = (geometry: BufferGeometry): this => { + public fromWireframeGeometry(geometry: BufferGeometry): this { this.setPositions(Array.from(geometry.attributes.position.array)) return this } - public fromEdgesGeometry = (geometry: BufferGeometry): this => { + public fromEdgesGeometry(geometry: BufferGeometry): this { this.setPositions(Array.from(geometry.attributes.position.array)) return this } - public fromMesh = (mesh: Mesh): this => { + public fromMesh(mesh: Mesh): this { this.fromWireframeGeometry(new WireframeGeometry(mesh.geometry)) return this } - public fromLineSegments = (lineSegments: LineSegments): this => { + public fromLineSegments(lineSegments: LineSegments): this { const geometry = lineSegments.geometry if (geometry.isBufferGeometry) { @@ -133,7 +133,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { private box = new Box3() - public computeBoundingBox = (): void => { + public computeBoundingBox(): void { if (this.boundingBox === null) { this.boundingBox = new Box3() } @@ -152,7 +152,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { private vector = new Vector3() - public computeBoundingSphere = (): void => { + public computeBoundingSphere(): void { if (this.boundingSphere === null) { this.boundingSphere = new Sphere() } @@ -192,7 +192,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry { } } - public toJSON = (): void => { + public toJSON(): void { // todo } }