Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Landrade1093 · Nov 04, 2015 at 11:11 AM · shadertexturetransparency

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by helarts · Nov 04, 2015 at 02:47 PM

Just to illustrate in Photoshop terms what you are doing in the first shader: alt text

And what you are trying to do in the second shader: alt text

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));

screenshot-2015-11-04-15-19-39.jpg (10.2 kB)
screenshot-2015-11-04-15-21-01.jpg (14.2 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Landrade1093 · Nov 04, 2015 at 04:47 PM 0
Share

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.

avatar image helarts Landrade1093 · Nov 04, 2015 at 05:31 PM 0
Share

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);

avatar image laur-ganta · May 11, 2017 at 05:52 PM 0
Share

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: alt text

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges