|
@@ -1,5 +1,6 @@
|
|
|
import {
|
|
|
Triangle,
|
|
|
+ Vector2,
|
|
|
Vector3
|
|
|
} from 'three';
|
|
|
|
|
@@ -16,6 +17,7 @@ import {
|
|
|
|
|
|
const _face = new Triangle();
|
|
|
const _color = new Vector3();
|
|
|
+const _uva = new Vector2(), _uvb = new Vector2(), _uvc = new Vector2();
|
|
|
|
|
|
class MeshSurfaceSampler {
|
|
|
|
|
@@ -36,6 +38,7 @@ class MeshSurfaceSampler {
|
|
|
|
|
|
this.positionAttribute = this.geometry.getAttribute( 'position' );
|
|
|
this.colorAttribute = this.geometry.getAttribute( 'color' );
|
|
|
+ this.uvAttribute = this.geometry.getAttribute( 'uv' );
|
|
|
this.weightAttribute = null;
|
|
|
|
|
|
this.distribution = null;
|
|
@@ -106,10 +109,10 @@ class MeshSurfaceSampler {
|
|
|
|
|
|
}
|
|
|
|
|
|
- sample( targetPosition, targetNormal, targetColor ) {
|
|
|
+ sample( targetPosition, targetNormal, targetColor, targetUV ) {
|
|
|
|
|
|
const faceIndex = this.sampleFaceIndex();
|
|
|
- return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor );
|
|
|
+ return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -154,7 +157,7 @@ class MeshSurfaceSampler {
|
|
|
|
|
|
}
|
|
|
|
|
|
- sampleFace( faceIndex, targetPosition, targetNormal, targetColor ) {
|
|
|
+ sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV ) {
|
|
|
|
|
|
let u = this.randomFunction();
|
|
|
let v = this.randomFunction();
|
|
@@ -200,6 +203,15 @@ class MeshSurfaceSampler {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ if ( targetUV !== undefined && this.uvAttribute !== undefined ) {
|
|
|
+
|
|
|
+ _uva.fromBufferAttribute( this.uvAttribute, faceIndex * 3 );
|
|
|
+ _uvb.fromBufferAttribute( this.uvAttribute, faceIndex * 3 + 1 );
|
|
|
+ _uvc.fromBufferAttribute( this.uvAttribute, faceIndex * 3 + 2 );
|
|
|
+ targetUV.set( 0, 0 ).addScaledVector( _uva, u ).addScaledVector( _uvb, v ).addScaledVector( _uvc, 1 - ( u + v ) );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
return this;
|
|
|
|
|
|
}
|