- Home /
Shader lighting is too dark
Cross posted on the Unity Forums here.
Ok, let me preface this with: this is the first vertex/fragment shader I have written. I have a surface shader version of this that is working well - but I need it to work in the Vertex pipeline. The shader basically uses a reference map to recolour various parts of a combined mesh with different colours. That works fine. The problem is that it's too dark! The shader is written to handle one directional light, but I've also tried calling ShadeVertexLights and it's has the same effect.
Shader "Custom/ColorRemappingVertexLit" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color1Tex ("Color1 (RGB)", 2D) = "white" {}
_Color2Tex ("Color2 (RGB)", 2D) = "white" {}
_Color3Tex ("Color3 (RGB)", 2D) = "white" {}
}
SubShader {
Lighting On
Tags { "RenderType"="Opaque" "IgnoreProjector"="True" "LightMode" = "Vertex" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define USING_DIRECTIONAL_LIGHT
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _Color1Tex;
sampler2D _Color2Tex;
sampler2D _Color3Tex;
float4 _Color;
float4 _MainTex_ST;
float4 _Color1Tex_ST;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float4 color : COLOR;
};
v2f vert (a2v v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
o.uv2 = TRANSFORM_TEX (v.texcoord1, _Color1Tex);
float3 n = mul(UNITY_MATRIX_IT_MV, float4 ( v.normal, 1));
float LdotN = saturate (dot (normalize(unity_LightPosition[0]), n));
o.color = UNITY_LIGHTMODEL_AMBIENT * _Color;
o.color += LdotN * unity_LightColor[0];
return o;
}
half4 frag(v2f i) : COLOR
{
half4 c1 = tex2D (_Color1Tex, i.uv2);
half4 c = tex2D (_MainTex, i.uv);
if(c1.r==1 && c1.g==1 && c1.b==1)
{
return c * i.color;
}
else
{
half4 c2 = tex2D (_Color2Tex, i.uv2);
half4 c3 = tex2D (_Color3Tex, i.uv2);
half3 f = saturate(((c1.rgb * c.r) + (c2.rgb * c.g) + (c3.rgb * c.b))) * i.color;
return half4(f, c.a);
}
}
ENDCG
}
}
FallBack "Diffuse"
}
Answer by whydoidoit · Oct 17, 2012 at 08:34 AM
Thanks to help on the forum, it was pointed out to me that the lighting needs to be doubled, this is a fudge factor to have a smoother lit scene.
The new code looks like this and works fine:
Shader "Custom/ColorRemappingVertexLit" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color1Tex ("Color1 (RGB)", 2D) = "white" {}
_Color2Tex ("Color2 (RGB)", 2D) = "white" {}
_Color3Tex ("Color3 (RGB)", 2D) = "white" {}
}
SubShader {
Lighting On
Tags { "RenderType"="Opaque" "IgnoreProjector"="True" "LightMode" = "Vertex" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define USING_DIRECTIONAL_LIGHT
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _Color1Tex;
sampler2D _Color2Tex;
sampler2D _Color3Tex;
float4 _Color;
float4 _MainTex_ST;
float4 _Color1Tex_ST;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float4 color : COLOR;
};
v2f vert (a2v v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
o.uv2 = TRANSFORM_TEX (v.texcoord1, _Color1Tex);
float3 n = normalize(mul(UNITY_MATRIX_IT_MV, float4 ( v.normal, 1)));
float LdotN = dot (unity_LightPosition[0], n);
o.color = UNITY_LIGHTMODEL_AMBIENT * _Color * 2 ;
o.color += (LdotN * unity_LightColor[0]) * _Color * 2;
return o;
}
half4 frag(v2f i) : COLOR
{
half4 c1 = tex2D (_Color1Tex, i.uv2);
half4 c = tex2D (_MainTex, i.uv);
if(c1.r==1 && c1.g==1 && c1.b==1)
{
return c * i.color * 2;
}
else
{
half4 c2 = tex2D (_Color2Tex, i.uv2);
half4 c3 = tex2D (_Color3Tex, i.uv2);
half3 f = saturate(((c1.rgb * c.r) + (c2.rgb * c.g) + (c3.rgb * c.b))) * i.color;
return half4(f, c.a) * 2;
}
}
ENDCG
}
}
FallBack "Diffuse"
}