Repository: mrdoob/three.js (JavaScript)
Date: 2021-08-24 10:41:08+00:00
Task type: Feature

Issue:
Triangle.setFromPointsAndIndices buffer attribute version?
I just noticed that while geometry.vertices is gone, triangle.setFromPointsAndIndices is still there, and only used in unit test. It seems that accepting geometry.attributes.position would make more sense now, no?


Issue discussion:
The method was not necessarily intended to directly work on `geometry.vertices`. Most math methods assume points are defined as `Vector3`s.
It is super obvious that the method was intended to work on clicked face, but you can always ask @bhouston who had [added it](https://github.com/mrdoob/three.js/commit/c5d38e8993a56fa7c2fc964a3ee7e946b69f01a1#diff-3f643cee94a92b6bf2260109a4c1092c7ce645e8f72d1f0b01aaf08647eaff6bR35)

Solution patch:
diff --git a/docs/api/en/math/Triangle.html b/docs/api/en/math/Triangle.html
--- a/docs/api/en/math/Triangle.html
+++ b/docs/api/en/math/Triangle.html
@@ -126,6 +126,16 @@ <h3>[method:Triangle set]( [param:Vector3 a], [param:Vector3 b], [param:Vector3
 		Please note that this method only copies the values from the given objects.
 		</p>
 
+		<h3>[method:Triangle setFromAttributeAndIndices]( [param:BufferAttribute attribute], [param:Integer i0], [param:Integer i1], [param:Integer i2] ) [param:Triangle this]</h3>
+		<p>
+		attribute - [page:BufferAttribute] of vertex data <br />
+		i0 - [page:Integer] index <br />
+		i1 - [page:Integer] index <br />
+		i2 - [page:Integer] index<br /><br />
+
+		Sets the triangle's vertices from the buffer attribute vertex data.
+		</p>
+
 		<h3>[method:Triangle setFromPointsAndIndices]( [param:Array points], [param:Integer i0], [param:Integer i1], [param:Integer i2] ) [param:Triangle this]</h3>
 		<p>
 		points - [page:Array] of [page:Vector3]s <br />
diff --git a/docs/api/zh/math/Triangle.html b/docs/api/zh/math/Triangle.html
--- a/docs/api/zh/math/Triangle.html
+++ b/docs/api/zh/math/Triangle.html
@@ -123,6 +123,17 @@ <h3>[method:Triangle set]( [param:Vector3 a], [param:Vector3 b], [param:Vector3
 			请注意，此方法仅复制给定对象的值。
 		</p>
 
+		<h3>[method:Triangle setFromAttributeAndIndices]( [param:BufferAttribute attribute], [param:Integer i0], [param:Integer i1], [param:Integer i2] ) [param:Triangle this]</h3>
+		<p>
+		attribute - [page:BufferAttribute] of vertex data <br />
+		i0 - [page:Integer] index <br />
+		i1 - [page:Integer] index <br />
+		i2 - [page:Integer] index<br /><br />
+
+		Sets the triangle's vertices from the buffer attribute vertex data.
+		</p>
+
+
 		<h3>[method:Triangle setFromPointsAndIndices]( [param:Array points], [param:Integer i0], [param:Integer i1], [param:Integer i2] ) [param:Triangle this]</h3>
 		<p>
 		points - [page:Vector3]数组（[page:Array]） <br />
diff --git a/src/math/Triangle.js b/src/math/Triangle.js
--- a/src/math/Triangle.js
+++ b/src/math/Triangle.js
@@ -124,6 +124,16 @@ class Triangle {
 
 	}
 
+	setFromAttributeAndIndices( attribute, i0, i1, i2 ) {
+
+		this.a.fromBufferAttribute( attribute, i0 );
+		this.b.fromBufferAttribute( attribute, i1 );
+		this.c.fromBufferAttribute( attribute, i2 );
+
+		return this;
+
+	}
+
 	clone() {
 
 		return new this.constructor().copy( this );


Test patch:
diff --git a/test/unit/src/math/Triangle.tests.js b/test/unit/src/math/Triangle.tests.js
--- a/test/unit/src/math/Triangle.tests.js
+++ b/test/unit/src/math/Triangle.tests.js
@@ -1,5 +1,6 @@
 /* global QUnit */
 
+import { BufferAttribute } from '../../../../src/core/BufferAttribute';
 import { Triangle } from '../../../../src/math/Triangle';
 import { Box3 } from '../../../../src/math/Box3';
 import { Plane } from '../../../../src/math/Plane';
@@ -72,6 +73,18 @@ export default QUnit.module( 'Maths', () => {
 
 		} );
 
+		QUnit.test( "setFromAttributeAndIndices", ( assert ) => {
+
+			var a = new Triangle();
+			var attribute = new BufferAttribute( new Float32Array( [ 1, 1, 1, - 1, - 1, - 1, 2, 2, 2 ] ), 3 );
+
+			a.setFromAttributeAndIndices( attribute, 1, 0, 2 );
+			assert.ok( a.a.equals( one3.clone().negate() ), "Passed!" );
+			assert.ok( a.b.equals( one3 ), "Passed!" );
+			assert.ok( a.c.equals( two3 ), "Passed!" );
+
+		} );
+
 		QUnit.todo( "clone", ( assert ) => {
 
 			assert.ok( false, "everything's gonna be alright" );