Skip to content

Commit

Permalink
fix: move LineGeometry methods from instance to prototype methods (#42)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Ellis <joshua.ellis18@gmail.com>
  • Loading branch information
disambiguator and joshuaellis authored Mar 21, 2021
1 parent 5f0afe8 commit fc4389a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
28 changes: 3 additions & 25 deletions src/lines/LineGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
20 changes: 10 additions & 10 deletions src/lines/LineSegmentsGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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()
}
Expand All @@ -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()
}
Expand Down Expand Up @@ -192,7 +192,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry {
}
}

public toJSON = (): void => {
public toJSON(): void {
// todo
}
}
Expand Down

0 comments on commit fc4389a

Please sign in to comment.