iPhone Shader: Simple Texture with Decal 2 UV

I don’t know exactly what to call this but it has two textures. The first texture is just simple unlit and uses 1st set of UV’s. The one on top is and unlit decal and uses the 2nd set of UV’s.

The idea is to use this with a texture atlas so that the base texture can be decorated with transparent details from another part of the atlas.

If you use this shader on multiple objects, you can update both sets of UV’s on each object and all the objects will still batch. :slight_smile:

Shader "iPhone/Simple Texture with Decal 2 UV" {
	
   Properties {
      _Color ("Main Color", Color) = (1, 1, 1, 1)
      _MainTex ("Base (RGB)", 2D) = "white" {}
      _DecalTex ("Decal (RGBA)", 2D) = "black" {}
   }
   
   SubShader {
   	
   	  Tags {
         "Queue"="Transparent"
         "IgnoreProjector"="True"
         "RenderType"="Transparent"
      }
      
      Pass {
      	
      	 BindChannels {
            Bind "Vertex", vertex
            Bind "normal", normal
            Bind "texcoord", texcoord0 // main uses 1st uv
            Bind "texcoord1", texcoord1  // decal uses 2nd uv
         }
      	
         SetTexture [_MainTex] {
            constantColor [_Color]
            Combine texture * constant
         }
         
         SetTexture [_DecalTex] {
         	combine texture lerp (texture) previous
         }
      }
   }
}

Here is a screen shot showing one mesh but with uv1 shows one frame from the texture while uv2 shows another frame with transparency.

4 different looking objects all in one draw call.

I’ll post the project after I get some sleep. :slight_smile:

199286--7329--$photo_120.jpg

Niiice! Really looking forward to using this.

Awesome!

But it doesn’t seem to wrk on my system - I get a gray-squared texture instead of a decal floating in apha.

Let me dig up this project and post it later today.

Thanks mucho, kenlem!

My intention for making this shader was so I could have one master texture with several skins and several outfits which I would then use to create a cast of unique characters.

I’m actually having trouble just getting a decal shader to work on the iPhone.

I need a shader that will trick its material into rendering on Z above any coincident textures, such as on the surface the decal is applied to. I’ve tried the one on the Wiki and yours but the alpha doesn’t work on either.

Since your project works, I’m sure I’ll be able to figure out what I’m doing wrong by looking at it. Can’t wait to see it!

Here’s that shader form the Wiki that puts the decal above any others textures on Z, BTW …

Shader "BlendedDecal"
{
    Properties
    {
        _Color ("Tint", Color) = (1,1,1,1)
        _MainTex ("Texture", 2D) = "white" {}
    }
    
    SubShader
    {
        Lighting Off
        ZTest LEqual
        ZWrite Off
        Tags {"Queue" = "Transparent"}
        Pass
        {
            Alphatest Greater 0
            Blend SrcAlpha OneMinusSrcAlpha
            Offset -1, -1
            SetTexture [_MainTex]
            {
                ConstantColor[_Color]
                Combine texture * constant
            }
        }
    }
}

[/code]

Don’t forget that you can use vertex colors; that will batch too. This was designed for a different purpose, but hopefully you can see how it might be beneficial; instead of multiplying the UV1 texture by one color, for every object, you can use any number of colors. It’s no big deal because you already needed separate meshed to account for the varying UV2 coordinates anyway. So those seams on your cubes at the edges that come from compression? They can disappear.

So, kenlem could use vertex coloring if the batching he wanted just involved changing the tint. That’s pretty cool.

But, I’m curious, how would the use of vertex colors help him avoid the compression artifacts on his cube meshes?

He hasn’t posted the project yet, but here’s what I’m observing:

There are both red and green cubes.
There is only one total draw call.
The shader multiplies the main texture by a solid color.
You can not have batching if two colors were to be used.
There are compression artifacts on the cube, the likes of which happen when you have two vastly different colors on large portions of a texture atlas.

This leads me to conclude that the first texture is a mix of red and green, and the color he’s multiplying them both by is white.

Vertex colors do not involve compression, so they will not exhibit the kind of artifacts we’re seeing above. As it is, the shader could even be simplified, if only a solid color with a decal is needed.

Shader "Vertex Color + Decal" { 

Properties
{ 
	_MainTex ("Decals (RGBA)", 2D) = ""
} 

SubShader
{
	BindChannels
	{
		Bind "vertex", vertex
		Bind "color", color
		Bind "texcoord", texcoord
	}
	
	Pass
	{
		SetTexture[_MainTex] {Combine texture Lerp(texture) primary}
	}	
} 

}

I just noticed that you put this shader in the Transparent queue. Move it out of there! It’s not blending with anything, and the iPhone will give you better performance if you keep it in the same queue as the rest of the opaque geometry, due to tile-based deferred rendering.