Ejemplo n.º 1
0
        void CreateLineMaterial()
        {
            if (!mat)
            {
                //KSPAssets.Loaders.AssetLoader.GetAssetDefinitionsWithType("JSI/RasterPropMonitor/rasterpropmonitor", typeof(Shader));
                mat = new Material(KVrUtilsCore.getShaderById("KVV/Lines/Colored Blended"));
                //Material mat = new Material(KVrUtilsCore.getShaderById("KVV/Lines/Colored Blended"));

                /*mat = new Material("Shader \"Lines/Colored Blended\" {" +
                 *  "SubShader { Pass { " +
                 *  "    Blend SrcAlpha OneMinusSrcAlpha " +
                 *  "    ZWrite Off ZTest Always Cull Off Fog { Mode Off } " +
                 *  "    BindChannels {" +
                 *  "      Bind \"vertex\", vertex Bind \"color\", color }" +
                 *  "} } }");*/
                mat.hideFlags        = HideFlags.HideAndDontSave;
                mat.shader.hideFlags = HideFlags.HideAndDontSave;
            }
        }
        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));
        }