I have written this procedural textures fragment shader with shadows, and it is totally white without shadows as if light attenuation value is 1.0 on every pixel?
float4 frag(v2f i) : COLOR {
half atten = LIGHT_ATTENUATION(i);
return half4(atten,atten,atten,0.0);
}
Shader "Custom/Pattern" {
Properties {
}
SubShader {
Pass {
CGPROGRAM
#pragma debug
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#pragma exclude_renderers gles
#pragma multi_compile_fwdadd_fullshadows
#include "UnityCG.cginc"
#include "AutoLight.cginc"
//user defined variables
struct input {
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float3 color : COLOR0;
float2 texcoord : TEXCOORD0;
LIGHTING_COORDS(3,4)
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o);
o.texcoord = v.texcoord;
return o;
}
// fragment function
float4 frag(v2f i) : COLOR {
half r1 = sin(i.texcoord.x*555);//sinus stripey textures
half atten = LIGHT_ATTENUATION(i);
return half4(atten,atten,atten,0.0);
}
ENDCG
}
}
fallback "Diffuse"
}
I believe you need a LightMode pass tag if you want any of the lighting environment values to be set up.
Thank you. I added this line and tried various modes:
Tags { “LightMode” = “ForwardBase” }
The light attenuation value is still the same for every pixel of the mesh.
Because your multi_compile pragma is telling it to be a ForwardAdd pass, not a ForwardBase.
The LightMode tag and the multi_compile pragmas need to be talking about the same pass.
Thanks, I had actually tried all the different possible light modes, I am a reasonably thorough! I still couldn’t get shadow information, what can I be missing? could you copy the shader code onto a sphere and find the error? It would permit me to progress on code for an organic and cartoon shader texture generator I wish to do:)
This shader does a bit more than that - it’s essentially the default Bumped Spec shader but in vert/frag - but you should be able to see the important parts.
http://forum.unity3d.com/threads/198141-Lighting-using-tangent-vector-and-also-using-surface-shaders?p=1344367&viewfull=1#post1344367
do I need unity pro?
Thanks! i checked it all, my code still runs wrong, it uses same statements and methods as you, except it’ shorter and simpler, I tried all code permutations I could, there is a weird error, no shadows still. I’ll start from scratch on someone else’s.
your code in the link you posted also displays everything without shadows, all white. my scene has directional lights, it’s in unity free. This didn’t Show any shadows, it is your bump mapped code word for word, just the first pass:
Shader "ForwardRendering" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
}
SubShader {
Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}
Pass {
Tags {"LightMode" = "ForwardBase"} // This Pass tag is important or Unity may not give it the correct light information.
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase // This line tells Unity to compile this pass for forward base.
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#pragma target 3.0
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 viewDir : TEXCOORD1;
float3 lightDir : TEXCOORD2;
LIGHTING_COORDS(3,4) // Macro to send shadow attenuation to the vertex shader.
};
v2f vert (appdata_tan v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
TANGENT_SPACE_ROTATION; // Macro for unity to build the Object>Tangent rotation matrix "rotation".
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex));
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex));
TRANSFER_VERTEX_TO_FRAGMENT(o); // Macro to send shadow attenuation to the fragment shader.
return o;
}
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
half _Shininess;
fixed4 _SpecColor;
fixed4 _LightColor0; // Colour of the light used in this pass.
fixed4 frag(v2f i) : COLOR
{
i.viewDir = normalize(i.viewDir);
i.lightDir = normalize(i.lightDir);
fixed atten = LIGHT_ATTENUATION(i); // Macro to get you the combined shadow attenuation value.
fixed4 tex = tex2D(_MainTex, i.uv);
fixed gloss = tex.a;
tex *= _Color;
fixed3 normal = UnpackNormal(tex2D(_BumpMap, i.uv));
half3 h = normalize(i.lightDir + i.viewDir);
fixed diff = saturate(dot(normal, i.lightDir));
float nh = saturate(dot (normal, h));
float spec = pow(nh, _Shininess * 128.0) * gloss;
fixed4 c;
c.rgb = UNITY_LIGHTMODEL_AMBIENT.rgb * 2 * tex.rgb; // Ambient term. Only do this in Forward Base. It only needs calculating once.
c.rgb += (tex.rgb * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2); // Diffuse and specular.
c.a = tex.a + _LightColor0.a * _SpecColor.a * spec * atten;
return c;
}
ENDCG
}
}
fallback "Diffuse"
}
To get shadows in Free you need Unity 4.2, that will give you hard shadows from one directional light, assuming you have the light set to cast hard shadows.
In Unity Free before version 4.2 you can’t have shadows at all.
Also FallBack might be case sensitive, you have it in lower case. It’s the fallback that gives it the shadow caster/receiver. Without those, it won’t cast or receive shadows at all, no matter what you do.
Thanks, I just want to have meshes with a dark side, and a bright side toward the light. Is that also shadows and unity 4.2?
in this pic, the snow thing is normally a landscape. it’s the previous code.
I think for that you want the clamped dot product of the normal and the light direction.
thanks, that’s a brilliant idea. that’s indeed the solution.