Skip to content

Instantly share code, notes, and snippets.

@braaad
Created October 31, 2014 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braaad/e2a77260282c5f4e3b5a to your computer and use it in GitHub Desktop.
Save braaad/e2a77260282c5f4e3b5a to your computer and use it in GitHub Desktop.
Shader "Jove/Deferred/TerrainTest"
{
Properties
{
[HideInInspector] _Splat0("Layer 0 (R)", 2D) = "white" {}
[HideInInspector] _Splat1("Layer 1 (G)", 2D) = "white" {}
[HideInInspector] _Splat2("Layer 2 (B)", 2D) = "white" {}
[HideInInspector] _Splat3("Layer 3 (A)", 2D) = "white" {}
[HideInInspector] _Normal0("Normal 0 (R)", 2D) = "bump" {}
[HideInInspector] _Normal1("Normal 1 (G)", 2D) = "bump" {}
[HideInInspector] _Normal2("Normal 2 (B)", 2D) = "bump" {}
[HideInInspector] _Normal3("Normal 3 (A)", 2D) = "bump" {}
[HideInInspector]_Control("Control (RGBA)", 2D) = "white" {}
[HideInInspector]_MainTex("Never Used", 2D) = "white" {}
_Smoothness0("Smoothness 0", Range(0, 1)) = 1
_AttributeTex0("0 M(R) AO(G) Tran(B) Sm(A)", 2D) = "black" {}
_Smoothness1("Smoothness 1", Range(0, 1)) = 1
_AttributeTex1("1 M(R) AO(G) Tran(B) Sm(A)", 2D) = "black" {}
_Smoothness2("Smoothness 2", Range(0, 1)) = 1
_AttributeTex2("2 M(R) AO(G) Tran(B) Sm(A)", 2D) = "black" {}
_Smoothness3("Smoothness 3", Range(0, 1)) = 1
_AttributeTex3("3 M(R) AO(G) Tran(B) Sm(A)", 2D) = "black" {}
}
Category
{
Fog{ Mode Off }
SubShader
{
Tags
{
"SplatCount" = "4"
"Queue" = "Geometry-100"
"RenderType" = "Opaque"
"JoveShadowCaster" = "Opaque"
"JoveRenderType" = "Deferred"
}
Pass
{
Stencil
{
Ref 128
WriteMask 128
Comp Always
Pass Replace
}
CGPROGRAM
#include "DeferredGBuffer.cginc"
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
#pragma target 5.0
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
sampler2D _Splat0, _Splat1, _Splat2, _Splat3;
sampler2D _Normal0, _Normal1, _Normal2, _Normal3;
sampler2D _AttributeTex0, _AttributeTex1, _AttributeTex2, _AttributeTex3;
sampler2D_half _Control;
float _Smoothness0, _Smoothness1, _Smoothness2, _Smoothness3;
half4 _Splat0_ST;
half4 _Splat1_ST;
half4 _Splat2_ST;
half4 _Splat3_ST;
half4 _Control_ST;
struct v2fBase
{
float4 pos : SV_POSITION;
float3 vnorm : NORMAL;
float depth : TEXCOORD0;
float3 normalDir : TEXCOORD1;
float3 tangentDir : TEXCOORD2;
float3 binormalDir : TEXCOORD3;
half2 uv0 : TEXCOORD4;
half2 uv1 : TEXCOORD5;
half2 uv2 : TEXCOORD6;
half2 uv3 : TEXCOORD7;
half2 uv : TEXCOORD8;
};
v2fBase vert(a2v v)
{
v2fBase o;
o.tangentDir = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, cross(v.normal, float3(0, 0, 1))));
o.normalDir = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normal));
o.binormalDir = normalize(cross(o.normalDir, o.tangentDir) * -1);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.depth = o.pos.w;
o.uv = v.texcoord;
o.uv0 = TRANSFORM_TEX(v.texcoord, _Splat0);
o.uv1 = TRANSFORM_TEX(v.texcoord, _Splat1);
o.uv2 = TRANSFORM_TEX(v.texcoord, _Splat2);
o.uv3 = TRANSFORM_TEX(v.texcoord, _Splat3);
return o;
}
GBufferProperties frag(v2fBase i)
{
fixed4 control = tex2D(_Control, i.uv);
// ======== NORMALS ===========
float4 n;
n = control.r * tex2D(_Normal0, i.uv0);
n += control.g * tex2D(_Normal1, i.uv1);
n += control.b * tex2D(_Normal2, i.uv2);
n += control.a * tex2D(_Normal3, i.uv3);
// ======== DIFFUSE ==============
float4 d;
d = control.r * tex2D(_Splat0, i.uv0);
d += control.g * tex2D(_Splat1, i.uv1);
d += control.b * tex2D(_Splat2, i.uv2);
d += control.a * tex2D(_Splat3, i.uv3);
// ======== ATTRIBUTES ==============
float4 a;
a = control.r * tex2D(_AttributeTex0, i.uv0) * float4(1, 1, 1, _Smoothness0);
a += control.g * tex2D(_AttributeTex1, i.uv1) * float4(1, 1, 1, _Smoothness1);
a += control.b * tex2D(_AttributeTex2, i.uv2) * float4(1, 1, 1, _Smoothness2);
a += control.a * tex2D(_AttributeTex3, i.uv3) * float4(1, 1, 1, _Smoothness3);
float splatSum = dot(control, fixed4(1, 1, 1, 1));
float4 flatNormal = fixed4(0.5, 0.5, 1, 0.5);
n = lerp(flatNormal, n, splatSum);
float3 textureNormal = DeferredUnpackNormal(n);
GBufferProperties gBufferProp;
gBufferProp.GBuffTex1 = float4(d.rgb, 0.0f);
gBufferProp.GBuffTex2 = a;
gBufferProp.GBuffTex3 = EncodeTangentNormals(textureNormal, i.tangentDir, i.binormalDir, i.normalDir);
gBufferProp.GBuffTex4.x = EncodeDepth(i.depth);
return gBufferProp;
}
ENDCG
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment