public static void GenerateFXMap(Material targetMat, string title, string prop) { GUI.backgroundColor = EditorHelper.ButtonBackgroundActive; string buttonString = "Repack Textures into MaterialFX FX Map"; if (FXMapEnabled) { GUI.backgroundColor = EditorHelper.ButtonBackgroundDelete; buttonString = "Cancel"; } GUILayout.BeginHorizontal(); if (GUILayout.Button(buttonString)) { FXMapEnabled = !FXMapEnabled; } EditorHelper.HelpButton("prop" + "_help"); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); EditorHelper.HelpItem("prop" + "_help", "Text"); GUILayout.EndHorizontal(); GUI.backgroundColor = EditorHelper.backgroundDefault; if (FXMapEnabled == true) { GUILayout.BeginHorizontal("BOX"); Texture2D CheckTexture; GUILayout.BeginVertical(); GUILayout.Label("Snow"); CheckTexture = (Texture2D)EditorGUILayout.ObjectField("", tempMetallic, typeof(Texture2D), false, GUILayout.Width(EditorHelper.TextureWidth)); if (CheckTexture != tempMetallic) { tempMetallic = CheckTexture; } tempMetallicChannel = (EditorHelper.TextureChannels)EditorGUILayout.Popup((int)tempMetallicChannel, EditorHelper.textureChannelStrings, EditorHelper.PopUp, GUILayout.Width(70)); GUILayout.Space(5); GUILayout.EndVertical(); GUILayout.BeginVertical(); GUILayout.Label("Growth"); CheckTexture = (Texture2D)EditorGUILayout.ObjectField("", tempThick, typeof(Texture2D), false, GUILayout.Width(EditorHelper.TextureWidth)); if (CheckTexture != tempThick) { tempThick = CheckTexture; } tempThickChannel = (EditorHelper.TextureChannels)EditorGUILayout.Popup((int)tempThickChannel, EditorHelper.textureChannelStrings, EditorHelper.PopUp, GUILayout.Width(70)); GUILayout.Space(5); GUILayout.EndVertical(); GUILayout.BeginVertical(); GUILayout.Label("Wetness"); CheckTexture = (Texture2D)EditorGUILayout.ObjectField("", tempCurve, typeof(Texture2D), false, GUILayout.Width(EditorHelper.TextureWidth)); if (CheckTexture != tempCurve) { tempCurve = CheckTexture; } tempCurveChannel = (EditorHelper.TextureChannels)EditorGUILayout.Popup((int)tempCurveChannel, EditorHelper.textureChannelStrings, EditorHelper.PopUp, GUILayout.Width(70)); GUILayout.Space(5); GUILayout.EndVertical(); GUILayout.BeginVertical(); GUILayout.Label("Unused"); CheckTexture = (Texture2D)EditorGUILayout.ObjectField("", tempSmooth, typeof(Texture2D), false, GUILayout.Width(EditorHelper.TextureWidth)); if (CheckTexture != tempSmooth) { tempSmooth = CheckTexture; } tempSmoothChannel = (EditorHelper.TextureChannels)EditorGUILayout.Popup((int)tempSmoothChannel, EditorHelper.textureChannelStrings, EditorHelper.PopUp, GUILayout.Width(70)); GUILayout.Space(5); GUILayout.EndVertical(); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("Output size :", GUILayout.Width(75)); tempResolution = EditorGUILayout.Popup(tempResolution, new string[] { "256 x 256", "512 x 512", "1024 x 1024", "2048 x 2048", "4096 x 4096", "8192 x 8192" }, EditorHelper.PopUp, GUILayout.Width(100)); GUILayout.Label("Format :", GUILayout.Width(55)); tempFormat = EditorGUILayout.Popup(tempFormat, new string[] { "PNG", "JPEG" }, EditorHelper.PopUp, GUILayout.Width(80)); GUILayout.Space(5); GUILayout.EndHorizontal(); GUILayout.Space(5); if (GUILayout.Button("Generate " + targetMat.name + "_MaterialFX_FXMap")) { GenerateRepackedTexture(targetMat, tempMetallic, tempMetallicChannel, tempThick, tempThickChannel, tempCurve, tempCurveChannel, tempSmooth, tempSmoothChannel, targetMat.name + "_MaterialFX_FXMap", tempResolution, tempFormat, "_FXTex"); FXMapEnabled = false; } } }
public static void GenerateRepackedTexture(Material targetMat, Texture2D textureA, EditorHelper.TextureChannels channelA, Texture2D textureB, EditorHelper.TextureChannels channelB, Texture2D textureC, EditorHelper.TextureChannels channelC, Texture2D textureD, EditorHelper.TextureChannels channelD, string filename, int resolution, int format, string MatTex) { int res = 256; if (resolution == 1) { res = 512; } if (resolution == 2) { res = 1024; } if (resolution == 3) { res = 2048; } if (resolution == 4) { res = 4096; } if (resolution == 5) { res = 8192; } Texture2D emptyTex = new Texture2D(1, 1, TextureFormat.ARGB32, false); emptyTex.SetPixel(0, 0, new Color(0, 0, 0, 0)); int width = res; int height = res; string path = Path.GetDirectoryName(AssetDatabase.GetAssetPath(targetMat)); string file = path + "/" + filename + "."; if (format == 0) { file += "png"; } if (format == 1) { file += "jpg"; } Texture2D generatedTexture = new Texture2D(width, height, TextureFormat.ARGB32, false); Color[] outputColors = new Color[width * height]; Color blankColor = new Color(0, 0, 0, 0); for (int i = 0; i < width * height; i++) { outputColors[i] = blankColor; } RenderTexture previous = RenderTexture.active; for (int i = 0; i < 4; i++) { Texture2D targetTex = textureA; EditorHelper.TextureChannels channel = channelA; if (i == 1) { targetTex = textureB; channel = channelB; } if (i == 2) { targetTex = textureC; channel = channelC; } if (i == 3) { targetTex = textureD; channel = channelD; } if (targetTex == null) { targetTex = emptyTex; } int srcWidth = targetTex.width; int srcHeight = targetTex.height; RenderTexture tmp = RenderTexture.GetTemporary( srcWidth, srcHeight, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); Graphics.Blit(targetTex, tmp); RenderTexture.active = tmp; Texture2D readableTex = new Texture2D(srcWidth, srcHeight); readableTex.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0); readableTex.Apply(); Color[] channelColors = readableTex.GetPixels(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int sx = Mathf.FloorToInt((x * 1.0f) / (width * 1.0f) * (srcWidth * 1.0f)); int sy = Mathf.FloorToInt((y * 1.0f) / (height * 1.0f) * (srcHeight * 1.0f)); float chVal = 0; if (channel == EditorHelper.TextureChannels.Red) { chVal += channelColors[sy * srcWidth + sx].r; } if (channel == EditorHelper.TextureChannels.Green) { chVal += channelColors[sy * srcWidth + sx].g; } if (channel == EditorHelper.TextureChannels.Blue) { chVal += channelColors[sy * srcWidth + sx].b; } if (channel == EditorHelper.TextureChannels.Alpha) { chVal += channelColors[sy * srcWidth + sx].a; } if (channel == EditorHelper.TextureChannels.RGB || channel == EditorHelper.TextureChannels.Gray) { chVal += (channelColors[sy * srcWidth + sx].r + channelColors[sy * srcWidth + sx].g + channelColors[sy * srcWidth + sx].b) / 3f; } if (channel == EditorHelper.TextureChannels.RGBA) { chVal += (channelColors[sy * srcWidth + sx].r + channelColors[sy * srcWidth + sx].g + channelColors[sy * srcWidth + sx].b + channelColors[sy * srcWidth + sx].a) / 4f; } if (i == 0) { outputColors[y * width + x].r = chVal; } if (i == 1) { outputColors[y * width + x].g = chVal; } if (i == 2) { outputColors[y * width + x].b = chVal; } if (i == 3) { outputColors[y * width + x].a = chVal; } } } RenderTexture.active = previous; RenderTexture.ReleaseTemporary(tmp); } generatedTexture.SetPixels(outputColors); SaveTexture(generatedTexture, file); Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath(file, typeof(Texture2D)); targetMat.SetTexture(MatTex, t); }