Blend Lightmap

I’'m trying to blend a lightmap but it doesn’t seem to work, I’m not sure why.
Here the code:

Shader "Example/LightMapped-Blend" {
	Properties {
		_Blend ("Blend", Range (0, 1)) = 0.5 
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LightMap ("Lightmap (RGB)", 2D) = "white" {}
	}
	SubShader {
		UsePass "Lightmapped/VertexLit/BASE"
		UsePass "Diffuse/PPL"
		Pass { 
			SetTexture [_MainTex]
			
			SetTexture [_LightMap] { 
				constantColor (0, 0, 0, [_Blend]) 
				combine texture lerp (constant) previous 
			} 
		}		 
	}
	FallBack "Lightmapped/VertexLit", 1
}

Anyone got an idea?

Fixed!

Could you post the final shader? I need something like this as well.

What the (modified) shader does is blending (using a slider) the strength of the lightmap, so it doesn’t blend between different lightmaps at the moment, although this should be trivial to implement.

I should be able to post the shader in the evening.

I managed to piece it together myself, with blending between two lightmaps as well. And you were right, it was pretty trivial. Thanks for the start!

Shader "Lightmapped/Diffuse Blend" {
	Properties {
		_Blend ("Blend", Range (0, 1)) = 0
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LightMap ("Lightmap From (RGB)", 2D) = "black" {}
		_LightMap2 ("Lightmap To (RGB)", 2D) = "black" {}
	}
	SubShader {
		Pass {
			Name "BASE"
			Tags {"LightMode" = "PixelOrNone"}
			Color [_PPLAmbient]
			BindChannels {
				Bind "Vertex", vertex
				Bind "normal", normal
				Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
				Bind "texcoord", texcoord1 // main uses 1st uv
			}
			SetTexture [_LightMap] {
				constantColor [_Color]
				combine texture * constant
			}
			SetTexture [_LightMap2] {
				constantColor (0, 0, 0, [_Blend])
				combine texture lerp (constant) previous
			}
			SetTexture [_MainTex] {
				constantColor [_Color]
				combine texture * previous, texture * constant
			}
		}
		UsePass "Diffuse/PPL"
	}
	FallBack "Lightmapped/VertexLit", 1
}

I was about to post the shader, good to see you’ve managed to do it by yourself :slight_smile:

One thing you might have overlooked are the fallback modes apparent in the original Lightmap-VertexLit shader. E.g. Your version supports three texture cards only. Older cards won’t be supported this way. My main shader file looks like this:

Shader "Custom/Lightmapped/Diffuse-Blend" {
	Properties {
		_Blend ("Blend", Range (0, 1)) = 0.5 
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LightMap ("Lightmap (RGB)", 2D) = "black" {}
	}
	SubShader {
		UsePass "Custom/Lightmapped/VertexLit-Blend/BASE"
		UsePass "Diffuse/PPL"
	}
	FallBack "Custom/Lightmapped/VertexLit-Blend", 1
}

The modifications I’ve done are mainly in the VertexLit-Blend shader which is based on the VertexLit shader(which has fallback modes, so you can check it out).

Herewith the shader with fallback modes/ support for older gfx cards (DirectX 7):
Suggestions/improvements are welcome :wink:

Primary shader:

Shader "Custom/Lightmapped/Diffuse-Blend" {
	Properties {
		_Blend ("Blend", Range (0, 1)) = 0 
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LightMap ("Lightmap From (RGB)", 2D) = "black" {} 
		_LightMap2 ("Lightmap To (RGB)", 2D) = "white" {} 
	}
	SubShader {
		UsePass "Custom/Lightmapped/VertexLit-Blend/BASE"
		UsePass "Diffuse/PPL"
	}
	FallBack "Custom/Lightmapped/VertexLit-Blend", 1
}

Dependent shader:

