/Code/PloobsEngine/Engine/Material/IShadder/Deferred/GlassShader.cs
C# | 234 lines | 186 code | 35 blank | 13 comment | 7 complexity | 8bbb3e2d251eaccc0723ebf5ae7701c0 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using PloobsEngine.Material;
- using PloobsEngine;
- using PloobsEngine.Modelo;
- using PloobsEngine.SceneControl;
- using PloobsEngine.Cameras;
-
- namespace Engine.Material.Deferred
- {
-
- public class GlassShader : IShader
- {
- private float specularIntensity = 0f;
- private float specularPower = 0f;
- private RenderTarget2D refractionRT;
- private RenderTarget2D reflectionRT;
- private Texture2D refractionMap;
- private Texture2D reflectionMap;
- private Texture2D normal;
- private Effect _shader;
- private int WIDTH;
- private int HEIGHT;
- private Plane plane;
- private Matrix reflectiveViewMatrix;
- private string distorcionMap = "normal";
- bool internalTexture = true;
- private float waveLength = 0.1f;
-
- /// <summary>
- /// Default 0.1f
- /// </summary>
- public float WaveLength
- {
- get { return waveLength; }
- set { waveLength = value; }
- }
- private float waveHeight = 0.3f;
-
- /// <summary>
- /// Default 0.3f
- /// </summary>
- public float WaveHeight
- {
- get { return waveHeight; }
- set { waveHeight = value; }
- }
- private float colorInterpolator = 0.2f;
-
- /// <summary>
- /// Default 0.2
- /// </summary>
- public float ColorInterpolator
- {
- get { return colorInterpolator; }
- set { colorInterpolator = value; }
- }
-
-
-
- public override MaterialType MaterialType
- {
- get { return MaterialType.DEFERRED; }
- }
-
-
- public GlassShader(int width, int height, Plane plane)
- {
- this.WIDTH = width;
- this.HEIGHT = height;
- this.plane = plane;
- }
-
- public string DistorcionMap
- {
- get { return distorcionMap; }
- set { distorcionMap = value; internalTexture = false;
- }
- }
-
- public float SpecularIntensity
- {
- get { return specularIntensity; }
- set { specularIntensity = value; }
- }
-
- public float SpecularPower
- {
- get { return specularPower; }
- set { specularPower = value; }
- }
-
- private Plane CreatePlane(float height, Vector3 planeNormalDirection, Matrix currentViewMatrix, bool clipSide,ICamera cam)
- {
- planeNormalDirection.Normalize();
- Vector4 planeCoeffs = new Vector4(planeNormalDirection, height);
- if (clipSide)
- planeCoeffs *= -1;
-
- Matrix worldViewProjection = currentViewMatrix * cam.Projection;
- Matrix inverseWorldViewProjection = Matrix.Invert(worldViewProjection);
- inverseWorldViewProjection = Matrix.Transpose(inverseWorldViewProjection);
-
- planeCoeffs = Vector4.Transform(planeCoeffs, inverseWorldViewProjection);
- Plane finalPlane = new Plane(planeCoeffs);
-
- return finalPlane;
- }
-
-
-
-
- public override void PreDrawPhase(IWorld mundo, IModelo modelo, IRenderHelper render, ICamera cam)
- {
- ///REFRACAO
- Plane refractionClipPlane = CreateReflectionPlane(plane.D, plane.Normal, cam.View, cam.Projection, true);
- EngineStuff.GraphicsDevice.ClipPlanes[0].Plane = refractionClipPlane;
- EngineStuff.GraphicsDevice.ClipPlanes[0].IsEnabled = true;
- EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
- EngineStuff.GraphicsDevice.RenderState.DepthBufferEnable = true;
- EngineStuff.GraphicsDevice.SetRenderTarget(0, refractionRT);
- EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
- render.RenderSceneWithoutMaterial(mundo, new List<IObject>() { obj }, cam.View, cam.Projection, true);
- EngineStuff.GraphicsDevice.ClipPlanes[0].IsEnabled = false;
- EngineStuff.GraphicsDevice.SetRenderTarget(0, null);
- refractionMap = refractionRT.GetTexture();
- //refractionMap.Save("teste2.png", ImageFileFormat.Png);
-
- ///REFLEXAO
- Matrix m = Matrix.CreateReflection(plane);
- Vector3 pos;
- Vector3 target;
- Vector3 up;
- pos = Vector3.Transform(cam.Position, m);
- target = Vector3.Transform(cam.Target, m);
- up = Vector3.Transform(cam.Up, m);
- reflectiveViewMatrix = Matrix.CreateLookAt(pos, target, up);
- Plane reflectionClipPlane = CreateReflectionPlane(plane.D , plane.Normal, reflectiveViewMatrix, cam.Projection, false);
- EngineStuff.GraphicsDevice.ClipPlanes[0].Plane = reflectionClipPlane;
- EngineStuff.GraphicsDevice.ClipPlanes[0].IsEnabled = true;
- EngineStuff.GraphicsDevice.SetRenderTarget(0, reflectionRT);
- EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
- render.RenderSceneWithoutMaterial(mundo, new List<IObject>() { obj }, reflectiveViewMatrix, cam.Projection,true);
- EngineStuff.GraphicsDevice.SetRenderTarget(0, null);
- reflectionMap = reflectionRT.GetTexture();
- EngineStuff.GraphicsDevice.ClipPlanes[0].IsEnabled = false;
- //reflectionMap.Save("teste3.png", ImageFileFormat.Png);
-
- EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
- }
-
- public override void Draw(IModelo modelo, IRenderHelper render, ICamera camera)
- {
- if (obj != null)
- {
-
- this._shader.Parameters["specularIntensity"].SetValue(specularIntensity);
- this._shader.Parameters["specularPower"].SetValue(specularPower);
- this._shader.Parameters["View"].SetValue(camera.View);
- this._shader.Parameters["camPos"].SetValue(camera.Position);
- this._shader.Parameters["ReflectionView"].SetValue(reflectiveViewMatrix);
- this._shader.Parameters["RefractionView"].SetValue(camera.View);
- this._shader.Parameters["ReflectionMap"].SetValue(reflectionMap);
- this._shader.Parameters["RefractionMap"].SetValue(refractionMap);
- this._shader.Parameters["normalMap"].SetValue(normal);
- this._shader.Parameters["xWaveLength"].SetValue(waveLength);
- this._shader.Parameters["xWaveHeight"].SetValue(waveHeight);
- this._shader.Parameters["colorInterpolator"].SetValue(colorInterpolator);
- this._shader.Parameters["difuse"].SetValue(modelo.getTexture(TextureType.DIFFUSE));
- this._shader.Parameters["Projection"].SetValue(camera.Projection);
-
- Matrix[] boneTransforms = modelo.getBonesTransformation();
- Matrix wld = obj.getWorldMatrix();
- for (int i = 0; i < modelo.MeshNumber; i++)
- {
- BatchInformation[] bi = modelo.GetBatchInformation(i);
- Matrix w1 = Matrix.Multiply(wld, boneTransforms[modelo.GetParentBoneIndex(i)]);
- this._shader.Parameters["World"].SetValue(w1);
- for (int j = 0; j < bi.Count(); j++)
- {
-
- this._shader.Begin();
- EngineStuff.GraphicsDevice.VertexDeclaration = bi[j].VertexDeclaration;
- EngineStuff.GraphicsDevice.Vertices[0].SetSource(modelo.GetVertexBuffer(i), bi[j].StreamOffset, bi[j].VertexStride);
- EngineStuff.GraphicsDevice.Indices = modelo.GetIndexBuffer(i);
-
- this._shader.CurrentTechnique.Passes[0].Begin();
- EngineStuff.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, bi[j].BaseVertex, 0, bi[j].NumVertices, bi[j].StartIndex, bi[j].PrimitiveCount);
- this._shader.CurrentTechnique.Passes[0].End();
- this._shader.End();
- }
-
- }
- }
- }
-
- private Plane CreateReflectionPlane(float height, Vector3 planeNormalDirection, Matrix currentViewMatrix, Matrix camProj ,bool clipSide)
- {
- planeNormalDirection.Normalize();
- Vector4 planeCoeffs = new Vector4(planeNormalDirection, height);
- if (clipSide)
- planeCoeffs *= -1;
-
- Matrix worldViewProjection = currentViewMatrix * camProj;
- Matrix inverseWorldViewProjection = Matrix.Invert(worldViewProjection);
- inverseWorldViewProjection = Matrix.Transpose(inverseWorldViewProjection);
-
- planeCoeffs = Vector4.Transform(planeCoeffs, inverseWorldViewProjection);
- Plane finalPlane = new Plane(planeCoeffs);
-
- return finalPlane;
- }
-
- public override void Initialize()
- {
- this._shader = EngineStuff.InternalContentManager.GetAsset<Effect>("Vidro");
- refractionRT = EngineStuff.GetDefaultColorBuffer(WIDTH, HEIGHT);
- reflectionRT = EngineStuff.GetDefaultColorBuffer(WIDTH, HEIGHT);
- if(internalTexture )
- {
- normal = EngineStuff.InternalContentManager.GetAsset<Texture2D>(distorcionMap);
- }
- else
- {
- normal = EngineStuff.CustomContentManager.GetAsset<Texture2D>(distorcionMap);
- }
-
- }
- }
- }