using System; using System.Linq; using UnityEditor; using UnityEngine; namespace RoboticWeaselAssault.Gamutmamut.Editor { public class EditorTools { /// /// Copies a splat map into a mesh's vertex colors. A mesh and a texture must be selected simultaneously. /// Read/write must be enabled in the texture's import settings. /// [MenuItem("Tools/Copy SplatMap to Vertex Colors")] public static void SplatMapToVertexColors() { var mesh = Selection.objects.OfType().FirstOrDefault(); var texture = Selection.objects.OfType().FirstOrDefault(); if (mesh == null || texture == null) { throw new Exception("Please select a mesh and a texture."); } var newMesh = CopyMesh(mesh); var colors = new Color[newMesh.vertexCount]; var uvs = newMesh.uv; for (var i = 0; i < colors.Length; i++) { // Get the texture coordinates var uv = uvs[i]; // Sample the texture on the given UV position colors[i] = texture.GetPixelBilinear(uv.x, uv.y); } newMesh.colors = colors; // Show a UI to get the path where the new mesh should be saved string targetPath = EditorUtility.SaveFilePanel("Save mesh", "Assets/", mesh.name, "asset"); if (targetPath != null) { AssetDatabase.CreateAsset(newMesh, FileUtil.GetProjectRelativePath(targetPath)); } } private static Mesh CopyMesh(Mesh mesh) { return new Mesh { vertices = mesh.vertices, triangles = mesh.triangles, uv = mesh.uv, uv2 = mesh.uv2, normals = mesh.normals, colors = mesh.colors, tangents = mesh.tangents }; } } }