예제 #1
0
 private void RebuildImage(GraphicsDevice graphicsDevice, ISector sector)
 {
     // combination images
     using (Texture2D rendered = new RenderTarget2D(graphicsDevice, 512, 512, false, graphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24))
     {
         int highestZoom = ZCoords.GetSectorManager().GetHighestCacheZoom();
         foreach (var parent in sector.GetAllParents().OrderBy(x => - x.Zoom).Where(x => x.Zoom <= highestZoom))
         {
             List <ISector> roadSectors = parent.GetChildrenAtLevel(parent.Zoom == highestZoom - 1 ? ZCoords.GetSectorManager().GetHighestOSMZoom() : parent.Zoom + 1);
             Texture2D[]    textures    = new Texture2D[roadSectors.Count];
             for (int i = 0; i < roadSectors.Count; i++)
             {
                 IGraphicsBuffer buffer = null;
                 if (File.Exists(OSMPaths.GetSectorImagePath(roadSectors[i])))
                 {
                     using (var reader = File.OpenRead(OSMPaths.GetSectorImagePath(roadSectors[i])))
                     {
                         buffer = new ImageTileBuffer(graphicsDevice, Texture2D.FromStream(graphicsDevice, reader), roadSectors[i]);
                     }
                 }
                 else
                 {
                     throw new NotImplementedException();
                 }
                 textures[i] = buffer.GetImage(graphicsDevice);
             }
             if (textures.Any(x => x != null))
             {
                 graphicsDevice.SetRenderTarget((RenderTarget2D)rendered);
                 for (int i = 0; i < roadSectors.Count; i++)
                 {
                     int size, x, y;
                     size = 512 >> (roadSectors[i].Zoom - parent.Zoom);
                     x    = parent.GetRelativeXOf(roadSectors[i]) * size;
                     y    = parent.GetRelativeYOf(roadSectors[i]) * size;
                     if (textures[i] != null)
                     {
                         GraphicsBasic.DrawSpriteRect(graphicsDevice, x, y, size, size, textures[i], BlendState.AlphaBlend, Microsoft.Xna.Framework.Color.White);
                     }
                 }
             }
             for (int i = 0; i < textures.Length; i++)
             {
                 if (textures[i] != null && textures[i] != GlobalContent.Error)
                 {
                     textures[i].Dispose();
                 }
             }
             SuperSave(rendered, OSMPaths.GetSectorImagePath(parent));
         }
     }
 }