Shader "Custom/Lightmapped/VertexLit-Blend" {
// ------------------------------------------------------------------
// Three texture cards (Radeons, GeForce3/4Ti and up)

SubShader {
	Blend AppSrcAdd AppDstAdd
	Fog { Color [_AddFog] }

	// Ambient pass
	Pass {
		Name "BASE"
		Tags {"LightMode" = "PixelOrNone"}
		Color [_PPLAmbient]
		BindChannels {
			Bind "Vertex", vertex
			Bind "normal", normal
			Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
			Bind "texcoord", texcoord1 // main uses 1st uv
		}
         SetTexture [_LightMap] { 
            constantColor [_Color] 
            combine texture * constant 
         } 
         SetTexture [_LightMap2] { 
            constantColor (0, 0, 0, [_Blend]) 
            combine texture lerp (constant) previous 
         } 
		SetTexture [_MainTex] {
			constantColor [_Color]
			combine texture * previous, texture * constant
		}
	}
	
	// Vertex lights
	Pass {
		Name "BASE"
		Tags {"LightMode" = "Vertex"}
		Material {
			Diffuse [_Color]
			Shininess [_Shininess]
			Specular [_SpecColor]
		}

		Lighting On
		SeparateSpecular On

		BindChannels {
			Bind "Vertex", vertex
			Bind "normal", normal
			Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
			Bind "texcoord1", texcoord1 // lightmap uses 2nd uv
			Bind "texcoord", texcoord2 // main uses 1st uv
		}
		
		SetTexture [_LightMap] {
			constantColor [_Color]
			combine texture * constant
		}
		SetTexture [_LightMap] {
			constantColor (0.5,0.5,0.5,0.5)
			combine previous * constant + primary
		}
		SetTexture [_MainTex] {
			combine texture * previous DOUBLE, texture * primary
		}
	}
}

// ------------------------------------------------------------------
// Dual texture cards - draw in two passes

SubShader {
	Blend AppSrcAdd AppDstAdd
	Fog { Color [_AddFog] }

	// Always drawn base pass: texture * lightmap
	Pass {
		Name "BASE"
		Tags {"LightMode" = "Always"}
		Color [_PPLAmbient]
		BindChannels {
			Bind "Vertex", vertex
			Bind "normal", normal
			Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
			Bind "texcoord", texcoord1 // main uses 1st uv
		}
         SetTexture [_LightMap] { 
            constantColor [_Color] 
            combine texture * constant 
         } 
         SetTexture [_LightMap2] { 
            constantColor (0, 0, 0, [_Blend]) 
            combine texture lerp (constant) previous 
         } 
		SetTexture [_MainTex] {
			combine texture * previous, texture * primary
		}
	}
	
	// Vertex lights: add lighting on top of base pass
	Pass {
		Name "BASE"
		Tags {"LightMode" = "Vertex"}
		Material {
			Diffuse [_Color]
			Shininess [_Shininess]
			Specular [_SpecColor]
		}

		Lighting On
		SeparateSpecular On
		
		ColorMask RGB

		SetTexture [_MainTex] {
			combine texture * primary DOUBLE, texture
		}
	}
}

// ------------------------------------------------------------------
// Single texture cards - lightmap and texture in two passes; no lighting

SubShader {
	Blend AppSrcAdd AppDstAdd
	Fog { Color [_AddFog] }

	// Base pass: lightmap
	Pass {
		Name "BASE"
		Tags {"LightMode" = "Always"}
		BindChannels {
			Bind "Vertex", vertex
			Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
		}
         SetTexture [_LightMap] { 
            constantColor [_Color] 
            combine texture * constant 
         } 
         SetTexture [_LightMap2] { 
            constantColor (0, 0, 0, [_Blend]) 
            combine texture lerp (constant) previous 
         } 
	}
	
	// Second pass: modulate with texture
	Pass {
		Name "BASE"
		Tags {"LightMode" = "Always"}
		BindChannels {
			Bind "Vertex", vertex
			Bind "texcoord", texcoord0 // main uses 1st uv
		}
		Blend Zero SrcColor
		SetTexture [_MainTex] { combine texture }
	}
}

Fallback "VertexLit", 1

}

Yeah, I skipped all that since I didn’t really know how to fit the second lightmap into all of them. :slight_smile:

Are you sure that this really supports all the cards of the original shader? It’s using three textures in one pass in the dual texture subshader, and two textures in one pass in the single texture subshader. I’m no shader guru, but it sounds to me like a dual texture card only supports two textures in a pass. Also, shouldn’t the second lightmap be somewhere in the vertex lighting pass of the three texture subshader as well? I don’t know how, but it feels like it should be in there as well.

I thought so as well, but I checked by going through the various emulation modes and it all appeared to work well… Perhaps a shader expert can step in here?

yes, you assumed right.

Sorry for my ignorance, I am not quite used to read shader code, but does your shader is solving this issue

http://forum.unity3d.com/viewtopic.php?t=23940

That mean we need a second lightmap to raise the exposure of texture.

Thanks :wink: :roll:

No, it doesn’t.

So what is the purpose of this shader, what is the second lightmap for

This shader will blend/crossfade between 2 lightmaps.

…which is good if you are doing a day/night-cycle with baked lighting. For example.

…so yeah, it’s cool :slight_smile: