Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- public class EditorVoxMaps : EditorWindow
- {
- [MenuItem("Tools/Voxel Material")]
- private static void ShowEditor()
- {
- EditorVoxMaps win = EditorWindow.GetWindow<EditorVoxMaps>("Voxel Material");
- win.Show();
- }
- private class VoxMat
- {
- public float metallic;
- public float smooth;
- public bool emission;
- }
- private Dictionary<Color, VoxMat> colors = new Dictionary<Color, VoxMat>();
- private Renderer rend;
- private Texture2D colorTexture;
- private Texture2D metallicTexture;
- private Texture2D emissionTexture;
- private void Awake()
- {
- OnSelectionChange();
- }
- private void OnInspectorUpdate()
- {
- Repaint();
- }
- private void InvalidateMesh()
- {
- colors.Clear();
- rend = null;
- colorTexture = null;
- metallicTexture = null;
- emissionTexture = null;
- }
- private void OnSelectionChange()
- {
- InvalidateMesh();
- GameObject go = Selection.activeGameObject;
- if (go == null) return;
- rend = go.GetComponent<Renderer>();
- if (rend == null) return;
- UpdateSelected();
- }
- private void UpdateSelected()
- {
- colorTexture = GetReadableTexture(rend.sharedMaterial.mainTexture);
- if (colorTexture == null)
- {
- InvalidateMesh();
- return;
- }
- metallicTexture = GetReadableTexture(rend.sharedMaterial.GetTexture("_MetallicGlossMap"));
- if (metallicTexture != null)
- {
- if (metallicTexture.width != colorTexture.width || metallicTexture.height != colorTexture.height)
- {
- InvalidateMesh();
- return;
- }
- }
- emissionTexture = GetReadableTexture(rend.sharedMaterial.GetTexture("_EmissionMap"));
- if (emissionTexture != null)
- {
- if (emissionTexture.width != colorTexture.width || emissionTexture.height != colorTexture.height)
- {
- InvalidateMesh();
- return;
- }
- }
- for (int y = 0; y < colorTexture.height; y++)
- {
- for (int x = 0; x < colorTexture.width; x++)
- {
- Color c = colorTexture.GetPixel(x, y);
- if (colors.ContainsKey(c)) continue;
- VoxMat vm = new VoxMat();
- if (metallicTexture != null)
- {
- Color mc = metallicTexture.GetPixel(x, y);
- vm.metallic = mc.r;
- vm.smooth = mc.a;
- }
- if (emissionTexture != null)
- {
- Color ec = emissionTexture.GetPixel(x, y);
- if (ec != Color.black) vm.emission = true;
- }
- colors.Add(c, vm);
- }
- }
- if (colors.Count > 256) InvalidateMesh();
- }
- private Texture2D GetReadableTexture(Texture source)
- {
- if (source == null) return null;
- RenderTexture tmp = RenderTexture.GetTemporary(source.width, source.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
- Graphics.Blit(source, tmp);
- RenderTexture previous = RenderTexture.active;
- RenderTexture.active = tmp;
- Texture2D myTexture2D = new Texture2D(source.width, source.height);
- myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
- myTexture2D.Apply();
- RenderTexture.active = previous;
- RenderTexture.ReleaseTemporary(tmp);
- return myTexture2D;
- }
- private void BakeTextures()
- {
- if (colorTexture == null) return;
- Texture2D mt = new Texture2D(colorTexture.width, colorTexture.height, TextureFormat.RGBA32, false);
- mt.filterMode = FilterMode.Point;
- Texture2D et = new Texture2D(colorTexture.width, colorTexture.height, TextureFormat.RGBA32, false);
- et.filterMode = FilterMode.Point;
- for (int y = 0; y < colorTexture.height; y++)
- {
- for (int x = 0; x < colorTexture.width; x++)
- {
- Color c = colorTexture.GetPixel(x, y);
- float m = colors[c].metallic;
- float s = colors[c].smooth;
- mt.SetPixel(x, y, new Color(m, 0, 0, s));
- if (colors[c].emission)
- et.SetPixel(x, y, c);
- else
- et.SetPixel(x, y, Color.black);
- }
- }
- mt.Apply();
- et.Apply();
- rend.sharedMaterial.SetTexture("_MetallicGlossMap", mt);
- rend.sharedMaterial.EnableKeyword("_EMISSION");
- rend.sharedMaterial.SetTexture("_EmissionMap", et);
- rend.sharedMaterial.SetColor("_EmissionColor", Color.white);
- rend.sharedMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.BakedEmissive;
- //Todo: Proper saving....
- string n = rend.gameObject.transform.root.name;
- AssetDatabase.CreateAsset(mt, "Assets/" + n + "_m.asset");
- AssetDatabase.CreateAsset(et, "Assets/" + n + "_e.asset");
- AssetDatabase.Refresh();
- }
- private Vector2 scroll;
- private void OnGUI()
- {
- if (rend == null || colors.Keys.Count == 0)
- {
- GUILayout.Label("Plese Select Voxel Mesh");
- return;
- }
- if (GUILayout.Button("Bake Maps")) BakeTextures();
- List<Color> keys = new List<Color>(colors.Keys);
- scroll = GUILayout.BeginScrollView(scroll);
- foreach (Color c in keys)
- {
- GUILayout.BeginHorizontal();
- GUI.backgroundColor = c;
- GUILayout.Box(GUIContent.none, GUILayout.Width(25), GUILayout.Height(15));
- GUILayout.BeginHorizontal();
- colors[c].metallic = GUILayout.HorizontalSlider(colors[c].metallic, 0, 1);
- colors[c].smooth = GUILayout.HorizontalSlider(colors[c].smooth, 0, 1);
- colors[c].emission = GUILayout.Toggle(colors[c].emission, GUIContent.none, GUILayout.Width(20));
- GUILayout.EndHorizontal();
- GUILayout.EndHorizontal();
- }
- GUILayout.EndScrollView();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement