need help making a lightmap shader with texture blending

I need some help making a lightmap shader with texture blending based of vertex colors done in another app.

So here’s the basic light map shader I’m using that I burrowed for Penelope,

Shader "iPhone/LightmapOnlyShader"
{
	Properties
	{
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LightMap ("Lightmap (RGB)", 2D) = "white" { LightmapMode }
	}

	SubShader
	{
		Pass
		{
			Name "BASE"
			Tags {"LightMode" = "Always"}
	
			BindChannels {
				Bind "Vertex", vertex
				Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
				Bind "texcoord", texcoord1 // main uses 1st uv
			}
			SetTexture [_LightMap] {
				constantColor [_Color]
				combine texture * constant
			}
			SetTexture [_MainTex] {
				combine texture * previous
			}
		}
	}
}

I was looking at this shader http://www.unifycommunity.com/wiki/index.php?title=Blend_2_Textures_by_Lightmap_Alpha to see how it blends the texures and figured I could just figure out how to replace the “splatmap” blending with vertex color, But I’m just completely confused and lost when it comes to this kind of stuff.

The game is being made with iphone specs in mind, but will be released for the web first.

Is your lightmap in the vertex colors, an RGB texture, or the alpha channel of one of the two RGB textures? Are you using the standard…

two textures are tiling using the 1st UV set.
lightmap is using the 2nd uv set

?

Tutorial 5 in the ShaderLab section deals with a lightmapped-type shader, and though it’s not not exactly what you need, I believe you’ll be able to gain all the knowledge you need from watching it, and the videos that lead up to it. Let me know if I confuse you, but I’d say give it a try either way and I’ll give you pointers if you can’t figure it out exactly.

Light map is a rbg, for the textures I’m using, I’m using the same UV’s as the lightmap but I’m repeating the texture ~20 time. But the repetition looks bad/bland, so I want to mix it up by introducing some texture blending based of vertex colors.

In that case, you don’t need two UV sets. You can just change the texture tiling in the material settings. You will still need to bind the vertex colors, and again, I show how to do everything you need in the videos. I’ll give you a hint, though. If you don’t want to use a BindChannels block, you can use this line and save yourself some lines. Whether it’s worth having the block for clarity is up to you.

ColorMaterial Diffuse

if I use lerp could I blend 4 images using each of the color channels for each image, plus the original? could that work on the iphone?

Only with Unity iPhone 3 (unsure if there are restrictions on the shaders on the non-pro, you might want to clarify that with UT) and targeting ARMV7 OpenGL ES 2.0 only then potentially.

given you wrote the corresponding GLSL shader causing using the surface shader system would be a pretty bad idea performance wise

so the best idea performance wise is just blending 2 images?

Okay, I’m use to node based shaders so I figure thinking about in that way may help me figure this out quicker.

Under properties I’ll need it to have 2 images + the light map so it’ll need

Properties
{
_MainTex (“Base (RGB)”, 2D) = “white” {}
_BlendTex (“Blend (RGB)”, 2D) = “white” {}
_LightMap (" (RGB)", 2D) = “white” { LightmapMode }
}

Right?

Then under subshader it needs to load the vertex colors and mix it with the blend texture and then add it ontop of the main before the light map is figured in?

What is this? You said you wanted to blend based on vertex colors, not an image.

Also, in my experience, LightmapMode is useless and undocumented. No idea if it can actually do anything.

How do I set up the 2nd image? The shader I’ve been using has the lightmap stuff in it and works well.

I think I figured out how to get everything set up and loaded, now I just need to figure out set up the actual blending.

Shader "1UV lightmap with vertex blending" {

Properties
{
   _MainTex ("Texture 1  ()", 2D) = ""
   _Texture2 ("Texture 2 ()", 2D) = ""
   _LightMap ("Lightmap (RGB)", 2D) = "" { LightmapMode } 
}

SubShader
{
   BindChannels
   {
      Bind "Vertex", vertex
      Bind "Texcoord", texcoord
   }
   
   Pass
   {
      Tags {"LightMode" = "Always"} 
	  
      SetTexture [_MainTex]
      {
         combine texture
      }
      
      SetTexture [_Texture2]
      {
         combine texture
      }
	  SetTexture [_LightMap] { 
	  combine texture * previous 
	  }
	  
   }
}

 
}

I’m stupid, I just realized my vertex colors aren’t exporting, duh.

Shader "1UV lightmap with vertex blending" {

Properties
{
   _Color ("", Color)= (1,1,1)
   _MainTex ("Texture 1  ()", 2D) = ""
   _Texture2 ("Texture 2 ()", 2D) = ""
   _LightMap ("Lightmap (RGB)", 2D) = "" { LightmapMode } 

}

SubShader
{
   BindChannels
   {
      Bind "Vertex", vertex
      Bind "color", color
	  Bind "Texcoord", texcoord 
   }
   
   Pass
   {   

      Tags {"LightMode" = "Always"}
    
      SetTexture [_MainTex]
      {
         combine texture lerp(texture) primary 
      }
     
      SetTexture [_Texture2]
      {
         combine texture * previous
      }
     SetTexture [_LightMap] {
     combine texture * previous
     } 
    
	  
	  
   }
}

 
}

Seems like I have everything set up but the mixer, how do I go about doing that? my vertex colors are just black/gray/white.

I think I was stupid and asked for help how to blend when I more just want to mask textures.

Okay I tried to simplify it to see what I was doing wrong.

Shader "Blend 2" {

Properties
{
   _MainTex ("Texture 1  (vertex A  = white)", 2D) = ""
   _Texture2 ("Texture 2  (vertex A = black)", 2D) = ""
}

SubShader
{
   BindChannels
   {
      Bind "vertex", vertex
      Bind "color", color
      Bind "texcoord", texcoord
   }
   
   Pass
   {
      SetTexture [_MainTex]
      SetTexture [_Texture2] {combine previous lerp(primary) texture}
   }
}

 
}

I can’t even get that to work right, if I delete everything under pass, the vertex colors show up right, but when I paste it back and put in textures it just shows up as the first image.

Awesome work on that last shader. I couldn’t have done it that cleanly myself as of last year. The only thing I could see going wrong with it is that you don’t actually have vertex coloring in the alpha channel. By default, they’re white. Try this line by itself in the pass; I expect you’ll see pure white instead of a greyscale mesh.

SetTexture[_] {Combine primary alpha}

Yeah I see pure white when I put that in.

Finally figured it out.

Now I have a new issue :smile:

Sometimes the vertex alpha imports kinda crazy.

It won’t do that unless you tell it to. Turn off Unity’s calculation of normals; I find it useless, personally.

Not only are your results wrong there, but you’re also adding a ton of vertices to the mesh with such a low smoothing angle. It’s got to be at zero, no?? Vertex lighting ought to look pretty awful on those meshes, too, I’d imagine.