PageRenderTime 123ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/PloobsEngine/Engine/Material/IShadder/Deferred/GlassShader.cs

#
C# | 234 lines | 186 code | 35 blank | 13 comment | 7 complexity | 8bbb3e2d251eaccc0723ebf5ae7701c0 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using PloobsEngine.Material;
  8. using PloobsEngine;
  9. using PloobsEngine.Modelo;
  10. using PloobsEngine.SceneControl;
  11. using PloobsEngine.Cameras;
  12. namespace Engine.Material.Deferred
  13. {
  14. public class GlassShader : IShader
  15. {
  16. private float specularIntensity = 0f;
  17. private float specularPower = 0f;
  18. private RenderTarget2D refractionRT;
  19. private RenderTarget2D reflectionRT;
  20. private Texture2D refractionMap;
  21. private Texture2D reflectionMap;
  22. private Texture2D normal;
  23. private Effect _shader;
  24. private int WIDTH;
  25. private int HEIGHT;
  26. private Plane plane;
  27. private Matrix reflectiveViewMatrix;
  28. private string distorcionMap = "normal";
  29. bool internalTexture = true;
  30. private float waveLength = 0.1f;
  31. /// <summary>
  32. /// Default 0.1f
  33. /// </summary>
  34. public float WaveLength
  35. {
  36. get { return waveLength; }
  37. set { waveLength = value; }
  38. }
  39. private float waveHeight = 0.3f;
  40. /// <summary>
  41. /// Default 0.3f
  42. /// </summary>
  43. public float WaveHeight
  44. {
  45. get { return waveHeight; }
  46. set { waveHeight = value; }
  47. }
  48. private float colorInterpolator = 0.2f;
  49. /// <summary>
  50. /// Default 0.2
  51. /// </summary>
  52. public float ColorInterpolator
  53. {
  54. get { return colorInterpolator; }
  55. set { colorInterpolator = value; }
  56. }
  57. public override MaterialType MaterialType
  58. {
  59. get { return MaterialType.DEFERRED; }
  60. }
  61. public GlassShader(int width, int height, Plane plane)
  62. {
  63. this.WIDTH = width;
  64. this.HEIGHT = height;
  65. this.plane = plane;
  66. }
  67. public string DistorcionMap
  68. {
  69. get { return distorcionMap; }
  70. set { distorcionMap = value; internalTexture = false;
  71. }
  72. }
  73. public float SpecularIntensity
  74. {
  75. get { return specularIntensity; }
  76. set { specularIntensity = value; }
  77. }
  78. public float SpecularPower
  79. {
  80. get { return specularPower; }
  81. set { specularPower = value; }
  82. }
  83. private Plane CreatePlane(float height, Vector3 planeNormalDirection, Matrix currentViewMatrix, bool clipSide,ICamera cam)
  84. {
  85. planeNormalDirection.Normalize();
  86. Vector4 planeCoeffs = new Vector4(planeNormalDirection, height);
  87. if (clipSide)
  88. planeCoeffs *= -1;
  89. Matrix worldViewProjection = currentViewMatrix * cam.Projection;
  90. Matrix inverseWorldViewProjection = Matrix.Invert(worldViewProjection);
  91. inverseWorldViewProjection = Matrix.Transpose(inverseWorldViewProjection);
  92. planeCoeffs = Vector4.Transform(planeCoeffs, inverseWorldViewProjection);
  93. Plane finalPlane = new Plane(planeCoeffs);
  94. return finalPlane;
  95. }
  96. public override void PreDrawPhase(IWorld mundo, IModelo modelo, IRenderHelper render, ICamera cam)
  97. {
  98. ///REFRACAO
  99. Plane refractionClipPlane = CreateReflectionPlane(plane.D, plane.Normal, cam.View, cam.Projection, true);
  100. EngineStuff.GraphicsDevice.ClipPlanes[0].Plane = refractionClipPlane;
  101. EngineStuff.GraphicsDevice.ClipPlanes[0].IsEnabled = true;
  102. EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
  103. EngineStuff.GraphicsDevice.RenderState.DepthBufferEnable = true;
  104. EngineStuff.GraphicsDevice.SetRenderTarget(0, refractionRT);
  105. EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  106. render.RenderSceneWithoutMaterial(mundo, new List<IObject>() { obj }, cam.View, cam.Projection, true);
  107. EngineStuff.GraphicsDevice.ClipPlanes[0].IsEnabled = false;
  108. EngineStuff.GraphicsDevice.SetRenderTarget(0, null);
  109. refractionMap = refractionRT.GetTexture();
  110. //refractionMap.Save("teste2.png", ImageFileFormat.Png);
  111. ///REFLEXAO
  112. Matrix m = Matrix.CreateReflection(plane);
  113. Vector3 pos;
  114. Vector3 target;
  115. Vector3 up;
  116. pos = Vector3.Transform(cam.Position, m);
  117. target = Vector3.Transform(cam.Target, m);
  118. up = Vector3.Transform(cam.Up, m);
  119. reflectiveViewMatrix = Matrix.CreateLookAt(pos, target, up);
  120. Plane reflectionClipPlane = CreateReflectionPlane(plane.D , plane.Normal, reflectiveViewMatrix, cam.Projection, false);
  121. EngineStuff.GraphicsDevice.ClipPlanes[0].Plane = reflectionClipPlane;
  122. EngineStuff.GraphicsDevice.ClipPlanes[0].IsEnabled = true;
  123. EngineStuff.GraphicsDevice.SetRenderTarget(0, reflectionRT);
  124. EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  125. render.RenderSceneWithoutMaterial(mundo, new List<IObject>() { obj }, reflectiveViewMatrix, cam.Projection,true);
  126. EngineStuff.GraphicsDevice.SetRenderTarget(0, null);
  127. reflectionMap = reflectionRT.GetTexture();
  128. EngineStuff.GraphicsDevice.ClipPlanes[0].IsEnabled = false;
  129. //reflectionMap.Save("teste3.png", ImageFileFormat.Png);
  130. EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  131. }
  132. public override void Draw(IModelo modelo, IRenderHelper render, ICamera camera)
  133. {
  134. if (obj != null)
  135. {
  136. this._shader.Parameters["specularIntensity"].SetValue(specularIntensity);
  137. this._shader.Parameters["specularPower"].SetValue(specularPower);
  138. this._shader.Parameters["View"].SetValue(camera.View);
  139. this._shader.Parameters["camPos"].SetValue(camera.Position);
  140. this._shader.Parameters["ReflectionView"].SetValue(reflectiveViewMatrix);
  141. this._shader.Parameters["RefractionView"].SetValue(camera.View);
  142. this._shader.Parameters["ReflectionMap"].SetValue(reflectionMap);
  143. this._shader.Parameters["RefractionMap"].SetValue(refractionMap);
  144. this._shader.Parameters["normalMap"].SetValue(normal);
  145. this._shader.Parameters["xWaveLength"].SetValue(waveLength);
  146. this._shader.Parameters["xWaveHeight"].SetValue(waveHeight);
  147. this._shader.Parameters["colorInterpolator"].SetValue(colorInterpolator);
  148. this._shader.Parameters["difuse"].SetValue(modelo.getTexture(TextureType.DIFFUSE));
  149. this._shader.Parameters["Projection"].SetValue(camera.Projection);
  150. Matrix[] boneTransforms = modelo.getBonesTransformation();
  151. Matrix wld = obj.getWorldMatrix();
  152. for (int i = 0; i < modelo.MeshNumber; i++)
  153. {
  154. BatchInformation[] bi = modelo.GetBatchInformation(i);
  155. Matrix w1 = Matrix.Multiply(wld, boneTransforms[modelo.GetParentBoneIndex(i)]);
  156. this._shader.Parameters["World"].SetValue(w1);
  157. for (int j = 0; j < bi.Count(); j++)
  158. {
  159. this._shader.Begin();
  160. EngineStuff.GraphicsDevice.VertexDeclaration = bi[j].VertexDeclaration;
  161. EngineStuff.GraphicsDevice.Vertices[0].SetSource(modelo.GetVertexBuffer(i), bi[j].StreamOffset, bi[j].VertexStride);
  162. EngineStuff.GraphicsDevice.Indices = modelo.GetIndexBuffer(i);
  163. this._shader.CurrentTechnique.Passes[0].Begin();
  164. EngineStuff.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, bi[j].BaseVertex, 0, bi[j].NumVertices, bi[j].StartIndex, bi[j].PrimitiveCount);
  165. this._shader.CurrentTechnique.Passes[0].End();
  166. this._shader.End();
  167. }
  168. }
  169. }
  170. }
  171. private Plane CreateReflectionPlane(float height, Vector3 planeNormalDirection, Matrix currentViewMatrix, Matrix camProj ,bool clipSide)
  172. {
  173. planeNormalDirection.Normalize();
  174. Vector4 planeCoeffs = new Vector4(planeNormalDirection, height);
  175. if (clipSide)
  176. planeCoeffs *= -1;
  177. Matrix worldViewProjection = currentViewMatrix * camProj;
  178. Matrix inverseWorldViewProjection = Matrix.Invert(worldViewProjection);
  179. inverseWorldViewProjection = Matrix.Transpose(inverseWorldViewProjection);
  180. planeCoeffs = Vector4.Transform(planeCoeffs, inverseWorldViewProjection);
  181. Plane finalPlane = new Plane(planeCoeffs);
  182. return finalPlane;
  183. }
  184. public override void Initialize()
  185. {
  186. this._shader = EngineStuff.InternalContentManager.GetAsset<Effect>("Vidro");
  187. refractionRT = EngineStuff.GetDefaultColorBuffer(WIDTH, HEIGHT);
  188. reflectionRT = EngineStuff.GetDefaultColorBuffer(WIDTH, HEIGHT);
  189. if(internalTexture )
  190. {
  191. normal = EngineStuff.InternalContentManager.GetAsset<Texture2D>(distorcionMap);
  192. }
  193. else
  194. {
  195. normal = EngineStuff.CustomContentManager.GetAsset<Texture2D>(distorcionMap);
  196. }
  197. }
  198. }
  199. }