Skip to content

Instantly share code, notes, and snippets.

@facybenbook
Forked from SkaillZ/SplatMapToVertColors.cs
Created April 12, 2019 19:07
Show Gist options
  • Save facybenbook/b08b0af69bb596afdc7d183dc3b904d1 to your computer and use it in GitHub Desktop.
Save facybenbook/b08b0af69bb596afdc7d183dc3b904d1 to your computer and use it in GitHub Desktop.

Revisions

  1. @SkaillZ SkaillZ revised this gist Apr 2, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions SplatMapToVertColors.cs
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@ namespace RoboticWeaselAssault.Gamutmamut.Editor
    public class EditorTools
    {
    /// <summary>
    /// Copies a splat map into vertex colors. A mesh and a texture must be selected. Read/write must be enabled in
    /// the texture's import settings.
    /// 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.
    /// </summary>
    [MenuItem("Tools/Copy SplatMap to Vertex Colors")]
    public static void SplatMapToVertexColors()
  2. @SkaillZ SkaillZ created this gist Apr 2, 2019.
    62 changes: 62 additions & 0 deletions SplatMapToVertColors.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    using System;
    using System.Linq;
    using UnityEditor;
    using UnityEngine;

    namespace RoboticWeaselAssault.Gamutmamut.Editor
    {
    public class EditorTools
    {
    /// <summary>
    /// Copies a splat map into vertex colors. A mesh and a texture must be selected. Read/write must be enabled in
    /// the texture's import settings.
    /// </summary>
    [MenuItem("Tools/Copy SplatMap to Vertex Colors")]
    public static void SplatMapToVertexColors()
    {
    var mesh = Selection.objects.OfType<Mesh>().FirstOrDefault();
    var texture = Selection.objects.OfType<Texture2D>().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
    };
    }
    }
    }