Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/examples/en/math/MeshSurfaceSampler.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ <h3>[method:this build]()</h3>
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.
</p>

<h3>[method:this sample]( [param:Vector3 targetPosition], [param:Vector3 targetNormal] )</h3>
<h3>[method:this sample]( [param:Vector3 targetPosition], [param:Vector3 targetNormal], [param:Color targetColor] )</h3>
<p>
Selects a random point on the surface of the input geometry, returning the position and normal vector at that point. Time complexity is <i>O(log n)</i> for a surface with <i>n</i> faces.</i></p>
Selects a random point on the surface of the input geometry, returning the position and optional 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>

<h2>Source</h2>

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/zh/math/MeshSurfaceSampler.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ <h3>[method:this build]()</h3>
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.
</p>

<h3>[method:this sample]( [param:Vector3 targetPosition], [param:Vector3 targetNormal] )</h3>
<h3>[method:this sample]( [param:Vector3 targetPosition], [param:Vector3 targetNormal], [param:Color targetColor] )</h3>
<p>
Selects a random point on the surface of the input geometry, returning the position and normal vector at that point. Time complexity is <i>O(log n)</i> for a surface with <i>n</i> faces.</i></p>
Selects a random point on the surface of the input geometry, returning the position and optional 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>

<h2>Source</h2>

Expand Down
5 changes: 3 additions & 2 deletions examples/jsm/math/MeshSurfaceSampler.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BufferGeometry,
Color,
Mesh,
Vector3
} from '../../../src/Three';
Expand All @@ -14,8 +15,8 @@ export class MeshSurfaceSampler {
constructor( mesh: Mesh );
binarySearch( x: number ): number;
build(): this;
sample( targetPosition: Vector3, targetNormal: Vector3 ): this;
sampleFace( faceIndex: number, targetPosition: Vector3, targetNormal: Vector3 ): this;
sample( targetPosition: Vector3, targetNormal?: Vector3, targetColor?: Color ): this;
sampleFace( faceIndex: number, targetPosition: Vector3, targetNormal?: Vector3, targetColor?: Color ): this;
setWeightAttribute( name: string | null ): this;

}
35 changes: 30 additions & 5 deletions examples/jsm/math/MeshSurfaceSampler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Triangle
Triangle,
Vector3
} from "../../../build/three.module.js";

/**
Expand All @@ -15,6 +16,7 @@ import {
var MeshSurfaceSampler = ( function () {

var _face = new Triangle();
var _color = new Vector3();

function MeshSurfaceSampler( mesh ) {

Expand All @@ -38,6 +40,7 @@ var MeshSurfaceSampler = ( function () {
this.randomFunction = Math.random;

this.positionAttribute = this.geometry.getAttribute( 'position' );
this.colorAttribute = this.geometry.getAttribute( 'color' );
this.weightAttribute = null;

this.distribution = null;
Expand Down Expand Up @@ -112,13 +115,13 @@ var MeshSurfaceSampler = ( function () {

},

sample: function ( targetPosition, targetNormal ) {
sample: function ( targetPosition, targetNormal, targetColor ) {

var cumulativeTotal = this.distribution[ this.distribution.length - 1 ];

var faceIndex = this.binarySearch( this.randomFunction() * cumulativeTotal );

return this.sampleFace( faceIndex, targetPosition, targetNormal );
return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor );

},

Expand Down Expand Up @@ -156,7 +159,7 @@ var MeshSurfaceSampler = ( function () {

},

sampleFace: function ( faceIndex, targetPosition, targetNormal ) {
sampleFace: function ( faceIndex, targetPosition, targetNormal, targetColor ) {

var u = this.randomFunction();
var v = this.randomFunction();
Expand All @@ -178,7 +181,29 @@ var MeshSurfaceSampler = ( function () {
.addScaledVector( _face.b, v )
.addScaledVector( _face.c, 1 - ( u + v ) );

_face.getNormal( targetNormal );
if ( targetNormal !== undefined ) {

_face.getNormal( targetNormal );

}

if ( targetColor !== undefined && this.colorAttribute !== undefined ) {

_face.a.fromBufferAttribute( this.colorAttribute, faceIndex * 3 );
_face.b.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 1 );
_face.c.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 2 );

_color
.set( 0, 0, 0 )
.addScaledVector( _face.a, u )
.addScaledVector( _face.b, v )
.addScaledVector( _face.c, 1 - ( u + v ) );

targetColor.r = _color.x;
targetColor.g = _color.y;
targetColor.b = _color.z;

}

return this;

Expand Down