Converting RGB to alpha

Hi Everyone,

I’d like to take the RGB values of _MainTex and put them into the Alpha channel (adding them up and dividing by 3)

The code below is for a Transparency Projector (from http://forum.unity3d.com/threads/35808-Projecting-a-hole) - it uses the alpha to cut a hole in a mesh.

The input texture I’d like to use has no alpha, hence why I need to change things a bit…

Original code:

Shader "Transparency Projector" {	

Properties {
	_MainTex ("Base (RGB)", 2D) = "" {TexGen ObjectLinear}
}

SubShader
{
	Lighting On

	Tags {"Queue" = "Geometry+10"}

	Pass
	{
		ColorMask A   		
		SetTexture[_MainTex] {
		Matrix[_Projector]
		}
	}   
}


}

I’ve tried adding

CGPROGRAM
			#pragma vertex vert_img
			#pragma fragment frag

			#include "UnityCG.cginc"

			uniform sampler2D _MainTex;

			float4 frag(v2f_img i) : COLOR {
				fixed4 tex = tex2D(_MainTex, i.uv);
				float newAlpha = (tex.r+tex.g+tex.g)/3;
				return fixed4(0.0,0.0,0.0,newAlpha);
			}
			ENDCG

			ZWrite Off
			Blend SrcAlpha OneMinusSrcAlpha

… to the pass. This does the trick (converts texture to alpha) but the texture no longer acts as a projector (it doesnt move and just apples the texture all surfaces). Not certain why this is.

Any help would be greatly appreciated. Total novice when it comes to shaders :slight_smile:

Did you include the Matrix[_Projector] part? Sounds important, and probably why its not acting like a projector any more :slight_smile:

Yup I did :smile:

Final code is here…

Shader "Transparency Projector" {	

Properties {
	_MainTex ("Base (RGB)", 2D) = "" {TexGen ObjectLinear}
}

SubShader
{
	Lighting On

	Tags {"Queue" = "Geometry+10"}

	Pass
	{

		CGPROGRAM

            #pragma vertex vert_img

            #pragma fragment frag

 

            #include "UnityCG.cginc"

 

            uniform sampler2D _MainTex;

 

            float4 frag(v2f_img i) : COLOR {

                fixed4 tex = tex2D(_MainTex, i.uv);

                float newAlpha = (tex.r+tex.g+tex.g)/3;

                return fixed4(0.0,0.0,0.0,newAlpha);

            }

            ENDCG

 

            ZWrite Off

            Blend SrcAlpha OneMinusSrcAlpha

            
		ColorMask A   		
		SetTexture[_MainTex] {
		Matrix[_Projector]
		}
	}   
}


}

Which gives me this (projector is projecting onto a plane - green cubes shouldnt have a texture on them…)
1414202--74156--$fail2.png

Sorry to bump this - anyone got any ideas ?

The Reference says: The texture setup configures fixed function multitexturing pipeline, and is ignored if custom fragment shaders are used.(from Unity - Manual: ShaderLab: defining a Pass). So the SetTexture command in the pass is just ignored which make your shader failed.