Since reading the blog entry from Aras about the new shaderlab changes, I’m eager to learn shader language, and now trying to convert two separate relief shaders from 2.6 to 3.0. I got one to partially work, albeit without shadows and correct lighting.
I looked back at the older documentation for calculating light and shadows but adding these created problems with them, making them unable to be compiled.
LIGHTING_COORDS // in v2f struct
LIGHT_ATTENUATION(i) // return value at end of frag
I guess my question is, how should I look to add light attenutation and shadows to a relief shader being converted from 2.6? Or, should I just begin to rewrite them from the beginning in the new format?
Thanks for any insight.
I posted an example of a custom shader in this topic today. In it you can see how to use LIGHTING_COORDS and LIGHT_ATTENUATION.
Thanks Daniel, looking now.
I’m running into some difficulty so I’ll post what I have here until I can figure it out.
If anyone has any idea what might be wrong, please let me know! 
This is my new v3 relief shader in the game with height set at +1, it appear to be inside out:

I change the height to be -1 and it looks correct, but that shouldn’t be:

I chose to write this relief shader as a v3 surface shader (in the format of the new shaders) as opposed to writing the forward and deferred passes manually.
Shader "Relief_surfaceShader" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_Height("Height", Range(0, -1)) = 0.0
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_HeightMap ("Heightmap (A)", 2D) = "black" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
LOD 600
CGPROGRAM
#pragma surface surf BlinnPhong
#pragma target 3.0
#pragma profileoption MaxTexIndirections=256
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _HeightMap;
float _Height;
float4 _Color;
float _Shininess;
struct Input { // vertex out properties
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
void ray_intersect_relief(sampler2D relief_map, inout float3 p, inout float3 v ){
const int linearSearchSteps = 20;
const int binarySearchSteps = 10;
v /= v.z * linearSearchSteps;
int i;
for( i=0;i<linearSearchSteps;i++ )
{
float4 tex = tex2D(relief_map, p.xy);
if (p.z<tex.w) p+=v;
}
for( i=0;i<binarySearchSteps;i++ )
{
v *= 0.5;
float4 tex = tex2D(relief_map, p.xy);
if (p.z<tex.w) p+=v; else p-=v;
}
}
float ray_intersect_rm(in sampler2D reliefMap, in float2 uvBumpMap, in float2 viewXY, in float bias){
const int LINEAR_SEARCH = 20;
const int BINARY_SEARCH = 10;
float size = 1.0 / LINEAR_SEARCH; // current size of search window
float depth = -bias; // current depth position
int i;
for(i = 0; i < LINEAR_SEARCH - 1; i++){ // search front to back for first point inside object
float4 tex = tex2D(reliefMap, uvBumpMap + viewXY * depth).a;
if(depth < tex.w - bias)
depth += size;
}
for(i = 0; i < BINARY_SEARCH; i++) // recurse around first point (depth) for closest match
{
size *= 0.5;
float4 tex = tex2D(reliefMap, uvBumpMap + viewXY * depth).a;
if(depth < tex.w - bias)
depth += (2*size);
depth -= size;
}
return depth;
}
void surf (Input IN, inout SurfaceOutput o) {
IN.viewDir = normalize(IN.viewDir);
float2 view = IN.viewDir.xy * _Height;
float depth = ray_intersect_rm(_HeightMap, IN.uv_BumpMap, view, 1.0f);
float2 offset = view * depth;
float4 tex = tex2D(_MainTex, IN.uv_MainTex);
o.Gloss = tex.a;
o.Specular = _Shininess;
o.Albedo = tex2D(_MainTex, IN.uv_MainTex + offset) *_Color.rgb;
o.Normal = tex2D(_BumpMap, IN.uv_BumpMap + offset);
}
ENDCG
}
FallBack "Bumped Specular"
}
Ok, I’ve got it somewhat working and casting shadows, but the shadows seem to be operating on only the vertices.
With the new v3 surface shaders, how would I get the pixel information into the deferred depth buffer so self shadowing and SSAO can be used on the occluded areas created by the relief maps?
btw, updated relief code here, it still uses negative height for proper relief depth and lighting direction appears to be inversed. :
Shader "Relief Retro 3" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_Height ("Height", Float) = -0.05
_MainTex ("Base (RGB)", 2D) = "white" {}
_NormalMap ("Normalmap, Height (A)", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
CGPROGRAM
#pragma surface surf BlinnPhong
#pragma target 3.0
sampler2D _MainTex;
sampler2D _NormalMap;
float4 _Color;
float _Height;
struct Input {
float2 uv_MainTex;
float2 uv_NormalMap;
float3 viewDir;
};
void surf (Input IN, inout SurfaceOutput o) {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//setup the view ray
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float3 p = float3(IN.uv_NormalMap,0);
float3 v = normalize(IN.viewDir);
v.z = abs(v.z);
//depth bias
float depthBias = 1.0 - v.z;
depthBias *= depthBias;
depthBias *= depthBias;
depthBias = 1.0 - depthBias * depthBias;
v.xy *= depthBias;
v.xy *= _Height;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//ray intersection
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const int linearSearchSteps = 20;
const int binarySearchSteps = 10;
v /= v.z * linearSearchSteps;
int i;
for( i=0;i<linearSearchSteps;i++ )
{
float4 tex = tex2D(_NormalMap, p.xy);
if (p.z<tex.w) p+=v;
}
for( i=0;i<binarySearchSteps;i++ )
{
v *= 0.5;
float4 tex = tex2D(_NormalMap, p.xy);
if (p.z < tex.w) p += v; else p -= v;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//final output
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
half4 tex = tex2D(_MainTex, p.xy);
o.Albedo = tex.rgb * _Color.rgb;
float4 normal = tex2D(_NormalMap,p.xy); // normal map
normal.xy = 2 * normal.xy - 1;
normal.y = -normal.y;
normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
o.Normal = normal;
}
ENDCG
}
FallBack "Diffuse"
}
Ok, fixed the light direction by adding a custom vertex function:
void vert (inout appdata_full v, out Input o) {
TANGENT_SPACE_ROTATION;
o.viewDir = _WorldSpaceCameraPos;
}