예제 #2
0
        /**
         * Post-load initialisation.
         */
        public void load()
        {
            Texture2D[] envMapFaces = new Texture2D[6];

            foreach (GameDatabase.TextureInfo texInfo in GameDatabase.Instance.databaseTexture)
            {
                Texture2D texture = texInfo.texture;
                if (texture == null || !texture.name.StartsWith(DIR_ENVMAP, System.StringComparison.Ordinal))
                {
                    continue;
                }

                string originalName = texture.name.Substring(DIR_ENVMAP.Length);

                switch (originalName)
                {
                case "PositiveX":
                    envMapFaces[0] = texture;
                    break;

                case "NegativeX":
                    envMapFaces[1] = texture;
                    break;

                case "PositiveY":
                    envMapFaces[2] = texture;
                    break;

                case "NegativeY":
                    envMapFaces[3] = texture;
                    break;

                case "PositiveZ":
                    envMapFaces[4] = texture;
                    break;

                case "NegativeZ":
                    envMapFaces[5] = texture;
                    break;

                default:
                    Util.log("Invalid enironment map texture name {0}", texture.name);
                    break;
                }
            }

            // Generate generic reflection cube map texture.
            if (envMapFaces.Contains(null))
            {
                Util.log("Some environment map faces are missing. Static reflections disabled.");
            }
            else
            {
                int envMapSize = envMapFaces[0].width;

                if (envMapFaces.Any(t => t.width != envMapSize || t.height != envMapSize) ||
                    envMapFaces.Any(t => !Util.isPow2(t.width) || !Util.isPow2(t.height)))
                {
                    Util.log("Invalid environment map faces. Static reflections disabled.");
                }
                else
                {
                    try
                    {
                        staticEnvMap           = new Cubemap(envMapSize, TextureFormat.RGB24, true);
                        staticEnvMap.hideFlags = HideFlags.HideAndDontSave;
                        staticEnvMap.wrapMode  = TextureWrapMode.Clamp;
                        staticEnvMap.SetPixels(envMapFaces[0].GetPixels(), CubemapFace.PositiveX);
                        staticEnvMap.SetPixels(envMapFaces[1].GetPixels(), CubemapFace.NegativeX);
                        staticEnvMap.SetPixels(envMapFaces[2].GetPixels(), CubemapFace.PositiveY);
                        staticEnvMap.SetPixels(envMapFaces[3].GetPixels(), CubemapFace.NegativeY);
                        staticEnvMap.SetPixels(envMapFaces[4].GetPixels(), CubemapFace.PositiveZ);
                        staticEnvMap.SetPixels(envMapFaces[5].GetPixels(), CubemapFace.NegativeZ);
                        staticEnvMap.Apply(true, false);

                        Util.log("Static environment map cube texture generated.");
                    }
                    catch (UnityException)
                    {
                        if (staticEnvMap != null)
                        {
                            Object.DestroyImmediate(staticEnvMap);
                        }

                        staticEnvMap = null;

                        Util.log("Failed to set up static reflections. Textures not readable?");
                    }
                }
            }

            try
            {
                Assembly     assembly = Assembly.GetExecutingAssembly();
                Stream       stream   = assembly.GetManifestResourceStream("TextureReplacer.Visor-compiled.shader");
                StreamReader reader   = new StreamReader(stream);

                shaderMaterial = new Material(reader.ReadToEnd());
                visorShader    = shaderMaterial.shader;

                Util.log("Visor shader sucessfully compiled.");
            }
            catch
            {
                isVisorReflectionEnabled = false;
                Util.log("Visor shader loading failed. Visor reflections disabled.");
            }

            for (int i = 0; i < SHADER_MAP.GetLength(0); ++i)
            {
                Shader original   = Shader.Find(SHADER_MAP[i, 0]);
                Shader reflective = Shader.Find(SHADER_MAP[i, 1]);

                if (original == null)
                {
                    Util.log("Shader \"{0}\" missing", SHADER_MAP[i, 0]);
                }
                else if (reflective == null)
                {
                    Util.log("Shader \"{0}\" missing", SHADER_MAP[i, 1]);
                }
                else
                {
                    shaderMap[original] = reflective;
                }
            }

            setReflectionType(reflectionType);
        }
        /// ////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Post-load initialisation.
        /// called after the awake() of KSP
        /// </summary>
        /// ////////////////////////////////////////////////////////////////////////////////////////
        public void load()
        {
            Util.log("++++ 'load()' ++++");

            Texture2D[] envMapFaces = new Texture2D[6];

            // Foreach non-null Texture2D in any of the EnvMap Folders
            foreach (KeyValuePair <Texture2D, string> EnvMapTexture in Textures_Loader.ENVMAP())
            {
                Texture2D texture      = EnvMapTexture.Key;
                string    originalName = EnvMapTexture.Value;

                switch (originalName)
                {
                case "PositiveX":
                    envMapFaces[0] = texture;
                    break;

                case "NegativeX":
                    envMapFaces[1] = texture;
                    break;

                case "PositiveY":
                    envMapFaces[2] = texture;
                    break;

                case "NegativeY":
                    envMapFaces[3] = texture;
                    break;

                case "PositiveZ":
                    envMapFaces[4] = texture;
                    break;

                case "NegativeZ":
                    envMapFaces[5] = texture;
                    break;

                default:
                    Util.log("Invalid enironment map texture name {0}", texture.name);
                    break;
                }
            }

            // Generate generic reflection cube map texture.
            if (envMapFaces.Contains(null))
            {
                Util.log("Some environment map faces are missing. Static reflections disabled.");
            }
            else
            {
                int envMapSize = envMapFaces[0].width;

                if (envMapFaces.Any(t => t.width != envMapSize || t.height != envMapSize) ||
                    envMapFaces.Any(t => !Util.isPow2(t.width) || !Util.isPow2(t.height)))
                {
                    Util.log("Invalid environment map faces. Static reflections disabled.");
                }
                else
                {
                    try
                    {
                        staticEnvMap           = new Cubemap(envMapSize, TextureFormat.RGB24, true);
                        staticEnvMap.hideFlags = HideFlags.HideAndDontSave;
                        staticEnvMap.wrapMode  = TextureWrapMode.Clamp;
                        staticEnvMap.SetPixels(envMapFaces[0].GetPixels(), CubemapFace.PositiveX);
                        staticEnvMap.SetPixels(envMapFaces[1].GetPixels(), CubemapFace.NegativeX);
                        staticEnvMap.SetPixels(envMapFaces[2].GetPixels(), CubemapFace.PositiveY);
                        staticEnvMap.SetPixels(envMapFaces[3].GetPixels(), CubemapFace.NegativeY);
                        staticEnvMap.SetPixels(envMapFaces[4].GetPixels(), CubemapFace.PositiveZ);
                        staticEnvMap.SetPixels(envMapFaces[5].GetPixels(), CubemapFace.NegativeZ);
                        staticEnvMap.Apply(true, false);

                        Util.log("Static environment map cube texture generated.");
                    }
                    catch (UnityException)
                    {
                        if (staticEnvMap != null)
                        {
                            Object.DestroyImmediate(staticEnvMap);
                        }

                        staticEnvMap = null;

                        Util.log("Failed to set up static reflections. Textures not readable?");
                    }
                }
            }

            // we now save the visor shader in the placeholder. The shader got loaded through the ksp asset bundle
            visorShader = TextureReplacerReplaced.GetShader("KSP/TR/Visor");

            // fill the shaderMappings dict, if we find the right shader from the mapping config.
            // we could have used names here, but it is not in the fast path, so it is ok to leave it this way
            foreach (string origShaderName in shaderMappingConfig.Keys)
            {
                Shader original   = TextureReplacerReplaced.GetShader(origShaderName);
                Shader reflective = TextureReplacerReplaced.GetShader(shaderMappingConfig[origShaderName]);

                if (original == null || original.name == "Hidden/InternalErrorShader")
                {
                    Util.log("Shader \"{0}\" missing", origShaderName);
                }
                else if (reflective == null || reflective.name == "Hidden/InternalErrorShader")
                {
                    Util.log("Shader \"{0}\" missing", shaderMappingConfig[origShaderName]);
                }
                else
                {
                    shaderMappings[original] = reflective;
                }
            }

            setReflectionType(reflectionType);
        }
예제 #4
0
    private IEnumerator Process(IList <string> paths, Action <bool> worked)
    {
        if (GMapManager.BaseTexture == null)
        {
            StartCoroutine(GetImage(null, 0, x => GMapManager.BaseTexture = x.Texture));
            while (GMapManager.BaseTexture == null)
            {
                yield return(null);
            }
        }

        var outTexture = Instantiate(GMapManager.BaseTexture);
        var points     = new List <Point>();

        if (paths != null)
        {
            var pathTextures = new Texture2D[paths.Count];
            //for (var i = 0; i < paths.Count; i++) pathTextures[i] = null;

            for (var i = 0; i < paths.Count; i++)
            {
                StartCoroutine(GetImage(GMapManager.GetPathFromString(paths[i]), i, x => { pathTextures[x.Index] = x.Texture; }));
                if (i % 9 == 0)
                {
                    yield return(new WaitForSeconds(1f));          //не более 9 потоков за сек
                }
            }
            while (pathTextures.Any(p => p == null))
            {
                yield return(null);
            }

            //Debug.Log("all data recieved");

            for (var i = 0; i < paths.Count; i++)
            {
                for (var w = 0; w <= GMapManager.Size; w++)
                {
                    for (var h = 0; h <= GMapManager.Size; h++)
                    {
                        if (GMapManager.BaseTexture.GetPixel(w, h) != pathTextures[i].GetPixel(w, h))
                        {
                            //if (points.Count < 10000)
                            if (!points.Contains(new Point {
                                X = w, Y = h
                            }))
                            {
                                points.Add(new Point {
                                    X = w, Y = h
                                });
                            }
                        }
                    }
                }
            }
        }
        //Debug.Log("points=" + points.Count);
        foreach (var p in points)
        {
            outTexture.SetPixel(p.X, p.Y, Color.red);
        }
        outTexture.Apply();

        ApplyTexture(outTexture);

        worked(true);
        //Debug.Log("process END");
    }