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 James2Games · Jan 05, 2017 at 08:36 AM · graphicsprogrammingruntimemaskingblood

Create mask overlay from images at runtime

alt text

I've created a blood effect system that creates blood splatters on the game. However I want to mask the splatters to certain places. (Walls, floors etc... ) so they can only been seen on these surfaces.

All the images for the blood are stored under the same parent which currently contains a mask and a handmade alpha image of the map that it needs to mask.

I'm looking to create an overlay of the surfaces when the game starts by creating a Texture2D and iterating over the Images that will mask the blood. However this isn't working well as There's a lot of transformations that's not working well.

Here is the closest I have gotten. But this doesn't display anything in the alpha at this time.

 public class BloodMaskMaker : MonoBehaviour
 {

     public List<Image> ImagesToMask;
     
     public Image finalMask;
     
     void Start ()
     {
         int width = Screen.width;
         int height = Screen.height;
         Texture2D mask = new Texture2D(width, height, TextureFormat.ARGB32, false);
 
         for (int i = 0; i < width; i++)
         {
             for (int j = 0; j < height; j++)
             {
                 mask.SetPixel(i, j, GetColor(i, j, width, height));
             }
         }
         mask.Apply();
         var sprite = Sprite.Create(mask, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
         finalMask.sprite = sprite;
     }
 
     public Color GetColor(int xPixel, int yPixel, int screenWidth, int screenHeight)
     {
         Vector2 screenPoint = new Vector2(xPixel, yPixel);
         foreach (Image image in ImagesToMask)
         {
             Rect imageTransform = RectTransformToScreenSpace(image.rectTransform);
             int x = xPixel + (int)imageTransform.xMin;
             int y = yPixel + (int)imageTransform.yMin;
             int width  = (int)imageTransform.width;
             int height = (int)imageTransform.height;
 
             if (x < 0 || y < 0 || x >= width || y >= height)
                 continue;
 
             var tex = image.mainTexture as Texture2D;
             int colorX = Mathf.CeilToInt(x / width);
             int colorY = Mathf.CeilToInt(y / height);
             return new Color(1,1,1, tex.GetPixel(colorX, colorY).a);
         }
 
         return Color.clear;
     }
 
     public static Rect RectTransformToScreenSpace(RectTransform t)
     {
         Vector2 size = Vector2.Scale(t.rect.size, t.lossyScale);
         return new Rect((Vector2)t.position - (size * 0.5f), size);
     }
 }


 Hierarchy example is below
 Canvas
     Panel
         BackgroundImages
             Floor (Image Component)
             Wall (Image Component)
         BloodImages (Image & Mask Components)
             Blood (Clone) (Image Component)
             Blood (Clone) (Image Component)
             More and more (Image Component)


Can someone assist or provide an alternative to how I can mask the blood?

Cheers

screenshot.png (215.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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by lamphung719 · Jan 05, 2017 at 09:16 AM

What mask did you use? Mask or Mask 2d? As I know, you just need a Mask on an image( like your wall, floor), and your blood image is a child of that object, everything will be taken care of by unity.

Comment
Add comment · Show 4 · 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 James2Games · Jan 05, 2017 at 09:25 AM 0
Share

I'm using a $$anonymous$$ask Component.

I've added a Hierarchy example to the post to show how the components and objects are set up.

I added the blood splatters to a single parent to reduce the amount of images i'll need if a blood splatter needs to overlay more than 1 object.

avatar image lamphung719 James2Games · Jan 05, 2017 at 09:38 AM 0
Share

If you want to show your image only on your Wall, you need a $$anonymous$$ask on that Wall, right? Why is there a mask on Blood image?

avatar image James2Games lamphung719 · Jan 05, 2017 at 12:59 PM 0
Share

Ah perhaps I should have been a bit more clear on what I was asking.

I attempting to have 1 mask which is applied for all blood splatters in the scene; but the mask needs to be computed at runtime.

Show more comments
avatar image
0

Answer by James2Games · Jan 06, 2017 at 11:46 AM

alt text Showing Mask Graphics on the left, hiding the mask on the right.

Managed to figure it out once I learned about the RectTransformUtility from this post This code even works with rotation.

This code manages to build the mask in around 2ms with 8 different images on my PC. I'm sure there is a way to optimize this further by only iterating through the images instead of all pixels then all images. But at the moment 2ms is fast enough haha.

 public class BloodMaskMaker : MonoBehaviour
 {
     public List<Image> ImagesToMask;
     public Image finalMask;
 
     IEnumerator Start ()
     {
         int width = Screen.width;
         int height = Screen.height;
 
         Texture2D mask = new Texture2D(width, height, TextureFormat.ARGB32, false);
         for (int i = 0; i < width; i++)
         {
             for (int j = 0; j < height; j++)
             {
                 mask.SetPixel(i, j, GetColor(i, j, width, height));
             }
         }
         mask.Apply();
 
         var sprite = Sprite.Create(mask, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
         finalMask.sprite = sprite;
   
         yield break;
     }
 
     public Color GetColor(int xPixel, int yPixel, int screenWidth, int screenHeight)
     {
         float alpha = 0;
 
         Vector2 screenPoint = new Vector2(xPixel, yPixel);
         foreach (Image image in ImagesToMask)
         {
             if (RectTransformUtility.RectangleContainsScreenPoint(image.rectTransform, screenPoint,Camera.main))
             {
                 Vector2 localPoint;
                 RectTransformUtility.ScreenPointToLocalPointInRectangle(image.rectTransform, screenPoint, Camera.main, out localPoint);
 
                 float localX = Mathf.Abs(localPoint.x + image.rectTransform.rect.xMax);
                 float localY = Mathf.Abs(localPoint.y + image.rectTransform.rect.yMax);
 
                 var tex = image.mainTexture as Texture2D;
                 float colorX = (localX / image.rectTransform.rect.width) * tex.width;
                 float colorY = (localY / image.rectTransform.rect.height) * tex.height;
                 float colorAlpha = tex.GetPixel((int)colorX, (int)colorY).a;
                 alpha += colorAlpha;
             }
         }
         
         return new Color(1,1,1, Mathf.Clamp01(alpha));
     }
 }



screenshot.png (147.9 kB)
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How do I wrap blood decals around meshes that are not just a flat plane? (e.g. blood spatter on a round lamp) 0 Answers

Mask and Vignette artifacts on Android 0 Answers

Secondary maps problem 0 Answers

How can I hide terrain above a certain elevation? 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