Exemplo n.º 1
0
        public static bool GenerateBlankLevelBlock(Level level, Vector3 coordinate, string defaultTextureName)
        {
            if (level.Contains(coordinate))
            {
                return false;
            }

            string[] detailTextureNames = new string[5] { defaultTextureName, null, null, null, null };
            Vector2[] uvOffsets = new Vector2[5] { Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero };
            Vector2[] uvScales = new Vector2[5] { Vector2.One, Vector2.One, Vector2.One, Vector2.One, Vector2.One };

            Color[] blankHeightMap = new Color[HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_VERTICES];
            for (int i = 0; i < blankHeightMap.Length; i++)
            {
                blankHeightMap[i] = new Color(0, 0, 0, 0);
            }
            Texture2D blankHeightMapTexture = new Texture2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES);

            Color[] blankAlphaMap = new Color[HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD];
            for (int i = 0; i < blankAlphaMap.Length; i++)
            {
                blankAlphaMap[i] = new Color(0, 0, 0, 0);
            }
            Texture2D blankAlphaMapTexture = new Texture2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD, HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD);

            return AddNewLevelBlock(level, coordinate, blankHeightMapTexture, blankAlphaMapTexture, detailTextureNames, uvOffsets, uvScales);
        }
Exemplo n.º 2
0
        public static bool AddNewLevelBlock(Level level, Vector3 coordinate, Texture2D heightMap, Texture2D alphaMap, string[] detailTextureNames, Vector2[] uvOffsets, Vector2[] uvScales)
        {
            if (level.Contains(coordinate))
            {
                return false;
            }

            Color[] heightColors = new Color[HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_VERTICES];
            if (heightMap.Width != HeightMapMesh.NUM_SIDE_VERTICES || heightMap.Height != HeightMapMesh.NUM_SIDE_VERTICES)
            {
                RenderTarget2D resizedHeightMap = new RenderTarget2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES);
                GraphicsManager.Device.SetRenderTarget(resizedHeightMap);

                GraphicsManager.SpriteBatch.Begin();
                GraphicsManager.SpriteBatch.Draw(heightMap, new Rectangle(0, 0, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES), Color.White);
                GraphicsManager.SpriteBatch.End();

                GraphicsManager.Device.SetRenderTarget(null);
                resizedHeightMap.GetData(heightColors);
            }
            else
            {
                heightMap.GetData(heightColors);
            }

            float[,] heights = new float[HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES];
            for (int i = 0; i < heightColors.Length; i++)
            {
                heights[i % HeightMapMesh.NUM_SIDE_VERTICES, i / HeightMapMesh.NUM_SIDE_VERTICES] = ConvertColorToFloat(heightColors[i]);
            }

            if (alphaMap.Height != HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD ||
                alphaMap.Width != HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD)
            {
                RenderTarget2D resizedAlphaMap = new RenderTarget2D(GraphicsManager.Device,
                    HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD,
                    HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD);

                GraphicsManager.Device.SetRenderTarget(resizedAlphaMap);

                GraphicsManager.SpriteBatch.Begin();
                GraphicsManager.SpriteBatch.Draw(alphaMap,
                    new Rectangle(0,
                        0,
                        HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD,
                        HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD),
                    Color.White);
                GraphicsManager.SpriteBatch.End();

                GraphicsManager.Device.SetRenderTarget(null);

                alphaMap = resizedAlphaMap;
            }

            HeightMapMesh heightMapMesh = new HeightMapMesh(heights, alphaMap, detailTextureNames, uvOffsets, uvScales);

            AssetLibrary.AddHeightMap(level.Name + coordinate.ToString(), heightMapMesh);
            level.AddNewBlock(coordinate);
            return true;
        }