Skip to content

Commit

Permalink
fix(utils): toCreasedNormals(): call toNonIndexed() only on index…
Browse files Browse the repository at this point in the history
…ed geometries (#269)
  • Loading branch information
kpvhn authored Jul 4, 2023
1 parent 73b1298 commit 9ecea65
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/utils/BufferGeometryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,26 @@ export function computeMorphedAttributes(object: Mesh | Line | Points): Computed
}
}

// Creates a new, non-indexed geometry with smooth normals everywhere except faces that meet at
// an angle greater than the crease angle.
/**
* Modifies the supplied geometry if it is non-indexed, otherwise creates a new,
* non-indexed geometry. Returns the geometry with smooth normals everywhere except
* faces that meet at an angle greater than the crease angle.
*
* Backwards compatible with code such as @react-three/drei's `<RoundedBox>`
* which uses this method to operate on the original geometry.
*
* As of this writing, BufferGeometry.toNonIndexed() warns if the geometry is
* non-indexed and returns `this`, i.e. the same geometry on which it was called:
* `BufferGeometry is already non-indexed.`
*
* @param geometry
* @param creaseAngle
*/
export function toCreasedNormals(geometry: BufferGeometry, creaseAngle = Math.PI / 3 /* 60 degrees */): BufferGeometry {
const creaseDot = Math.cos(creaseAngle)
const hashMultiplier = (1 + 1e-10) * 1e2

// reusable vertors
// reusable vectors
const verts = [new Vector3(), new Vector3(), new Vector3()]
const tempVec1 = new Vector3()
const tempVec2 = new Vector3()
Expand All @@ -865,7 +878,7 @@ export function toCreasedNormals(geometry: BufferGeometry, creaseAngle = Math.PI
return `${x},${y},${z}`
}

const resultGeometry = geometry.toNonIndexed()
const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry
const posAttr = resultGeometry.attributes.position
const vertexMap: { [key: string]: Vector3[] } = {}

Expand Down

0 comments on commit 9ecea65

Please sign in to comment.