Example #1
0
        public SubShader AddSubShader()
        {
            SubShader subShader = new SubShader();

            subShader.AddPass();
            this.subShaders.Add(subShader);
            return(subShader);
        }
Example #2
0
 public void RemoveSubShader(SubShader subShader)
 {
     if (this.subShaders.Count == 1)
     {
         Log.Warning("[ExtendedMaterial] Cannot remove the only subShader in a material");
         return;
     }
     this.subShaders.Remove(subShader);
 }
Example #3
0
        public SubShader Copy()
        {
            SubShader copy = this.Clone();

            copy.passes = new Dictionary <string, Pass>();
            copy.binds  = new List <Bind>();
            copy.tags   = this.tags.Clone();
            foreach (var item in this.passes)
            {
                copy.passes[item.Key] = item.Value.Copy();
            }
            foreach (var item in this.binds)
            {
                copy.binds.Add(item.Clone());
            }
            copy.fog           = this.fog.Clone();
            copy.fixedFunction = this.fixedFunction.Clone();
            copy.blend         = copy.blend.Copy();
            copy.blendAlpha    = copy.blendAlpha.Copy();
            return(copy);
        }
Example #4
0
 public void CheckContext()
 {
     if (Event.current.type == EventType.ContextClick && this.hoverObject != null)
     {
         object      hover        = this.hoverObject;
         string      typeName     = "";
         GenericMenu menu         = new GenericMenu();
         Action      removeMethod = () => Log.Show("No Context Found.");
         Action      flagDirty    = () => MaterialBuffer.buildPreview = true;
         if (hover is Property)
         {
             Property property = (Property)hover;
             typeName     = "Property [" + property.name + "]";
             removeMethod = () => MaterialBuffer.active.properties.RemoveValue(property);
         }
         else if (hover is object[])
         {
             object[] items = (object[])hover;
             Pass     pass  = (Pass)items[1];
             string   name  = pass.name != "" ? " [" + pass.name + "]" : "";
             typeName     = "Pass" + name;
             removeMethod = () => ((SubShader)items[0]).RemovePass(pass);
         }
         else if (hover is SubShader)
         {
             SubShader subShader = (SubShader)hover;
             typeName     = "SubShader";
             removeMethod = () => MaterialBuffer.active.RemoveSubShader(subShader);
         }
         GUIContent field = new GUIContent("Remove " + typeName);
         menu.AddItem(field, false, new GenericMenu.MenuFunction(removeMethod + flagDirty));
         menu.ShowAsContext();
         MaterialBuffer.unsaved = true;
         this.hoverObject       = null;
         Event.current.Use();
     }
 }
