/// <value>
        /// Get the normals array for this mesh.
        /// </value>
        /// <returns>
        /// Returns the normals for this mesh.
        /// </returns>
        public Vector3[] GetNormals()
        {
            if (!HasArrays(MeshArrays.Normal))
                Normals.CalculateNormals(this);

            return normals.ToArray();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Collects coincident vertices and returns a rotation calculated from the average normal and bitangent.
        /// </summary>
        /// <param name="mesh">The target mesh.</param>
        /// <param name="indices">Vertex indices to consider in the rotation calculations.</param>
        /// <returns>A rotation calculated from the average normal of each vertex.</returns>
        public static Quaternion GetRotation(ProBuilderMesh mesh, IEnumerable <int> indices)
        {
            if (!mesh.HasArrays(MeshArrays.Normal))
            {
                Normals.CalculateNormals(mesh);
            }

            if (!mesh.HasArrays(MeshArrays.Tangent))
            {
                Normals.CalculateTangents(mesh);
            }

            var normals  = mesh.normalsInternal;
            var tangents = mesh.tangentsInternal;

            var   nrm   = Vector3.zero;
            var   tan   = Vector4.zero;
            float count = 0;

            foreach (var index in indices)
            {
                var n = normals[index];
                var t = tangents[index];

                nrm.x += n.x;
                nrm.y += n.y;
                nrm.z += n.z;

                tan.x += t.x;
                tan.y += t.y;
                tan.z += t.z;
                tan.w += t.w;

                count++;
            }

            nrm.x /= count;
            nrm.y /= count;
            nrm.z /= count;

            tan.x /= count;
            tan.y /= count;
            tan.z /= count;
            tan.w /= count;

            if (nrm == Vector3.zero || tan == Vector4.zero)
            {
                return(mesh.transform.rotation);
            }

            var bit = Vector3.Cross(nrm, tan * tan.w);

            return(mesh.transform.rotation * Quaternion.LookRotation(nrm, bit));
        }
Exemplo n.º 3
0
 void RefreshNormals()
 {
     Normals.CalculateNormals(this);
     mesh.normals = m_Normals;
 }