瀏覽代碼

BufferGeometryUtils: Fixes for MikkTSpace tangents. (#23802)

- Avoid top-level await.
- Fix error on non-indexed geometry.
Don McCurdy 3 年之前
父節點
當前提交
86171331ec

+ 8 - 6
docs/examples/en/utils/BufferGeometryUtils.html

@@ -96,10 +96,12 @@
 
 		</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.
@@ -110,8 +112,8 @@
 		</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>

+ 8 - 6
docs/examples/zh/utils/BufferGeometryUtils.html

@@ -91,10 +91,12 @@
 
 		</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.
@@ -105,8 +107,8 @@
 		</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>

File diff suppressed because it is too large
+ 0 - 0
examples/jsm/libs/mikktspace.module.js


+ 15 - 4
examples/jsm/utils/BufferGeometryUtils.js

@@ -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 ) {
 
@@ -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 ),
@@ -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;
 
 }
 

Some files were not shown because too many files changed in this diff