Пример #1
0
    public static Shader NewShader()
    {
        string path = "Assets";

        foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
        {
            path = AssetDatabase.GetAssetPath(obj);
            if (System.IO.File.Exists(path))
            {
                path = System.IO.Path.GetDirectoryName(path);
            }
            break;
        }
        path = path.Replace("\\", "/");
        path = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.shader");
        string name = path.Substring(path.LastIndexOf("/"));

        name = name.Substring(0, name.IndexOf("."));
        MicroSplatCompiler compiler = new MicroSplatCompiler();

        compiler.Init();
        string ret = compiler.Compile(new string[1] {
            "_MSRENDERLOOP_SURFACESHADER"
        }, name, name);

        System.IO.File.WriteAllText(path, ret);
        AssetDatabase.Refresh();
        return(AssetDatabase.LoadAssetAtPath <Shader> (path));
    }
    public static Material NewShaderAndMaterial(string path, string name)
    {
        string shaderPath     = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.shader");
        string shaderBasePath = shaderPath.Replace(".shader", "_Base.shader");
        string matPath        = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.mat");

        MicroSplatCompiler compiler = new MicroSplatCompiler();

        compiler.Init();

        string baseName = "Hidden/MicroSplat/" + name + "_Base";

        string baseShader    = compiler.Compile(new string[0], baseName);
        string regularShader = compiler.Compile(new string[0], name, baseName);

        System.IO.File.WriteAllText(shaderPath, regularShader);
        System.IO.File.WriteAllText(shaderBasePath, baseShader);
        AssetDatabase.Refresh();
        Shader s = AssetDatabase.LoadAssetAtPath <Shader>(shaderPath);

        Material m = new Material(s);

        AssetDatabase.CreateAsset(m, matPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        return(AssetDatabase.LoadAssetAtPath <Material>(matPath));
    }
    public static Shader NewShader()
    {
        string path = "Assets";

        foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
        {
            path = AssetDatabase.GetAssetPath(obj);
            if (System.IO.File.Exists(path))
            {
                path = System.IO.Path.GetDirectoryName(path);
            }
            break;
        }
        path = path.Replace("\\", "/");

        path = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.shader");
        string name = path.Substring(path.LastIndexOf("/"));

        name = name.Substring(0, name.IndexOf("."));
        name = name.Replace("/", "");

        ;
        string [] keywords = new string [0];
        var       pipeline = MicroSplatUtilities.DetectPipeline();

        if (pipeline == MicroSplatUtilities.PipelineType.HDPipeline)
        {
            System.Array.Resize(ref keywords, keywords.Length + 2);
            keywords [keywords.Length - 2] = "_MSRENDERLOOP_UNITYHD";
            keywords [keywords.Length - 1] = "_MSRENDERLOOP_UNITYHDRP2020";
        }
        else if (pipeline == MicroSplatUtilities.PipelineType.UniversalPipeline)
        {
            System.Array.Resize(ref keywords, keywords.Length + 2);
            keywords [keywords.Length - 2] = "_MSRENDERLOOP_UNITYLD";
            keywords [keywords.Length - 1] = "_MSRENDERLOOP_UNITYURP2020";
        }


        MicroSplatCompiler compiler = new MicroSplatCompiler();

        compiler.Init();
        string ret = compiler.Compile(keywords, name, null);

        System.IO.File.WriteAllText(path, ret);
        AssetDatabase.Refresh();
        return(AssetDatabase.LoadAssetAtPath <Shader> (path));
    }
    public static Material NewShaderAndMaterial(string path, string name, string[] keywords = null)
    {
        string shaderPath     = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.shader");
        string shaderBasePath = shaderPath.Replace(".shader", "_Base.shader");
        string matPath        = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.mat");

        MicroSplatCompiler compiler = new MicroSplatCompiler();

        compiler.Init();

        if (keywords == null)
        {
            keywords = new string[0];
        }

        string baseName = "Hidden/MicroSplat/" + name + "_Base";

        string baseShader    = compiler.Compile(keywords, baseName);
        string regularShader = compiler.Compile(keywords, name, baseName);

        System.IO.File.WriteAllText(shaderPath, regularShader);
        System.IO.File.WriteAllText(shaderBasePath, baseShader);


        if (keywords.Contains("_MESHOVERLAYSPLATS"))
        {
            string meshOverlayShader = compiler.Compile(keywords, name, null, true);
            System.IO.File.WriteAllText(shaderPath.Replace(".shader", "_MeshOverlay.shader"), meshOverlayShader);
        }

        AssetDatabase.Refresh();
        Shader s = AssetDatabase.LoadAssetAtPath <Shader>(shaderPath);

        Material m = new Material(s);

        AssetDatabase.CreateAsset(m, matPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        var kwds = MicroSplatUtilities.FindOrCreateKeywords(m);

        kwds.keywords = new List <string>(keywords);
        EditorUtility.SetDirty(kwds);

        return(AssetDatabase.LoadAssetAtPath <Material>(matPath));
    }
Пример #5
0
    public static Material NewShaderAndMaterial(string path, string name, string[] keywords = null)
    {
        string shaderPath     = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.shader");
        string shaderBasePath = shaderPath.Replace(".shader", "_Base.shader");
        string matPath        = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.mat");

        shaderPath     = shaderPath.Replace("//", "/");
        shaderBasePath = shaderBasePath.Replace("//", "/");
        matPath        = matPath.Replace("//", "/");

        MicroSplatCompiler compiler = new MicroSplatCompiler();

        compiler.Init();

        if (keywords == null)
        {
            keywords = new string[0];
        }

        var pipeline = MicroSplatUtilities.DetectPipeline();

        if (pipeline == MicroSplatUtilities.PipelineType.HDPipeline)
        {
            System.Array.Resize(ref keywords, keywords.Length + 1);
            keywords [keywords.Length - 1] = "_MSRENDERLOOP_UNITYHD";
        }
        else if (pipeline == MicroSplatUtilities.PipelineType.UniversalPipeline)
        {
            System.Array.Resize(ref keywords, keywords.Length + 1);
            keywords [keywords.Length - 1] = "_MSRENDERLOOP_UNITYLD";
        }


        string baseName = "Hidden/MicroSplat/" + name + "_Base";

        string baseShader    = compiler.Compile(keywords, baseName);
        string regularShader = compiler.Compile(keywords, name, baseName);

        System.IO.File.WriteAllText(shaderPath, regularShader);
        System.IO.File.WriteAllText(shaderBasePath, baseShader);


        if (keywords.Contains("_MESHOVERLAYSPLATS"))
        {
            string meshOverlayShader = compiler.Compile(keywords, name, null, true);
            System.IO.File.WriteAllText(shaderPath.Replace(".shader", "_MeshOverlay.shader"), meshOverlayShader);
        }

        AssetDatabase.Refresh();
        Shader s = AssetDatabase.LoadAssetAtPath <Shader> (shaderPath);

        if (s == null)
        {
            Debug.LogError("Shader not found at path " + shaderPath);
        }
        Material m = new Material(s);

        AssetDatabase.CreateAsset(m, matPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        var kwds = MicroSplatUtilities.FindOrCreateKeywords(m);

        kwds.keywords = new List <string> (keywords);
        EditorUtility.SetDirty(kwds);
        var propData = MicroSplatShaderGUI.FindOrCreatePropTex(m);

        if (propData != null)
        {
            EditorUtility.SetDirty(propData);
        }

        return(AssetDatabase.LoadAssetAtPath <Material> (matPath));
    }
Пример #6
0
    public static Material NewShaderAndMaterial(string path, string name, string[] keywords = null)
    {
        // if no branch sampling is specified, go straight to aggressive. Usually defaults are not done this way, but this seems
        // to make the most sense..
        if (!keywords.Contains("_BRANCHSAMPLES"))
        {
            System.Array.Resize(ref keywords, keywords.Length + 2);
            keywords [keywords.Length - 2] = "_BRANCHSAMPLES";
            keywords [keywords.Length - 1] = "_BRANCHSAMPLESARG";
        }
        string shaderPath     = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.shader");
        string shaderBasePath = shaderPath.Replace(".shader", "_Base.shader");
        string matPath        = AssetDatabase.GenerateUniqueAssetPath(path + "/MicroSplat.mat");

        shaderPath     = shaderPath.Replace("//", "/");
        shaderBasePath = shaderBasePath.Replace("//", "/");
        matPath        = matPath.Replace("//", "/");

        MicroSplatCompiler compiler = new MicroSplatCompiler();

        compiler.Init();

        if (keywords == null)
        {
            keywords = new string[0];
        }

        var pipeline = MicroSplatUtilities.DetectPipeline();

        if (pipeline == MicroSplatUtilities.PipelineType.HDPipeline)
        {
            System.Array.Resize(ref keywords, keywords.Length + 1);
            keywords [keywords.Length - 1] = "_MSRENDERLOOP_UNITYHD";
        }
        else if (pipeline == MicroSplatUtilities.PipelineType.UniversalPipeline)
        {
            System.Array.Resize(ref keywords, keywords.Length + 1);
            keywords [keywords.Length - 1] = "_MSRENDERLOOP_UNITYLD";
        }


        string baseName = "Hidden/MicroSplat/" + name + "_Base";

        string baseShader    = compiler.Compile(keywords, baseName);
        string regularShader = compiler.Compile(keywords, name, baseName);

        System.IO.File.WriteAllText(shaderPath, regularShader);
        System.IO.File.WriteAllText(shaderBasePath, baseShader);

        compiler.GenerateAuxShaders(name, shaderPath, new List <string>(keywords));

        AssetDatabase.Refresh();
        Shader s = AssetDatabase.LoadAssetAtPath <Shader> (shaderPath);

        if (s == null)
        {
            Debug.LogError("Shader not found at path " + shaderPath);
        }
        Material m = new Material(s);

        AssetDatabase.CreateAsset(m, matPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        var kwds = MicroSplatUtilities.FindOrCreateKeywords(m);

        kwds.keywords = new List <string> (keywords);
        EditorUtility.SetDirty(kwds);
        var propData = MicroSplatShaderGUI.FindOrCreatePropTex(m);

        if (propData != null)
        {
            EditorUtility.SetDirty(propData);
        }
        AssetDatabase.SaveAssets();

        return(AssetDatabase.LoadAssetAtPath <Material> (matPath));
    }