Advertisement
m_zandvliet

AtmosphericFog.cs

Sep 10th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode]
  4. [RequireComponent(typeof(Camera))]
  5. [AddComponentMenu("Image Effects/Rendering/Atmospheric Fog")]
  6.  
  7. public class AtmosphericFog : PostEffectsBase {
  8.     private float CAMERA_NEAR = 0.5f;
  9.     private float CAMERA_FAR = 50.0f;
  10.     private float CAMERA_FOV = 60.0f;  
  11.     private float CAMERA_ASPECT_RATIO = 1.333333f;
  12.  
  13.     [SerializeField] private Transform _sun;
  14.     [SerializeField] private float globalDensity = 1.0f;
  15.     [SerializeField] private float _seaLevel = 0f;
  16.     [SerializeField] private float heightScale = 0.001f;
  17.     [SerializeField] private float auraPower = 8f;
  18.  
  19.     [SerializeField] private Color fogColor = Color.grey;
  20.     [SerializeField] private Color sunColor = Color.grey;
  21.    
  22.     public Shader fogShader;
  23.     private Material fogMaterial = null;
  24.  
  25.     public override void Start() {
  26.         base.Start();
  27.  
  28.         if (!_sun) {
  29.             _sun = ServiceLocator.Instance.Sun;
  30.         }
  31.     }
  32.  
  33.     public override bool CheckResources() {
  34.         CheckSupport (true);
  35.        
  36.         fogMaterial = CheckShaderAndCreateMaterial (fogShader, fogMaterial);
  37.        
  38.         if(!isSupported)
  39.             ReportAutoDisable ();
  40.  
  41.         return isSupported;              
  42.     }
  43.  
  44.     private void OnRenderImage (RenderTexture source, RenderTexture destination) {
  45.         if (CheckResources() == false) {
  46.             Graphics.Blit(source, destination);
  47.             return;
  48.         }
  49.            
  50.         CAMERA_NEAR = camera.nearClipPlane;
  51.         CAMERA_FAR = camera.farClipPlane;
  52.         CAMERA_FOV = camera.fieldOfView;
  53.         CAMERA_ASPECT_RATIO = camera.aspect;
  54.    
  55.         Matrix4x4 frustumCorners = Matrix4x4.identity;      
  56.    
  57.         float fovWHalf = CAMERA_FOV * 0.5f;
  58.        
  59.         Vector3 toRight = camera.transform.right * CAMERA_NEAR * Mathf.Tan (fovWHalf * Mathf.Deg2Rad) * CAMERA_ASPECT_RATIO;
  60.         Vector3 toTop = camera.transform.up * CAMERA_NEAR * Mathf.Tan (fovWHalf * Mathf.Deg2Rad);
  61.    
  62.         Vector3 topLeft = (camera.transform.forward * CAMERA_NEAR - toRight + toTop);
  63.         float CAMERA_SCALE = topLeft.magnitude * CAMERA_FAR/CAMERA_NEAR;
  64.  
  65.         topLeft.Normalize();
  66.         topLeft *= CAMERA_SCALE;
  67.    
  68.         Vector3 topRight = (camera.transform.forward * CAMERA_NEAR + toRight + toTop);
  69.         topRight.Normalize();
  70.         topRight *= CAMERA_SCALE;
  71.        
  72.         Vector3 bottomRight = (camera.transform.forward * CAMERA_NEAR + toRight - toTop);
  73.         bottomRight.Normalize();
  74.         bottomRight *= CAMERA_SCALE;
  75.        
  76.         Vector3 bottomLeft = (camera.transform.forward * CAMERA_NEAR - toRight - toTop);
  77.         bottomLeft.Normalize();
  78.         bottomLeft *= CAMERA_SCALE;
  79.                
  80.         frustumCorners.SetRow (0, topLeft);
  81.         frustumCorners.SetRow (1, topRight);      
  82.         frustumCorners.SetRow (2, bottomRight);
  83.         frustumCorners.SetRow (3, bottomLeft);      
  84.                                
  85.         fogMaterial.SetMatrix ("_FrustumCornersWS", frustumCorners);
  86.         fogMaterial.SetVector ("_CameraWS", camera.transform.position);
  87.         fogMaterial.SetVector ("_SunDir", -_sun.forward);
  88.        
  89.         fogMaterial.SetFloat ("_GlobalDensity", globalDensity);
  90.         fogMaterial.SetFloat("_SeaLevel", _seaLevel);
  91.         fogMaterial.SetFloat("_HeightScale", heightScale);
  92.         fogMaterial.SetFloat("_AuraPower", auraPower);
  93.         fogMaterial.SetColor ("_FogColor", fogColor);
  94.         fogMaterial.SetColor("_SunColor", sunColor);
  95.  
  96.         CustomGraphicsBlit (source, destination, fogMaterial);
  97.     }
  98.  
  99.     private static void CustomGraphicsBlit(RenderTexture source, RenderTexture dest, Material fxMaterial) {
  100.         RenderTexture.active = dest;
  101.  
  102.         fxMaterial.SetTexture("_MainTex", source);
  103.  
  104.         GL.PushMatrix();
  105.         GL.LoadOrtho();
  106.  
  107.         fxMaterial.SetPass (0);  
  108.  
  109.         GL.Begin(GL.QUADS);
  110.  
  111.         GL.MultiTexCoord2(0, 0.0f, 0.0f);
  112.         GL.Vertex3(0.0f, 0.0f, 3.0f); // BL
  113.  
  114.         GL.MultiTexCoord2(0, 1.0f, 0.0f);
  115.         GL.Vertex3(1.0f, 0.0f, 2.0f); // BR
  116.  
  117.         GL.MultiTexCoord2(0, 1.0f, 1.0f);
  118.         GL.Vertex3(1.0f, 1.0f, 1.0f); // TR
  119.  
  120.         GL.MultiTexCoord2(0, 0.0f, 1.0f);
  121.         GL.Vertex3(0.0f, 1.0f, 0.0f); // TL
  122.  
  123.         GL.End();
  124.         GL.PopMatrix();
  125.     }      
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement