Example #1
0
        public int CompareTo(CompositeTexture other)
        {
            string A  = loadAssetAtGUID(textureAid).name;
            string oB = loadAssetAtGUID(other.id).name;

            return(A.CompareTo(oB));
        }
Example #2
0
        public static void enableTexture(string id, bool state)
        {
            CompositeTexture tex = csList.compositeTextures.Find(x => x.id == id);

            tex.enabled = state;
            texturesChanged.Invoke();
            EditorUtility.SetDirty(tex);
            AssetDatabase.SaveAssets();
        }
Example #3
0
        public static void removeTexture(CompositeTexture ct)
        {
            //CompositeTexture texture = csList.compositeTextures.Find(x => x.id == idToRemove);
            CompositeTexture tex = csList.compositeTextures.Find(x => x.id == ct.id);

            tex.Destroy();
            csList.compositeTextures.Remove(tex);
            texturesChanged.Invoke();
        }
Example #4
0
        public static void updateTexture(string id, string p_textureBID, float p_strength, CompositeModes mode)
        {
            CompositeTexture tex = csList.compositeTextures.Find(x => x.id == id);

            tex.textureBid    = p_textureBID;
            tex.strength      = p_strength;
            tex.compositeMode = mode;
            texturesChanged.Invoke();
            EditorUtility.SetDirty(tex);
            AssetDatabase.SaveAssets();
        }
Example #5
0
        public static void removeTexture(CompositeTexture p_ct)
        {
            Texture2D texA = p_ct.getTextureA();

            CompositeTextureData.removeTexture(p_ct);
            string assetPath = AssetDatabase.GetAssetPath(texA);

            updateAssetUserData(assetPath);
            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
            UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
        }
Example #6
0
        public static void addTexture(string p_id, Texture2D p_textureA, Texture2D p_textureB, float p_strength, CompositeModes mode)
        {
            CompositeTexture tex = CTScriptableObject.CreateMyAsset(p_id, p_textureA, p_textureB, p_strength, mode);

            if (validateTexture(tex))
            {
                csList.compositeTextures.Add(tex);
                //csList.compositeTextures.Sort();
                texturesChanged.Invoke();
                //Save();
            }
            else
            {
                EDebug.Log("texture invalid");
            }
        }
Example #7
0
        public static void Load()
        {
            var socList = AssetDatabase.FindAssets("t:CompositeTexture");

            //EDebug.Log("resources found " + socList.Count());

            csList.compositeTextures.Clear();
            foreach (var guid in socList)
            {
                String             path = AssetDatabase.GUIDToAssetPath(guid);
                UnityEngine.Object soc  = AssetDatabase.LoadAssetAtPath(path, typeof(CompositeTexture));

                CompositeTexture ct = soc as CompositeTexture;
                csList.compositeTextures.Add(ct);
            }
        }
        public static CompositeTexture CreateMyAsset(string p_id, Texture2D p_textureA, Texture2D p_textureB, float p_strength, CompositeModes mode)
        {
            CompositeTexture asset = ScriptableObject.CreateInstance <CompositeTexture>();

            //asset.setValues(texA, texB, str, compositeMode);

            asset.setValues(p_id, p_textureA, p_textureB, p_strength, mode);

            string g    = Guid.NewGuid().ToString();
            string name = g + ".asset";

            CreateFolders();
            AssetDatabase.CreateAsset(asset, "Assets/" + containingFolder + "/" + packageName + "/" + name);
            AssetDatabase.SaveAssets();

            return(asset);
        }
        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.
        }
Example #10
0
        public static CompositeTexture getTextureByAPath(string path)
        {
            CompositeTexture cts = csList.compositeTextures.Find(x => x.getPath() == path);

            return(cts);
        }
Example #11
0
        public static CompositeTexture getTexture(int id)
        {
            CompositeTexture cts = csList.compositeTextures.Find(x => x.getInstanceID() == id);

            return(cts);
        }
Example #12
0
        public static CompositeTexture getTexture(string id)
        {
            CompositeTexture cts = csList.compositeTextures.Find(x => x.id == id);

            return(cts);
        }
Example #13
0
        public static bool validateTexture(CompositeTexture tex)
        {
            bool exists = csList.compositeTextures.Exists(x => x.id == tex.id);

            return(!exists);
        }
        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();
        }
        private void ProcessTexture(CompositeTexture ct, Texture2D texture)
        {
            NormalToRoughness ntr = new NormalToRoughness();

            ntr.generateNormalToRoughnessTextureNew(ct, texture);
        }
        // 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);
            }
        }