sampleFace method

MeshSurfaceSampler sampleFace(
  1. int faceIndex,
  2. Vector3 targetPosition, [
  3. Vector3? targetNormal,
  4. Color? targetColor,
])

Implementation

MeshSurfaceSampler sampleFace(int faceIndex,Vector3 targetPosition,[Vector3? targetNormal,Color? targetColor ]) {
  double u = randomFunction();
  double v = randomFunction();

  if ( u + v > 1 ) {
    u = 1 - u;
    v = 1 - v;
  }

  _face.a.fromBuffer( positionAttribute, faceIndex * 3 );
  _face.b.fromBuffer( positionAttribute, faceIndex * 3 + 1 );
  _face.c.fromBuffer( positionAttribute, faceIndex * 3 + 2 );

  targetPosition.setValues( 0, 0, 0 ).addScaled( _face.a, u ).addScaled( _face.b, v ).addScaled( _face.c, 1 - ( u + v ) );

  if ( targetNormal != null ) {
    _face.getNormal( targetNormal );
  }

  if ( targetColor != null && colorAttribute != null ) {
    _face.a.fromBuffer( colorAttribute!, faceIndex * 3 );
    _face.b.fromBuffer( colorAttribute!, faceIndex * 3 + 1 );
    _face.c.fromBuffer( colorAttribute!, faceIndex * 3 + 2 );

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

    targetColor.red = _color.x;
    targetColor.green = _color.y;
    targetColor.blue = _color.z;
  }

  return this;
}