From ccbb6f33911d2f9bfe6626843f04053b0ddf62d3 Mon Sep 17 00:00:00 2001 From: kpvhn <129968478+kpvhn@users.noreply.github.com> Date: Wed, 5 Jul 2023 10:38:09 +0200 Subject: [PATCH] BufferGeometryUtils.toCreasedNormals(): call `toNonIndexed()` only on indexed geometries --- examples/jsm/utils/BufferGeometryUtils.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/jsm/utils/BufferGeometryUtils.js b/examples/jsm/utils/BufferGeometryUtils.js index ff86be0b7a8de9..7c82aeb37a6982 100644 --- a/examples/jsm/utils/BufferGeometryUtils.js +++ b/examples/jsm/utils/BufferGeometryUtils.js @@ -1229,8 +1229,22 @@ function mergeGroups( geometry ) { } -// 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 `` + * 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 {BufferGeometry} geometry + * @param {number} [creaseAngle] + * @return {BufferGeometry} + */ function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */ ) { const creaseDot = Math.cos( creaseAngle ); @@ -1253,7 +1267,7 @@ function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */ } - const resultGeometry = geometry.toNonIndexed(); + const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry; const posAttr = resultGeometry.attributes.position; const vertexMap = {};