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 DylanSemititsky · Mar 14, 2017 at 01:58 PM · shadermaskmultiple textures

How to make a Shader with multiple textures and an alpha mask?

I'm trying to make a shader that allows me to blend multiple textures and then mask them all at once. So far I have figured out how to blend 3 textures OR mask 1 texture, but I haven't been able to mask 3 blended textures. I'm very new to working with shaders. Thanks for the help!

Here is my code:

 Shader "Custom/testShader" {
     Properties {
         _Color ("Color Tint (A = Opacity)", Color) = (1,1,1,1)
         _Blend1 ("Blend", Range (0, 1)) = 0.0
         _Blend2 ("Blend", Range (0, 1)) = 0.0
         _MainTex1 ("Base (RGB)", 2D) = "red"
         _MainTex2 ("Base (RGB)", 2D) = "blue"
         _MainTex3 ("Base (RGB)", 2D) = "green"
         _Mask ("Culling Mask", 2D) = "white" {}
         _Cutoff ("Alpha cutoff", Range (0,1)) = 0.1
     }
 
     SubShader {
         Tags {"Queue"="Transparent"}
         Lighting On
         ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
         AlphaTest GEqual [_Cutoff]
         Pass {
 
             Material {
                 Diffuse [_Color]
                 Ambient [_Color]
             }
                  
             SetTexture [_Mask] {combine texture}
 
             SetTexture [_MainTex1]{combine texture, previous}
                      
             SetTexture [_MainTex2] {
                 constantColor (0, 0, 0, [_Blend1])
                 combine texture lerp (constant) previous 
             }
              
             SetTexture [_MainTex3] {
                 constantColor (0, 0, 0, [_Blend2])
                 combine texture lerp (constant) previous 
             }
         }
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by toddisarockstar · Mar 14, 2017 at 11:04 PM

I ran into your same problem just a couple weeks ago. I was actually surprised unity didnt have anything like this as one of there default shaders.

Anyways, I just wrote my own! it works great. You can use it if you would like.

When you make your map texture, just paint with black red green and blue colors to assign where the four actual textures go. the alpha channel of the map texture will mask all textures.

be sure to set the amount of textures you are using in the inspector

You can use it if you would like.

The "map" texture doesn't need to be assigned as an actual map in the inspector. it just works

 Shader "todds/Transparent 4Textures" {
     Properties {
        // _Color ("Overall Color", Color) = (1,0.5,0.5,1)
        
         _ac ("alpha cutoff for textures", Range(.0,1)) = 0
         _acm ("alpha cutoff for map", Range(.0,1)) = 0
         _t1 ("texture in black", 2D) = "white" {}
         _tint1 ("Tint1", Color) = (1.0, 1, 1, 0)
         
         _t2 ("texture in red", 2D) = "white" {}
         _tint2 ("Tint2", Color) = (1.0, 1, 1, 0)
         _rc ("amplify red", Range(1,80)) = 0.0 
         
         _t3 ("texture in blue", 2D) = "white" {}
         _tint3 ("Tint3", Color) = (1.0,1,1,0)
         _rb ("amplify blue", Range(1,80)) = 0.0 
         
         _t5 ("texture in green", 2D) = "white" {}
         _tint5 ("Tint4", Color) = (1.0,1,1,0)
         _rg ("amplify green", Range(1,80)) = 0.0 
         
         _t4 ("texture map", 2D) = "white" {}
         
         num ("Number of textures used", Int) = 5 
         
     }
     SubShader {
         Tags { "Queue"="Transparent" "RenderType"="Transparent" }
         LOD 200
         
         CGPROGRAM
              #pragma surface surf Lambert alpha
         int num;
         float _rc;
         float _rb;float _rg;
         float _ac;
         float _acm;
         sampler2D _t1;
         sampler2D _t2;
         sampler2D _t3;
         sampler2D _t4;
          sampler2D _t5;
         fixed4 _tint1;
         fixed4 _tint2;
         fixed4 _tint3;
          fixed4 _tint5;
         struct Input {
             
             float2 uv_t1;
             float2 uv_t2;
             float2 uv_t3;
             float2 uv_t4;
             float2 uv_t5;
         };
       
 
         void surf (Input IN, inout SurfaceOutput o) {
             
             float f;float f2;
           
             half4 pix2;
             half4 pix = tex2D (_t1, IN.uv_t1)*_tint1;
                   pix.a=tex2D (_t1, IN.uv_t1).a;
             half4 map = tex2D (_t4, IN.uv_t4);
             float alph;
             
                 if(num>1){
                 alph=tex2D (_t2, IN.uv_t2).a;
 
                 if(alph>_ac){
                 
                 f=map.r;
                 if(f>0){
                 f=f*_rc;
                
                 if(f>1){f=1;}
             
                 f*=alph;
                 f2=1-f;
                 pix=pix*f2;
                
                 pix=pix+tex2D (_t2, IN.uv_t2)*f*_tint2;
                 pix.a+=tex2D (_t2, IN.uv_t2).a*f;
                 }}
                 
                 
                  if(num>2){
                 alph=tex2D (_t3, IN.uv_t3).a;
 
                 if(alph>_ac){
                 f=map.b;
                 if(f>0){
                 f=f*_rb;
                 if(f>1){f=1;}
                 f*=alph;
                 f2=1-f;
                 pix=pix*f2;
                
                 pix=pix+tex2D (_t3, IN.uv_t3)*f*_tint3;}
                 pix.a+=tex2D (_t3, IN.uv_t3).a*f;
                 }
                 if(num>3){
                 alph=tex2D (_t5, IN.uv_t5).a;
 
                 if(alph>_ac){
                 f=map.g;
                 if(f>0){
                 f=f*_rg;
                 if(f>1){f=1;}
                 f*=alph;
                 f2=1-f;
                 pix=pix*f2;
                
                 pix=pix+tex2D (_t5, IN.uv_t5)*f*_tint5;}
                 pix.a+=tex2D (_t5, IN.uv_t5).a*f;
                 }
                
                 
                   }}
                 
                 }
             
             o.Albedo = pix.rgb;
             f=pix.a;
             if(map.a<_acm){map.a=0;}
             if(map.a<pix.a){f=map.a;}
             
             o.Alpha = f;
             
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
 
Comment
Add comment · Show 5 · 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 DylanSemititsky · Mar 16, 2017 at 04:12 AM 0
Share

Hey, thanks!

I will try it out and get back to you.

avatar image DylanSemititsky · Mar 16, 2017 at 05:09 AM 0
Share

Okay, I've played with it a bit and it seems pretty cool. I may not have figured it out completely but I'm not sure if it can do what I was looking for. $$anonymous$$aybe you can still help me.

Is there a way to multiply the 3 textures together so that they blend on top of each other? And with the map, can I use it to completely hide and show the same parts of all 3 textures (Like an alpha channel)? In my code above I use a black and white image for the "_$$anonymous$$ask" texture, and it hides parts of another texture. I just can't figure out how to get it to mask more than one other texture.

Thanks again!

avatar image toddisarockstar DylanSemititsky · Mar 18, 2017 at 03:31 PM 0
Share

I have updated my answer for you. I have adjusted my shader to fully support transparency in all the textures. Also the map texture's alpha channel works as a mask for all the textures!!!

your base color is now black ins$$anonymous$$d of white (black is absence of all colors).

and i added a fourth texture represented by green.

in the inspector be sure to set the number of textures you are useing.

and now this shader should have no problem mixing any or all of the textures if you play with the color on the map!!!

this should definately do what you need now!!!!

avatar image olgimpy · Nov 01, 2017 at 10:12 AM 0
Share

Is it possible to pull the blend color information from vertex colors ins$$anonymous$$d of a map texture?

avatar image Malt27 · Apr 08, 2018 at 05:54 PM 0
Share

Really thanks! It works, but I used it otherwise. I just SetTexture some 2D pictures to each texture, and it works fine

avatar image
0

Answer by Marks98 · Jul 11, 2018 at 09:05 PM

Hi.
I have a problem.
I don't understant how i can use this shader. I need transparent shader to my terrain but when i fill out all the textures only one is showing. What i do wrong?
Can somebody help me please?
Thank you ,Marks

Comment
Add comment · 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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Implement texture masking into simple existing shader 1 Answer

How to apply refraction at marked texture pixels while the rest is being rendered normal? 1 Answer

Shader: Cutout behavior mixed with masking behavior 0 Answers

Through bullet holes on the objects 0 Answers

Stencil inside Stencil? (Multiple masks on same object) 1 Answer


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