Example #1
0
        public ShaderMaterial(string contents)
            : this()
        {
            this.Material = new Material(contents);
            var p = @"Properties\s*\{[^\{\}]*(((?<Open>\{)[^\{\}]*)+((?<Close-Open>\})[^\{\}]*)+)*(?(Open)(?!))\}";
            var m = Regex.Match(contents, p, RegexOptions.Multiline | RegexOptions.IgnoreCase);

            if (!m.Success)
            {
                throw new Exception("Error parsing shader properties: " + this.Material.shader.name);
            }
            p = @"(?<name>\w*)\s*\(\s*""(?<displayname>[^""]*)""\s*,\s*(?<type>Float|Vector|Color|2D|Rect|Cube|Range\s*\(\s*(?<rangemin>[\d.]*)\s*,\s*(?<rangemax>[\d.]*)\s*\))\s*\)";
            MonoBehaviour.print("1 " + m.Value);
            foreach (Match match in Regex.Matches(m.Value, p))
            {
                ShaderMaterialProperty prop;
                var name        = match.Groups["name"].Value;
                var displayname = match.Groups["displayname"].Value;
                var typestr     = match.Groups["type"].Value;
                switch (typestr.ToUpperInvariant())
                {
                case "VECTOR":
                    prop = new ShaderMaterialProperty.VectorProperty(this.Material, name, displayname);
                    break;

                case "COLOR":
                    prop = new ShaderMaterialProperty.ColorProperty(this.Material, name, displayname);
                    break;

                case "2D":
                case "RECT":
                case "CUBE":
                    prop = new ShaderMaterialProperty.TextureProperty(this.Material, name, displayname);
                    break;

                default:     /// Defaults to Range(*,*)
                    prop = new ShaderMaterialProperty.FloatProperty(this.Material, name, displayname, float.Parse(match.Groups["rangemin"].Value), float.Parse(match.Groups["rangemax"].Value));
                    break;
                }
                this.properties.Add(prop);
                this.propertiesByName[prop.Name] = prop;
            }
        }
 public ShaderMaterial(string contents)
     : this()
 {
     this.Material = new Material(contents);
     var p = @"Properties\s*\{[^\{\}]*(((?<Open>\{)[^\{\}]*)+((?<Close-Open>\})[^\{\}]*)+)*(?(Open)(?!))\}";
     var m = Regex.Match(contents, p, RegexOptions.Multiline | RegexOptions.IgnoreCase);
     if (!m.Success)
     {
         throw new Exception("Error parsing shader properties: " + this.Material.shader.name);
     }
     p = @"(?<name>\w*)\s*\(\s*""(?<displayname>[^""]*)""\s*,\s*(?<type>Float|Vector|Color|2D|Rect|Cube|Range\s*\(\s*(?<rangemin>[\d.]*)\s*,\s*(?<rangemax>[\d.]*)\s*\))\s*\)";
     MonoBehaviour.print("1 " + m.Value);
     foreach(Match match in Regex.Matches(m.Value, p))
     {
         ShaderMaterialProperty prop;
         var name = match.Groups["name"].Value;
         var displayname = match.Groups["displayname"].Value;
         var typestr = match.Groups["type"].Value;
         switch (typestr.ToUpperInvariant())
         {
             case "VECTOR":
                 prop = new ShaderMaterialProperty.VectorProperty(this.Material, name, displayname);
                 break;
             case "COLOR":
                 prop = new ShaderMaterialProperty.ColorProperty(this.Material, name, displayname);
                 break;
             case "2D":
             case "RECT":
             case "CUBE":
                 prop = new ShaderMaterialProperty.TextureProperty(this.Material, name, displayname);
                 break;
             default: /// Defaults to Range(*,*)
                 prop = new ShaderMaterialProperty.FloatProperty(this.Material, name, displayname, float.Parse(match.Groups["rangemin"].Value), float.Parse(match.Groups["rangemax"].Value));
                 break;
         }
         this.properties.Add(prop);
         this.propertiesByName[prop.Name] = prop;
     }
 }
        public ShaderMaterial(string fileName, string contentName)
            : this()
        {
            log.debug(string.Format("Enter ShaderMaterial, filename: {0}   contentName: {1}", fileName, contentName));
            string contents;

            try
            {
                this.Material = new Material(KVrUtilsCore.getShaderById(contentName));
            } catch (Exception e)
            {
                log.error(string.Format("{0} creating material: {1}", e, fileName));
                return;
            }

            try
            {
                contents = System.IO.File.ReadAllText(KSPUtil.ApplicationRootPath + "GameData/KronalVesselViewer/Resources/" + fileName + ".shader");
            } catch (Exception e)
            {
                log.error(string.Format("{0} reading file: {1}", e, fileName));
                return;
            }



            var p = @"Properties\s*\{[^\{\}]*(((?<Open>\{)[^\{\}]*)+((?<Close-Open>\})[^\{\}]*)+)*(?(Open)(?!))\}";
            var m = Regex.Match(contents, p, RegexOptions.Multiline | RegexOptions.IgnoreCase);

            if (!m.Success)
            {
                throw new Exception(string.Format("Error parsing shader properties: {0}", this.Material.shader.name));
            }
            p = @"(?<name>\w*)\s*\(\s*""(?<displayname>[^""]*)""\s*,\s*(?<type>Float|Vector|Color|2D|Rect|Cube|Range\s*\(\s*(?<rangemin>[\d.]*)\s*,\s*(?<rangemax>[\d.]*)\s*\))\s*\)";

            log.debug(string.Format("ShaderMaterial1 {0}", m.Value));

            foreach (Match match in Regex.Matches(m.Value, p))
            {
                ShaderMaterialProperty prop;
                var name        = match.Groups["name"].Value;
                var displayname = match.Groups["displayname"].Value;
                var typestr     = match.Groups["type"].Value;
                switch (typestr.ToUpperInvariant())
                {
                case "VECTOR":
                    prop = new ShaderMaterialProperty.VectorProperty(this.Material, name, displayname);
                    break;

                case "COLOR":
                    prop = new ShaderMaterialProperty.ColorProperty(this.Material, name, displayname);
                    break;

                case "2D":
                case "RECT":
                case "CUBE":
                    prop = new ShaderMaterialProperty.TextureProperty(this.Material, name, displayname);
                    break;

                default:     /// Defaults to Range(*,*)
                    prop = new ShaderMaterialProperty.FloatProperty(this.Material, name, displayname, float.Parse(match.Groups["rangemin"].Value), float.Parse(match.Groups["rangemax"].Value));
                    break;
                }
                this.properties.Add(prop);
                this.propertiesByName[prop.Name] = prop;
            }
            log.debug(string.Format("Enter ShaderMaterial, filename: {0} contentName: {1} properties.count: {2}", fileName, contentName, properties.Count));
        }