Advertisement
Guest User

Untitled

a guest
Feb 4th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.05 KB | None | 0 0
  1.  public void LoadContent(IServiceProvider serviceProvider, GraphicsDevice graphics)
  2.         {
  3.             PresentationParameters pp = graphics.PresentationParameters;
  4.             int width = pp.BackBufferWidth;
  5.             int height = pp.BackBufferHeight;
  6.             SurfaceFormat format = pp.BackBufferFormat;
  7.  
  8.             combinedTarget = new RenderTarget2D(graphics, width, height);
  9.             normalTarget = new RenderTarget2D(graphics, width, height);
  10.             refractTarget = new RenderTarget2D(graphics, width, height);
  11.  
  12.             colorTechnique = ColorEffect.Techniques["ColorFilter"];
  13.             refractTechnique = ColorEffect.Techniques["ColorRefract"];
  14.         }
  15.          
  16.          public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
  17.         {
  18.             //sw.Reset();
  19.             //sw.Start();
  20.  
  21.             spriteBatch.GraphicsDevice.SetRenderTarget(normalTarget);
  22.             spriteBatch.GraphicsDevice.Clear(Color.Transparent);
  23.  
  24.             DrawNormalTarget(spriteBatch);
  25.  
  26.             spriteBatch.GraphicsDevice.SetRenderTarget(null);
  27.             spriteBatch.GraphicsDevice.SetRenderTarget(refractTarget);
  28.             spriteBatch.GraphicsDevice.Clear(Color.Transparent);
  29.  
  30.             DrawEffectsTarget(spriteBatch);
  31.  
  32.             spriteBatch.GraphicsDevice.SetRenderTarget(null);
  33.  
  34.             GenerateCombineTexture(spriteBatch);
  35.  
  36.             spriteBatch.GraphicsDevice.Clear(Color.Black);
  37.  
  38.             DrawCombinedTargets(spriteBatch);
  39.  
  40.             SetShaderParameters();
  41.             #region hud
  42.  
  43.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, Resolution.getTransformationMatrix());
  44.  
  45.             //hud draw and stuff i don't want the camara to resize or changed via shader
  46.             #endregion
  47.         }
  48.  
  49.         private void DrawNormalTarget(SpriteBatch spriteBatch)
  50.         {
  51.             SetShaderParameters();
  52.  
  53.             spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, ColorEffect, camara2d.camaraMatrix * Resolution.getTransformationMatrix());
  54.  
  55.             //game draws such as entities, particles, etc
  56.  
  57.             spriteBatch.End();
  58.         }
  59.  
  60.         private void DrawEffectsTarget(SpriteBatch spriteBatch)
  61.         {
  62.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, camara2d.camaraMatrix * Resolution.getTransformationMatrix());
  63.  
  64.             // simple texture to create the refraction effect  
  65.             spriteBatch.Draw(shockWave, player[0].Position, Color.White);
  66.  
  67.             spriteBatch.End();
  68.         }
  69.  
  70.         private Texture2D GenerateCombineTexture(SpriteBatch spriteBatch)
  71.         {
  72.             spriteBatch.GraphicsDevice.SetRenderTarget(combinedTarget);
  73.             spriteBatch.GraphicsDevice.Clear(Color.Transparent);
  74.  
  75.             spriteBatch.GraphicsDevice.Textures[1] = refractTarget;
  76.  
  77.             ResetShaderParameters();
  78.  
  79.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, ColorEffect, Resolution.getTransformationMatrix());
  80.  
  81.             spriteBatch.Draw(normalTarget, Vector2.Zero, Color.Red);
  82.  
  83.             spriteBatch.End();
  84.  
  85.             spriteBatch.GraphicsDevice.Textures[1] = null;
  86.             spriteBatch.GraphicsDevice.SetRenderTarget(null);
  87.  
  88.             return combinedTarget;
  89.         }
  90.  
  91.         private void DrawCombinedTargets(SpriteBatch spriteBatch)
  92.         {
  93.             spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null);
  94.  
  95.             spriteBatch.Draw(combinedTarget, Vector2.Zero, Color.White);
  96.  
  97.             spriteBatch.End();
  98.  
  99.             spriteBatch.GraphicsDevice.SetRenderTarget(null);
  100.         }
  101.  
  102.         private void SetShaderParameters()
  103.         {
  104.             ColorEffect.Parameters["burn"].SetValue(0f);
  105.             ColorEffect.Parameters["saturation"].SetValue(1f);
  106.             ColorEffect.Parameters["r"].SetValue(.5f);
  107.             ColorEffect.Parameters["g"].SetValue(.5f);
  108.             ColorEffect.Parameters["b"].SetValue(.75f);
  109.             ColorEffect.Parameters["brite"].SetValue(0f);
  110.             ColorEffect.CurrentTechnique = colorTechnique;
  111.         }
  112.  
  113.         private void ResetShaderParameters()
  114.         {
  115.             ColorEffect.Parameters["burn"].SetValue(0);
  116.             ColorEffect.Parameters["saturation"].SetValue(1);
  117.             ColorEffect.Parameters["r"].SetValue(1);
  118.             ColorEffect.Parameters["g"].SetValue(1);
  119.             ColorEffect.Parameters["b"].SetValue(1);
  120.             ColorEffect.Parameters["brite"].SetValue(0);
  121.  
  122.             ColorEffect.CurrentTechnique = refractTechnique;
  123.         }
  124.  
  125. Shader :
  126.  
  127. //filter.fx
  128. sampler samplerState;
  129. sampler refractSampler;
  130.  
  131. float burn = 0.01f;
  132. float saturation = 1.0f;
  133. float r = 1.0f;
  134. float g = 1.0f;
  135. float b = 1.0f;
  136. float brite = 0.0f;
  137. bool slow = false;
  138. float fadeTimer;
  139.  
  140. struct PS_INPUT
  141. {
  142.     float2 TexCoord : TEXCOORD0;
  143. };
  144.  
  145. float2 GetDif(float2 _tex)  
  146. {
  147.     float2 dif;
  148.      
  149.     float2 tex = _tex;
  150.     float2 btex = _tex;
  151.     tex.x -= 0.003;
  152.     btex.x += 0.003;
  153.     dif.x = tex2D(refractSampler, tex).r - tex2D(refractSampler, btex).r;
  154.      
  155.     tex = _tex;
  156.     btex = _tex;
  157.     tex.y -= 0.003;
  158.     btex.y += 0.003;
  159.      
  160.     dif.y = tex2D(refractSampler, tex).r - tex2D(refractSampler, btex).r;
  161.      
  162.     tex = _tex;
  163.     dif *= (1.5 - tex2D(refractSampler, tex).r);
  164.      
  165.     return dif;
  166. }
  167.  
  168. float4 Filter(float4 color: COLOR0, float2 tex: TEXCOORD0) : COLOR0
  169. {
  170.     float4 col = tex2D(samplerState, tex);
  171.     //float d = sqrt(pow((tex.x - 0.5), 2) + pow((tex.y - 0.5), 2));
  172.      
  173.     //col.rgb -= d * burn;
  174.      
  175.     float a = col.r + col.g + col.b;
  176.     a *= 0.333f;
  177.      
  178.     a *= 1.0f - saturation;
  179.      
  180.     col.r = (col.r * saturation + a) * r;
  181.     col.g = (col.g * saturation + a) * g;
  182.     col.b = (col.b * saturation + a) * b;
  183.      
  184.     col.rgb += brite;
  185.  
  186.     return (col * color) * color.a;
  187. }
  188.  
  189.  
  190. float4 Refract(PS_INPUT Input) : COLOR0
  191. {
  192.     float2 tex = Input.TexCoord + GetDif(Input.TexCoord) * 0.1f;
  193.     float4 col = tex2D(samplerState, tex);
  194.      
  195.     //float d = sqrt(pow((tex.x - 0.5), 2) + pow((tex.y - 0.5), 2));
  196.     //col.rgb -= d * burn;
  197.      
  198.     float a = col.r + col.g + col.b;
  199.     a *= 0.333f;
  200.      
  201.     a *= 1.0f - saturation;
  202.      
  203.     col.r = (col.r * saturation + a) * r;
  204.     col.g = (col.g * saturation + a) * g;
  205.     col.b = (col.b * saturation + a) * b;
  206.      
  207.     col.rgb += brite;
  208.  
  209.     return col;
  210. }
  211.  
  212. technique ColorFilter
  213. {
  214.     pass P0
  215.     {
  216.         PixelShader = compile ps_2_0 Filter();
  217.     }
  218. }
  219.  
  220. technique ColorRefract
  221. {
  222.     pass P0
  223.     {
  224.         PixelShader = compile ps_2_0 Refract();
  225.     }
  226. }
  227.  
  228. Camera class :
  229.  
  230. using System;
  231. using System.Collections.Generic;
  232. using System.Linq;
  233. using System.Text;
  234. using Microsoft.Xna.Framework;
  235. using Microsoft.Xna.Framework.Graphics;
  236.  
  237.  
  238. namespace NobuFatRevenge
  239. {
  240.     public class Camara2d
  241.     {
  242.         public Vector2 ScreenCenter
  243.         {
  244.             get;
  245.             protected set;
  246.         }
  247.         protected float viewportHeight;
  248.         protected float viewportWidth;
  249.  
  250.         private Vector2 Posicao;
  251.         float timer;
  252.         float camaraSpeed;
  253.  
  254.         private float Zoom;
  255.         private Rectangle? limits;
  256.  
  257.  
  258.         public Vector2 posicao
  259.         {
  260.             get { return Posicao; }
  261.             set
  262.             {
  263.                 Posicao = value;
  264.                 ValidatePosition();
  265.             }
  266.         }
  267.         public float zoom
  268.         {
  269.             get { return Zoom; }
  270.             set
  271.             {
  272.                 Zoom = MathHelper.Max(value, .01f);
  273.                 ValidateZoom();
  274.                 ValidatePosition();
  275.             }
  276.         }
  277.         public Rectangle? Limits
  278.         {
  279.             get { return limits; }
  280.             set
  281.             {
  282.                 limits = value;
  283.                 ValidateZoom();
  284.                 ValidatePosition();
  285.             }
  286.         }
  287.  
  288.  
  289.         float previousZoom;
  290.  
  291.         public Rectangle screenCheckRec;
  292.  
  293.         public Vector2 previousPosition;
  294.  
  295.         float newZoom = 1f;
  296.         Rectangle? newLimits;
  297.  
  298.  
  299.         public Camara2d()
  300.         {
  301.             viewportWidth = Resolution.gameResolutionWidth;
  302.             viewportHeight = Resolution.gameResolutionHeight;
  303.  
  304.             ScreenCenter = new Vector2(viewportWidth / 2, viewportHeight / 2);
  305.         }
  306.  
  307.         public void Update(GameTime gameTime, Player[] player)
  308.         {
  309.         }
  310.  
  311.         public Matrix camaraMatrix
  312.         {
  313.             get
  314.             {
  315.                 return Matrix.CreateTranslation(new Vector3(-Posicao, 0f)) *
  316.                        Matrix.CreateScale(Zoom, Zoom, 1f) *
  317.                        Matrix.CreateTranslation(new Vector3(ScreenCenter, 0f));
  318.             }
  319.         }
  320.  
  321.         public Matrix camaraBackgroundMatrix
  322.         {
  323.             get
  324.             {
  325.                 return Matrix.CreateTranslation(new Vector3(new Vector2(-1280, -Posicao.Y), 0f)) *
  326.                        Matrix.CreateScale(Zoom, Zoom, 1f) *
  327.                        Matrix.CreateTranslation(new Vector3(ScreenCenter, 0f));
  328.             }
  329.         }
  330.  
  331.         private void ValidateZoom()
  332.         {
  333.             if (limits.HasValue)
  334.             {
  335.                 float minZoomX = (float)(viewportWidth / limits.Value.Width);
  336.                 float minZoomY = (float)(viewportHeight / limits.Value.Height);
  337.  
  338.                 Zoom = MathHelper.Max(Zoom, MathHelper.Max(minZoomX, minZoomY));
  339.             }
  340.         }
  341.  
  342.         private void ValidatePosition()
  343.         {
  344.             if (limits.HasValue)
  345.             {
  346.                 Vector2 cameraWorldMin = Vector2.Transform(Vector2.Zero, Matrix.Invert(camaraMatrix));
  347.                 Vector2 cameraSize = new Vector2(viewportWidth, viewportHeight) / Zoom;
  348.                 Vector2 limitWorldMin = new Vector2(limits.Value.Left, limits.Value.Top);
  349.                 Vector2 limitWorldMax = new Vector2(limits.Value.Right, limits.Value.Bottom);
  350.                 Vector2 positionOffSet = Posicao - cameraWorldMin;
  351.  
  352.                 Posicao = Vector2.Clamp(cameraWorldMin, limitWorldMin, limitWorldMax - cameraSize) + positionOffSet;
  353.             }
  354.         }
  355.  
  356.         protected void SetRectangle(int x, int y, int width, int height)
  357.         {
  358.             screenCheckRec.X = x;
  359.             screenCheckRec.Y = y;
  360.             screenCheckRec.Width = width;
  361.             screenCheckRec.Height = height;
  362.         }
  363.     }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement