Skip to content

Instantly share code, notes, and snippets.

@lyuma
Created April 19, 2021 15:13
Show Gist options
  • Save lyuma/c3fb790b2ce03f5c4bc649b7a5bf1239 to your computer and use it in GitHub Desktop.
Save lyuma/c3fb790b2ce03f5c4bc649b7a5bf1239 to your computer and use it in GitHub Desktop.
Simple parallax Unity surface shader using tangent space constructed in the vertex shader.
Shader "Custom/SimpleParallax"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
_ParallaxMap ("Height Map", 2D) = "black" {}
_ParallaxZeroPoint ("0=Blk,Gamma,Grey", Range(-0.2157, 0.2843)) = -0.2157
_ParallaxOffset ("Extra Parallax Offset (CM)", Float) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:vert
// Use shader model 3.5 target, to get nicer looking lighting
#pragma target 3.5
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
float3 tangentSpaceCameraPos;
float3 tangentSpacePos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float _Parallax;
float _ParallaxOffset;
float _ParallaxZeroPoint;
sampler2D _ParallaxMap;
void vert(inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input,o);
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
float3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w * unity_WorldTransformParams.w;
// output the world to tangent space matrix
float3x3 tangentSpace = float3x3(worldTangent, worldBinormal, worldNormal);
o.tangentSpaceCameraPos = mul(tangentSpace, float4(_WorldSpaceCameraPos, 1.0)).xyz;
o.tangentSpacePos = mul(tangentSpace, mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1.0))).xyz;
}
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
float3 viewVector = (IN.tangentSpacePos.xyz - IN.tangentSpaceCameraPos).xyz;
float3 dir = (normalize(viewVector));
float2 parallax = (2 * step(0, dir.z) - 1) * dir.xy / (.001+abs(dir.z));
float2 parallaxuv = IN.uv_MainTex.xy + parallax * (_ParallaxOffset * 0.01);
float parallax_amount = tex2D(_ParallaxMap, parallaxuv).g - 0.2157 - _ParallaxZeroPoint;
parallaxuv += parallax * parallax_amount * _Parallax;
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, parallaxuv) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment