Created
November 3, 2019 07:49
-
-
Save kasari/8ccac42c8477596df75d4cb5247fdb30 to your computer and use it in GitHub Desktop.
[Unity] Post Process Scan Effect
This file contains 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
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); | |
} | |
} |
This file contains 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
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考元
https://www.ronja-tutorials.com/2018/07/01/postprocessing-depth.html