Advertisement
Guest User

EditorVoxMaps

a guest
Sep 27th, 2018
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.35 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4.  
  5. public class EditorVoxMaps : EditorWindow
  6. {
  7.     [MenuItem("Tools/Voxel Material")]
  8.     private static void ShowEditor()
  9.     {
  10.         EditorVoxMaps win = EditorWindow.GetWindow<EditorVoxMaps>("Voxel Material");
  11.         win.Show();
  12.     }
  13.  
  14.     private class VoxMat
  15.     {
  16.         public float metallic;
  17.         public float smooth;
  18.         public bool emission;
  19.     }
  20.  
  21.     private Dictionary<Color, VoxMat> colors = new Dictionary<Color, VoxMat>();
  22.  
  23.     private Renderer rend;
  24.     private Texture2D colorTexture;
  25.     private Texture2D metallicTexture;
  26.     private Texture2D emissionTexture;
  27.  
  28.     private void Awake()
  29.     {
  30.         OnSelectionChange();
  31.     }
  32.  
  33.     private void OnInspectorUpdate()
  34.     {
  35.         Repaint();
  36.     }
  37.  
  38.     private void InvalidateMesh()
  39.     {
  40.         colors.Clear();
  41.         rend = null;
  42.         colorTexture = null;
  43.         metallicTexture = null;
  44.         emissionTexture = null;
  45.     }
  46.  
  47.     private void OnSelectionChange()
  48.     {
  49.         InvalidateMesh();
  50.  
  51.         GameObject go = Selection.activeGameObject;
  52.         if (go == null) return;
  53.  
  54.         rend = go.GetComponent<Renderer>();
  55.         if (rend == null) return;
  56.  
  57.         UpdateSelected();
  58.     }
  59.  
  60.     private void UpdateSelected()
  61.     {
  62.         colorTexture = GetReadableTexture(rend.sharedMaterial.mainTexture);
  63.         if (colorTexture == null)
  64.         {
  65.             InvalidateMesh();
  66.             return;
  67.         }
  68.  
  69.         metallicTexture = GetReadableTexture(rend.sharedMaterial.GetTexture("_MetallicGlossMap"));
  70.         if (metallicTexture != null)
  71.         {
  72.             if (metallicTexture.width != colorTexture.width || metallicTexture.height != colorTexture.height)
  73.             {
  74.                 InvalidateMesh();
  75.                 return;
  76.             }
  77.         }
  78.  
  79.         emissionTexture = GetReadableTexture(rend.sharedMaterial.GetTexture("_EmissionMap"));
  80.         if (emissionTexture != null)
  81.         {
  82.             if (emissionTexture.width != colorTexture.width || emissionTexture.height != colorTexture.height)
  83.             {
  84.                 InvalidateMesh();
  85.                 return;
  86.             }
  87.         }
  88.  
  89.         for (int y = 0; y < colorTexture.height; y++)
  90.         {
  91.             for (int x = 0; x < colorTexture.width; x++)
  92.             {
  93.                 Color c = colorTexture.GetPixel(x, y);
  94.                 if (colors.ContainsKey(c)) continue;
  95.  
  96.                 VoxMat vm = new VoxMat();
  97.  
  98.                 if (metallicTexture != null)
  99.                 {
  100.                     Color mc = metallicTexture.GetPixel(x, y);
  101.                     vm.metallic = mc.r;
  102.                     vm.smooth = mc.a;
  103.                 }
  104.                 if (emissionTexture != null)
  105.                 {
  106.                     Color ec = emissionTexture.GetPixel(x, y);
  107.                     if (ec != Color.black) vm.emission = true;
  108.                 }
  109.  
  110.                 colors.Add(c, vm);
  111.             }
  112.         }
  113.  
  114.         if (colors.Count > 256) InvalidateMesh();
  115.     }
  116.  
  117.     private Texture2D GetReadableTexture(Texture source)
  118.     {
  119.         if (source == null) return null;
  120.  
  121.         RenderTexture tmp = RenderTexture.GetTemporary(source.width, source.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
  122.         Graphics.Blit(source, tmp);
  123.  
  124.         RenderTexture previous = RenderTexture.active;
  125.         RenderTexture.active = tmp;
  126.  
  127.         Texture2D myTexture2D = new Texture2D(source.width, source.height);
  128.         myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
  129.         myTexture2D.Apply();
  130.  
  131.         RenderTexture.active = previous;
  132.         RenderTexture.ReleaseTemporary(tmp);
  133.  
  134.         return myTexture2D;
  135.     }
  136.  
  137.     private void BakeTextures()
  138.     {
  139.         if (colorTexture == null) return;
  140.  
  141.         Texture2D mt = new Texture2D(colorTexture.width, colorTexture.height, TextureFormat.RGBA32, false);
  142.         mt.filterMode = FilterMode.Point;
  143.  
  144.         Texture2D et = new Texture2D(colorTexture.width, colorTexture.height, TextureFormat.RGBA32, false);
  145.         et.filterMode = FilterMode.Point;
  146.  
  147.         for (int y = 0; y < colorTexture.height; y++)
  148.         {
  149.             for (int x = 0; x < colorTexture.width; x++)
  150.             {
  151.                 Color c = colorTexture.GetPixel(x, y);
  152.                 float m = colors[c].metallic;
  153.                 float s = colors[c].smooth;
  154.  
  155.                 mt.SetPixel(x, y, new Color(m, 0, 0, s));
  156.                 if (colors[c].emission)
  157.                     et.SetPixel(x, y, c);
  158.                 else
  159.                     et.SetPixel(x, y, Color.black);
  160.             }
  161.         }
  162.         mt.Apply();
  163.         et.Apply();
  164.  
  165.         rend.sharedMaterial.SetTexture("_MetallicGlossMap", mt);
  166.  
  167.         rend.sharedMaterial.EnableKeyword("_EMISSION");
  168.         rend.sharedMaterial.SetTexture("_EmissionMap", et);
  169.         rend.sharedMaterial.SetColor("_EmissionColor", Color.white);
  170.         rend.sharedMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.BakedEmissive;
  171.  
  172.         //Todo: Proper saving....
  173.         string n = rend.gameObject.transform.root.name;
  174.         AssetDatabase.CreateAsset(mt, "Assets/" + n + "_m.asset");
  175.         AssetDatabase.CreateAsset(et, "Assets/" + n + "_e.asset");
  176.         AssetDatabase.Refresh();
  177.     }
  178.  
  179.     private Vector2 scroll;
  180.  
  181.     private void OnGUI()
  182.     {
  183.         if (rend == null || colors.Keys.Count == 0)
  184.         {
  185.             GUILayout.Label("Plese Select Voxel Mesh");
  186.             return;
  187.         }
  188.  
  189.         if (GUILayout.Button("Bake Maps")) BakeTextures();
  190.  
  191.         List<Color> keys = new List<Color>(colors.Keys);
  192.  
  193.         scroll = GUILayout.BeginScrollView(scroll);
  194.  
  195.         foreach (Color c in keys)
  196.         {
  197.             GUILayout.BeginHorizontal();
  198.             GUI.backgroundColor = c;
  199.             GUILayout.Box(GUIContent.none, GUILayout.Width(25), GUILayout.Height(15));
  200.  
  201.             GUILayout.BeginHorizontal();
  202.             colors[c].metallic = GUILayout.HorizontalSlider(colors[c].metallic, 0, 1);
  203.             colors[c].smooth = GUILayout.HorizontalSlider(colors[c].smooth, 0, 1);
  204.             colors[c].emission = GUILayout.Toggle(colors[c].emission, GUIContent.none, GUILayout.Width(20));
  205.             GUILayout.EndHorizontal();
  206.  
  207.             GUILayout.EndHorizontal();
  208.         }
  209.  
  210.         GUILayout.EndScrollView();
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement