1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
4 using System.Collections.Generic;
15 public WaterVertexData(
float distortStrengthX,
float distortStrengthY,
float waterColorStrength,
float waterAlpha)
46 public readonly Color
waterColor =
new Color(0.75f * 0.5f, 0.8f * 0.5f, 0.9f * 0.5f, 1.0f);
53 public Dictionary<EntityGrid, VertexPositionColorTexture[]>
IndoorsVertices =
new Dictionary<EntityGrid, VertexPositionColorTexture[]>();
60 private BasicEffect basicEffect;
69 WaterEffect = EffectLoader.Load(
"Effects/watershader");
71 WaterTexture = TextureLoader.FromFile(
"Content/Effects/waterbump.png");
75 if (basicEffect ==
null)
79 VertexColorEnabled =
false,
85 private readonly VertexPositionColorTexture[] tempVertices =
new VertexPositionColorTexture[6];
86 private readonly Vector3[] tempCorners =
new Vector3[4];
90 spriteBatch.GraphicsDevice.BlendState = BlendState.NonPremultiplied;
92 WaterEffect.Parameters[
"xTexture"].SetValue(texture);
94 WaterEffect.Parameters[
"xWaveWidth"].SetValue(distortionStrength.X);
95 WaterEffect.Parameters[
"xWaveHeight"].SetValue(distortionStrength.Y);
117 offset.Y = -offset.Y;
119 WaterEffect.Parameters[
"xBumpPos"].SetValue(Vector2.Zero);
123 WaterEffect.Parameters[
"xBumpScale"].SetValue(
new Vector2(
129 * Matrix.CreateOrthographicOffCenter(0, spriteBatch.GraphicsDevice.Viewport.Width * 2, spriteBatch.GraphicsDevice.Viewport.Height * 2, 0, 0, 1) * Matrix.CreateTranslation(0.5f, 0.5f, 0.0f));
133 WaterEffect.Parameters[
"xBumpScale"].SetValue(
new Vector2(1.0f, 1.0f));
134 WaterEffect.Parameters[
"xTransform"].SetValue(Matrix.Identity * Matrix.CreateTranslation(-1.0f, 1.0f, 0.0f));
135 WaterEffect.Parameters[
"xUvTransform"].SetValue(Matrix.CreateScale(0.5f, -0.5f, 0.0f));
140 Rectangle view = cam !=
null ? cam.
WorldView : spriteBatch.GraphicsDevice.Viewport.Bounds;
142 tempCorners[0] =
new Vector3(view.X, view.Y, 0.1f);
143 tempCorners[1] =
new Vector3(view.Right, view.Y, 0.1f);
144 tempCorners[2] =
new Vector3(view.Right, view.Y - view.Height, 0.1f);
145 tempCorners[3] =
new Vector3(view.X, view.Y - view.Height, 0.1f);
148 tempVertices[0] =
new VertexPositionColorTexture(tempCorners[0], backGroundColor, Vector2.Zero);
149 tempVertices[1] =
new VertexPositionColorTexture(tempCorners[1], backGroundColor, Vector2.Zero);
150 tempVertices[2] =
new VertexPositionColorTexture(tempCorners[2], backGroundColor, Vector2.Zero);
151 tempVertices[3] =
new VertexPositionColorTexture(tempCorners[0], backGroundColor, Vector2.Zero);
152 tempVertices[4] =
new VertexPositionColorTexture(tempCorners[2], backGroundColor, Vector2.Zero);
153 tempVertices[5] =
new VertexPositionColorTexture(tempCorners[3], backGroundColor, Vector2.Zero);
155 spriteBatch.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, tempVertices, 0, 2);
162 if (subVerts.Key.Submarine !=
null) { offset -= subVerts.Key.Submarine.WorldPosition; }
165 offset += cam.Position -
new Vector2(cam.WorldView.Width / 2.0f, -cam.WorldView.Height / 2.0f);
166 offset.Y += cam.WorldView.Height;
167 offset.X += cam.WorldView.Width;
170 offset.Y = -offset.Y;
175 spriteBatch.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, subVerts.Value, 0,
PositionInIndoorsBuffer[subVerts.Key] / 3);
178 WaterEffect.Parameters[
"xTexture"].SetValue((Texture2D)
null);
187 public void RenderAir(GraphicsDevice graphicsDevice,
Camera cam, RenderTarget2D texture, Matrix transform)
191 basicEffect.Texture = texture;
193 basicEffect.View = Matrix.Identity;
194 basicEffect.World = transform
196 basicEffect.CurrentTechnique.Passes[0].Apply();
198 graphicsDevice.SamplerStates[0] = SamplerState.PointWrap;
199 graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList,
vertices, 0,
PositionInBuffer / 3);
201 basicEffect.Texture =
null;
202 basicEffect.CurrentTechnique.Passes[0].Apply();
205 private readonly List<EntityGrid> buffersToRemove =
new List<EntityGrid>();
210 buffersToRemove.Clear();
213 if (buffer.Submarine?.Removed ??
false)
215 buffersToRemove.Add(buffer);
218 foreach (var bufferToRemove
in buffersToRemove)
232 if (basicEffect !=
null)
234 basicEffect.Dispose();
static int GraphicsHeight
void RenderAir(GraphicsDevice graphicsDevice, Camera cam, RenderTarget2D texture, Matrix transform)
const int DefaultIndoorsBufferSize
VertexPositionTexture[] vertices
readonly WaterVertexData IndoorsSurfaceBottomColor
WaterRenderer(GraphicsDevice graphicsDevice)
void ScrollWater(Vector2 vel, float deltaTime)
void RenderWater(SpriteBatch spriteBatch, RenderTarget2D texture, Camera cam)
static Vector2 DistortionStrength
const int DefaultBufferSize
static Vector2 DistortionScale
Dictionary< EntityGrid, int > PositionInIndoorsBuffer
static WaterRenderer Instance
readonly Color waterColor
Dictionary< EntityGrid, VertexPositionColorTexture[]> IndoorsVertices
readonly WaterVertexData IndoorsWaterColor
readonly WaterVertexData IndoorsSurfaceTopColor
WaterVertexData(float distortStrengthX, float distortStrengthY, float waterColorStrength, float waterAlpha)