Beispiel #1
0
        public static ShaderInfo CreateShaderInfo(string assetPath)
        {
            if (!EditorPath.IsShader(assetPath))
            {
                return(null);
            }

            ShaderInfo shaderInfo = null;

            if (!_dictShaderInfo.TryGetValue(assetPath, out shaderInfo))
            {
                shaderInfo = new ShaderInfo();
                _dictShaderInfo.Add(assetPath, shaderInfo);
            }
            Shader shader     = AssetDatabase.LoadAssetAtPath <Shader>(assetPath);
            string shaderText = File.ReadAllText(assetPath);

            //ShaderUtil.OpenCompiledShader(shader, (int)ShaderPlatformModes.Custom, 1 << (int)ShaderUtil.ShaderCompilerPlatformType.D3D11, false);

            typeof(ShaderUtil).GetMethod("OpenCompiledShader", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, new object[] { shader, (int)ShaderPlatformModes.Custom, 1 << (int)ShaderCompilerPlatformType.D3D11, false });
            try
            {
                CompiledShaderInfo compiledShaderInfo = CompiledShaderInfo.CreateCompiledShaderInfo(shaderText);
                shaderInfo.Path = assetPath;
                //shaderInfo.MaxLOD = ShaderUtil.GetLOD(shader);
                //shaderInfo.Variant = ShaderUtil.GetVariantCount(shader, true);
                shaderInfo.Property    = ShaderUtil.GetPropertyCount(shader);
                shaderInfo.RenderQueue = shader.renderQueue;
                shaderInfo.Pass        = compiledShaderInfo.GetPass();
                shaderInfo.Instruction = compiledShaderInfo.GetInstruction();
                shaderInfo.SubShader   = compiledShaderInfo.GetSubShaderCount();
                shaderInfo.Sample      = compiledShaderInfo.GetSample();
                shaderInfo.RenderType  = compiledShaderInfo.GetRenderType();
                shaderInfo.CompiledShaderInfoList.Add(compiledShaderInfo);
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message);
                return(null);
            }
            return(shaderInfo);
        }
Beispiel #2
0
        //private ShaderUtil.ShaderCompilerPlatformType shaderCompilerPlatformType;

        public static CompiledShaderInfo CreateCompiledShaderInfo(string shaderText)
        {
            if (shaderText == string.Empty)
            {
                throw new ArgumentNullException("shaderText");
            }

            CompiledShaderInfo compiledShaderInfo = new CompiledShaderInfo();
            Match shaderNameMatch = Regex.Match(shaderText, "Shader\\s*\"(.*)\"");

            compiledShaderInfo.shaderName = shaderNameMatch.Success ? shaderNameMatch.Result("$1") : string.Empty;

            string tempShaderText = File.ReadAllText("Temp/Compiled-" + compiledShaderInfo.shaderName.Replace("/", "-") + ".shader");

            if (tempShaderText == string.Empty)
            {
                throw new ArgumentNullException("tempShaderText");
            }

            // Subshader
            MatchCollection subShaderMatches = Regex.Matches(tempShaderText, @"SubShader\s*\{(?>[^\{\}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*(?(DEPTH)(?!))\}");

            compiledShaderInfo.subShaderList = new List <SubShader>();
            foreach (Match subShaderMatch in subShaderMatches)
            {
                if (subShaderMatch.Success)
                {
                    // Tags
                    string renderType = DefaultRenderType;
                    Match  tagsMatch  = Regex.Match(subShaderMatch.Groups[0].Value, @"Tags\s*\{(?>[^\{\}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*(?(DEPTH)(?!))\}");
                    if (tagsMatch.Success)
                    {
                        // RenderType
                        Match renderTypeMatch = Regex.Match(tagsMatch.Groups[0].Value, "\"RenderType\"=\"(?>[^\"\"]+|\"(?<DEPTH>)|\"(?<-DEPTH>))*(?(DEPTH)(?!))\"");
                        if (renderTypeMatch.Success)
                        {
                            renderType = Regex.Match(renderTypeMatch.Groups[0].Value, "\"RenderType\"=\"(.*)\"").Result("$1") ?? DefaultRenderType;
                        }
                    }

                    // Pass
                    List <Pass>     passList    = new List <Pass>();
                    MatchCollection passMatches = Regex.Matches(subShaderMatch.Groups[0].Value, @"Pass\s*\{(?>[^\{\}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*(?(DEPTH)(?!))\}");
                    foreach (Match passMatch in passMatches)
                    {
                        if (passMatch.Success)
                        {
                            // Variant
                            List <Variant> variantList = new List <Variant>();
                            string[]       splitShader = Regex.Split(passMatch.Groups[0].Value, "shader for \"");
                            for (int i = 0; i < splitShader.Length / 2; i++)    // index-0 is deprecated
                            {
                                variantList.Add(new Variant(splitShader[2 * i + 1], splitShader[2 * (i + 1)]));
                            }
                            passList.Add(new Pass(variantList));
                        }
                    }
                    compiledShaderInfo.subShaderList.Add(new SubShader(passList, new Tags(renderType)));
                }
            }
            return(compiledShaderInfo);
        }