Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void LoadContent(IServiceProvider serviceProvider, GraphicsDevice graphics)
- {
- PresentationParameters pp = graphics.PresentationParameters;
- int width = pp.BackBufferWidth;
- int height = pp.BackBufferHeight;
- SurfaceFormat format = pp.BackBufferFormat;
- combinedTarget = new RenderTarget2D(graphics, width, height);
- normalTarget = new RenderTarget2D(graphics, width, height);
- refractTarget = new RenderTarget2D(graphics, width, height);
- colorTechnique = ColorEffect.Techniques["ColorFilter"];
- refractTechnique = ColorEffect.Techniques["ColorRefract"];
- }
- public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
- {
- //sw.Reset();
- //sw.Start();
- spriteBatch.GraphicsDevice.SetRenderTarget(normalTarget);
- spriteBatch.GraphicsDevice.Clear(Color.Transparent);
- DrawNormalTarget(spriteBatch);
- spriteBatch.GraphicsDevice.SetRenderTarget(null);
- spriteBatch.GraphicsDevice.SetRenderTarget(refractTarget);
- spriteBatch.GraphicsDevice.Clear(Color.Transparent);
- DrawEffectsTarget(spriteBatch);
- spriteBatch.GraphicsDevice.SetRenderTarget(null);
- GenerateCombineTexture(spriteBatch);
- spriteBatch.GraphicsDevice.Clear(Color.Black);
- DrawCombinedTargets(spriteBatch);
- SetShaderParameters();
- #region hud
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, Resolution.getTransformationMatrix());
- //hud draw and stuff i don't want the camara to resize or changed via shader
- #endregion
- }
- private void DrawNormalTarget(SpriteBatch spriteBatch)
- {
- SetShaderParameters();
- spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, ColorEffect, camara2d.camaraMatrix * Resolution.getTransformationMatrix());
- //game draws such as entities, particles, etc
- spriteBatch.End();
- }
- private void DrawEffectsTarget(SpriteBatch spriteBatch)
- {
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, camara2d.camaraMatrix * Resolution.getTransformationMatrix());
- // simple texture to create the refraction effect
- spriteBatch.Draw(shockWave, player[0].Position, Color.White);
- spriteBatch.End();
- }
- private Texture2D GenerateCombineTexture(SpriteBatch spriteBatch)
- {
- spriteBatch.GraphicsDevice.SetRenderTarget(combinedTarget);
- spriteBatch.GraphicsDevice.Clear(Color.Transparent);
- spriteBatch.GraphicsDevice.Textures[1] = refractTarget;
- ResetShaderParameters();
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, ColorEffect, Resolution.getTransformationMatrix());
- spriteBatch.Draw(normalTarget, Vector2.Zero, Color.Red);
- spriteBatch.End();
- spriteBatch.GraphicsDevice.Textures[1] = null;
- spriteBatch.GraphicsDevice.SetRenderTarget(null);
- return combinedTarget;
- }
- private void DrawCombinedTargets(SpriteBatch spriteBatch)
- {
- spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null);
- spriteBatch.Draw(combinedTarget, Vector2.Zero, Color.White);
- spriteBatch.End();
- spriteBatch.GraphicsDevice.SetRenderTarget(null);
- }
- private void SetShaderParameters()
- {
- ColorEffect.Parameters["burn"].SetValue(0f);
- ColorEffect.Parameters["saturation"].SetValue(1f);
- ColorEffect.Parameters["r"].SetValue(.5f);
- ColorEffect.Parameters["g"].SetValue(.5f);
- ColorEffect.Parameters["b"].SetValue(.75f);
- ColorEffect.Parameters["brite"].SetValue(0f);
- ColorEffect.CurrentTechnique = colorTechnique;
- }
- private void ResetShaderParameters()
- {
- ColorEffect.Parameters["burn"].SetValue(0);
- ColorEffect.Parameters["saturation"].SetValue(1);
- ColorEffect.Parameters["r"].SetValue(1);
- ColorEffect.Parameters["g"].SetValue(1);
- ColorEffect.Parameters["b"].SetValue(1);
- ColorEffect.Parameters["brite"].SetValue(0);
- ColorEffect.CurrentTechnique = refractTechnique;
- }
- Shader :
- //filter.fx
- sampler samplerState;
- sampler refractSampler;
- float burn = 0.01f;
- float saturation = 1.0f;
- float r = 1.0f;
- float g = 1.0f;
- float b = 1.0f;
- float brite = 0.0f;
- bool slow = false;
- float fadeTimer;
- struct PS_INPUT
- {
- float2 TexCoord : TEXCOORD0;
- };
- float2 GetDif(float2 _tex)
- {
- float2 dif;
- float2 tex = _tex;
- float2 btex = _tex;
- tex.x -= 0.003;
- btex.x += 0.003;
- dif.x = tex2D(refractSampler, tex).r - tex2D(refractSampler, btex).r;
- tex = _tex;
- btex = _tex;
- tex.y -= 0.003;
- btex.y += 0.003;
- dif.y = tex2D(refractSampler, tex).r - tex2D(refractSampler, btex).r;
- tex = _tex;
- dif *= (1.5 - tex2D(refractSampler, tex).r);
- return dif;
- }
- float4 Filter(float4 color: COLOR0, float2 tex: TEXCOORD0) : COLOR0
- {
- float4 col = tex2D(samplerState, tex);
- //float d = sqrt(pow((tex.x - 0.5), 2) + pow((tex.y - 0.5), 2));
- //col.rgb -= d * burn;
- float a = col.r + col.g + col.b;
- a *= 0.333f;
- a *= 1.0f - saturation;
- col.r = (col.r * saturation + a) * r;
- col.g = (col.g * saturation + a) * g;
- col.b = (col.b * saturation + a) * b;
- col.rgb += brite;
- return (col * color) * color.a;
- }
- float4 Refract(PS_INPUT Input) : COLOR0
- {
- float2 tex = Input.TexCoord + GetDif(Input.TexCoord) * 0.1f;
- float4 col = tex2D(samplerState, tex);
- //float d = sqrt(pow((tex.x - 0.5), 2) + pow((tex.y - 0.5), 2));
- //col.rgb -= d * burn;
- float a = col.r + col.g + col.b;
- a *= 0.333f;
- a *= 1.0f - saturation;
- col.r = (col.r * saturation + a) * r;
- col.g = (col.g * saturation + a) * g;
- col.b = (col.b * saturation + a) * b;
- col.rgb += brite;
- return col;
- }
- technique ColorFilter
- {
- pass P0
- {
- PixelShader = compile ps_2_0 Filter();
- }
- }
- technique ColorRefract
- {
- pass P0
- {
- PixelShader = compile ps_2_0 Refract();
- }
- }
- Camera class :
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace NobuFatRevenge
- {
- public class Camara2d
- {
- public Vector2 ScreenCenter
- {
- get;
- protected set;
- }
- protected float viewportHeight;
- protected float viewportWidth;
- private Vector2 Posicao;
- float timer;
- float camaraSpeed;
- private float Zoom;
- private Rectangle? limits;
- public Vector2 posicao
- {
- get { return Posicao; }
- set
- {
- Posicao = value;
- ValidatePosition();
- }
- }
- public float zoom
- {
- get { return Zoom; }
- set
- {
- Zoom = MathHelper.Max(value, .01f);
- ValidateZoom();
- ValidatePosition();
- }
- }
- public Rectangle? Limits
- {
- get { return limits; }
- set
- {
- limits = value;
- ValidateZoom();
- ValidatePosition();
- }
- }
- float previousZoom;
- public Rectangle screenCheckRec;
- public Vector2 previousPosition;
- float newZoom = 1f;
- Rectangle? newLimits;
- public Camara2d()
- {
- viewportWidth = Resolution.gameResolutionWidth;
- viewportHeight = Resolution.gameResolutionHeight;
- ScreenCenter = new Vector2(viewportWidth / 2, viewportHeight / 2);
- }
- public void Update(GameTime gameTime, Player[] player)
- {
- }
- public Matrix camaraMatrix
- {
- get
- {
- return Matrix.CreateTranslation(new Vector3(-Posicao, 0f)) *
- Matrix.CreateScale(Zoom, Zoom, 1f) *
- Matrix.CreateTranslation(new Vector3(ScreenCenter, 0f));
- }
- }
- public Matrix camaraBackgroundMatrix
- {
- get
- {
- return Matrix.CreateTranslation(new Vector3(new Vector2(-1280, -Posicao.Y), 0f)) *
- Matrix.CreateScale(Zoom, Zoom, 1f) *
- Matrix.CreateTranslation(new Vector3(ScreenCenter, 0f));
- }
- }
- private void ValidateZoom()
- {
- if (limits.HasValue)
- {
- float minZoomX = (float)(viewportWidth / limits.Value.Width);
- float minZoomY = (float)(viewportHeight / limits.Value.Height);
- Zoom = MathHelper.Max(Zoom, MathHelper.Max(minZoomX, minZoomY));
- }
- }
- private void ValidatePosition()
- {
- if (limits.HasValue)
- {
- Vector2 cameraWorldMin = Vector2.Transform(Vector2.Zero, Matrix.Invert(camaraMatrix));
- Vector2 cameraSize = new Vector2(viewportWidth, viewportHeight) / Zoom;
- Vector2 limitWorldMin = new Vector2(limits.Value.Left, limits.Value.Top);
- Vector2 limitWorldMax = new Vector2(limits.Value.Right, limits.Value.Bottom);
- Vector2 positionOffSet = Posicao - cameraWorldMin;
- Posicao = Vector2.Clamp(cameraWorldMin, limitWorldMin, limitWorldMax - cameraSize) + positionOffSet;
- }
- }
- protected void SetRectangle(int x, int y, int width, int height)
- {
- screenCheckRec.X = x;
- screenCheckRec.Y = y;
- screenCheckRec.Width = width;
- screenCheckRec.Height = height;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement