Beispiel #1
0
            internal ShaderProperty[] GetConditionalShaderProperties(ParsedLine[] parsedLines, out Dictionary <int, GUIContent> headers)
            {
                headers = new Dictionary <int, GUIContent>();

                var shaderPropertiesList = new List <ShaderProperty>();

                for (var i = 0; i < parsedLines.Length; i++)
                {
                    var line = parsedLines[i].line;

                    if (line.StartsWith("#PROPERTIES_NEW"))
                    {
                        while (i < parsedLines.Length)
                        {
                            line = parsedLines[i].line;
                            i++;

                            if (line.StartsWith("#END"))
                            {
                                return(shaderPropertiesList.ToArray());
                            }

                            if (line.StartsWith("//") || line.StartsWith("#") || string.IsNullOrEmpty(line))
                            {
                                continue;
                            }

                            if (line.Trim().StartsWith("header"))
                            {
                                var data = line.Split(new string[] { "\t" }, System.StringSplitOptions.RemoveEmptyEntries);
                                var gc   = new GUIContent(data[1], data.Length > 2 ? data[2].Trim('\"') : null);
                                if (!headers.ContainsKey(shaderPropertiesList.Count))
                                {
                                    headers.Add(shaderPropertiesList.Count, null);
                                }
                                headers[shaderPropertiesList.Count] = gc;                                       // only take the last one into account, so that empty headers will be ignored
                                continue;
                            }

                            try
                            {
                                var shaderProperty = ShaderProperty.CreateFromTemplateData(line);
                                var match          = GetShaderPropertyByName(shaderProperty.Name);
                                if (match == null)
                                {
                                    Debug.LogError(ShaderGenerator2.ErrorMsg("Can't find Shader Property in Template, yet it was found for Config"));
                                }
                                else
                                {
                                    shaderPropertiesList.Add(match);
                                }
                            }
                            catch (Exception e)
                            {
                                Debug.LogError(ShaderGenerator2.ErrorMsg(string.Format("Parsing error in <b>#PROPERTIES_NEW</b> block:\n'{0}'\n{1}", e.Message, e.StackTrace)));
                            }
                        }
                    }
                }

                return(shaderPropertiesList.ToArray());
            }
Beispiel #2
0
            //Get all Shader Properties regardless of conditions, only their visibility will be affected by the Config
            //This ensures that they are always in the correct order
            //Also link the pending Imp_ShaderPropertyReferences at this time, if any
            //and assign the correct pass bitmask based on usage
            static ShaderProperty[] GetShaderProperties(string[] lines, int i)
            {
                var    shaderPropertiesList = new List <ShaderProperty>();
                string subline;

                do
                {
                    subline = lines[i];
                    i++;

                    if (subline == "#END")
                    {
                        break;
                    }

                    if (subline.Trim().StartsWith("//") || subline.StartsWith("#") || string.IsNullOrEmpty(subline))
                    {
                        continue;
                    }

                    if (subline.Trim().StartsWith("header"))
                    {
                        continue;
                    }

                    try
                    {
                        var shaderProperty = ShaderProperty.CreateFromTemplateData(subline);
                        shaderPropertiesList.Add(shaderProperty);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(ShaderGenerator2.ErrorMsg(string.Format("Parsing error in <b>#PROPERTIES_NEW</b> block:\n\nError: '{0}'\n\nLine: '{1}'", e.ToString(), subline)));
                    }
                }while (subline != "#END" && subline != null);

                //link shader property references
                foreach (var shaderProperty in shaderPropertiesList)
                {
                    if (shaderProperty.implementations != null && shaderProperty.implementations.Count > 0)
                    {
                        foreach (var imp in shaderProperty.implementations)
                        {
                            var impSpRef = imp as ShaderProperty.Imp_ShaderPropertyReference;
                            if (impSpRef != null && !string.IsNullOrEmpty(impSpRef.LinkedShaderPropertyName))
                            {
                                var match = shaderPropertiesList.Find(sp => sp.Name == impSpRef.LinkedShaderPropertyName);
                                if (match != null)
                                {
                                    var channels = impSpRef.Channels;
                                    impSpRef.LinkedShaderProperty = match;
                                    //restore channels from template data, it's up to the template to match the referenced shader property
                                    if (!string.IsNullOrEmpty(channels))
                                    {
                                        impSpRef.Channels = channels.ToUpperInvariant();
                                    }
                                    impSpRef.ForceUpdateParentDefaultHash();
                                }
                                else
                                {
                                    Debug.LogError(ShaderGenerator2.ErrorMsg(string.Format("Can't find referenced Shader Property in template.\n'{0}' tried to reference '{1}'", shaderProperty.Name, impSpRef.LinkedShaderPropertyName)));
                                }
                            }
                        }
                    }
                }

                //iterate rest of template to check usage of each shader property per pass

                int currentPass = -1;

                for (; i < lines.Length; i++)
                {
                    var line = lines[i].Trim();

                    // update pass
                    if (line.StartsWith("#PASS"))
                    {
                        currentPass++;
                        continue;
                    }

                    // check value usage: used in which pass(es), and which generic implementation they can use
                    var end = 0;
                    while (line.IndexOf("[[", end) >= 0)
                    {
                        var start = line.IndexOf("[[", end);
                        end = line.IndexOf("]]", end + 1);
                        var tag = line.Substring(start + 2, end - start - 2);
                        if (tag.StartsWith("VALUE:") || tag.StartsWith("SAMPLE_VALUE_SHADER_PROPERTY:"))
                        {
                            var propName  = tag.Substring(tag.IndexOf(':') + 1);
                            int argsStart = propName.IndexOf('(');
                            if (argsStart > 0)
                            {
                                propName = propName.Substring(0, argsStart);
                            }

                            var sp = shaderPropertiesList.Find(x => x.Name == propName);
                            if (sp != null)
                            {
                                // found used Shader Property
                                sp.AddPassUsage(currentPass);
                            }
                            else
                            {
                                Debug.LogError(ShaderGenerator2.ErrorMsg(string.Format("No match for used Shader Property in code: '<b>{0}</b>'", tag)));
                            }
                        }
                    }
                }

                return(shaderPropertiesList.ToArray());
            }