Alpha from one texture to another

Hi,

What would be the best way to transfer the alpha of one texture to another? Would be setting the pixels the only way or could you apply alpha from a texture to another texture using a shader?

Thanks,

Juha

are you asking how to get a pixel from the texture or what

–

2 Answers

2

It's quite easy to change write a shader that takes the color values of one texture and the alpha value on another one:

Shader "Transparent/Copy Alpha" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {} // Your color texture
        _AlphaTex ("Alpha (A)", 2D) = "white" {} // your alpha texture
    }
    SubShader {
        Tags { "RenderType"="Transparent" }
        Blend SrcAlpha OneMinusSrcAlpha

        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;
        sampler2D _AlphaTex;

        struct Input {
            float2 uv_MainTex;
            float2 uv_AlphaTex; // add this to access the texture tiling and offset values from the inspector
        };

        // This is the only code you need to touch    
        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex); // This is your color texture
            half4 a = tex2D (_AlphaTex, IN.uv_AlphaTex); // This is your alpha texture
            o.Albedo = c.rgb; // Setting your color from the one texture
            o.Alpha = a.a; // Setting your alpha from the other texture
        }
        ENDCG
    } 
    FallBack "Transparent/Diffuse"
}    

Np. it's "sis" btw ;)

–

Is it possible to modify this script in order to use 2 images, one is the original without alpha, and the other is equal but in black and white and where the second is black, apply alpha on the original? EDIT: just figured it out! For someone that may want to know it is just substitute "Tags" line for this: Tags {"Queue"="transparent" "RenderType"="transparent" } and setting alpha like this: o.Alpha = a.rgb;

–

Thanks Vivien! This is exactly what I was looking for.

–

It would be more efficient I think to use a shader, though I don't know off the top if one exists. You can get Strumpy's Shader Editor (free) from the asset store and make that probably.

For me, the shader didn't work in Unity5 (5.3.3). I changed the line: #pragma surface surf Lambert to #pragma surface surf Lambert alpha and it worked again.

–