/// <summary> /// Apply the weights to "mesh" /// </summary> /// <param name="mesh">The mesh we want to apply weights to</param> internal void Apply(PolyMesh mesh) { foreach (AttributeLayout al in attributeLayout) { switch (al.channel) { case MeshChannel.UV0: case MeshChannel.UV2: case MeshChannel.UV3: case MeshChannel.UV4: { List <Vector4> uv = new List <Vector4>(weights[channelMap[al.channel]]); mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(al.channel), uv); } break; case MeshChannel.Color: { // @todo consider storing Color array separate from Vec4 since this cast costs ~5ms mesh.colors = System.Array.ConvertAll(weights[channelMap[al.channel]], x => Vec4ToColor32(x)); break; } case MeshChannel.Tangent: { mesh.tangents = weights[channelMap[MeshChannel.Tangent]]; break; } } } }
internal void ApplyAttributesFromUnityMesh(Mesh mesh, MeshChannel attrib = MeshChannel.All) { #pragma warning disable 0162 if (attrib == MeshChannel.All) { vertices = mesh.vertices; normals = mesh.normals; colors = mesh.colors32; tangents = mesh.tangents; mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV0), uv0); mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV2), uv1); mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV3), uv2); mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV4), uv3); /// Update submeshes only if there were none set or if we are no /// about to change the submeshCount. if (subMeshCount == 0 || mesh.subMeshCount == subMeshCount) { SetSubMeshes(mesh); RefreshTriangles(); } } else { if ((attrib & MeshChannel.Position) > 0) { vertices = mesh.vertices; } if ((attrib & MeshChannel.Normal) > 0) { normals = mesh.normals; } if ((attrib & MeshChannel.Color) > 0) { colors = mesh.colors32; } if ((attrib & MeshChannel.Tangent) > 0) { tangents = mesh.tangents; } if ((attrib & MeshChannel.UV0) > 0) { mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV0), uv0); } if ((attrib & MeshChannel.UV2) > 0) { mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV2), uv1); } if ((attrib & MeshChannel.UV3) > 0) { mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV3), uv2); } if ((attrib & MeshChannel.UV4) > 0) { mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV4), uv3); } } #pragma warning restore 0162 }
/// <summary> /// Initialize a SplatSet with mesh and attribute layout. /// </summary> /// <param name="mesh"></param> /// <param name="attributes"></param> internal SplatSet(PolyMesh mesh, AttributeLayout[] attributes) : this(mesh.vertexCount, attributes, false) { foreach (var kvp in channelMap) { switch (kvp.Key) { case MeshChannel.UV0: case MeshChannel.UV2: case MeshChannel.UV3: case MeshChannel.UV4: { List <Vector4> uv = mesh.GetUVs(MeshChannelUtility.UVChannelToIndex(kvp.Key)); weights[kvp.Value] = uv.Count == weightCount?uv.ToArray() : new Vector4[weightCount]; } break; case MeshChannel.Color: { Color32[] color = mesh.colors; weights[kvp.Value] = color != null && color.Length == weightCount?System.Array.ConvertAll(color, x => Color32ToVec4(x)) : new Vector4[weightCount]; } break; case MeshChannel.Tangent: { Vector4[] tangent = mesh.tangents; weights[kvp.Value] = tangent != null && tangent.Length == weightCount ? tangent : new Vector4[weightCount]; } break; } } }
/// <summary> /// Apply the vertex attributes to a UnityEngine mesh (does not set triangles) /// </summary> /// <param name="mesh"></param> /// <param name="attrib"></param> internal void ApplyAttributesToUnityMesh(Mesh mesh, MeshChannel attrib = MeshChannel.All) { // I guess the default value for attrib makes the compiler think that else is never // activated? #pragma warning disable 0162 if (attrib == MeshChannel.All) { mesh.vertices = vertices; mesh.normals = normals; mesh.colors32 = colors; mesh.tangents = tangents; mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV0), uv0); mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV2), uv1); mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV3), uv2); mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV4), uv3); mesh.subMeshCount = subMeshCount; for (int i = 0; i < subMeshCount; ++i) { mesh.SetIndices(m_SubMeshes[i].indexes, m_SubMeshes[i].topology, i); } RefreshTriangles(); } else { if ((attrib & MeshChannel.Position) > 0) { mesh.vertices = vertices; } if ((attrib & MeshChannel.Normal) > 0) { mesh.normals = normals; } if ((attrib & MeshChannel.Color) > 0) { mesh.colors32 = colors; } if ((attrib & MeshChannel.Tangent) > 0) { mesh.tangents = tangents; } if ((attrib & MeshChannel.UV0) > 0) { mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV0), uv0); } if ((attrib & MeshChannel.UV2) > 0) { mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV2), uv1); } if ((attrib & MeshChannel.UV3) > 0) { mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV3), uv2); } if ((attrib & MeshChannel.UV4) > 0) { mesh.SetUVs(MeshChannelUtility.UVChannelToIndex(MeshChannel.UV4), uv3); } } #pragma warning restore 0162 }