Created
December 11, 2024 20:17
-
-
Save unitycoder/2b39d009505895169ef0fd34fe9b3aa2 to your computer and use it in GitHub Desktop.
MotionVector Effect: Object "disappears" when paused
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // attach to main camera, BIRP | |
| using UnityEngine; | |
| [ExecuteInEditMode] | |
| public class MotionFX : MonoBehaviour | |
| { | |
| public Material mat; | |
| Camera cam; | |
| void Start() | |
| { | |
| cam = Camera.main; | |
| //cam.depthTextureMode = DepthTextureMode.Depth | DepthTextureMode.DepthNormals | DepthTextureMode.MotionVectors; | |
| cam.depthTextureMode = DepthTextureMode.Depth | DepthTextureMode.MotionVectors; | |
| } | |
| void OnRenderImage(RenderTexture source, RenderTexture destination) | |
| { | |
| Graphics.Blit(source, destination, mat); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://discussions.unity.com/t/use-shader-motion-vectors-pass-in-surface-shader/203853/2 | |
| Shader "Custom/MotionFX" { | |
| Properties | |
| { | |
| _PatternTex ("Base (RGB)", 2D) = "white" {} | |
| } | |
| SubShader { | |
| Tags { "RenderType"="Opaque" } | |
| Cull Back | |
| Pass{ | |
| CGPROGRAM | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| #include "UnityCG.cginc" | |
| sampler2D _CameraDepthTexture; | |
| sampler2D _CameraMotionVectorsTexture; | |
| sampler2D _PatternTex; | |
| sampler2D _MainTex; | |
| float4 _MainTex_ST; | |
| float4 _PatternTex_ST; | |
| struct appdata | |
| { | |
| float4 vertex : POSITION; | |
| float2 uv : TEXCOORD0; | |
| }; | |
| struct v2f { | |
| float4 pos : SV_POSITION; | |
| float4 scrPos:TEXCOORD1; | |
| float2 uv : TEXCOORD0; | |
| }; | |
| //Vertex Shader | |
| v2f vert (appdata v) | |
| { | |
| v2f o; | |
| o.pos = UnityObjectToClipPos (v.vertex); | |
| //o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
| o.uv = TRANSFORM_TEX(v.uv, _PatternTex); | |
| o.scrPos=ComputeScreenPos(o.pos); | |
| return o; | |
| } | |
| //Fragment Shader | |
| half4 frag (v2f i) : COLOR | |
| { | |
| // half4 depth; | |
| // depth.r = saturate(abs(tex2Dproj(_CameraMotionVectorsTexture, UNITY_PROJ_COORD(i.scrPos)).r)); | |
| // depth.g = saturate(abs(tex2Dproj(_CameraMotionVectorsTexture, UNITY_PROJ_COORD(i.scrPos)).g)); | |
| // depth.b = saturate(abs(tex2Dproj(_CameraMotionVectorsTexture, UNITY_PROJ_COORD(i.scrPos)).b)); | |
| // depth.a = 1; | |
| // float combined = sqrt( depth.r*depth.r + depth.g*depth.g + depth.b*depth.b); | |
| // depth.r = combined; | |
| // depth.g = combined; | |
| // depth.b = combined; | |
| //float4 c = tex2D(_MainTex, i.uv); | |
| //return float4(1,0,0,1); | |
| //return i.scrPos.xxxx; | |
| //return tex2D(_CameraMotionVectorsTexture,i.uv); | |
| // r = x axis, g = y axis | |
| float4 mot = tex2D(_CameraMotionVectorsTexture,i.scrPos.xy/i.scrPos.w).xyxy; | |
| //mot = saturate(mot); | |
| float moving = mot.x!=0; | |
| //float4 c = tex2D(_MainTex, ((i.scrPos.xy+float2(0,moving*0.1))/i.scrPos.w)+float2(0,frac(_Time.x))); | |
| //float4 c = tex2D(_PatternTex, ((i.scrPos.xy+float2(0,frac(_Time.x)))/i.scrPos.w)); | |
| float4 c = tex2D(_PatternTex, ((i.scrPos.xy+float2(0,frac(_Time.x*moving)))/i.scrPos.w)); | |
| //float4 c = tex2D(_MainTex, (i.scrPos.xy+float2(0,mot.x*mot.y))/i.scrPos.w); | |
| //return mot*c; | |
| //return i.uv.xyxy; | |
| return lerp(c,c, mot.x*mot.y); | |
| //return c*depth; | |
| //return c; | |
| //return depth; | |
| } | |
| ENDCG | |
| } | |
| } | |
| FallBack "Diffuse" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment