Skip to content

Instantly share code, notes, and snippets.

@kasari
Created November 3, 2019 07:49
Show Gist options
  • Save kasari/8ccac42c8477596df75d4cb5247fdb30 to your computer and use it in GitHub Desktop.
Save kasari/8ccac42c8477596df75d4cb5247fdb30 to your computer and use it in GitHub Desktop.
[Unity] Post Process Scan Effect
using UnityEngine;
public class DepthScan : MonoBehaviour
{
[SerializeField] private Camera cam;
[SerializeField] private Material mat;
[SerializeField] private float scanSpeed = 80f;
private float scanDistance;
private void Start()
{
cam.depthTextureMode = cam.depthTextureMode | DepthTextureMode.Depth;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.S)) scanDistance = 0.0f;
scanDistance = scanDistance + scanSpeed * Time.deltaTime;
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
mat.SetFloat("_ScanDistance", scanDistance);
Graphics.Blit(source, destination, mat);
}
}
Shader "DepthScan"
{
Properties
{
[HideInspector] _MainTex ("Texture", 2D) = "white" {}
_ScanDistance ("Distance", float) = 10
_ScanTrail ("Trail", float) = 4
_ScanColor ("Color", Color) = (0,1,1,1)
}
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float _ScanDistance;
float _ScanTrail;
float4 _ScanColor;
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
fixed4 frag (v2f i) : SV_Target
{
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
depth = LinearEyeDepth(depth);
fixed4 mainColor = tex2D(_MainTex, i.uv);
if (depth > _ScanDistance) return mainColor;
float ScanColorIntensity = clamp((depth - _ScanDistance + _ScanTrail) / _ScanTrail, 0, 1);
fixed4 col = lerp(mainColor, _ScanColor, ScanColorIntensity);
return col;
}
ENDCG
}
}
}
@kasari
Copy link
Author

kasari commented Nov 3, 2019

PostProcessScanEffect

@kasari
Copy link
Author

kasari commented Nov 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment