internal static void ApplySmoothingGroups(ProBuilderMesh mesh, IEnumerable <Face> faces, float angleThreshold, Vector3[] normals) { if (mesh == null || faces == null) { throw new System.ArgumentNullException("mesh"); } // Reset the selected faces to no smoothing group bool anySmoothed = false; foreach (Face face in faces) { if (face.smoothingGroup != smoothingGroupNone) { anySmoothed = true; } face.smoothingGroup = Smoothing.smoothingGroupNone; } // if a set of normals was not supplied, get a new set of normals // with no prior smoothing groups applied. if (normals == null) { if (anySmoothed) { mesh.mesh.normals = null; } normals = mesh.GetNormals(); } float threshold = Mathf.Abs(Mathf.Cos(Mathf.Clamp(angleThreshold, 0f, 89.999f) * Mathf.Deg2Rad)); HashSet <int> used = new HashSet <int>(mesh.facesInternal.Select(x => x.smoothingGroup)); int group = GetNextUnusedSmoothingGroup(1, used); HashSet <Face> processed = new HashSet <Face>(); List <WingedEdge> wings = WingedEdge.GetWingedEdges(mesh, faces, true); foreach (WingedEdge wing in wings) { // Already part of a group if (!processed.Add(wing.face)) { continue; } wing.face.smoothingGroup = group; if (FindSoftEdgesRecursive(normals, wing, threshold, processed)) { used.Add(group); group = GetNextUnusedSmoothingGroup(group, used); } else { wing.face.smoothingGroup = Smoothing.smoothingGroupNone; } } }
/// <summary> /// Compile a UnityEngine.Mesh from a ProBuilderMesh. /// </summary> /// <param name="probuilderMesh">The mesh source.</param> /// <param name="targetMesh">Destination UnityEngine.Mesh.</param> /// <param name="preferredTopology">If specified, the function will try to create topology matching the reqested format (and falling back on triangles where necessary).</param> /// <returns>The resulting material array from the compiled faces array. This is suitable to assign to the MeshRenderer.sharedMaterials property.</returns> public static void Compile(ProBuilderMesh probuilderMesh, Mesh targetMesh, MeshTopology preferredTopology = MeshTopology.Triangles) { if (probuilderMesh == null) { throw new ArgumentNullException("probuilderMesh"); } if (targetMesh == null) { throw new ArgumentNullException("targetMesh"); } targetMesh.Clear(); targetMesh.vertices = probuilderMesh.positionsInternal; targetMesh.uv = probuilderMesh.texturesInternal; if (probuilderMesh.HasArrays(MeshArrays.Texture2)) { List <Vector4> uvChannel = new List <Vector4>(); probuilderMesh.GetUVs(2, uvChannel); targetMesh.SetUVs(2, uvChannel); } if (probuilderMesh.HasArrays(MeshArrays.Texture3)) { List <Vector4> uvChannel = new List <Vector4>(); probuilderMesh.GetUVs(3, uvChannel); targetMesh.SetUVs(3, uvChannel); } targetMesh.normals = probuilderMesh.GetNormals(); targetMesh.tangents = probuilderMesh.GetTangents(); if (probuilderMesh.HasArrays(MeshArrays.Color)) { targetMesh.colors = probuilderMesh.colorsInternal; } var materialCount = probuilderMesh.GetComponent <Renderer>().sharedMaterials.Length; var submeshes = Submesh.GetSubmeshes(probuilderMesh.facesInternal, materialCount, preferredTopology); targetMesh.subMeshCount = submeshes.Length; for (int i = 0; i < targetMesh.subMeshCount; i++) { targetMesh.SetIndices(submeshes[i].m_Indexes, submeshes[i].m_Topology, i, false); } targetMesh.name = string.Format("pb_Mesh{0}", probuilderMesh.id); }
public static void CalculateTangents(ProBuilderMesh mesh) { int vc = mesh.vertexCount; if (!mesh.HasArrays(MeshArrays.Tangent)) { mesh.tangentsInternal = new Vector4[vc]; } if (!mesh.HasArrays(MeshArrays.Position) || !mesh.HasArrays(MeshArrays.Texture0)) { return; } // http://answers.unity3d.com/questions/7789/calculating-tangents-vector4.html Vector3[] normals = mesh.GetNormals(); var positions = mesh.positionsInternal; var textures = mesh.texturesInternal; var tan1 = new Vector3[vc]; var tan2 = new Vector3[vc]; var tangents = mesh.tangentsInternal; foreach (var face in mesh.facesInternal) { int[] triangles = face.indexesInternal; for (int a = 0, c = triangles.Length; a < c; a += 3) { long i1 = triangles[a + 0]; long i2 = triangles[a + 1]; long i3 = triangles[a + 2]; Vector3 v1 = positions[i1]; Vector3 v2 = positions[i2]; Vector3 v3 = positions[i3]; Vector2 w1 = textures[i1]; Vector2 w2 = textures[i2]; Vector2 w3 = textures[i3]; float x1 = v2.x - v1.x; float x2 = v3.x - v1.x; float y1 = v2.y - v1.y; float y2 = v3.y - v1.y; float z1 = v2.z - v1.z; float z2 = v3.z - v1.z; float s1 = w2.x - w1.x; float s2 = w3.x - w1.x; float t1 = w2.y - w1.y; float t2 = w3.y - w1.y; float r = 1.0f / (s1 * t2 - s2 * t1); Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r); Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r); tan1[i1] += sdir; tan1[i2] += sdir; tan1[i3] += sdir; tan2[i1] += tdir; tan2[i2] += tdir; tan2[i3] += tdir; } } for (long a = 0; a < vc; ++a) { Vector3 n = normals[a]; Vector3 t = tan1[a]; Vector3.OrthoNormalize(ref n, ref t); tangents[a].x = t.x; tangents[a].y = t.y; tangents[a].z = t.z; tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f; } }