- Home /
How do I blend or overlay a texture over the main texture?
Basically, what I'm trying to do is apply a texture over a game model's main texture. The texture being applied contains some colors that the player is painting on in order to make the object look like it's being painted. However, some of the things I've tried to do don't seem to work, and I'm not sure what the issue is. Here is what I've tried so far:
Shader "Gameplay/PaintingShader"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_PaintRenderTex ("Paint Render Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf StandardSpecular fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
sampler2D _PaintRenderTex;
struct Input
{
float2 uv_MainTex;
float2 uv_PaintRenderTex;
};
void surf (Input IN, inout SurfaceOutputStandardSpecular o)
{
fixed4 main = tex2D (_MainTex, IN.uv_MainTex);
fixed4 paint = tex2D (_PaintRenderTex, IN.uv_PaintRenderTex);
o.Albedo = main.rgb * paint.rgb;
o.Alpha = main.a;
}
ENDCG
}
FallBack "Diffuse"
}
This causes the textures to "mix" together. So if the main texture has brown and the applied texture is blue, a green color is painted on the game model instead of the blue because the colors are mixing. here is my other attempt:
Shader "Custom/Paint"
{
Properties
{
_MainTex ("Albedo (RGBA)", 2D) = "white" {}
_PaintTex ("Paint Texture (RGBA)", 2D) = "white" {}
_BlendAmount("Blend Amount", Range(0.0, 1.0)) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _PaintTex;
fixed _BlendAmount;
struct Input {
float2 uv_MainTex;
float2 uv_PaintTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 main = tex2D (_MainTex, IN.uv_MainTex);
fixed4 paint = tex2D (_PaintTex, IN.uv_PaintTex);
o.Albedo = lerp(main.rgb, paint, main.a * _BlendAmount);
}
ENDCG
}
FallBack "Diffuse"
}
This has been a problem because even though the applied texture is transparent over the areas with no color, the applied texture will completely take over the main texture with a single color. The blending between textures works, but Unity (or something) doesn't seem to recognize that the applied texture has any transparency. Can anyone suggest what I should be doing to achieve the affect that I am looking for?
Answer by helarts · Nov 04, 2015 at 02:47 PM
Just to illustrate in Photoshop terms what you are doing in the first shader:
And what you are trying to do in the second shader:
The issue is that you are using the alpha from "main" instead of "paint", line 30 replace 'main.a' by 'paint.a':
o.Albedo = lerp(main.rgb, paint.rgb, paint.a * _BlendAmount);
If you want your paint to spread instead of fade in you can try something like that with a gradient in your paint alpha:
o.Albedo = lerp(main.rgb, paint, saturate((main.a - 1 + _PaintAmount) * 100));
Thank you for the explanation and help. I'll try out what you said and see what happens. Real quick though, do you have any suggestions on where I can find more information on methods that can be called in shaders, like that saturate() method? I was trying to look those up before I posted the question to see what other things I could do, but I couldn't find much information.
Sure, HLSL Intrinsic functions: https://msdn.microsoft.com/en-us/library/windows/desktop/bb509645(v=vs.85).aspx
saturate is just a shortcut for clamp(x, 0, 1);
Thanks to both of you!
I was trying to get something like this to work for a mobile game I am working on.
I want to put the texture of my environment in a single 1024x1024 texture, but this is not enough for the detail I want. I decided to tile my diffuse colors using a few 128x128 files, and bake in blender the Ambient Occlusion with transparency in the 1024x1024 file.
All I needed was this shader! So thank you very much!!! Helped me a lot!
Result sample:
Your answer
Follow this Question
Related Questions
Add transparency to this shader? 0 Answers
Transparency artifacts android 0 Answers
Shader Question about Alpha 0 Answers
how to make texture transparent 3 Answers
transparent shader objects disappear on certain camera angles 6 Answers