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
0
Question by sethuraj · Sep 28, 2015 at 06:14 AM · shaderspriteuv

How to do a sprite shader with masking for individual sprites ?

Hi everyone,

Can someone help me how to modify the uv in this sprite shader to get the expected result.Here is my simple unlit sprite shader code.

 CGPROGRAM            
 #pragma vertex vert
 #pragma fragment frag
 #pragma multi_compile DUMMY PIXELSNAP_ON                       
                        
 //Color sampler
 fixed4 _Color;
 //Texture samplers
 sampler2D _MainTex;
 sampler2D _MaskTex;
                          
 struct appdata_t
 {
     float4 vertex   : POSITION;
     float4 color    : COLOR;
     float2 texcoord : TEXCOORD0;
 };
  
 struct v2f
 {
     float4 vertex   : SV_POSITION;
     fixed4 color    : COLOR;
     half2 texcoord  : TEXCOORD0;
 };               
 
                          
 //The vertex shader
 v2f vert(appdata_t IN)
 {
     v2f OUT;
     OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
     OUT.texcoord = IN.texcoord;
     OUT.color = IN.color * _Color;
     #ifdef PIXELSNAP_ON
     OUT.vertex = UnityPixelSnap (OUT.vertex);
     #endif 
     return OUT;
 }
 
 //The fragment shader
 fixed4 frag(v2f IN) : COLOR
 {
     //Get the main texture color
     float4 FinalColor = tex2D (_MainTex,IN.texcoord);                            
     //Get alpha from the alpha texture
     FinalColor.a = tex2D(_MaskTex,IN.texcoord).a; 
     //Return the final color
     return FinalColor;
 }
                         
 ENDCG

Lets say i have a 512x512 size with sprites in grid as 128x128 which gives 16 sprites in total.For all the 16 sprites,I'm applying one material with this shader and a circular mask texture.Now the mask should apply for each sprites. The below image will explain what i need in detail.

alt text

Thanks for any help in advance .... :-)

illustration.jpg (148.8 kB)
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
0

Answer by komodor · Sep 29, 2015 at 07:16 AM

not sure right now and i don't have time to test it right now, i remember you need to mutliply the rgb with alpha

 FinalColor.rgb *= FinalColor.a;
 return FinalColor;

i'll get to that later if i am wrong

Comment
Add comment · Show 1 · 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 sethuraj · Sep 29, 2015 at 11:55 AM 0
Share

@komodor Hi,thanks for the help.But that wont give me the expected output.See the alpha blending is working fine.Its the uv which have to be altered.I actually figured it out.$$anonymous$$y quad mesh requires 2 uv channels.First uv for the maincolor will have value based on atlas but the second uv value will be full 0,0 to 1,1 to map the entire mask texture and the alpha have to be chose from that

  //Get the main texture color
      float4 FinalColor = tex2D (_$$anonymous$$ainTex,IN.texcoord);                            
      //Get alpha from the alpha texture to second uv channel
      FinalColor.a = tex2D(_$$anonymous$$askTex,IN.uv2).a; 
      //Return the final color
      return FinalColor;

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

34 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

Related Questions

Sprites consisting of two packed textures 0 Answers

Accessing a second texture's offset in a modified sprite shader 0 Answers

Efficient way to keep a fixed rotation for sprite? 0 Answers

Sprite shader relative UV's 0 Answers

Sprite not visible from behind 0 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