void OnPostprocessTexture(Texture2D texture) { bool isCompositeTexture = false; try { //id = CompositeTexture.assetToGUID(texture); int instanceID = AssetDatabase.LoadMainAssetAtPath(assetPath).GetInstanceID(); EDebug.Log("OnPostProcessTexture " + instanceID); if (CompositeTextureData.ExistsByInstanceID(instanceID) && CompositeTextureData.getTexture(instanceID).enabled) { EDebug.Log("is an enabled composite texture"); isCompositeTexture = true; ct = CompositeTextureData.getTexture(instanceID); } } catch (System.Exception) { EDebug.Log("exception in texture processor"); } if (!isCompositeTexture) { return; } EDebug.Log("do work"); NormalToRoughness nr = new NormalToRoughness(); ProcessTexture(ct, texture); ct.getTextureA().setReadable(false); // Instead of setting pixels for each mip map levels, you can also // modify only the pixels in the highest mip level. And then simply use // texture.Apply(true); to generate lower mip levels. }
static string[] OnWillSaveAssets(string[] paths) { if (canSave) { return(paths); } else { //foreach (string path in paths) //{ // EDebug.Log(path); //} var myPaths = paths.Except(seenPaths); foreach (string path in paths) { if (path.EndsWith(".mat")) { //EDebug.Log(path); Material mat = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material)); string matID = AssetDatabase.AssetPathToGUID(path); int?rTextureID = null; int?nTextureID = null; rTextureID = getTextureIDFromMat(mat, "_SpecGlossMap"); nTextureID = getTextureIDFromMat(mat, "_BumpMap"); if (matPriors.Contains(matID)) { if (rTextureID != (((MaterialPrior)matPriors[matID]).rTextureID)) { EDebug.Log("ROUGHNESS changed @" + mat.name); } if (nTextureID != (((MaterialPrior)matPriors[matID]).nTextureID)) { EDebug.Log("NORMAL changed @" + mat.name); } } // log this as a prior matPriors.Remove(matID); matPriors.Add(matID, new MaterialPrior(matID, rTextureID, nTextureID)); //EDebug.Log(rTextureID + " " + nTextureID); } } // these are our UNSAVED CHANGES in the project. // these operations occour between actaul saves... // check if either the normal or roughness map was changed // foreach (string path in myPaths) { EDebug.Log("watching for changes @" + path); } seenPaths = paths; string[] empty = new string[0]; return(empty); } }
void OnGUI() { EditorGUILayout.BeginVertical("box", GUILayout.Height(150f)); GUILayout.Label("New Composite Texture", EditorStyles.boldLabel); matTemplate = EditorGUILayout.ObjectField("Import from material", matTemplate, typeof(Material), true) as Material; GUIStyle style = new GUIStyle(GUI.skin.label); style.alignment = TextAnchor.UpperCenter; style.fixedWidth = 70; EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginVertical(); GUILayout.Label("Texture A", style); textureAToAdd = EditorGUILayout.ObjectField("", textureAToAdd, typeof(Texture2D), false, GUILayout.Width(70), GUILayout.Height(70)) as Texture2D; EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(); GUILayout.Label("Texture B", style); textureBToAdd = EditorGUILayout.ObjectField("", textureBToAdd, typeof(Texture2D), false, GUILayout.Width(70), GUILayout.Height(70)) as Texture2D; EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(); GUILayout.Label("Mode"); cmToAdd = (CompositeModes)EditorGUILayout.Popup("", (int)cmToAdd, modesAsArray, GUILayout.Width(170)); string texA_guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(textureAToAdd)); // create texture bool texutresInPlace = false; if (textureAToAdd != null && textureBToAdd != null) { if (textureAToAdd.height == textureBToAdd.height && textureAToAdd.width == textureBToAdd.width) { texutresInPlace = true; } else { texutresInPlace = true; EDebug.Log("textures not of same size"); EditorGUILayout.HelpBox("Texture dimensions don't match", type: MessageType.Info); } } EditorGUI.BeginDisabledGroup(texutresInPlace == false); if (GUILayout.Button("Create Composite", GUILayout.Width(170))) { string errorMsg = CompositeTextureController.createCompositeTexture(texA_guid, textureAToAdd, textureBToAdd, 1f, cmToAdd); matTemplate = null; // clear the previous fields after creating a new CT textureAToAdd = null; textureBToAdd = null; lastAddedCT_ID = texA_guid; //shouldScroll = true; EDebug.Log(errorMsg); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); ////////////////// MATERIAL TYPE DETECTION if (matTemplate != null) { textureBToAdd = (Texture2D)matTemplate.GetTexture("_BumpMap"); String shaderName = matTemplate.shader.name; // shaderName Standard (Roughness setup), Standard, Standard (Specular setup) // //Debug.Log("shadername " + shaderName); if (shaderName == "Standard") { cmToAdd = CompositeModes.normalToRoughnessRGB; } else if (shaderName == "Standard (Specular setup)") { cmToAdd = CompositeModes.standardShaderSpecularSetup; } else if (shaderName == "Standard (Roughness setup)") { cmToAdd = CompositeModes.normalToRoughnessAlpha; } if (matTemplate.IsKeywordEnabled("_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A")) { textureAToAdd = (Texture2D)matTemplate.GetTexture("_MainTex"); } else if (matTemplate.HasProperty("_SpecGlossMap")) { // standard and roughness setup have this if (matTemplate.GetTexture("_SpecGlossMap") != null) { textureAToAdd = (Texture2D)matTemplate.GetTexture("_SpecGlossMap"); } } else if (matTemplate.HasProperty("_MetallicGlossMap")) { if (matTemplate.GetTexture("_MetallicGlossMap") != null) { textureAToAdd = (Texture2D)matTemplate.GetTexture("_MetallicGlossMap"); } } else { textureAToAdd = null; textureBToAdd = null; } matTemplate = null; //clear material area } EditorGUILayout.EndVertical(); List <CompositeTexture> removalList = new List <CompositeTexture>(); float scrollWidth = position.width; float scrollHeight = position.height - 160f; scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(scrollWidth), GUILayout.Height(scrollHeight)); float itemHeight = 135f; List <CompositeTexture> ctsAll = CompositeTextureData.getTextures(); List <CompositeTexture> cts = GetCTinUIVisible(ctsAll, (int)itemHeight, (int)scrollPos.y); int fullScrollableHeight = (int)itemHeight * ctsAll.Count; GUILayout.BeginVertical("", GUIStyle.none, GUILayout.Height(fullScrollableHeight)); int firstindex = (int)(scrollPos.y / itemHeight); GUILayout.Space(firstindex * itemHeight); foreach (CompositeTexture ct in cts) { if (ct.filesExist() == false) { EDebug.Log("missing a file"); continue; } Texture2D texA = ct.getTextureA(); Texture2D texB = ct.getTextureB(); Texture2D texPreview = null; if (!AssetPreview.IsLoadingAssetPreview(texA.GetInstanceID())) { texPreview = AssetPreview.GetMiniThumbnail(texA); } Rect lastRect2 = EditorGUILayout.BeginVertical("box", GUILayout.Height(itemHeight)); GUIStyle lab = EditorStyles.label; if (lastAddedCT_ID == ct.id) { lab = EditorStyles.boldLabel; } if (lastAddedCT_ID == ct.id && shouldScroll) { if (lastRect2.y > 0f) { //lastAddedCT_ID = null; EDebug.Log(lastRect2.y); scrollPos = lastRect2.position; shouldScroll = false; } } ///////// GUILayoutOption[] ctrlButtonOptions = new GUILayoutOption[1]; ctrlButtonOptions[0] = GUILayout.Width(20f); GUILayoutOption[] toggleOptions = new GUILayoutOption[1]; toggleOptions[0] = GUILayout.Width(11f); EditorGUILayout.BeginHorizontal(); var toggle = GUILayout.Toggle(ct.enabled, "", toggleOptions); GUILayout.Label(texA.name, lab); if (GUILayout.Button("X", ctrlButtonOptions)) { removalList.Add(ct); } EditorGUILayout.EndHorizontal(); if (toggle != ct.enabled) { if (ct.enabled == true) { CompositeTextureController.enableTexture(ct.id, false); } else { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); CompositeTextureController.enableTexture(ct.id, true); sw.Stop(); EDebug.Log("ELAPSED " + sw.Elapsed.Milliseconds); } } //if (GUILayout.Button("mark unreadable")) //{ // texB.setReadable(true); // String assetPath = AssetDatabase.GetAssetPath(texB); // AssetDatabase.ImportAsset(assetPath); //} EditorGUI.BeginDisabledGroup(ct.enabled == false); EditorGUILayout.BeginHorizontal(); GUILayout.Box(texPreview, GUILayout.Width(105), GUILayout.Height(105)); EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUI.BeginChangeCheck(); //int texB_ID = EditorGUILayout.ObjectField("composite texture", texB, typeof(Texture2D), true).GetInstanceID(); string texB_ID = CompositeTexture.assetToGUID((Texture2D)EditorGUILayout.ObjectField("composite texture", texB, typeof(Texture2D), true)); CompositeModes cm = (CompositeModes)EditorGUILayout.Popup("Composit mode", (int)ct.compositeMode, modesAsArray); float strength = EditorGUILayout.FloatField("Strength", ct.strength); if (EditorGUI.EndChangeCheck()) { ct.update(texB_ID, strength, cm); CompositeTextureController.enableTexture(ct.id, true); EDebug.Log("value changed! " + texA.name); } EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); EditorGUI.EndDisabledGroup(); EditorGUILayout.EndVertical(); } GUILayout.EndVertical(); foreach (CompositeTexture ctr in removalList) { CompositeTextureController.removeTexture(ctr); } EditorGUILayout.EndScrollView(); }
static void refresh() { EDebug.Log("refresh requested by GUI"); }
public TexelIndex(Color[] texColors) { int mipLevels = (int)(Math.Log(texColors.Length) / Math.Log(4.0)); EDebug.Log("TI miplevels " + mipLevels); int pixelCount = texColors.Length; //setup texels texelLevels = new TexelLevel[mipLevels]; for (int i = 0; i < texelLevels.Length; i++) { texelLevels[i] = new TexelLevel(pixelCount); pixelCount /= 4; } int sideDimension = (int)Math.Sqrt(texColors.Length); //256 becomes 16 , the side dimension for (int i = 0; i < texColors.Length; i += sideDimension * 2) { for (int j = 0; j < sideDimension; j += 2) { int l = j + i; Color[] targetC = new Color[4]; targetC[0] = texColors[l]; targetC[1] = texColors[l + 1]; targetC[2] = texColors[l + sideDimension]; targetC[3] = texColors[l + sideDimension + 1]; Texel tex = new TexelCalculation(targetC).getTexel(); texelLevels[0].addTexel(tex); } } TexelOps to = new TexelOps(); Texel[] target = new Texel[4]; for (int i = 0; i < texelLevels.Length - 1; i++) // for each level (except the last one) { TexelLevel texelLevel = texelLevels[i]; int texelCount = texelLevels[i].getTexelCount(); int mipSideDimension = (int)Math.Sqrt(texelCount); int capacity = (mipSideDimension ^ 2) / 4; //Debug.Log("mip side dimension : " + mipSideDimension * 2); // combine 4 texels together and add the resulting texel to the next level for (int j = 0; j < texelCount; j += mipSideDimension * 2) { for (int k = 0; k < mipSideDimension; k += 2) { int l = j + k; target[0] = texelLevel.getTexel(l); target[1] = texelLevel.getTexel(l + 1); target[2] = texelLevel.getTexel(l + mipSideDimension); target[3] = texelLevel.getTexel(l + mipSideDimension + 1); Texel combinedTex = to.CombineTexels(target); // add combined texel to next texelLevels[i + 1].addTexel(combinedTex); capacity++; } } } }
// the latest method for generating public void generateNormalToRoughnessTextureNew(CompositeTexture ct, Texture2D texC) { Texture2D texA = texC; Texture2D texB = ct.getTextureB(); // handle user deleting normalmap texture in GUI if (texA == null || texB == null) { return; } Texture2D LoadedImage = new Texture2D(texB.width, texB.height, texB.format, true); LoadedImage.LoadRawTextureData(texB.GetRawTextureData()); // resize the textures if unevenly matched if (texB.mipmapCount < texA.mipmapCount) { EDebug.Log("sizing " + texA.width); // RGBA32, ARGB32, RGB24, RGBAFloat or RGBAHalf texB.filterMode = FilterMode.Trilinear; RenderTexture rt = RenderTexture.GetTemporary(texA.width, texA.height); rt.filterMode = FilterMode.Trilinear; RenderTexture.active = rt; Graphics.Blit(texB, rt); Texture2D nTex = new Texture2D(texA.width, texA.height, TextureFormat.ARGB32, false); nTex.ReadPixels(new Rect(0, 0, texA.width, texA.height), 0, 0); RenderTexture.active = null; texB = nTex; } else { texB = LoadedImage; // this makes the texture readable } int mipLevels = texA.mipmapCount; texelIndex = new TexelIndex(texB.GetPixels()); // do different things depending on blending mode requested if (ct.compositeMode == CompositeModes.normalToRoughnessAlpha) { bt = delegate(Color roughness, float normalStdDev) // standard shader roughness mode calculation { normalStdDev *= ct.strength; return(new Color(roughness.r + normalStdDev, roughness.g + normalStdDev, roughness.b + normalStdDev, 1.0f)); }; } else if (ct.compositeMode == CompositeModes.normalToRoughnessRGB) { bt = delegate(Color roughness, float normalStdDev) // standard shader w/ metalic alpha { normalStdDev *= ct.strength; return(new Color(roughness.r, roughness.g, roughness.b, roughness.a - (2 * normalStdDev))); }; } else if (ct.compositeMode == CompositeModes.standardShaderSpecularSetup) { bt = delegate(Color roughness, float normalStdDev) // standard (specular setup) { normalStdDev *= ct.strength; return(new Color(roughness.r, roughness.g, roughness.b, roughness.a - (2 * normalStdDev))); //return new Color(roughness.r, roughness.g, roughness.b, roughness.a + (2 * normalStdDev)); }; } int mipDelta = texB.mipmapCount - texA.mipmapCount; EDebug.Log("mipdelta: " + mipDelta); int startingMipLevel = 1; // start from here for equally sized textures if (mipDelta > 0) { startingMipLevel = 0; } if (mipDelta < 0) { mipDelta = 0; } for (int mipLevel = startingMipLevel; mipLevel < mipLevels - mipDelta; mipLevel++) { int mipSize = texA.width / (int)Math.Pow(2, mipLevel); //EDebug.Log("generating rougness mipsize:" + mipSize); Color32[] values = generateNormalToRoughnessValues(mipSize, mipLevel, rTexture: texA, p_mipDelta: mipDelta); texC.SetPixels32(values, mipLevel); } }