public void SetupGlitch(Texture2D map, GlitchParams param, Random random, Matrix transform, Matrix projection) { Shader.CurrentTechnique = Shader.Techniques["Glitch"]; Shader.Parameters["glitch_intensity"].SetValue(param.Intensity); Shader.Parameters["glitch_time"].SetValue((float)Frame); Shader.Parameters["glitch_resolution"].SetValue(new Vector2(Viewport.Width, Viewport.Height)); Shader.Parameters["glitch_rng_seed"].SetValue(Math.Max(param.Seed, 0.000001f)); Shader.Parameters["glitch_random_values"].SetValue(new Vector3(random.NextFloat(), random.NextFloat(), random.NextFloat())); Shader.Parameters["glitch_noise_texture"].SetValue(map); Shader.Parameters["glitch_line_speed"].SetValue(param.LineSpeed); Shader.Parameters["glitch_line_drift"].SetValue(param.LineDrift); Shader.Parameters["glitch_line_resolution"].SetValue(Math.Max(param.LineResolution, 0.000001f)); Shader.Parameters["glitch_line_vert_shift"].SetValue(param.LineVerticalShift); Shader.Parameters["glitch_line_shift"].SetValue(param.LineShift); Shader.Parameters["glitch_jumbleness"].SetValue(param.Jumbleness); Shader.Parameters["glitch_jumble_resolution"].SetValue(param.JumbleResolution); Shader.Parameters["glitch_jumble_shift"].SetValue(param.JumbleShift); Shader.Parameters["glitch_jumble_speed"].SetValue(param.JumbleSpeed); Shader.Parameters["glitch_dispersion"].SetValue(param.Dispersion); Shader.Parameters["glitch_channel_shift"].SetValue(param.ChannelShift); Shader.Parameters["glitch_noise_level"].SetValue(param.NoiseLevel); Shader.Parameters["glitch_shakiness"].SetValue(param.Shakiness); Shader.Parameters["WorldViewProjection"].SetValue(transform * projection); }
public override void Draw(GameTime gameTime) { DrawTextures(); GraphicsDevice.SetRenderTarget(null); if (CameraTargetA == null || CameraTargetA.IsContentLost) { CameraTargetA = new RenderTarget2D(GraphicsDevice, Viewport.Width, Viewport.Height); } if (CameraTargetB == null || CameraTargetB.IsContentLost) { CameraTargetB = new RenderTarget2D(GraphicsDevice, Viewport.Width, Viewport.Height); } if (DistortionMap == null || DistortionMap.IsContentLost) { DistortionMap = new RenderTarget2D(GraphicsDevice, Viewport.Width, Viewport.Height); } Projection = Matrix.CreateOrthographicOffCenter(0, Viewport.Width, Viewport.Height, 0, 0, -1); WorldTransform = CreateViewMatrix(); IEnumerable <VisualEffect> visualEffects = VisualEffects.Where(x => x.ShouldDraw(CameraMap)); IEnumerable <ScreenShake> screenShakes = visualEffects.OfType <ScreenShake>(); if (screenShakes.Any()) { ScreenShake screenShake = screenShakes.WithMax(effect => effect.Offset.LengthSquared()); if (screenShake != null) { WorldTransform *= Matrix.CreateTranslation(screenShake.Offset.X, screenShake.Offset.Y, 0); } } var gameObjects = GameObjects.Where(x => x.ShouldDraw(CameraMap)); var tiles = DrawMap(CameraMap); var drawPasses = gameObjects.Concat(tiles).ToMultiLookup(x => x.GetDrawPasses()); GraphicsDevice.SetRenderTarget(DistortionMap); /*var noise = SpriteLoader.Instance.AddSprite("content/noise"); * var noiseOffset = Util.AngleToVector(Frame * 0.1f) * 30; * noiseOffset = new Vector2(-Frame * 0.2f, -Frame * 0.5f); * PushSpriteBatch(samplerState: SamplerState.LinearWrap, blendState: BlendState.Additive); * SpriteBatch.Draw(Pixel, DistortionMap.Bounds, new Color(0,64,0)); * SpriteBatch.Draw(noise.Texture, DistortionMap.Bounds, new Rectangle((int)noiseOffset.X, (int)noiseOffset.Y, DistortionMap.Width, DistortionMap.Height), Color.Red); * PopSpriteBatch();*/ PushSpriteBatch(samplerState: SamplerState.PointWrap, blendState: NonPremultiplied, transform: WorldTransform, projection: Projection); drawPasses.DrawPass(this, DrawPass.SeaDistort); PopSpriteBatch(); GraphicsDevice.SetRenderTarget(CameraTargetA); //Render Liquid to Target A PushSpriteBatch(samplerState: SamplerState.PointWrap, blendState: NonPremultiplied, transform: WorldTransform, projection: Projection); drawPasses.DrawPass(this, DrawPass.SeaFloor); drawPasses.DrawPass(this, DrawPass.Sea); PopSpriteBatch(); GraphicsDevice.SetRenderTarget(CameraTargetB); SwapBuffers(); PushSpriteBatch(samplerState: SamplerState.PointWrap, blendState: NonPremultiplied, transform: WorldTransform, projection: Projection); drawPasses.DrawPass(this, DrawPass.LiquidFloor); //Render Target A (Liquid) to Target B (with distortion) PushSpriteBatch(samplerState: SamplerState.PointWrap, blendState: NonPremultiplied, transform: Matrix.Identity, shader: Shader, shaderSetup: (matrix, projection) => { SetupDistortion(DistortionMap, new Vector2(30f / DistortionMap.Width, 30f / DistortionMap.Height), Matrix.Identity, Matrix.Identity, Projection); }); SpriteBatch.Draw(CameraTargetB, CameraTargetB.Bounds, Color.White); //SpriteBatch.Draw(DistortionMap, DistortionMap.Bounds, Color.White); PopSpriteBatch(); drawPasses.DrawPass(this, DrawPass.Liquid); //Render Map drawPasses.DrawPass(this, DrawPass.Tile); drawPasses.DrawPass(this, DrawPass.Item); drawPasses.DrawPass(this, DrawPass.EffectLow); PushSpriteBatch(blendState: BlendState.Additive); drawPasses.DrawPass(this, DrawPass.EffectLowAdditive); PopSpriteBatch(); drawPasses.DrawPass(this, DrawPass.Creature); drawPasses.DrawPass(this, DrawPass.Effect); PushSpriteBatch(blendState: BlendState.Additive); drawPasses.DrawPass(this, DrawPass.EffectAdditive); PopSpriteBatch(); PopSpriteBatch(); GraphicsDevice.SetRenderTarget(CameraTargetB); SwapBuffers(); //Draw screenflashes ColorMatrix color = ColorMatrix.Identity; IEnumerable <ScreenFlash> screenFlashes = visualEffects.OfType <ScreenFlash>(); foreach (ScreenFlash screenFlash in screenFlashes) { color *= screenFlash.Color; } //SetupColorMatrix(color, Matrix.Identity, Projection); //SetupGlitch(Game.Noise, Matrix.Identity, Projection, Random); PushSpriteBatch(samplerState: SamplerState.PointWrap, blendState: NonPremultiplied, shader: Shader, shaderSetup: (transform, projection) => { SetupColorMatrix(color, Matrix.Identity, Projection); }); SpriteBatch.Draw(CameraTargetB, CameraTargetB.Bounds, Color.White); PopSpriteBatch(); GraphicsDevice.SetRenderTarget(CameraTargetB); SwapBuffers(); //Draw glitches IEnumerable <ScreenGlitch> screenGlitches = visualEffects.OfType <ScreenGlitch>(); foreach (var glitch in screenGlitches) { GlitchParams glitchParams = glitch.Glitch; PushSpriteBatch(samplerState: SamplerState.PointWrap, blendState: NonPremultiplied, shader: Shader, shaderSetup: (transform, projection) => { SetupGlitch(Game.Noise, glitchParams, Random, Matrix.Identity, Projection); }); SpriteBatch.Draw(CameraTargetB, CameraTargetB.Bounds, Color.White); PopSpriteBatch(); GraphicsDevice.SetRenderTarget(CameraTargetB); SwapBuffers(); } //Draw to screen GraphicsDevice.SetRenderTarget(null); PushSpriteBatch(samplerState: SamplerState.PointWrap, blendState: NonPremultiplied, shader: Shader, shaderSetup: (transform, projection) => { SetupColorMatrix(ColorMatrix.Identity, Matrix.Identity, Projection); }); SpriteBatch.Draw(CameraTargetB, CameraTargetB.Bounds, Color.White); PopSpriteBatch(); SpriteReference cursor_tile = SpriteLoader.Instance.AddSprite("content/cursor_tile"); SetupNormal(Matrix.Identity, Projection); //SpriteBatch.Begin(blendState: NonPremultiplied, rasterizerState: RasterizerState.CullNone, samplerState: SamplerState.PointWrap, transformMatrix: WorldTransform); PushSpriteBatch(blendState: NonPremultiplied, samplerState: SamplerState.PointWrap, transform: WorldTransform, projection: Projection); if (TileCursor.HasValue) { DrawSprite(cursor_tile, Frame / 8, new Vector2(TileCursor.Value.X * 16, TileCursor.Value.Y * 16), SpriteEffects.None, 0); } DrawQuests(CameraMap); drawPasses.DrawPass(this, DrawPass.UIWorld); //SpriteBatch.End(); PopSpriteBatch(); //SetupNormal(Matrix.Identity); //SpriteBatch.Begin(blendState: NonPremultiplied, rasterizerState: RasterizerState.CullNone, samplerState: SamplerState.PointWrap); PushSpriteBatch(blendState: NonPremultiplied, samplerState: SamplerState.PointWrap, projection: Projection); DrawQuestText(CameraMap); drawPasses.DrawPass(this, DrawPass.UI); Menu.Draw(this); DrawTooltip(); PopSpriteBatch(); }