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 Mercy404 · Mar 14, 2014 at 11:20 PM · c#spritemask

(2D) Rendering only the parts of a sprite that are within specific bounds

I'm working in Unity, using the 2D features to recreate the match-3 style of 10000000 (Ten Million).

I have a parent object (green square) with several children objects (blue rectangles) on top of it, as pictured in the first image. I want to only render the parts of the child objects that are over top of the parent object, as shown in the second image.

I can figure out the size and position of everything just fine, what I'm having trouble with is figuring out how to mask off the parts of the child objects that don't overlap with the parent.

![Non-Masked](/storage/temp/23607-untitled2.png) ![Masked](/storage/temp/23608-untitled.png)

untitled.png (817 B)
untitled2.png (832 B)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Xorglord · Apr 15, 2014 at 09:23 AM

Coincidentally this is the exact same problem I'm having and trying to fix right now. By googling I think the answer should be to use a custom shader which renders things based on a position, but I am not really fluent enough in shader writing to know if this is a good option. If you find a solution I'd love to hear it!

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
avatar image
0

Answer by GrahamReeves · Apr 15, 2014 at 03:03 PM

I got this half-working, (maybe somewhere to start from at least!) it works in the editor, but not in the game view, really it needs to do the world->screen calculation in the shader, not in update to cope with multiple cameras, but I haven't got the parent-bounds-world-to-screen conversion working yet.

I attach this script to the children-to-be-clipped objects(blue) under the parent (green)

 [ExecuteInEditMode]
 public class ClippedChild : MonoBehaviour {
 
     void Update () {
 
         Camera cam = Camera.current;
         if (!cam) {
             return;
         }
 
         GameObject Parent = transform.parent.gameObject;
         Vector4 ParentMin = cam.WorldToViewportPoint( Parent.renderer.bounds.min );
         Vector4 ParentMax = cam.WorldToViewportPoint (Parent.renderer.bounds.max);
 
         Material Mat = renderer.sharedMaterial;
         Mat.SetVector("_ParentMin", ParentMin );
         Mat.SetVector("_ParentMax", ParentMax );
 
     }
 }


Then this in a shader, also on the child object.

 Shader "Custom/ClippedChild" 
 {
     Properties {
         _ColourInside ("ColourInside", Color) = (0,1,0,1)
         _ColourOutside ("ColourOutside", Color) = (1,0,0,0.5)
         _ParentMin("ParentMin", Vector ) = (0,0,0,0)
         _ParentMax("ParentMax", Vector ) = (1,1,0,0)
     }
     
     SubShader {
         Tags { "RenderType"="Transparent" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Lambert
 
         float4 _ColourInside;
         float4 _ColourOutside;
         float4 _ParentMin;
         float4 _ParentMax;
 
         struct Input {
             float3 worldPos;
             float2 screenPos;
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             float4 Colour = _ColourInside;
             
             float2 ScreenPos = IN.screenPos;
             float2 ScreenMin = _ParentMin.xy;
             float2 ScreenMax =  _ParentMax.xy;
             if ( ScreenPos.x < ScreenMin.x || ScreenPos.x > ScreenMax.x ||
                  ScreenPos.y < ScreenMin.y || ScreenPos.y > ScreenMax.y 
             )
             {
                 Colour =  _ColourOutside; 
                 //discard;
             }
                 
             o.Albedo = Colour;
             o.Alpha = Colour.a;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }

This code renders the pixel "in" or "out", but you could use discard; to just not draw the pixel entirely, (colours just helped debug/demo)

As you can see, not wholly working yet, but I'll see if I can get further. (I think just need to provide world-bounds positions to the shader and calculate the screen bounds there).

alt text


screen shot 2014-04-15 at 3.57.06 pm.png (64.5 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
avatar image
0

Answer by komodor · Apr 15, 2014 at 03:20 PM

maybe this might help:

http://wiki.unity3d.com/index.php/DepthMask

and maybe this:

http://docs.unity3d.com/Documentation/Components/SL-Stencil.html

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Saving Rotation of Sprite 1 Answer

Cut out sprite alpha using other textures 1 Answer

Unity streching sprite gameobject to fit two positions. 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