Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3.  
  4. [ExecuteInEditMode]
  5. [RequireComponent(typeof(Camera))]
  6. public class Fog : MonoBehaviour
  7. {
  8. public Color fogColor;
  9. public float minDistance;
  10. public float maxDistance;
  11.  
  12. Shader _shader;
  13. Material _material;
  14.  
  15. static class Uniforms
  16. {
  17. internal static readonly int _FogColor = Shader.PropertyToID("_FogColor");
  18. internal static readonly int _MinMax = Shader.PropertyToID("_MinMax");
  19. }
  20.  
  21.  
  22. public enum BlendMode { Blend, Additive, Multiplicative};
  23.  
  24. public BlendMode blendMode;
  25.  
  26. void OnEnable()
  27. {
  28. if (_shader == null) {
  29. _shader = Shader.Find("Hidden/Fog");
  30. }
  31. _material = new Material(_shader);
  32. _material.hideFlags = HideFlags.DontSave;
  33. }
  34.  
  35. void OnDisable()
  36. {
  37. DestroyImmediate(_material);
  38. }
  39.  
  40. void OnRenderImage(RenderTexture source, RenderTexture destination)
  41. {
  42. _material.SetColor(Uniforms._FogColor, fogColor);
  43. _material.SetVector(Uniforms._MinMax, new Vector4(minDistance, maxDistance, 0, 0));
  44. Graphics.Blit(source, destination, _material, (int)blendMode);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement