Hi guys.
Could anyone explain me how to write a shader that works in the following way: It should use Diffusemap with uv1 and a lightmap with uv2. Or maybe im not the first who needs that shader and its already exists.
Here’s one I’m using in my project.
Shader "Custom/Custom Lightmapped" {
Properties {
_MainTex ("Diffuse (RGB)", 2D) = "grey" {}
_LightMap ("Lightmap (RGB)", 2D) = "grey" {}
}
Category {
Tags { "RenderType"="Opaque" "IgnoreProjector"="True" "Queue"="Geometry" }
Lighting Off
SubShader {
LOD 200
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
v2f vert (appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv.xy = v.texcoord.xy;
o.uv.zw = v.texcoord1.xy;
return o;
}
sampler2D _MainTex, _Lightmap;
fixed4 frag(v2f i) : COLOR {
fixed4 c;
c.rgb = tex2D ( _MainTex, i.uv.xy ).rgb; // Diffuse.
c.rgb *= tex2D ( _Lightmap, i.uv.zw ).rgb * 2; // Lightmap.
c.a = 1;
return c;
}
ENDCG
}
}
SubShader {
LOD 100
Pass {
Tags { "LightMode" = "Always" }
Lighting Off
BindChannels {
Bind "Vertex", vertex
Bind "texcoord", texcoord0
Bind "texcoord1", texcoord1
}
SetTexture [_MainTex] {
combine texture
}
SetTexture [_Lightmap] {
combine texture * previous DOUBLE
}
}
}
}
FallBack Off
}
Thank you. ![]()
Bear in mind I’m currently doubling the lightmap. So anything that’s 50% grey will turn out fullbright, but anything lighter than that will start to overexpose the base texture - up to 2x it’s brightness if you put white in the lightmap.
To disable that and simply have the lightmap multiply over the diffuse, find the line
c.rgb *= tex2D ( _Lightmap, i.uv.zw ).rgb * 2; // Lightmap.
and remove the “* 2” to get
c.rgb *= tex2D ( _Lightmap, i.uv.zw ).rgb; // Lightmap.
and also find the line
combine texture * previous DOUBLE
and remove the “DOUBLE” to get
combine texture * previous