computeVertexNormals method

dynamic computeVertexNormals({
  1. bool areaWeighted = true,
})

Implementation

computeVertexNormals({bool areaWeighted = true}) {
  var vertices = List<THREE.Vector3>.filled(
      this.vertices.length, THREE.Vector3(0, 0, 0));

  for (var v = 0, vl = this.vertices.length; v < vl; v++) {
    vertices[v] = new THREE.Vector3.init();
  }

  if (areaWeighted) {
    // vertex normals weighted by triangle areas
    // http://www.iquilezles.org/www/articles/normals/normals.htm

    var cb = new THREE.Vector3.init(), ab = new THREE.Vector3.init();

    for (var f = 0, fl = this.faces.length; f < fl; f++) {
      var face = this.faces[f];

      var vA = this.vertices[face.a];
      var vB = this.vertices[face.b];
      var vC = this.vertices[face.c];

      cb.subVectors(vC, vB);
      ab.subVectors(vA, vB);
      cb.cross(ab);

      vertices[face.a].add(cb);
      vertices[face.b].add(cb);
      vertices[face.c].add(cb);
    }
  } else {
    this.computeFaceNormals();

    for (var f = 0, fl = this.faces.length; f < fl; f++) {
      var face = this.faces[f];

      vertices[face.a].add(face.normal);
      vertices[face.b].add(face.normal);
      vertices[face.c].add(face.normal);
    }
  }

  for (var v = 0, vl = this.vertices.length; v < vl; v++) {
    vertices[v].normalize();
  }

  for (var f = 0, fl = this.faces.length; f < fl; f++) {
    var face = this.faces[f];

    var vertexNormals = face.vertexNormals;

    if (vertexNormals.length == 3) {
      vertexNormals[0].copy(vertices[face.a]);
      vertexNormals[1].copy(vertices[face.b]);
      vertexNormals[2].copy(vertices[face.c]);
    } else {
      vertexNormals[0] = vertices[face.a].clone();
      vertexNormals[1] = vertices[face.b].clone();
      vertexNormals[2] = vertices[face.c].clone();
    }
  }

  if (this.faces.length > 0) {
    this.normalsNeedUpdate = true;
  }
}