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
0
Question by Selzier · Dec 30, 2020 at 04:29 AM · shadertextureshader programmingmaskshader writing

Shader: Mask Without a Texture?

I have a shader working that has a sampler2D for a mask. Black is hidden and white areas are visible:

 void surf(Input IN, inout SurfaceOutput o) {
             float2 newuv = TRANSFORM_TEX(IN.uv_ArmUpper, _MainTex);
             
             fixed4 MainColor = tex2D(_MainTex, newuv);
             fixed4 ArmUpperColor = tex2D(_ArmUpper, IN.uv_ArmUpper);
             fixed4 MaskColor = tex2D(_MaskTex, IN.uv_MaskTex);
 
             o.Albedo = ArmUpperColor * MaskColor.rgb + MainColor.rgb * (1 - MaskColor.rgb);
 }


The mask is perfect square. It begins at 0,0 and ends at 256,256 (Full image is 512x512):

alt text
How can I avoid using this image and send in the data 0,0 - 256,256 as a "Mask" parameter ?

simplemask.jpg (13.4 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

2 Replies

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

Answer by Namey5 · Dec 30, 2020 at 06:46 AM

A simple mask like this can just be done by checking if the UV coordinate is within the desired range;

 //This will be 1 if the UV is within the range of [min,max] and 0 otherwise
 float getMask (float2 uv, float2 min, float2 max)
 {
     return uv.x >= min.x && uv.x <= max.x && uv.y >= min.y && uv.y <= max.y;
 
     //Alternatively, a faster way of doing the above
     return all (abs (uv - min) <= max);
 }
 
 ...
 
 half4 MainColor = tex2D (_MainTex, newuv);
 half4 ArmUpperColor = tex2D (_ArmUpper, IN.uv_ArmUpper);
 
 //UV is in a range of [0,1], so get a mask of the first quarter [0.0,0.5];
 half mask = getMask (IN.uv_ArmUpper, 0.0, 0.5);
 
 //Note that this may be flipped vertically, so alternatively use this;
 half mask = getMask (IN.uv_ArmUpper, float2 (0.0, 0.5), float2 (0.5, 1.0));
 
 //lerp() will do the same thing as your mask in the code sample
 o.Albedo = lerp (MainColor, ArmUpperColor, mask);
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 sacredgeometry · Dec 30, 2020 at 06:55 AM 0
Share

Yep, this is the same answer but a lot more full featured I would accept this one.

avatar image
1

Answer by sacredgeometry · Dec 30, 2020 at 06:26 AM

UVs are normalised arent they? I think between 0 and 1 but you would have to double check that.

If they are then your logic is just


 if(coord.x <= 0.5 && coord.y <= 0.5)
 {
    // In white square
 }
 else
 {
     // In black part
 }
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

196 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 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: Modify XY Position of Texture 0 Answers

Shader Tiling Texture Overlapping between X and Z Axes 0 Answers

Trying to blend 3 textures in shader 1 Answer

cartoon lit shader with transparency? 0 Answers

Rewrite simple alphamask fixed function shader to surface or cg shader 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