I’m trying to hack the Unity builtin Grass Shader at the moment…
For starters I would like the gras to drop shadows and to be bump mapped… yet as I’m pretty new to the whole shader programming, I’m struggling as to how to achieve this.
I used the example with bump mapped shaders in the documentation as starting point for the bump mapping, now the question is how I can send the bump map to the shader (I know others have achieved this, but I don’t quite understand how… anyone could give me a hint?
EDIT: I found a bug in my code, so now the shader works, but I get a warning telling me “Shader wants tangents, but the mesh doesn’t have them” At runtime… what causes this problem? Is bump mapping not possible for simple Meshes like the Gras one? Or do I need to use a different algorithm to make it work (like what is used for the nature/leaves material shader)?
I’m also not quite sure how I add the the capability to drop directional shadows to the existing Gras Shader… I see that there are special tags for that, but I don’t get how to use these and where to put them.
Here the code I use at the moment EDIT: Working now, with the warning as written above:
Shader "Hidden/TerrainEngine/Details/WavingDoublePass" {
Properties {
_WavingTint ("Fade Color", Color) = (.7,.6,.5, 0)
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_WaveAndDistance ("Wave and distance", Vector) = (12, 3.6, 1, 1)
_GrasBump0 ("Bumpmap", 2D) = "bump" {}
_Cutoff ("Cutoff", float) = 0.5
}
SubShader {
Tags {
"Queue" = "Geometry+200"
//"IgnoreProjector"="True"
"RenderType"="Opaque"
}
Cull Off
LOD 200
ColorMask RGB
CGPROGRAM
#pragma surface surf Lambert vertex:WavingGrassVert addshadow
#include "TerrainEngine.cginc"
sampler2D _MainTex;
sampler2D _GrasBump0;
fixed _Cutoff;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
fixed4 color : COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
o.Albedo = c.rgb;
o.Alpha = c.a;
clip (o.Alpha - _Cutoff);
o.Alpha *= IN.color.a;
o.Normal = UnpackNormal (tex2D (_GrasBump0, IN.uv_BumpMap));
}
ENDCG
}
/*
SubShader {
Tags {
"Queue" = "Geometry+200"
//"IgnoreProjector"="True"
"RenderType"="Opaque"
}
Cull Off
LOD 200
ColorMask RGB
Pass {
Tags { "LightMode" = "ForwardAdd" }
Material {
Diffuse (1,1,1,1)
Ambient (1,1,1,1)
}
Lighting On
ColorMaterial AmbientAndDiffuse
AlphaTest Greater [_Cutoff]
SetTexture [_MainTex] { combine texture * primary DOUBLE, texture }
}
Pass {
Tags { "LightMode" = "VertexLMRGBM" }
AlphaTest Greater [_Cutoff]
BindChannels {
Bind "Vertex", vertex
Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
Bind "texcoord", texcoord1 // main uses 1st uv
}
SetTexture [unity_Lightmap] {
matrix [unity_LightmapMatrix]
combine texture * texture alpha DOUBLE
}
SetTexture [_MainTex] { combine texture * previous QUAD, texture }
}
}*/
Fallback Off
}
I commented the subshaders out as it seemed they are not doing anything on my machine at least (and I wanted to reduce the possible “point of failures”)