Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by ivarhill · May 13, 2017 at 09:47 PM · shadertexturematerialalphaoffset

Shader with two textures separated by an alpha channel?

I have a bunch of wall assets which look like this (quick screenshot, the full texture tiles):

alt text

I am trying to make the red parts animate and move horizontally, whereas the gray foreground parts stay static. Currently this is my solution:

  1. Use two planes with separate textures, one 0.001 units behind the other.

  2. For the foreground, use a cutout alpha shader to mask out the parts that should be red/animated.

  3. For the background, use a separate texture with a script that changes the X offset and time it so that it tiles.

While this works, it's clumsy to use two planes for each wall section, it makes it harder to snap to a grid since it often snaps to the element with an offset, and it uses more draw calls through separate materials.

Is there a way to make a shader which has these properties?

  1. Use a diffuse map which is masked by its alpha channel.

  2. Use a second diffuse map which uses no alpha and always renders below the first one.

  3. Have the second diffuse map be targetable with a script so its offset can be animated separately.

This way I could do it all in a single plane with no (or minimal) extra draw calls and it'd all work much more elegantly. I'm not very knowledgeable on shaders and any material on this kind of approach I found online was either incomplete or obsolete at this point. This is a mobile-targeted game so optimally there'd only be a diffuse map and it'd be lit with a lightmap.

texture.jpg (82.4 kB)
Comment
Add comment · Show 1
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 Owen-Reynolds · May 14, 2017 at 04:37 PM 0
Share

Yes. With the standard shader, you can use the Secondary$$anonymous$$aps area. Sliding offset2 will animate the red parts of yours. In a custom shader, generally you make/animate both textures and end with: finCol=lerp(col2,col1,col1.a);

1 Reply

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

Answer by toddisarockstar · May 14, 2017 at 10:59 PM

is this what you need ?

 Shader "2 layers transparent" {
     Properties {
        // _Color ("Overall Color", Color) = (1,0.5,0.5,1)
         _t1 ("texture in white", 2D) = "white" {}
         _tint1 ("Tint1", Color) = (1.0, 0.6, 0.6, 1.0)
         
         _t2 ("texture in red", 2D) = "white" {}
         _tint2 ("Tint2", Color) = (1.0, 0.6, 0.6, 1.0)
         
         _t3 ("use for alpha channel", 2D) = "white" {}
 
         
         _rc ("low cutoff", Range(1,80)) = 1.0 
     
         
     }
     SubShader {
         //Tags { "RenderType"="Opaque" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Lambert
         float _rc;
 
         
         sampler2D _t1;
         sampler2D _t2;
         sampler2D _t3;
 
         
         fixed4 _tint1;
         fixed4 _tint2;
 
 
         
         struct Input {
             
             float2 uv_t1;
             float2 uv_t2;
             float2 uv_t3;
 
             
         };
       
 
         void surf (Input IN, inout SurfaceOutput o) {
             
             float f;
             
             half4 pix = tex2D (_t1, IN.uv_t1)*_tint1;
             half4 pix2 = tex2D (_t2, IN.uv_t2)*_tint2;
             f=tex2D (_t3, IN.uv_t3).a;
             
             f=f*_rc;
             if(f>1){f=1;}
             f=1-f;
             pix=pix*f;
             f=1-f;
             pix=pix+pix2*f;
             
             
             
             o.Albedo = pix.rgb;
             
         }
         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 ivarhill · May 14, 2017 at 11:33 PM 0
Share

Thanks! This seems to work well in displaying the texture properly, though I'm having some problems making the animation - I've named the texture to be animated "Background" in the shader, and got a script with this code:

 void Update () {
 
         meshRenderer.material.SetTextureOffset("Background", meshRenderer.material.GetTextureOffset("Background") + new Vector2((0.01f * Time.deltaTime), 0f));
         if (meshRenderer.material.GetTextureOffset("Background").x >= offsetAmount)
             meshRenderer.material.SetTextureOffset("Background", defaultPos);
 
     }


When running the game I got a "$$anonymous$$aterial doesn't have a texture property 'Background'" error - any thoughts on this?

avatar image toddisarockstar ivarhill · May 14, 2017 at 11:50 PM 0
Share
 //animate like this
 
 float newoff;
 newoff = newoff + Time.deltaTime;
 if(newoff>1){newoff=0;}
 
 gameObject.renderer.material.SetTextureOffset("_t2",new Vector2(newoff,0f));
avatar image toddisarockstar ivarhill · May 15, 2017 at 12:04 AM 0
Share

when you are looking up a texture in a shader, you have to reference the variable in the shader script that is holding the texture......not the name of the actual asset. if you look near the top of the script, i named the variable holding the second texture as "_t2". It doesn't matter the name of the texture you drag into it.

avatar image toddisarockstar ivarhill · May 15, 2017 at 12:29 AM 0
Share

also i was just re reading your question. i wrote this so that it would make it look like blood running through tubes. did you want to have the position of the red tubes to move? i could give you another shorter script for that if that's all you what wanted to do. otherwise you would move the offset of both "_t2" and "_t3".

actually with this script you could both move the position of the tubes with "_t3" and if you moved "_t2" a bit faster you would get an effect of stuff moving through the tubes at the same time!!!

avatar image ivarhill toddisarockstar · May 15, 2017 at 01:03 AM 0
Share

Replacing "Background" with the correct variable worked well, thanks!

Indeed all I need to do is animate the red part, and keep the alpha and foreground still- your shader seems to do the job fine so far, thank you for the assistance :)

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

115 People are following this question.

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

Related Questions

Shader: separate alpha texture, independent offset 1 Answer

Boat wake/foam 0 Answers

Soft edge shader - Issues when objects using same material overlap 0 Answers

(c#) Transparency Problem - Toggle not Gradient 1 Answer

Changing Eye Colour (Colour only non-white parts of a texture?) 2 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