Exemplo n.º 1
0
        public void Compile(Material m, string shaderName = null)
        {
            int hash = 0;

            MicroSplatKeywords keywords = MicroSplatUtilities.FindOrCreateKeywords(m);

            for (int i = 0; i < keywords.keywords.Count; ++i)
            {
                hash += 31 + keywords.keywords [i].GetHashCode();
            }
            var    path = AssetDatabase.GetAssetPath(m.shader);
            string nm   = m.shader.name;

            if (!string.IsNullOrEmpty(shaderName))
            {
                nm = shaderName;
            }
            string baseName = "Hidden/" + nm + "_Base" + hash.ToString();

            string terrainShader = Compile(keywords.keywords.ToArray(), nm, baseName);

            if (renderLoop != null)
            {
                keywords.EnableKeyword(renderLoop.GetRenderLoopKeyword());
            }

            GenerateAuxShaders(nm, path, keywords.keywords);

            MicroSplatUtilities.Checkout(path);
            System.IO.File.WriteAllText(path, terrainShader);

            if (!keywords.IsKeywordEnabled("_MICROMESH") && !keywords.IsKeywordEnabled("_MICROVERTEXMESH") && !keywords.IsKeywordEnabled("_MEGASPLAT") && !keywords.IsKeywordEnabled("_MICROTERRAINMESH"))
            {
                // generate fallback
                string[] oldKeywords = new string[keywords.keywords.Count];
                System.Array.Copy(keywords.keywords.ToArray(), oldKeywords, keywords.keywords.Count);
                keywords.DisableKeyword("_TESSDISTANCE");
                keywords.DisableKeyword("_PARALLAX");
                keywords.DisableKeyword("_DETAILNOISE");
                keywords.EnableKeyword("_MICROSPLATBASEMAP");

                string fallback = Compile(keywords.keywords.ToArray(), baseName);
                keywords.keywords = new List <string> (oldKeywords);
                string fallbackPath = path.Replace(".shader", "_Base.shader");
                MicroSplatUtilities.Checkout(fallbackPath);
                System.IO.File.WriteAllText(fallbackPath, fallback);
            }


            EditorUtility.SetDirty(m);
            AssetDatabase.Refresh();
#if __MICROSPLAT_MESH__
            MicroSplatMesh.ClearMaterialCache();
#endif
            MicroSplatObject.SyncAll();
        }
Exemplo n.º 2
0
    public static void SyncAll()
    {
        MicroSplatTerrain.SyncAll();
#if __MICROSPLAT_MESHTERRAIN__
        MicroSplatMeshTerrain.SyncAll();
#endif
#if __MICROSPLAT_MESH__
        MicroSplatMesh.SyncAll();
#endif
    }
    public static void SyncAll()
    {
        MicroSplatTerrain.SyncAll();
#if __MICROSPLAT_MESHTERRAIN__
        MicroSplatMeshTerrain.SyncAll();
#endif
#if __MICROSPLAT_MESH__
        MicroSplatMesh.SyncAll();
        MicroSplatVertexMesh.SyncAll();
#endif
#if __MICROSPLAT_POLARIS__
        MicroSplatPolarisMesh.SyncAll();
#endif
    }
        public void Revert(MicroSplatObject mso)
        {
            MicroSplatTerrain t = mso as MicroSplatTerrain;

            if (t != null)
            {
                if (t.templateMaterial == null)
                {
                    Debug.LogError("MicroSplatTerrain " + mso.gameObject.name + " does not have template material");
                }
                else
                {
                    RevertTerrainSplats(t);
                }
            }

#if __MICROSPLAT_MESH__
            MicroSplatMesh msm = mso as MicroSplatMesh;
            if (msm != null)
            {
                foreach (var subMesh in msm.subMeshEntries)
                {
                    foreach (var tex in subMesh.subMeshOverride.controlTextures)
                    {
                        UncompressTexture(tex);
                    }
                }
            }
#endif

#if __MICROSPLAT_PROCTEX__
            UncompressTexture(mso.procBiomeMask);
            UncompressTexture(mso.procBiomeMask2);
            UncompressTexture(mso.templateMaterial, "_ProcTexBiomeMask");
            UncompressTexture(mso.templateMaterial, "_ProcTexBiomeMask2");
#endif
#if __MICROSPLAT_SNOW__
            UncompressTexture(mso.snowMaskOverride);
            UncompressTexture(mso.templateMaterial, "_SnowMask");
#endif
#if __MICROSPLAT_STREAMS__
            UncompressTexture(mso.streamTexture);
            UncompressTexture(mso.templateMaterial, "_StreamControl");
#endif
#if __MICROSPLAT_GLOBALTEXTURE__
            UncompressTexture(mso.streamTexture);
            UncompressTexture(mso.templateMaterial, "_GlobalTintTex");
#endif
        }
