Latest package: Dropbox - File Deleted - Simplify your life
I’m trying to add parallax effect to default terrain shader “TerrBumpFirstPass” which comes with Unity 4. Here’s the current code:
Shader "Custom/Simple Terrain Parallax" {
Properties {
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
_Parallax ("Height", Range (0.005, 0.108)) = 0.02
// set by terrain engine
[HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
[HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
[HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
[HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
[HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
[HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {}
[HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {}
[HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {}
[HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {}
// used in fallback on old cards base map
[HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {}
[HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
}
SubShader {
Tags {
"SplatCount" = "4"
"Queue" = "Geometry-100"
"RenderType" = "Opaque"
}
CGPROGRAM
#pragma surface surf BlinnPhong vertex:vert
#pragma target 3.0
sampler2D _Control;
sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
sampler2D _Normal0,_Normal1,_Normal2,_Normal3;
half _Shininess;
float _Parallax;
struct Input {
float2 uv_Control : TEXCOORD0;
float2 uv_Splat0 : TEXCOORD1;
float2 uv_Splat1 : TEXCOORD2;
float2 uv_Splat2 : TEXCOORD3;
float2 uv_Splat3 : TEXCOORD4;
float3 viewDir;
};
void vert (inout appdata_full v, out Input o)
{
o.uv_Control = v.texcoord;
o.uv_Splat0 = v.texcoord1;
o.uv_Splat1 = v.texcoord1;
o.uv_Splat2 = v.texcoord1;
o.uv_Splat3 = v.texcoord1;
v.tangent.xyz = cross(v.normal, float3(0,0,1));
v.tangent.w = -1;
o.viewDir = WorldSpaceViewDir(v.vertex);
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 splat_control = tex2D (_Control, IN.uv_Control);
fixed4 col;
// Parallax
half h;
float2 offset;
h = tex2D (_Splat0, IN.uv_Splat0).w;
offset = ParallaxOffset (h, _Parallax, IN.viewDir);
IN.uv_Splat0 += offset;
h = tex2D (_Splat1, IN.uv_Splat1).w;
offset = ParallaxOffset (h, _Parallax, IN.viewDir);
IN.uv_Splat1 += offset;
h = tex2D (_Splat2, IN.uv_Splat2).w;
offset = ParallaxOffset (h, _Parallax, IN.viewDir);
IN.uv_Splat2 += offset;
h = tex2D (_Splat3, IN.uv_Splat3).w;
offset = ParallaxOffset (h, _Parallax, IN.viewDir);
IN.uv_Splat3 += offset;
col = splat_control.r * tex2D (_Splat0, IN.uv_Splat0);
col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1);
col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2);
col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3);
o.Albedo = col.rgb;
fixed4 nrm;
nrm = splat_control.r * tex2D (_Normal0, IN.uv_Splat0);
nrm += splat_control.g * tex2D (_Normal1, IN.uv_Splat1);
nrm += splat_control.b * tex2D (_Normal2, IN.uv_Splat2);
nrm += splat_control.a * tex2D (_Normal3, IN.uv_Splat3);
fixed splatSum = dot(splat_control, fixed4(1,1,1,1));
fixed4 flatNormal = fixed4(0.5,0.5,1,0.5);
nrm = lerp(flatNormal, nrm, splatSum);
o.Normal = UnpackNormal(nrm);
o.Gloss = col.a * splatSum;
o.Specular = _Shininess;
o.Alpha = 0.0;
}
ENDCG
}
Dependency "AddPassShader" = "Hidden/Nature/Terrain/Bumped Specular AddPass"
Dependency "BaseMapShader" = "Specular"
Fallback "Nature/Terrain/Diffuse"
}
As I need to get terrain’s vetex data such as camera distance in surf part, so I need to calculate needed data in vert including out parameters. But you may see that there is a problem with the View Direction “viewDir” or maybe tangents in different terrain angles when you increase “Height” value of the parallax in shader. I’ve also noticed that if you change the name of “viewDir” to something else in shader, the results will vary as it’s a predefined variable in Unity.
I’m really stuck at this part and I’m no expert at shaders. I’m sure solving this issue will help many others and this shader will be useful for many.
I’ve attached sample Diffuse Normal textures. Displacement data is in the alpha channel of the diffuse texture. Add textures to your terrain in Unity and check the shader for yourself.
1414063–74138–$Pavement.zip (1.82 MB)