Multiply with Alpha from texture

Hi there.
Just starting with ShaderLab and I want to make a shader that:

  • you giva a colour
  • you give a texture with a with a transparent background
  • and that multiplies these two with each other and the background.

I’m currently at a loss because I don’t really get how to multiply with the rendered background.
I found a thread on a nice and simple multiply effect without texture (http://forum.unity3d.com/threads/52831-Multiply-Shader-with-tint-colour), but as I said I would like to use a texture to define a shape that is to be rendered with a multiply effect.

Currently I have a simply alpha diffuse shader. Now I want to multiply it on the rendered background.

Who would like to help me out?

Shader "Custom/MultiplyColourFromAlphaTexture" {
	Properties {
		_MainColor ("Color", Color) = (1,1,1)
		_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	}
	
	SubShader {
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
		
        CGPROGRAM
        #pragma surface surf Lambert alpha
		
        sampler2D _MainTex;
        fixed4 _MainColor;

		struct Input {
			float2 uv_MainTex;
			fixed4 _MainColor;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 tex = tex2D (_MainTex, IN.uv_MainTex);
			tex.rgb *= _MainColor;
			o.Albedo = tex.rgb;
			o.Alpha = tex.a;
		}
		ENDCG
	}
}

So I found a way to partially achieve what I was looking for.
Only I’m currently using an AlphaTest to completely remove pixels with a certain alpha level.
However, I would like to use the texture’s alpha level, to gradually determine it’s visibility (instead of cutting it off somewhere).
I have yet to find a way of achieving this without losing something else. Any idea’s on this?

Shader "Custom/MultiplyColourFromAlphaTexture" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1)
		_Texture ("Base (RGB) Trans (A)", 2D) = "white" {}
		_Cutoff ("Alpha Cutoff", Range(0,1)) = 0.1
    }

    SubShader {
    	Tags {
    		"Queue"="Transparent"
			"RenderType"="Transparent"
		}
    
		Blend Zero SrcColor
		
        Pass {
    		ZWrite Off
    		AlphaTest Greater [_Cutoff]
            Material {
                Diffuse [_Color]
            }
            SetTexture [_Texture] {
                constantColor [_Color]
                Combine texture * constant, texture
            }
        }
    }
}
1 Like

I ended up filling my textures with white to avoid black alpha, not sure how to solve the alpha issue on multiply, even if setting blending modes for alpha individually still writes the alpha to the destination