1 | using Jypeli; |
---|
2 | using Microsoft.Xna.Framework; |
---|
3 | using Microsoft.Xna.Framework.Content; |
---|
4 | using Microsoft.Xna.Framework.Graphics; |
---|
5 | using System; |
---|
6 | using System.Collections.Generic; |
---|
7 | using System.Linq; |
---|
8 | using System.Text; |
---|
9 | |
---|
10 | class Shader |
---|
11 | { |
---|
12 | private Effect effect; |
---|
13 | private RenderTarget2D renderTarget; |
---|
14 | private SpriteBatch spriteBatch; |
---|
15 | private GraphicsDevice device; |
---|
16 | private Camera camera; |
---|
17 | |
---|
18 | struct Shockwave |
---|
19 | { |
---|
20 | public Vector Center; |
---|
21 | public double Time; |
---|
22 | } |
---|
23 | |
---|
24 | private const int MAX_WAVES = 3; |
---|
25 | private readonly Shockwave[] waves = new Shockwave[MAX_WAVES]; |
---|
26 | private int waveIndex = 0; |
---|
27 | |
---|
28 | public Shader(GraphicsDevice device, ContentManager content, Camera camera) |
---|
29 | { |
---|
30 | for (int i = 0; i < MAX_WAVES; i++) |
---|
31 | { |
---|
32 | waves[i].Time = 1000f; |
---|
33 | } |
---|
34 | |
---|
35 | renderTarget = new RenderTarget2D(device, |
---|
36 | device.PresentationParameters.BackBufferWidth, |
---|
37 | device.PresentationParameters.BackBufferHeight); |
---|
38 | |
---|
39 | effect = content.Load<Effect>("Shockwave"); |
---|
40 | spriteBatch = new SpriteBatch(device); |
---|
41 | this.camera = camera; |
---|
42 | this.device = device; |
---|
43 | } |
---|
44 | |
---|
45 | public void NewWave(Vector position) |
---|
46 | { |
---|
47 | waves[waveIndex].Time = 0f; |
---|
48 | waves[waveIndex].Center = position; |
---|
49 | waveIndex = ++waveIndex % MAX_WAVES; |
---|
50 | } |
---|
51 | |
---|
52 | public void Draw(GameTime gameTime, Action<GameTime> baseDraw) |
---|
53 | { |
---|
54 | |
---|
55 | device.Clear(Microsoft.Xna.Framework.Color.Black); |
---|
56 | device.SetRenderTarget(renderTarget); |
---|
57 | |
---|
58 | baseDraw(gameTime); |
---|
59 | |
---|
60 | device.SetRenderTarget(null); |
---|
61 | |
---|
62 | var projection = Matrix.CreateOrthographicOffCenter(0, |
---|
63 | device.Viewport.Width, device.Viewport.Height, 0, 0, 1); |
---|
64 | var halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0); |
---|
65 | effect.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection); |
---|
66 | |
---|
67 | Vector resolution = new Vector(device.Viewport.Width, device.Viewport.Height); |
---|
68 | Vector2 resolution2 = new Vector2(device.Viewport.Width, device.Viewport.Height); |
---|
69 | effect.Parameters["Resolution"].SetValue(resolution2); |
---|
70 | |
---|
71 | // TestShader |
---|
72 | //effect.Parameters["Resolution"].SetValue(new Vector2(GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight)); |
---|
73 | //effect.Parameters["Misalign"].SetValue(0.003f); |
---|
74 | //effect.Parameters["Time"].SetValue(100f * (float)gameTime.TotalGameTime.TotalSeconds); |
---|
75 | |
---|
76 | // Shockwave |
---|
77 | var waveArray = new Vector4[MAX_WAVES]; |
---|
78 | for (int i = 0; i < MAX_WAVES; i++) |
---|
79 | { |
---|
80 | waves[i].Time += 0.5 * gameTime.ElapsedGameTime.TotalSeconds; |
---|
81 | Vector screenCoord = camera.WorldToScreen(waves[i].Center); |
---|
82 | screenCoord.Y = resolution.Y - screenCoord.Y; |
---|
83 | screenCoord.X /= resolution.X; |
---|
84 | screenCoord.Y /= resolution.Y; |
---|
85 | screenCoord += new Vector(0.5f, -0.5f); |
---|
86 | |
---|
87 | var v = new Vector4((float)screenCoord.X, (float)screenCoord.Y, (float)waves[i].Time, 1f); |
---|
88 | waveArray[i] = v; |
---|
89 | } |
---|
90 | effect.Parameters["Waves"].SetValue(waveArray); |
---|
91 | |
---|
92 | device.Clear(Microsoft.Xna.Framework.Color.Black); |
---|
93 | spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); |
---|
94 | effect.CurrentTechnique.Passes[0].Apply(); |
---|
95 | spriteBatch.Draw(renderTarget, Vector2.Zero, Microsoft.Xna.Framework.Color.White); |
---|
96 | spriteBatch.End(); |
---|
97 | } |
---|
98 | } |
---|