Advertisement
noradninja

Crepuscular

Jul 8th, 2023
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Camera))]
  6. [AddComponentMenu("Effects/Crepuscular Rays", -1)]
  7. public class Crepuscular : MonoBehaviour
  8. {
  9.  
  10.     public Material material;
  11.     public GameObject mainLight;
  12.     public RenderTexture stencilRT;
  13.     static readonly int blurTexString = Shader.PropertyToID("_BlurTex");
  14.     static readonly int cosAngle = Shader.PropertyToID("_CosAngle");
  15.     static readonly int frameValue = Shader.PropertyToID("_FrameValue");
  16.     [Range(0, 20)]
  17.     public float blurSize = 3;
  18.     [Range(1, 16)]
  19.     public int resolutionDivisor = 1;
  20.  
  21.     public static readonly int LightPos = Shader.PropertyToID("_LightPos");
  22.     public Vector4 lightVector;
  23.     private static readonly int Parameter = Shader.PropertyToID("_Parameter");
  24.     public float cosineAngle;
  25.  
  26.     private int tick;
  27.     // Start is called before the first frame update
  28.     void Start()
  29.     {
  30.      
  31.     }
  32.  
  33.     void Update()
  34.     {
  35.         if (tick != 3)
  36.         {
  37.             material.SetFloat(frameValue, 1f);
  38.             tick++;
  39.         //print("Wait frame " + tick);
  40.         }
  41.         else
  42.  
  43.         {
  44.             //print("Depth tick");
  45.             material.SetFloat(frameValue, 0f);
  46.             tick = 0;
  47.            
  48.         }
  49.     }
  50.    
  51.     //[ImageEffectOpaque]
  52.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
  53.     {
  54.         var blurTex = RenderTexture.GetTemporary(128, 128, 0, source.format);
  55.         //blurTex.filterMode = FilterMode.Point;
  56.         lightVector =GetComponent<Camera>().WorldToViewportPoint(transform.position - mainLight.transform.forward);
  57.         material.SetVector(LightPos, lightVector);
  58.         Graphics.Blit(source, blurTex, material, 0);
  59.         material.SetTexture(blurTexString, blurTex);
  60.  
  61.         float widthMod = 1.0f / resolutionDivisor;
  62.     if (blurSize > 0){
  63.         for(int i = 0; i < 1; i++) {
  64.                 float iterationOffs = (i*1.0f);
  65.                 material.SetVector (Parameter, new Vector4 (blurSize * widthMod + iterationOffs, -blurSize * widthMod - iterationOffs, 0.0f, 0.0f));
  66.  
  67.                 // vertical blur
  68.                 RenderTexture rt2 = RenderTexture.GetTemporary(128, 128, 0, source.format);
  69.                 rt2.filterMode = FilterMode.Bilinear;
  70.                 Graphics.Blit (blurTex, rt2, material, 1);
  71.                 RenderTexture.ReleaseTemporary (blurTex);
  72.                 blurTex = rt2;
  73.  
  74.                 // horizontal blur
  75.                 rt2 = RenderTexture.GetTemporary(128, 128, 0, source.format);
  76.                 rt2.filterMode = FilterMode.Bilinear;
  77.                 Graphics.Blit (blurTex, rt2, material, 2);
  78.                 RenderTexture.ReleaseTemporary (blurTex);
  79.                 blurTex = rt2;
  80.         }
  81.     }
  82.         RenderTexture.ReleaseTemporary(blurTex);
  83.         Graphics.Blit(source, destination, material, 3);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement