Created
February 22, 2023 08:17
-
-
Save unitycoder/696a07c9de34e6a8fd28dc7de2362501 to your computer and use it in GitHub Desktop.
Fake 3D effect with depth map (Unity Shader)
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; | |
// https://gist.github.com/bozzin/5895d97130e148e66b88ff4c92535b59 | |
public class DepthFX : MonoBehaviour | |
{ | |
public float power = -0.02f; | |
void Update() | |
{ | |
var mpos = Input.mousePosition; | |
mpos.x = (mpos.x / Screen.width * 2 - 1) * -power; | |
mpos.y = (mpos.y / Screen.height * 2 - 1) * -power; | |
Shader.SetGlobalVector("_MousePos", new Vector4(mpos.x, mpos.y, 0, 0)); | |
} | |
} |
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
// https://gist.github.com/bozzin/5895d97130e148e66b88ff4c92535b59 | |
// requires DepthFX.cs script to send mouseposition | |
Shader "Unlit/DepthFX" | |
{ | |
Properties | |
{ | |
_MainTex ("Texture", 2D) = "white" {} | |
_DepthTex ("Depth Texture", 2D) = "white" {} | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
LOD 100 | |
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; | |
}; | |
sampler2D _MainTex; | |
sampler2D _DepthTex; | |
float4 _MainTex_ST; | |
float4 _MousePos; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float parallaxMult = tex2D(_DepthTex, i.uv); | |
float2 parallax = _MousePos.xy*parallaxMult; | |
fixed4 c = tex2D(_MainTex, i.uv+ parallax); | |
return c; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment