|
@@ -1,5 +1,6 @@
|
|
|
import {
|
|
|
- Triangle
|
|
|
+ Triangle,
|
|
|
+ Vector3
|
|
|
} from "../../../build/three.module.js";
|
|
|
|
|
|
/**
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
var MeshSurfaceSampler = ( function () {
|
|
|
|
|
|
var _face = new Triangle();
|
|
|
+ var _color = new Vector3();
|
|
|
|
|
|
function MeshSurfaceSampler( mesh ) {
|
|
|
|
|
@@ -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;
|
|
@@ -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 );
|
|
|
|
|
|
},
|
|
|
|
|
@@ -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();
|
|
@@ -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;
|
|
|
|