Hi guys, I'm getting mad with this. I'm using Unity 5 and I'm tring to resolve this problem from yesterday searching on Unity Answer or similar. Everything that I'm trying is not working and I don't know why.
So:
I'm new in Shader coding and I'm trying to figure out how to solve a problem:
My company has a shader
Shader "Curved Diffuse" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Main Color", Color) = (1,0.5,0.5,1)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 150
CGPROGRAM
#pragma exclude_renderers flash
#pragma surface surf Lambert noforwardadd vertex:vert
sampler2D _MainTex;
float4 _QOffset;
float _Dist;
float4 _Color;
struct Input {
float2 uv_MainTex;
};
void vert (inout appdata_full v) {
// Get the view space vertex position
float4 vertex_view = mul(UNITY_MATRIX_MV, v.vertex);
// Calculate the offset in view space
float zOff = vertex_view.z / _Dist;
// Convert the offset back to object space and add it to vertex
v.vertex.xyz += mul(_QOffset*zOff*zOff, UNITY_MATRIX_IT_MV).xyz;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo =c.rgb;
o.Albedo *= _Color.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Mobile/VertexLit"
}
Now, we add a light (a directional light that cast hard shadows) to the scene and we noticed that shadows don't follow the deformation. So I start looking for and answer.
Following these istruction: http://answers.unity3d.com/questions/693132/need-to-get-lighting-to-work-on-bending-vertex-sha.html
I try by erasing the Fallback "VertexLit"
then adding #pragma addshadow
but nothing happen.
So I try to find the solution provided by another source:
http://forum.unity3d.com/threads/modifying-vertex-position-shader-in-a-surface-shader-shadow-problems.216276/
I try to add after my code the code developed from jvo3dc, but nothing change.
Maybe I'm using the code in the wrong way, This is what I did:
Shader "Curved Diffuse shadows" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Main Color", Color) = (1,0.5,0.5,1)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 150
CGPROGRAM
#pragma exclude_renderers flash
#pragma surface surf Lambert vertex:vert addshadow
// #pragma surface surf Lambert alpha
sampler2D _MainTex;
float4 _QOffset;
float _Dist;
float4 _Color;
struct Input {
float2 uv_MainTex;
};
void vert (inout appdata_full v) {
// Get the view space vertex position
float4 vertex_view = mul(UNITY_MATRIX_MV, v.vertex);
// Calculate the offset in view space
float zOff = vertex_view.z / _Dist;
// Convert the offset back to object space and add it to vertex
v.vertex.xyz += mul(_QOffset*zOff*zOff, UNITY_MATRIX_IT_MV).xyz;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo =c.rgb;
o.Albedo *= _Color.rgb;
o.Alpha = c.a;
}
ENDCG
// Pass to render object as a shadow caster
Pass {
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
Fog {Mode Off}
ZWrite On ZTest LEqual Cull Off
Offset 1, 1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
float4 _QOffset;
float _Dist;
struct v2f {
V2F_SHADOW_CASTER;
};
v2f vert( appdata_base v ) {
float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
float zOff = vPos.z/_Dist;
vPos += _QOffset*zOff*zOff;
v.vertex = mul (vPos, UNITY_MATRIX_IT_MV);
v2f o;
TRANSFER_SHADOW_CASTER(o)
return o;
}
float4 frag( v2f i ) : COLOR {
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
// Pass to render object as a shadow collector
Pass {
Name "ShadowCollector"
Tags { "LightMode" = "ShadowCollector" }
Fog {Mode Off}
ZWrite On ZTest LEqual
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_shadowcollector
#define SHADOW_COLLECTOR_PASS
#include "UnityCG.cginc"
float4 _QOffset;
float _Dist;
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
V2F_SHADOW_COLLECTOR;
};
v2f vert (appdata v) {
float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
float zOff = vPos.z/_Dist;
vPos += _QOffset*zOff*zOff;
v.vertex = mul (vPos, UNITY_MATRIX_IT_MV);
v2f o;
TRANSFER_SHADOW_COLLECTOR(o)
return o;
}
fixed4 frag (v2f i) : COLOR {
SHADOW_COLLECTOR_FRAGMENT(i)
}
ENDCG
}
}
//Fallback "Mobile/VertexLit"
}
I really don't know what to do, I'm tring to read as much as possible about SurfaceShader, but I'm not understanding why things are not working in the proper way.
Any help will be really appreciated.
Thanks a lot
↧