sampleFace method

dynamic sampleFace(
  1. dynamic faceIndex,
  2. dynamic targetPosition,
  3. dynamic targetNormal,
  4. dynamic targetColor,
)

Implementation

sampleFace(faceIndex, targetPosition, targetNormal, targetColor) {
  var u = randomFunction();
  var v = randomFunction();

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

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

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

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

  if (targetColor != null) {
    _face.a.fromBufferAttribute(colorAttribute, faceIndex * 3);
    _face.b.fromBufferAttribute(colorAttribute, faceIndex * 3 + 1);
    _face.c.fromBufferAttribute(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;
}