Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [ExecuteInEditMode]
  5. public class VRNightVision : MonoBehaviour {
  6.  
  7. private Material material;
  8. private Camera cam;
  9.  
  10. [Tooltip("Grain size in pixel. Should be greater than 1 with supersampling.")]
  11. [Range(1, 100)]
  12. public float scaling = 6;
  13.  
  14. [Tooltip("Areas that are brighter than this value won't receive any grain.")]
  15. public float cutoff = 0.0025f;
  16. [Tooltip("Proportion of dark grains.")]
  17. [Range(0, 1)]
  18. public float distribution = 0.9f;
  19. [Tooltip("Brightness of light grains.")]
  20. public float lightBrightness = 0.0025f;
  21. [Tooltip("Brightness of dark grains. Raising it can improve user experience.")]
  22. public float darkBrightness = 0.000f;
  23.  
  24.  
  25. // Creates a private material used to the effect
  26. void Awake() {
  27. material = new Material(Shader.Find("Hidden/VRNightVision"));
  28. }
  29.  
  30. void Start() {
  31. cam = GetComponent<Camera>();
  32. cam.depthTextureMode = DepthTextureMode.Depth;
  33. }
  34.  
  35. private Matrix4x4 world2head;
  36.  
  37. void LateUpdate() {
  38. world2head = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false) * cam.worldToCameraMatrix;
  39. }
  40.  
  41. // Postprocess the image
  42. void OnRenderImage(RenderTexture source, RenderTexture destination) {
  43. Matrix4x4 world2Screen = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false) * cam.worldToCameraMatrix;
  44. Matrix4x4 screen2World = world2Screen.inverse;
  45. material.SetMatrix("_EyeMat", screen2World);
  46. material.SetMatrix("_HeadMat", world2head);
  47. material.SetVector("_Resolution", new Vector2(source.width / scaling, source.height / scaling));
  48. material.SetFloat("_Cutoff", cutoff);
  49. material.SetFloat("_Distribution", distribution);
  50. material.SetFloat("_Light", lightBrightness);
  51. material.SetFloat("_Dark", darkBrightness);
  52. Graphics.Blit(source, destination,material);
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement