Ejemplo n.º 1
0
        private void SaveTexture(String fileName)
        {
            int fileWidth  = this.rt.width;
            int fileHeight = this.rt.height;

            log.debug(string.Format("SIZE: {0} x {1}", fileWidth, fileHeight));

            Texture2D screenShot = new Texture2D(fileWidth, fileHeight, TextureFormat.ARGB32, false);

            var saveRt = RenderTexture.active;

            RenderTexture.active = this.rt;
            screenShot.ReadPixels(new Rect(0, 0, fileWidth, fileHeight), 0, 0);
            screenShot.Apply();
            RenderTexture.active = saveRt;
            byte[] bytes            = screenShot.EncodeToPNG();
            string ShipNameFileSafe = MakeValidFileName(fileName);
            uint   file_inc         = 0;
            string filename         = "";
            string filenamebase     = "";

            do
            {
                ++file_inc;
                filenamebase = ShipNameFileSafe + "_" + file_inc.ToString() + ".png";
                //filename = Path.Combine(System.IO.Directory.GetParent(KSPUtil.ApplicationRootPath).ToString(), "Screenshots" + Path.DirectorySeparatorChar + filenamebase);
                filename = Path.Combine(KVrUtilsCore.ModExport() + Path.DirectorySeparatorChar, filenamebase);
            } while (File.Exists(filename));
            System.IO.File.WriteAllBytes(filename, bytes);

            log.debug(string.Format("Took screenshot to: {0}", filename));

            screenShot = null;
            bytes      = null;
        }
Ejemplo n.º 2
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;
            }
        }
        private void InitShaders()
        {
            LoadBundle();

#if false
            log.debug(string.Format("InitShaders 1: {0}", KSPAssets.Loaders.AssetLoader.ApplicationRootPath));

            string KVrPath = KVrUtilsCore.ModRoot();
            log.debug(string.Format("KVrPath: {0}", KVrPath));

            string KVrAssetPath = Path.GetDirectoryName(KVrPath + Path.DirectorySeparatorChar);
            log.debug(string.Format("KVrAssetPath: {0}", KVrAssetPath));
            KVrAssetPath = "KronalUtils";
            log.debug(string.Format("InitShaders 2 KVrPath{0} \n\t- Directory  Path.GetFileName( KVrAssetPath ...) {1} ", KVrPath, Path.Combine(KVrAssetPath, "kvv")));
            //, String.Join("\n\t- ", System.IO.Directory.GetFiles(KVrPath))
            KSPAssets.AssetDefinition[] KVrShaders = KSPAssets.Loaders.AssetLoader.GetAssetDefinitionsWithType(KVrAssetPath + "/kvv", typeof(Shader));//path to kvv.ksp
            if (KVrShaders == null || KVrShaders.Length == 0)
            {
                log.error(string.Format("Failed to load Asset Package KronalUtils/kvv.ksp in {0}.", KVrPath));
                return;
            }
            else
            {
                KSPAssets.Loaders.AssetLoader.LoadAssets(ShadersLoaded, KVrShaders[0]);
            }

            log.debug(string.Format("InitShaders 4"));

            /*
             * foreach (KeyValuePair<string, string> itKey in ShaderData)
             * {
             *  //KSPAssets.AssetDefinition[itKey.Key] KVrShaders = KSPAssets.Loaders.AssetLoader.GetAssetDefinitionsWithType(KronalUtils.Properties.Resources.ShaderFXAA, typeof(Shader));
             *  //KSPAssets.Loaders.AssetLoader newShad = KSPAssets.Loaders.AssetLoader.GetAssetDefinitionsWithType(ShaderData[itKey.Key], typeof(Shader));
             *  //KVrShaders.Add(itKey.Key, new KSPAssets.Loaders.AssetLoader());
             *  //KVrShaders[itKey.Key] = KSPAssets.Loaders.AssetLoader.GetAssetDefinitionsWithType(ShaderData[itKey.Key], typeof(UnityEngine.Shader));
             *  // KSPAssets.Loaders.AssetLoader.GetAssetDefinitionsWithType(KronalUtils.Properties.Resources.ShaderFXAA, typeof(Shader));
             * }*/
#endif
        }
        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));
        }