Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Pollyfun · Dec 06, 2013 at 09:04 AM · 2dshadertexturealphamask

Rewrite simple alphamask fixed function shader to surface or cg shader

Until now I've used this shader to dynamically make transparent holes in a texture.

 Shader "AlphaMask" {
   Properties {
       _MainTex ("Base (RGB)", 2D) = "white" {}
       _AlphaTex ("Alpha (A)", 2D) = "white" {}
   }
   SubShader {
     Tags { "RenderType" = "Transparent" "Queue" = "Transparent"}
     ZWrite Off
     Blend SrcAlpha OneMinusSrcAlpha
     ColorMask RGB
  
     Pass {
        SetTexture[_MainTex]  { Combine texture }
        SetTexture[_AlphaTex] { Combine previous, texture }
     }
   }
 }

_MainTex is a high-resolution texture that's read-only, while _alphaTex is a low-res texture that's frequently updated with SetPixels()/Apply().

After updating from my custom 2D-system where I used texture2D to the new Sprite-class, the SpriteRenderer won't accept this shader since it's fixed function.

I've spent time searching for a solution to this, but no luck so far. Surely there's some simple way to update this code for surface or cg?

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
2

Answer by OP_toss · Dec 07, 2013 at 12:06 AM

So I don't normally code things for people, on principle, but this seemed like the easiest way to explain it. And it's mostly copy pasted from unity docs. I just tweaked it to serve your purpose.

(I also didn't test it so I'll leave the bugs to you :) )

  • I made it a transparent shader by placing it in the right queue and setting the correct blend mode.

  • I added 2 textures as input.

  • I use the same uvs for both.

  • I read in main tex.

  • Then I read in alphatex and set the alpha based on my alphatex's alpha.

    Shader "Tutorial/Textured Colored" { Properties { _Color ("Main Color", Color) = (1,1,1,0.5) _MainTex ("Texture", 2D) = "white" { } _AlphaTex ("Texture", 2D) = "white" { } } SubShader {

       Tags { "RenderType"="Transparent" "Queue"="Transparent" }
         LOD 200
         Blend SrcAlpha OneMinusSrcAlpha
         ZTest Less
     
         Pass {
     
     CGPROGRAM
     #pragma vertex vert
     #pragma fragment frag
     
     #include "UnityCG.cginc"
     
     float4 _Color;
     sampler2D _MainTex;
     sampler2D _AlphaTex;
     
     struct v2f {
         float4  pos : SV_POSITION;
         float2  uv : TEXCOORD0;
     };
     
     float4 _MainTex_ST;
     
     v2f vert (appdata_base v)
     {
         v2f o;
         o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
         o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
         return o;
     }
     
     half4 frag (v2f i) : COLOR
     {
         half4 texcol = tex2D (_MainTex, i.uv);
         texcol.a = tex2D (_AlphaTex, i.uv).a;
         return texcol * _Color;
     }
     ENDCG
     
         }
     }
     Fallback "VertexLit"
     } 
    
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 Pollyfun · Dec 07, 2013 at 01:09 AM 0
Share

I tried it out, and with that code the transparency works, but the main texture is rendered with only one color. I'm sure it would work with some $$anonymous$$or modification (I'm a neophyte with shaders though). $$anonymous$$eanwhile, I found another way to make it work http://answers.unity3d.com/questions/272749/how-to-write-unlit-surface-shader.html. Thanks for taking the time to answer, it got the Tags and some other details sorted out.

avatar image OP_toss · Dec 07, 2013 at 01:20 AM 0
Share

So I actually ended up testing it and it definitely works for me... Did you leave the Color var set to white with full alpha??

avatar image Pollyfun · Dec 08, 2013 at 03:32 PM 0
Share

I used the code as is. I assigned it to a new Foreground$$anonymous$$aterial in the editor. In the inspector the $$anonymous$$ain Color is (255,255,255,255), and textures were added. Still, where the main texture should be rendered, just the selected main color is rendered. If I comment this line out "texcol.a = tex2D (_AlphaTex, i.uv).a;" the main texture is rendered (but transparency of course disappears). Any idea what might be wrong?

avatar image OP_toss · Dec 09, 2013 at 06:10 PM 0
Share

Well yeah a number of possibilities:

  1. Does your maintex texture have an alpha? If not, then it won't be transparent without using the alphatex.

  2. Does your alphatex have an alpha? If not, then use a different channel.

  3. Why did you comment out that line?

  4. What exactly is going wrong?

Shoot me answers to those questions and I should be able to sort you out, if you haven't figured it out already.

avatar image OP_toss · Dec 11, 2013 at 10:04 PM 0
Share

Can you close this question by either answering it yourself or accepting my answer please? Hope I helped, thanks!

avatar image
0

Answer by transporter_gate_studios · Mar 11, 2018 at 08:35 PM

This works for me except for one strange issue. The edges of the sprite are being "repeat stretched". Have no idea what could be doing this. I do have many sprites near each other.

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

18 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

Related Questions

is it possible to make 2d real time coloring with masks (paint app) 1 Answer

Shader to apply Alpha to other images 0 Answers

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

Cancel out mesh alpha 1 Answer

Unity 2D sprite mask? 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