Advertisement
VelGD

Motion Extraction - Effect Final

May 1st, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class MotionExtractionBaseEffect : MonoBehaviour
  5. {
  6.     public RenderTexture sourceRenderTexture;
  7.     public RenderTexture delayedRenderTexture;
  8.  
  9.     public Vector2 resolution = new Vector2(960, 540);
  10.  
  11.     public int framesOfDelay;
  12.     public Image image;
  13.  
  14.     public int gradientResolution;
  15.     public Gradient mainGrad;
  16.     public string mainGradTexturePath;
  17.  
  18.     public Gradient altGrad;
  19.     public string altGradTexturePath;
  20.    
  21.     public Camera mainCamera;
  22.     public float fadeOutSpeed;
  23.     public float fadeInSpeed;
  24.     public float fadeOutAlpha = 0.1f;
  25.  
  26.     private Quaternion lastFrameRotation;
  27.     private Vector3 lastFramePosition;
  28.     private float alpha = 1.0f;
  29.  
  30.     Texture2D[] textures;
  31.  
  32.     int currentFrameIndex;
  33.  
  34.     private void Start()
  35.     {
  36.         textures = new Texture2D[framesOfDelay];
  37.         currentFrameIndex = 0;
  38.         for (int i = 0; i < framesOfDelay; i++)
  39.         {
  40.             textures[i] = new Texture2D((int)resolution.x, (int)resolution.y, TextureFormat.ARGB32, false, true);
  41.             textures[i].filterMode = FilterMode.Point;
  42.             textures[i].Apply();
  43.         }
  44.  
  45.         ApplyShaderParams();
  46.  
  47.         lastFramePosition = mainCamera.transform.position;
  48.         lastFrameRotation = mainCamera.transform.rotation;
  49.     }
  50.  
  51.     private void ApplyShaderParams()
  52.     {
  53.         GenerateTextures();
  54.     }
  55.  
  56.     [ContextMenu("Generate Textures")]
  57.     private void GenerateTextures()
  58.     {
  59.         GenerateTexture(mainGrad, mainGradTexturePath);
  60.         GenerateTexture(altGrad, altGradTexturePath);
  61.     }
  62.    
  63.     private void GenerateTexture(Gradient grad, string path)
  64.     {
  65.         Texture2D tex = new Texture2D(gradientResolution, 1,
  66.             TextureFormat.ARGB32, false, true);
  67.         tex.filterMode = FilterMode.Point;
  68.  
  69.         Color[] colors = new Color[gradientResolution];
  70.  
  71.         for (int i = 0; i < gradientResolution; i++)
  72.         {
  73.             float t = (float)i / gradientResolution;
  74.             colors[i] = grad.Evaluate(t);
  75.         }
  76.  
  77.         tex.SetPixels(colors);
  78.         tex.Apply();
  79.  
  80.         byte[] bytes = tex.EncodeToPNG();
  81.        
  82.         System.IO.File.WriteAllBytes(Application.dataPath + path, bytes);
  83.     }
  84.  
  85.     private void Update()
  86.     {
  87.         Graphics.Blit(textures[currentFrameIndex], delayedRenderTexture);
  88.  
  89.         CaptureFrame();
  90.  
  91.         currentFrameIndex++;
  92.         if (currentFrameIndex >= framesOfDelay)
  93.         {
  94.             currentFrameIndex = 0;
  95.         }
  96.  
  97.         bool isMoving = Vector3.Distance(mainCamera.transform.position, lastFramePosition) > 0.01f;
  98.         bool isRotating = mainCamera.transform.rotation != lastFrameRotation;
  99.  
  100.         bool shouldFade = isMoving || isRotating;
  101.  
  102.         alpha = Mathf.Lerp(alpha, shouldFade ? fadeOutAlpha : 1,
  103.             (shouldFade ? fadeOutSpeed : fadeInSpeed) * Time.deltaTime);
  104.  
  105.         image.material.SetFloat("_AlphaMultiplier", alpha);
  106.         lastFramePosition = mainCamera.transform.position;
  107.         lastFrameRotation = mainCamera.transform.rotation;
  108.     }
  109.  
  110.     private void CaptureFrame()
  111.     {
  112.         RenderTexture.active = sourceRenderTexture;
  113.         Texture2D texture2D = textures[currentFrameIndex];
  114.         texture2D.ReadPixels(new Rect(0, 0, (int)resolution.x, (int)resolution.y), 0, 0);
  115.         texture2D.Apply();
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement