Skip to content

Commit

Permalink
BufferGeometryUtils: Fixes for MikkTSpace tangents. (#23802)
Browse files Browse the repository at this point in the history
- Avoid top-level await.
- Fix error on non-indexed geometry.
  • Loading branch information
donmccurdy authored Mar 31, 2022
1 parent fce7ca7 commit d54586e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
14 changes: 8 additions & 6 deletions docs/examples/en/utils/BufferGeometryUtils.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ <h3>[method:Object computeMorphedAttributes]( [param:Mesh | Line | Points object

</p>

<h3>[method:Object computeTangents]( [param:BufferGeometry geometry] )</h3>
<p>
geometry -- Instance of [page:BufferGeometry].
</p>
<h3>[method:Object computeTangents]( [param:BufferGeometry geometry], [param:Object MikkTSpace], [param:Boolean negateSign] = true )</h3>
<ul>
<li>geometry -- Instance of [page:BufferGeometry].</li>
<li>MikkTSpace -- Instance of <i>examples/jsm/libs/mikktspace.module.js</i>, or <i>mikktspace</i> npm package. Await <i>MikkTSpace.ready</i> before use.
<li>negateSign -- Whether to negate the sign component (.w) of each tangent. Required for normal map conventions in some formats, including glTF.</li>
</ul>

<p>
Computes vertex tangents using the [link:http://www.mikktspace.com/ MikkTSpace] algorithm.
Expand All @@ -110,8 +112,8 @@ <h3>[method:Object computeTangents]( [param:BufferGeometry geometry] )</h3>
</p>

<p>
In comparison to [page:BufferGeometryUtils.computeTangents], [page:BufferGeometry.computeTangents]
(a custom algorithm) generates tangents that probably will not match the tangents
In comparison to this method, [page:BufferGeometry.computeTangents] (a
custom algorithm) generates tangents that probably will not match the tangents
in other software. The custom algorithm is sufficient for general use with a
[page:ShaderMaterial], and may be faster than MikkTSpace.
</p>
Expand Down
14 changes: 8 additions & 6 deletions docs/examples/zh/utils/BufferGeometryUtils.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ <h3>[method:Object computeMorphedAttributes]( [param:Mesh | Line | Points object

</p>

<h3>[method:Object computeTangents]( [param:BufferGeometry geometry] )</h3>
<p>
geometry -- Instance of [page:BufferGeometry].
</p>
<h3>[method:Object computeTangents]( [param:BufferGeometry geometry], [param:Object MikkTSpace], [param:Boolean negateSign] = true )</h3>
<ul>
<li>geometry -- Instance of [page:BufferGeometry].</li>
<li>MikkTSpace -- Instance of <i>examples/jsm/libs/mikktspace.module.js</i>, or <i>mikktspace</i> npm package. Await <i>MikkTSpace.ready</i> before use.
<li>negateSign -- Whether to negate the sign component (.w) of each tangent. Required for normal map conventions in some formats, including glTF.</li>
</ul>

<p>
Computes vertex tangents using the [link:http://www.mikktspace.com/ MikkTSpace] algorithm.
Expand All @@ -105,8 +107,8 @@ <h3>[method:Object computeTangents]( [param:BufferGeometry geometry] )</h3>
</p>

<p>
In comparison to [page:BufferGeometryUtils.computeTangents], [page:BufferGeometry.computeTangents]
(a custom algorithm) generates tangents that probably will not match the tangents
In comparison to this method, [page:BufferGeometry.computeTangents] (a
custom algorithm) generates tangents that probably will not match the tangents
in other software. The custom algorithm is sufficient for general use with a
[page:ShaderMaterial], and may be faster than MikkTSpace.
</p>
Expand Down
11 changes: 9 additions & 2 deletions examples/jsm/libs/mikktspace.module.js

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions examples/jsm/utils/BufferGeometryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import {
TrianglesDrawMode,
Vector3,
} from 'three';
import { generateTangents } from '../libs/mikktspace.module.js';


function computeTangents( geometry, negateSign = true ) {
function computeTangents( geometry, MikkTSpace, negateSign = true ) {

if ( ! MikkTSpace || ! MikkTSpace.isReady ) {

throw new Error( 'BufferGeometryUtils: Initialized MikkTSpace library required.' );

}

function getAttributeArray( attribute ) {

Expand Down Expand Up @@ -55,7 +60,7 @@ function computeTangents( geometry, negateSign = true ) {

// Compute vertex tangents.

const tangents = generateTangents(
const tangents = MikkTSpace.generateTangents(

getAttributeArray( _geometry.attributes.position ),
getAttributeArray( _geometry.attributes.normal ),
Expand All @@ -80,7 +85,13 @@ function computeTangents( geometry, negateSign = true ) {

_geometry.setAttribute( 'tangent', new BufferAttribute( tangents, 4 ) );

return geometry.copy( _geometry );
if ( geometry !== _geometry ) {

geometry.copy( _geometry )

}

return geometry;

}

Expand Down

0 comments on commit d54586e

Please sign in to comment.