Example #5
0
        public void Load(string path)
        {
            path = File.Exists(path) ? path : File.Find(path).path;
            string contents  = "";
            int    parsePass = 0;

            using (StreamReader file = new StreamReader(path)){
                contents = file.ReadToEnd();
            }
            if (contents != "")
            {
                string quote = "\"";
                contents      = contents.Replace("Prepass", "Prelight", true);
                contents      = contents.Replace("UsePass", "UseShader", true);
                contents      = contents.Replace("GrabPass", "GrabScreen", true);
                this.menuPath = contents.Cut(quote, quote).Remove(quote);
                this.editor   = contents.Cut("CustomEditor", "\n").Remove("CustomEditor").Pack();
                this.fallback = contents.Cut("Fallback", "\n").Remove("Fallback").Pack();
                this.source   = Shader.Find(this.menuPath);
                string propertyBlock = this.GetBlock(contents, "Properties");
                contents = contents.ReplaceFirst(propertyBlock, "", true);
                string [] lines = propertyBlock.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                foreach (string current in lines)
                {
                    string line = current.Remove(" ", "\t", quote).Condense();
                    if (line.Contains("="))
                    {
                        Property property = new Property();
                        property.variable = line.Substring(0, line.IndexOf("("));
                        property.name     = current.Cut("(", ",").Remove("(", ",", quote);
                        string type = line.Cut(",", ")").Remove(",", ")");
                        if (type.Contains("Range", true))
                        {
                            type = "Range";
                            string data = line.Cut(",", ")").Remove("Range", "(", ")").TrimLeft(",");
                            property.minimum = Convert.ToSingle(data.TrySplit(',', 0));
                            property.maximum = Convert.ToSingle(data.TrySplit(',', 1));
                        }
                        if (type == "2D")
                        {
                            type = "Texture";
                        }
                        string value = line.TrySplit('=', 1);
                        property.type         = (PropertyType)property.type.Get(type, 5);
                        property.defaultValue = value;
                        if (type == "Range" || type == "Float")
                        {
                            property.defaultValue = Convert.ToSingle(value);
                        }
                        if (type == "Color" || type == "Vector")
                        {
                            float[] values = value.Remove("(", ")").Split(',').ConvertAll <float>();
                            property.defaultValue = values;
                            if (type == "Color")
                            {
                                property.defaultValue = values.ToColor();
                            }
                            if (type == "Vector")
                            {
                                property.defaultValue = values.ToVector4();
                            }
                        }
                        if (type == "Texture" || type == "Rect" || type == "Cube")
                        {
                            string extra = current.Cut("{", "}").Remove("{", "}") + " ";
                            if (extra.Contains("TexGen"))
                            {
                                string texgen = extra.Cut("TexGen", " ", 0, true, 2).Remove("TexGen", "\t", " ");
                                property.texgenMode = (TexGen)property.texgenMode.Get(texgen);
                            }
                            int bracket = value.IndexOf("{");
                            property.defaultValue = new TextureDefault().Get(value.Substring(0, bracket));
                        }
                        this.properties[property.name] = property;
                    }
                }
                while (true)
                {
                    if (parsePass > 128)
                    {
                        Log.Error("[ExtendedMaterial] Error parsing shader -- " + this.menuPath);
                        return;
                    }
                    string nextSubShader = this.GetBlock(contents, "SubShader");
                    if (nextSubShader == "")
                    {
                        break;
                    }
                    contents = contents.ReplaceFirst(nextSubShader, "", true);
                    SubShader subShader = new SubShader();
                    string    tagBlock;
                    while (true)
                    {
                        if (parsePass > 128)
                        {
                            Log.Error("[ExtendedMaterial] Error parsing shader -- " + this.menuPath);
                            return;
                        }
                        Pass   pass     = new Pass();
                        string nextType = nextSubShader.FindFirst("UseShader", "GrabScreen", "Pass");
                        string passName = "!" + pass.GetHashCode().ToString();
                        string usePass  = nextSubShader.Cut("UseShader", "\n");
                        if (nextType == "")
                        {
                            break;
                        }
                        if (nextType == "UseShader" && usePass != "")
                        {
                            nextSubShader = nextSubShader.ReplaceFirst(usePass, "", true);
                            pass.type     = PassType.Use;
                            pass.usePass  = usePass.Remove("UseShader").Pack();
                        }
                        string grabPass = this.GetBlock(nextSubShader, "GrabScreen");
                        if (nextType == "GrabScreen" && grabPass != "")
                        {
                            nextSubShader = nextSubShader.ReplaceFirst(grabPass, "", true);
                            pass.type     = PassType.Grab;
                            pass.grabPass = grabPass.Remove("GrabScreen").Pack();
                        }
                        string normalPass = this.GetBlock(nextSubShader, "Pass");
                        if (nextType == "Pass" && normalPass != "")
                        {
                            nextSubShader  = nextSubShader.ReplaceFirst(normalPass, "", true);
                            pass.name      = normalPass.Cut("Name \"", "\n").Remove("Name").Pack();
                            pass.gpuShader = normalPass.Cut("CGPROGRAM", "ENDCG");
                            normalPass     = normalPass.ReplaceFirst(pass.gpuShader, "", true);
                            if (pass.name != "")
                            {
                                passName = pass.name;
                            }
                            if (normalPass.Contains("GLSLPROGRAM"))
                            {
                                pass.gpuShader = normalPass.Cut("GLSLPROGRAM", "ENDGLSL");
                            }
                            pass.gpuShader = pass.gpuShader.Remove("\t\t\t");
                            tagBlock       = this.GetBlock(normalPass, "tags").Remove(" ", "\t").Replace("Prelight", "Prepass", true);
                            if (tagBlock != "")
                            {
                                string lightMode = tagBlock.Cut("LightMode", quote, 0, true, 3).Remove(quote).TrySplit('=', 1);
                                string require   = tagBlock.Cut("Require", quote, 0, true, 3).Remove(quote).TrySplit('=', 1);
                                pass.tags.lightMode = (LightMode)pass.tags.lightMode.Get(lightMode, 0);
                                pass.tags.require   = (Require)pass.tags.require.Get(require, 0);
                            }
                            this.SortCommon(pass, normalPass);
                            normalPass = normalPass.ReplaceFirst(normalPass, "", true);
                        }
                        subShader.passes[passName] = pass;
                        parsePass += 1;
                    }
                    tagBlock      = this.GetBlock(nextSubShader, "tags");
                    nextSubShader = nextSubShader.ReplaceFirst(tagBlock, "", true);
                    if (tagBlock != "")
                    {
                        tagBlock = tagBlock.Remove(" ", "\t").Replace("Prelight", "Prepass", true);
                        string renderQueue          = tagBlock.Cut("Queue", quote, 0, true, 3).Remove(quote).TrySplit('=', 1);
                        string renderType           = tagBlock.Cut("Rendertype", quote, 0, true, 3).Remove(quote).TrySplit('=', 1);
                        string ignoreProjector      = tagBlock.Cut("IgnoreProjector", quote, 0, true, 3).Remove(quote).TrySplit('=', 1);
                        string forceNoShadowCasting = tagBlock.Cut("ForceNoShadowCasting", quote, 0, true, 3).Remove(quote).TrySplit('=', 1);
                        string lightMode            = tagBlock.Cut("LightMode", quote, 0, true, 3).Remove(quote).TrySplit('=', 1);
                        string require = tagBlock.Cut("Require", quote, 0, true, 3).Remove(quote).TrySplit('=', 1);
                        if (renderQueue.Contains("+"))
                        {
                            subShader.tags.renderQueueOffset = Convert.ToInt16(renderQueue.Split('+')[1]);
                            renderQueue = renderQueue.Split('+')[0];
                        }
                        if (renderQueue.Contains("-"))
                        {
                            subShader.tags.renderQueueOffset = Convert.ToInt16(renderQueue.Split('-')[1]) * -1;
                            renderQueue = renderQueue.Split('-')[0];
                        }
                        subShader.tags.lightMode            = (LightMode)subShader.tags.lightMode.Get(lightMode, 0);
                        subShader.tags.require              = (Require)subShader.tags.require.Get(require, 0);
                        subShader.tags.renderQueue          = (RenderQueue)subShader.tags.renderQueue.Get(renderQueue, 0);
                        subShader.tags.renderType           = (RenderType)subShader.tags.renderType.Get(renderType, 0);
                        subShader.tags.ignoreProjector      = ignoreProjector.Matches("true", true) ? true : false;
                        subShader.tags.forceNoShadowCasting = forceNoShadowCasting.Matches("true", true) ? true : false;
                    }
                    this.SortCommon(subShader, nextSubShader);
                    this.subShaders.Add(subShader);
                    parsePass += 1;
                }
                if (this.subShaders.Count > 0)
                {
                    this.category = this.subShaders.First();
                }
            }
            this.path     = path;
            this.fileName = path.Substring(path.LastIndexOf("/") + 1);
        }