createNormalsForGeometry()

in src/engine/actvolume/georender.js [257:334]


  createNormalsForGeometry() {
    this.m_normals = new Array(this.m_numVertices);
    for (let i = 0; i < this.m_numVertices; i++) {
      this.m_normals[i] = new THREE.Vector3();
    }
    const triNormals = new Array(this.m_numTriangles);
    for (let i = 0; i < this.m_numTriangles; i++) {
      triNormals[i] = new THREE.Vector3();
    }

    const IND_A = 0;
    const IND_B = 1;
    const IND_C = 2;

    const OFF_X = 0;
    const OFF_Y = 1;
    const OFF_Z = 2;

    const VERTICES_IN_TRIANGLE = 3;
    const COORDS_IN_VERTEX = 4;

    let t, i3;
    for (t = 0, i3 = 0; t < this.m_numTriangles; t++, i3 += VERTICES_IN_TRIANGLE) {
      const ia = this.m_indices[i3 + IND_A] * COORDS_IN_VERTEX;
      const ib = this.m_indices[i3 + IND_B] * COORDS_IN_VERTEX;
      const ic = this.m_indices[i3 + IND_C] * COORDS_IN_VERTEX;
      const va = new THREE.Vector3();
      const vb = new THREE.Vector3();
      const vc = new THREE.Vector3();
      va.x = this.m_vertices[ia + OFF_X];
      va.y = this.m_vertices[ia + OFF_Y];
      va.z = this.m_vertices[ia + OFF_Z];
      vb.x = this.m_vertices[ib + OFF_X];
      vb.y = this.m_vertices[ib + OFF_Y];
      vb.z = this.m_vertices[ib + OFF_Z];
      vc.x = this.m_vertices[ic + OFF_X];
      vc.y = this.m_vertices[ic + OFF_Y];
      vc.z = this.m_vertices[ic + OFF_Z];
      const vab = new THREE.Vector3();
      const vbc = new THREE.Vector3();
      vab.subVectors(vb, va);
      vbc.subVectors(vc, vb);
      const vTriNormal = new THREE.Vector3();
      vTriNormal.crossVectors(vab, vbc);
      const TOO_SMALL = 1.0e-8;
      if (vTriNormal.lengthSq() < TOO_SMALL) {
        const ERR_BAD_NORMAL = -10;
        return ERR_BAD_NORMAL;
      }
      triNormals[t].set(vTriNormal.x, vTriNormal.y, vTriNormal.z);
    } // for (t) all triangles
    // init vert normals with 0 vector
    for (let i = 0; i < this.m_numVertices; i++) {
      this.m_normals[i].set(0.0, 0.0, 0.0);
    }
    // get additions
    for (t = 0, i3 = 0; t < this.m_numTriangles; t++, i3 += VERTICES_IN_TRIANGLE) {
      const ia = this.m_indices[i3 + IND_A];
      const ib = this.m_indices[i3 + IND_B];
      const ic = this.m_indices[i3 + IND_C];
      const vTriNormal = triNormals[t];
      this.m_normals[ia].add(vTriNormal);
      this.m_normals[ib].add(vTriNormal);
      this.m_normals[ic].add(vTriNormal);
    } // for (t) all triangles
    // normalize vertices normals
    for (let i = 0; i < this.m_numVertices; i++) {
      const len2 = this.m_normals[i].lengthSq();
      const TOO_SMALL = 1.0e-8;
      if (len2 < TOO_SMALL) {
        const ERR_BAD_VERT_NORMAL = -20;
        return ERR_BAD_VERT_NORMAL;
      }
      this.m_normals[i].normalize();
    } // for (i) all vertices

    return 1;
  } // createNormalsFormGeometry