- Home /
How to receive shadows on a simple custom shader
I've been trying to add shadows to a very simple vertex color+texture shader but unfortunately to no avail. The aim is to have a shader that renders a texture which it then blends with custom vertex colors (working) and to receive shadows from other objects in the scene. I've tried several approaches but none of them seems to do the trick.
Please find the shader code below (originated from: http://wiki.unity3d.com/index.php/VertexColor):
Shader " Vertex Colored" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass {
Material {
Diffuse(1,1,1,1)
Ambient(1,1,1,1)
}
ColorMaterial AmbientAndDiffuse
Lighting On
SetTexture [_MainTex] {
Combine texture * primary, texture * primary
}
SetTexture [_MainTex] {
constantColor [_Color]
Combine previous * constant DOUBLE, previous * constant
}
}
}
Fallback "VertexLit"
}
Answer by opri · Sep 23, 2014 at 07:15 PM
CG to the rescue, hopefully helps someone. A shader that blends vertex colors with texture colors and receives shadows.
Shader " Vertex Colored" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
float4 color : COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Albedo *= IN.color.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "VertexLit"
}
Can you translate this into vert/frag shader? I have done this but my results are very strange, I am getting some strange lines in shadow areas, I have maximized my settings of shadows from quality settings but still its not working fine.
Code is below:
Shader "Umirza47/NonSurf/Lambert"
{
Properties
{
_Color ("Color", Color) = (1.0,1.0,1.0,1.0)
}
CGINCLUDE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"
ENDCG
SubShader
{
LOD 200
Tags { "RenderType"="Opaque" }
Pass {
Lighting On
Tags {"Light$$anonymous$$ode" = "ForwardBase"}
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
uniform half4 _Color;
struct vertexInput
{
half4 vertex : POSITION;
half3 normal : NOR$$anonymous$$AL;
};
struct vertexOutput
{
half4 pos : SV_POSITION;
fixed4 lightDirection : TEXCOORD1;
fixed3 viewDirection : TEXCOORD2;
fixed3 normalWorld : TEXCOORD3;
LIGHTING_COORDS(4,6)
};
vertexOutput vert (vertexInput v)
{
vertexOutput o;
half4 posWorld = mul( _Object2World, v.vertex );
o.normalWorld = normalize( mul(half4(v.normal, 0.0), _World2Object).xyz );
o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
o.viewDirection = normalize(_WorldSpaceCameraPos.xyz - posWorld.xyz);
TRANSFER_VERTEX_TO_FRAG$$anonymous$$ENT(o);
return o;
}
half4 frag (vertexOutput i) : COLOR
{
fixed NdotL = dot(i.normalWorld, i.lightDirection);
half atten = LIGHT_ATTENUATION(i);
fixed3 diffuseReflection = _LightColor0.rgb * atten * _Color.rgb;
fixed3 finalColor = UNITY_LIGHT$$anonymous$$O$$anonymous$$_A$$anonymous$$BIENT.xyz + diffuseReflection;
return float4(finalColor, 1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613142938im_/https://answers.unity.com/themes/thub/images/avi.jpg)