Browse Source

Triangle.getInterpolation: Return null if triangle is degenerate (#27331)

* Return null if triangle is degenerate

* Update docs

* Ensure we support for vector4 and vector2

* Update Triangle.html

---------

Co-authored-by: Michael Herzog <[email protected]>
Garrett Johnson 1 year ago
parent
commit
23d8557c70
2 changed files with 11 additions and 3 deletions
  1. 2 2
      docs/api/en/math/Triangle.html
  2. 9 1
      src/math/Triangle.js

+ 2 - 2
docs/api/en/math/Triangle.html

@@ -97,7 +97,7 @@
 			[page:Vector3 target] — the result will be copied into this Vector3.<br /><br />
 			[page:Vector3 target] — the result will be copied into this Vector3.<br /><br />
 
 
 			Return a [link:https://en.wikipedia.org/wiki/Barycentric_coordinate_system barycentric coordinate] 
 			Return a [link:https://en.wikipedia.org/wiki/Barycentric_coordinate_system barycentric coordinate] 
-			from the given vector. Returns null if the triangle is degenerate.<br /><br />
+			from the given vector. Returns `null` if the triangle is degenerate.<br /><br />
 
 
 			[link:http://commons.wikimedia.org/wiki/File:Barycentric_coordinates_1.png Picture of barycentric coordinates]
 			[link:http://commons.wikimedia.org/wiki/File:Barycentric_coordinates_1.png Picture of barycentric coordinates]
 		</p>
 		</p>
@@ -137,7 +137,7 @@
 			[page:Vector target] — Result will be copied into this Vector.<br /><br />
 			[page:Vector target] — Result will be copied into this Vector.<br /><br />
 
 
 			Returns the value barycentrically interpolated for the given point on the
 			Returns the value barycentrically interpolated for the given point on the
-			triangle.
+			triangle. Returns `null` if the triangle is degenerate.
 		</p>
 		</p>
 
 
 		<h3>[method:Boolean intersectsBox]( [param:Box3 box] )</h3>
 		<h3>[method:Boolean intersectsBox]( [param:Box3 box] )</h3>

+ 9 - 1
src/math/Triangle.js

@@ -103,7 +103,15 @@ class Triangle {
 
 
 	static getInterpolation( point, p1, p2, p3, v1, v2, v3, target ) {
 	static getInterpolation( point, p1, p2, p3, v1, v2, v3, target ) {
 
 
-		this.getBarycoord( point, p1, p2, p3, _v3 );
+		if ( this.getBarycoord( point, p1, p2, p3, _v3 ) === null ) {
+
+			target.x = 0;
+			target.y = 0;
+			if ( 'z' in target ) target.z = 0;
+			if ( 'w' in target ) target.w = 0;
+			return null;
+
+		}
 
 
 		target.setScalar( 0 );
 		target.setScalar( 0 );
 		target.addScaledVector( v1, _v3.x );
 		target.addScaledVector( v1, _v3.x );