Advertisement
noradninja

Crepuscupular CS Script

Nov 30th, 2022 (edited)
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 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.     [Range(0, 20)]
  16.     public int blurSize = 3;
  17.     [Range(1, 16)]
  18.     public int resolutionDivisor = 1;
  19.  
  20.     public static readonly int LightPos = Shader.PropertyToID("_LightPos");
  21.     public Vector4 lightVector;
  22.     private static readonly int Parameter = Shader.PropertyToID("_Parameter");
  23.     public float cosineAngle;
  24.  
  25.     // Start is called before the first frame update
  26.     void Start()
  27.     {
  28.      
  29.     }
  30.  
  31.     void Update()
  32.     {
  33.         var getAngle = material.GetFloat(cosAngle);
  34.         cosineAngle = getAngle;
  35.     }
  36.  
  37.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
  38.     {
  39.         var blurTex = RenderTexture.GetTemporary(256, 128, 0, source.format);
  40.         lightVector =GetComponent<Camera>().WorldToViewportPoint(transform.position - mainLight.transform.forward);
  41.         material.SetVector(LightPos, lightVector);
  42.         Graphics.Blit(source, blurTex, material, 0);
  43.         material.SetTexture(blurTexString, blurTex);
  44.  
  45.         float widthMod = 1.0f / resolutionDivisor;
  46. //only execute this block if blurSize is > 0, otherwise we will skip this RT and two SGX blur passes (horiz/vert) in the shader
  47.     if (blurSize > 0){
  48.         for(int i = 0; i < 1; i++) {
  49.                 float iterationOffs = (i*1.0f);
  50.                 material.SetVector (Parameter, new Vector4 (blurSize * widthMod + iterationOffs, -blurSize * widthMod - iterationOffs, 0.0f, 0.0f));
  51.  
  52.                 // vertical blur
  53.                 RenderTexture rt2 = RenderTexture.GetTemporary(256, 128, 0, source.format);
  54.                 rt2.filterMode = FilterMode.Bilinear;
  55.                 Graphics.Blit (blurTex, rt2, material, 1);
  56.                 RenderTexture.ReleaseTemporary (blurTex);
  57.                 blurTex = rt2;
  58.  
  59.                 // horizontal blur
  60.                 rt2 = RenderTexture.GetTemporary(256, 128, 0, source.format);
  61.                 rt2.filterMode = FilterMode.Bilinear;
  62.                 Graphics.Blit (blurTex, rt2, material, 2);
  63.                 RenderTexture.ReleaseTemporary (blurTex);
  64.                 blurTex = rt2;
  65.         }
  66.     }
  67.         RenderTexture.ReleaseTemporary(blurTex);
  68.         Graphics.Blit(source, destination, material, 3);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement