Skip to content

Commit 63710e7

Browse files
makcIanSweeneyAC
andauthored
MeshSurfaceSampler: Add uv support. (mrdoob#26207)
* add uv support to MeshSurfaceSampler.js * update for target UV --------- Co-authored-by: IanSweeneyAC <[email protected]>
1 parent 2b8f360 commit 63710e7

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

docs/examples/en/math/MeshSurfaceSampler.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ <h3>[method:this build]()</h3>
8080
Processes the input geometry and prepares to return samples. Any configuration of the geometry or sampler must occur before this method is called. Time complexity is <i>O(n)</i> for a surface with <i>n</i> faces.
8181
</p>
8282

83-
<h3>[method:this sample]( [param:Vector3 targetPosition], [param:Vector3 targetNormal], [param:Color targetColor] )</h3>
83+
<h3>[method:this sample]( [param:Vector3 targetPosition], [param:Vector3 targetNormal], [param:Color targetColor], [param:Vector2 targetUV] )</h3>
8484
<p>
85-
Selects a random point on the surface of the input geometry, returning the position and optionally the normal vector and color at that point. Time complexity is <i>O(log n)</i> for a surface with <i>n</i> faces.</i></p>
85+
Selects a random point on the surface of the input geometry, returning the position and optionally the normal vector, color and UV Coordinate at that point. Time complexity is <i>O(log n)</i> for a surface with <i>n</i> faces.</i></p>
8686

8787
<h2>Source</h2>
8888

examples/jsm/math/MeshSurfaceSampler.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Triangle,
3+
Vector2,
34
Vector3
45
} from 'three';
56

@@ -16,6 +17,7 @@ import {
1617

1718
const _face = new Triangle();
1819
const _color = new Vector3();
20+
const _uva = new Vector2(), _uvb = new Vector2(), _uvc = new Vector2();
1921

2022
class MeshSurfaceSampler {
2123

@@ -36,6 +38,7 @@ class MeshSurfaceSampler {
3638

3739
this.positionAttribute = this.geometry.getAttribute( 'position' );
3840
this.colorAttribute = this.geometry.getAttribute( 'color' );
41+
this.uvAttribute = this.geometry.getAttribute( 'uv' );
3942
this.weightAttribute = null;
4043

4144
this.distribution = null;
@@ -106,10 +109,10 @@ class MeshSurfaceSampler {
106109

107110
}
108111

109-
sample( targetPosition, targetNormal, targetColor ) {
112+
sample( targetPosition, targetNormal, targetColor, targetUV ) {
110113

111114
const faceIndex = this.sampleFaceIndex();
112-
return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor );
115+
return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV );
113116

114117
}
115118

@@ -154,7 +157,7 @@ class MeshSurfaceSampler {
154157

155158
}
156159

157-
sampleFace( faceIndex, targetPosition, targetNormal, targetColor ) {
160+
sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV ) {
158161

159162
let u = this.randomFunction();
160163
let v = this.randomFunction();
@@ -200,6 +203,15 @@ class MeshSurfaceSampler {
200203

201204
}
202205

206+
if ( targetUV !== undefined && this.uvAttribute !== undefined ) {
207+
208+
_uva.fromBufferAttribute( this.uvAttribute, faceIndex * 3 );
209+
_uvb.fromBufferAttribute( this.uvAttribute, faceIndex * 3 + 1 );
210+
_uvc.fromBufferAttribute( this.uvAttribute, faceIndex * 3 + 2 );
211+
targetUV.set( 0, 0 ).addScaledVector( _uva, u ).addScaledVector( _uvb, v ).addScaledVector( _uvc, 1 - ( u + v ) );
212+
213+
}
214+
203215
return this;
204216

205217
}

0 commit comments

Comments
 (0)