Guest User

SetInteractiveShaderEffects.cs

a guest
Jul 25th, 2025
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SetInteractiveShaderEffects : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     RenderTexture rt;
  9.     [SerializeField]
  10.     Transform target;
  11.  
  12.     [SerializeField]
  13.     float yOffset = 20f;
  14.  
  15.     [SerializeField]
  16.     Camera m_cam;
  17.     // Start is called before the first frame update
  18.     void Awake()
  19.     {
  20.         if (m_cam == null)
  21.         {
  22.             m_cam = GetComponent<Camera>();
  23.         }
  24.         Shader.SetGlobalTexture("_GlobalEffectRT", rt);
  25.         Shader.SetGlobalFloat("_OrthographicCamSize", m_cam.orthographicSize);
  26.     }
  27.  
  28.     private void Update()
  29.     {
  30.         // more stable version thanks to NoveLL
  31.          // calculate render texture pixel size in world space
  32.         float pixelSize = 2.0f * m_cam.orthographicSize / rt.height;
  33.  
  34.         // round the camera's position to the nearest pixel
  35.         Vector3 targetPosition = new Vector3(
  36.             Mathf.Round(target.position.x / pixelSize) * pixelSize,
  37.             Mathf.Round((target.position.y + yOffset) / pixelSize) * pixelSize,
  38.             Mathf.Round(target.position.z / pixelSize) * pixelSize);
  39.  
  40.         transform.position = targetPosition;
  41.         Shader.SetGlobalVector("_Position", transform.position);
  42.     }
  43.  
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment