Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: toCreasedNormals(): call toNonIndexed() only on non-indexed geometries #26379

Merged
merged 6 commits into from
Jul 17, 2023
14 changes: 11 additions & 3 deletions docs/examples/en/utils/BufferGeometryUtils.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,19 @@ <h3>[method:BufferGeometry mergeVertices]( [param:BufferGeometry geometry], [par
</p>

<h3>[method:BufferGeometry toCreasedNormals]( [param:BufferGeometry geometry], [param:Number creaseAngle] )</h3>
<ul>
<li>geometry -- The input geometry.</li>
<li>creaseAngle -- The crease angle.</li>
</ul>

<p>
geometry -- The input geometry. <br />
creaseAngle -- The crease angle. <br /><br />
Modifies the supplied geometry if it is non-indexed, otherwise creates a new,
non-indexed geometry.
</p>

Creates a new, non-indexed geometry with smooth normals everywhere except faces that meet at an angle greater than the crease angle.
<p>
Returns the geometry with smooth normals everywhere except faces
that meet at an angle greater than the crease angle.
</p>

<h3>[method:BufferGeometry toTrianglesDrawMode]( [param:BufferGeometry geometry], [param:TrianglesDrawMode drawMode] )</h3>
Expand Down
13 changes: 10 additions & 3 deletions docs/examples/zh/utils/BufferGeometryUtils.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,19 @@ <h3>[method:BufferGeometry mergeVertices]( [param:BufferGeometry geometry], [par
</p>

<h3>[method:BufferGeometry toCreasedNormals]( [param:BufferGeometry geometry], [param:Number creaseAngle] )</h3>
<ul>
<li>geometry -- The input geometry.</li>
<li>creaseAngle -- The crease angle.</li>
</ul>

<p>
geometry -- The input geometry. <br />
creaseAngle -- The crease angle.
Modifies the supplied geometry if it is non-indexed, otherwise creates a new,
non-indexed geometry.
</p>

<p>
Creates a new, non-indexed geometry with smooth normals everywhere except faces that meet at an angle greater than the crease angle.
Returns the geometry with smooth normals everywhere except faces
that meet at an angle greater than the crease angle.
</p>

<h3>[method:BufferGeometry toTrianglesDrawMode]( [param:BufferGeometry geometry], [param:TrianglesDrawMode drawMode] )</h3>
Expand Down
20 changes: 17 additions & 3 deletions examples/jsm/utils/BufferGeometryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<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 {BufferGeometry} geometry
* @param {number} [creaseAngle]
* @return {BufferGeometry}
kpvhn marked this conversation as resolved.
Show resolved Hide resolved
*/
function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */ ) {

const creaseDot = Math.cos( creaseAngle );
Expand All @@ -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 = {};

Expand Down