Guest User

VertexGradientEditor

a guest
May 1st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. using UnityEditor;
  2. using Sirenix.OdinInspector.Editor;
  3. using Sirenix.OdinInspector;
  4. using Sirenix.Utilities.Editor;
  5. using Sirenix.Utilities;
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8.  
  9. public class VertexGradientEditor : OdinEditorWindow
  10. {
  11.     [MenuItem("Tools/VertexGradientEditor")]
  12.     private static void OpenWindow()
  13.     {
  14.         var window = GetWindow<VertexGradientEditor>();
  15.  
  16.         // Nifty little trick to quickly position the window in the middle of the editor.
  17.         window.position = GUIHelper.GetEditorWindowRect().AlignCenter(380, 200);
  18.     }
  19.  
  20.     [MinMaxSlider(0, 1, true)]
  21.     public Vector2 VertexGradientR;
  22.     [MinMaxSlider(0, 1, true)]
  23.     public Vector2 VertexGradientG;
  24.     [MinMaxSlider(0, 1, true)]
  25.     public Vector2 VertexGradientB;
  26.     [MinMaxSlider(0, 1, true)]
  27.     public Vector2 VertexGradientA;
  28.  
  29.     [HorizontalGroup("Split", 0.5f)]
  30.     [Button(ButtonSizes.Large), GUIColor(0.4f, 0.8f, 1)]
  31.     void BakeGradient()
  32.     {
  33.         var test = Selection.GetTransforms(SelectionMode.Deep);
  34.         foreach (var t in test)
  35.         {
  36.             MeshFilter mf = t.GetComponent<MeshFilter>();
  37.             if (mf != null)
  38.             {
  39.                 Mesh m = (Mesh)Instantiate(mf.sharedMesh);
  40.                 //m.name = mf.sharedMesh.name + "VC";
  41.                
  42.                 Vector3[] vertices = m.vertices;
  43.  
  44.                 // create new colors array where the colors will be created.
  45.                 Color[] colors = new Color[vertices.Length];
  46.  
  47.                 float highestVert = -9999;
  48.                 float lowestVert = 9999;
  49.  
  50.                 for (int i = 0; i < vertices.Length; i++)
  51.                 {
  52.                     if (vertices[i].y < lowestVert)
  53.                     {
  54.                         lowestVert = vertices[i].y;
  55.                     }
  56.                     if (vertices[i].y > highestVert)
  57.                     {
  58.                         highestVert = vertices[i].y;
  59.                     }
  60.                 }
  61.  
  62.                 for (int i = 0; i < vertices.Length; i++)
  63.                 {
  64.                     if (m.colors != null && m.colors.Length > 1)
  65.                     {
  66.                         colors[i] = m.colors[i];
  67.                     }
  68.                     else
  69.                     {
  70.                         colors[i] = Color.white;
  71.                     }
  72.                     colors[i].r = DoRemap(vertices[i].y, lowestVert, highestVert, VertexGradientR.x, VertexGradientR.y);
  73.                     colors[i].g = DoRemap(vertices[i].y, lowestVert, highestVert, VertexGradientG.x, VertexGradientG.y);
  74.                     colors[i].b = DoRemap(vertices[i].y, lowestVert, highestVert, VertexGradientB.x, VertexGradientB.y);
  75.                     colors[i].a = DoRemap(vertices[i].y, lowestVert, highestVert, VertexGradientA.x, VertexGradientA.y);
  76.                 }
  77.  
  78.                 // assign the array of colors to the Mesh.
  79.                 m.colors = colors;
  80.                 mf.mesh = m;
  81.             }
  82.         }
  83.     }
  84.  
  85.     [HorizontalGroup("Split", 0.5f)]
  86.     [Button(ButtonSizes.Large), GUIColor(0.8f, 0.4f, 1)]
  87.     public void SaveMesh()
  88.     {
  89.         Transform[] gos = Selection.activeTransform.GetComponentsInChildren<Transform>();
  90.  
  91.         foreach (Transform t in gos)
  92.         {
  93.             string savingString = "Assets/SavedModels/" + t.name + ".asset";
  94.             if (t.gameObject.GetComponent<MeshFilter>())
  95.             {
  96.  
  97.                 try
  98.                 {
  99.                     AssetDatabase.CreateAsset(t.gameObject.GetComponent<MeshFilter>().sharedMesh, savingString);
  100.                 }
  101.                 catch (UnityException e)
  102.                 {
  103.                     Debug.Log(e);
  104.                 }
  105.                 AssetDatabase.SaveAssets();
  106.             }
  107.             else
  108.             {
  109.                 Debug.Log("Mesh filter not found");
  110.             }
  111.  
  112.         }
  113.         AssetDatabase.Refresh();
  114.     }
  115.  
  116.  
  117.     public float DoRemap(float value, float from1, float to1, float from2, float to2)
  118.     {
  119.         return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
  120.     }
  121. }
Add Comment
Please, Sign In to add comment