Skip to content

Commit

Permalink
fix: toCreasedNormals(): call toNonIndexed() only on non-indexed geom…
Browse files Browse the repository at this point in the history
…etries (#26379)

* BufferGeometryUtils.toCreasedNormals(): call `toNonIndexed()` only on indexed geometries

* update BufferGeometry.toCreasedNormals() documentation

* BufferGeometry.toCreasedNormals() documentation: use more common HTML formatting & remove redundant info

* BufferGeometry.toCreasedNormals() documentation: remove redundant compatibility note

* BufferGeometryUtils.toCreasedNormals(): explain the indexed geometry check in code, not JSDoc

* BufferGeometryUtils.toCreasedNormals(): fix typo: vertors => vectors
  • Loading branch information
kpvhn committed Jul 17, 2023
1 parent 9e580ac commit 1afa3ad
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
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
17 changes: 13 additions & 4 deletions examples/jsm/utils/BufferGeometryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1229,14 +1229,21 @@ 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.
*
* @param {BufferGeometry} geometry
* @param {number} [creaseAngle]
* @return {BufferGeometry}
*/
function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */ ) {

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 @@ -1253,7 +1260,9 @@ function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */

}

const resultGeometry = geometry.toNonIndexed();
// BufferGeometry.toNonIndexed() warns if the geometry is non-indexed
// and returns the original geometry
const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry;
const posAttr = resultGeometry.attributes.position;
const vertexMap = {};

Expand Down

0 comments on commit 1afa3ad

Please sign in to comment.