Exemplo n.º 5
0
    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
    {
        if (cachedTitle == null)
        {
            cachedTitle = "Shader Generator        v:" + MicroSplatVersion;
        }
        if (GUI.enabled == false)
        {
            EditorGUILayout.HelpBox("You must edit the template material, not the instance being used", MessageType.Info);
            return;
        }
        EditorGUI.BeginChangeCheck(); // sync materials
        Material       targetMat = materialEditor.target as Material;
        Texture2DArray diff      = targetMat.GetTexture("_Diffuse") as Texture2DArray;

        var keywordSO = MicroSplatUtilities.FindOrCreateKeywords(targetMat);

        compiler.Init();
        // must unpack everything before the generator draws- otherwise we get IMGUI errors
        for (int i = 0; i < compiler.extensions.Count; ++i)
        {
            var ext = compiler.extensions[i];
            ext.Unpack(keywordSO.keywords.ToArray());
        }

        string shaderName = targetMat.shader.name;

        DrawModules();

        EditorGUI.BeginChangeCheck(); // needs compile

        if (MicroSplatUtilities.DrawRollup(cachedTitle))
        {
            shaderName = EditorGUILayout.DelayedTextField(CShaderName, shaderName);

            if (DrawRenderLoopGUI(keywordSO, targetMat))
            {
                needsCompile = true;
            }

            for (int i = 0; i < compiler.extensions.Count; ++i)
            {
                var e = compiler.extensions[i];
                if (e.GetVersion() == MicroSplatVersion)
                {
                    //using (new GUILayout.VerticalScope(GUI.skin.box))
                    {
                        e.DrawFeatureGUI(keywordSO);
                    }
                }
                else
                {
                    EditorGUILayout.HelpBox("Extension : " + e + " is version " + e.GetVersion() + " and MicroSplat is version " + MicroSplatVersion + ", please update", MessageType.Error);
                }
            }

            for (int i = 0; i < availableRenderLoops.Count; ++i)
            {
                var rl = availableRenderLoops[i];
                if (rl.GetVersion() != MicroSplatVersion)
                {
                    EditorGUILayout.HelpBox("Render Loop : " + rl.GetDisplayName() + " is version " + rl.GetVersion() + " and MicroSplat is version " + MicroSplatVersion + ", please update", MessageType.Error);
                }
            }
        }
        needsCompile = needsCompile || EditorGUI.EndChangeCheck();

        int featureCount = keywordSO.keywords.Count;

        // note, ideally we wouldn't draw the GUI for the rest of stuff if we need to compile.
        // But we can't really do that without causing IMGUI to split warnings about
        // mismatched GUILayout blocks
        if (!needsCompile)
        {
            for (int i = 0; i < compiler.extensions.Count; ++i)
            {
                var ext = compiler.extensions[i];
                if (ext.GetVersion() == MicroSplatVersion)
                {
                    ext.DrawShaderGUI(this, keywordSO, targetMat, materialEditor, props);
                }
                else
                {
                    EditorGUILayout.HelpBox("Extension : " + ext + " is version " + ext.GetVersion() + " and MicroSplat is version " + MicroSplatVersion + ", please update so that all modules are using the same version.", MessageType.Error);
                }
            }


            if (diff != null && MicroSplatUtilities.DrawRollup("Per Texture Properties"))
            {
                var propTex = FindOrCreatePropTex(targetMat);
                perTexIndex = MicroSplatUtilities.DrawTextureSelector(perTexIndex, diff);
                for (int i = 0; i < compiler.extensions.Count; ++i)
                {
                    var ext = compiler.extensions[i];
                    if (ext.GetVersion() == MicroSplatVersion)
                    {
                        ext.DrawPerTextureGUI(perTexIndex, keywordSO, targetMat, propTex);
                    }
                }
            }
        }

        if (!needsCompile)
        {
            if (featureCount != keywordSO.keywords.Count)
            {
                needsCompile = true;
            }
        }


        int arraySampleCount   = 0;
        int textureSampleCount = 0;
        int maxSamples         = 0;
        int tessSamples        = 0;
        int depTexReadLevel    = 0;

        builder.Length = 0;
        for (int i = 0; i < compiler.extensions.Count; ++i)
        {
            var ext = compiler.extensions[i];
            if (ext.GetVersion() == MicroSplatVersion)
            {
                ext.ComputeSampleCounts(keywordSO.keywords.ToArray(), ref arraySampleCount, ref textureSampleCount, ref maxSamples, ref tessSamples, ref depTexReadLevel);
            }
        }
        if (MicroSplatUtilities.DrawRollup("Debug"))
        {
            string shaderModel = compiler.GetShaderModel(keywordSO.keywords.ToArray());
            builder.Append("Shader Model : ");
            builder.AppendLine(shaderModel);
            if (maxSamples != arraySampleCount)
            {
                builder.Append("Texture Array Samples : ");
                builder.AppendLine(arraySampleCount.ToString());

                builder.Append("Regular Samples : ");
                builder.AppendLine(textureSampleCount.ToString());
            }
            else
            {
                builder.Append("Texture Array Samples : ");
                builder.AppendLine(arraySampleCount.ToString());
                builder.Append("Regular Samples : ");
                builder.AppendLine(textureSampleCount.ToString());
            }
            if (tessSamples > 0)
            {
                builder.Append("Tessellation Samples : ");
                builder.AppendLine(tessSamples.ToString());
            }
            if (depTexReadLevel > 0)
            {
                builder.Append(depTexReadLevel.ToString());
                builder.AppendLine(" areas with dependent texture reads");
            }

            EditorGUILayout.HelpBox(builder.ToString(), MessageType.Info);
        }

        if (EditorGUI.EndChangeCheck() && !needsCompile)
        {
            MicroSplatTerrain.SyncAll();
#if __MICROSPLAT_MESH__
            MicroSplatMesh.SyncAll();
#endif
        }

        if (needsCompile)
        {
            needsCompile = false;
            keywordSO.keywords.Clear();
            for (int i = 0; i < compiler.extensions.Count; ++i)
            {
                compiler.extensions[i].Pack(keywordSO);
            }
            if (compiler.renderLoop != null)
            {
                keywordSO.EnableKeyword(compiler.renderLoop.GetRenderLoopKeyword());
            }

            // horrible workaround to GUI warning issues
            compileMat     = targetMat;
            compileName    = shaderName;
            targetCompiler = compiler;
            EditorApplication.delayCall += TriggerCompile;
        }
    }
        public void Compress(MicroSplatObject mso, Options opt)
        {
            MicroSplatTerrain t = mso as MicroSplatTerrain;

            if (t != null)
            {
                if (t.templateMaterial == null)
                {
                    Debug.LogError("MicroSplatTerrain " + mso.gameObject.name + " does not have template material");
                }
                else
                {
                    if (opt.splatMaps)
                    {
                        CompressTerrainSplats(t);
                    }
                }
            }

#if __MICROSPLAT_MESH__
            MicroSplatMesh msm = mso as MicroSplatMesh;
            if (msm != null)
            {
                foreach (var subMesh in msm.subMeshEntries)
                {
                    foreach (var tex in subMesh.subMeshOverride.controlTextures)
                    {
                        CompressTexture(tex, false);
                    }
                }
            }
#endif

#if __MICROSPLAT_PROCTEX__
            if (opt.biomeMask)
            {
                CompressTexture(mso.procBiomeMask, false);
                CompressTexture(mso.procBiomeMask2, false);
                CompressTexture(mso.templateMaterial, "_ProcTexBiomeMask", false);
                CompressTexture(mso.templateMaterial, "_ProcTexBiomeMask2", false);
            }
#endif
#if __MICROSPLAT_SNOW__
            if (opt.snowMask)
            {
                CompressTexture(mso.snowMaskOverride, false);
                CompressTexture(mso.templateMaterial, "_SnowMask", false);
            }
#endif
#if __MICROSPLAT_STREAMS__
            if (opt.streamMaps)
            {
                CompressTexture(mso.streamTexture, false);
                CompressTexture(mso.templateMaterial, "_StreamControl", false);
            }
#endif
#if __MICROSPLAT_GLOBALTEXTURE__
            if (opt.tintMap)
            {
                CompressTexture(mso.streamTexture, true);
                CompressTexture(mso.templateMaterial, "_GlobalTintTex", true);
            }
#endif
        }