Creating 2d water texture

May 30, 2010 at 3:27 PM
Edited May 30, 2010 at 6:26 PM

Hi!

I'm trying to implent the water in my game it works fine, but I need to be able to draw the water in 2D. Not throught 3d as the sample says. As I'm going to use Silversprite. I've tried uing this code:

                Texture2D water = new Texture2D(spriteBatch.GraphicsDevice, spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height);
                Color[,] data = new Color[spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height];
                for (int x = (int)screenPos.X; x < spriteBatch.GraphicsDevice.Viewport.Width + (int)screenPos.X; x++)
                {
                    int node = x / waves.NodeCount;
                    int Height = 0;

                    if(node + 1 < waves.NodeCount)
                        Height = (int)MathHelper.Lerp((int)waves.CurrentWave[node], (int)waves.CurrentWave[node + 1], x - ((node - 1) * waves.Width));
                    else
                        Height = (int)MathHelper.Lerp((int)waves.CurrentWave[node], (int)waves.CurrentWave[node - 1], x);

                    for (int y = (int)MathHelper.Clamp(Height, 0, spriteBatch.GraphicsDevice.Viewport.Height); y > 0; y--)
                    {
                        data[x, y] = new Color(Color.Aquamarine, 150);
                    }
                }
                Color[] dataC = new Color[spriteBatch.GraphicsDevice.Viewport.Width * spriteBatch.GraphicsDevice.Viewport.Height];
                for (int x = 0; x < spriteBatch.GraphicsDevice.Viewport.Width; x++)
                {
                    for (int y = 0; y < spriteBatch.GraphicsDevice.Viewport.Height; y++)
                        dataC[x * y] = data[x, y];
                }
                water.SetData(dataC);

                spriteBatch.Draw(water, waves.Position - screenPos, Color.White);

It's slow and doesn't eaven work!

Does anyone know a better way??