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] = three.Vector3.init();
  }

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

    var cb = three.Vector3.init(), ab = three.Vector3.init();

    for (var f = 0, fl = faces.length; f < fl; f++) {
      var face = 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 {
    computeFaceNormals();

    for (var f = 0, fl = faces.length; f < fl; f++) {
      var face = 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 = faces.length; f < fl; f++) {
    var face = 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 (faces.isNotEmpty) {
    normalsNeedUpdate = true;
  }
}