• 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
Question by BJO · Jun 20, 2013 at 08:01 AM · 2d3dspritesclippingdepth buffer

Mixing 2D and 3D - Issues with clipping and the depth buffer

Hi Unity community,

I'm working on a project that mixes 2D sprites with 3D world geometry. Unsurprisingly, I'm running into undesired clipping behavior.

Below is an example... alt text

In this scene, both the white walls and the green grid are all true 3D objects. The red cube is a sprite textured to a camera-facing quad.

The desired behavior is for the cube to appear as if it were sitting behind the front wall (just like a normal 3D cube would). Unfortunately, the sprite's quad extends through the wall and thus I get the clipping visible in the image above.

So now the question; how would you achieve sprites that clip properly when positioned near 3D geometry?

One idea I had was to define a 3D bounding box that is correctly positioned in the world. I could then (theoretically) use this bounding box to provide depth information for rendering the sprite. The caveat is that I have no idea how to do this in Unity. I'm guessing it has something to do with customizing a shader.

Therefore, I throw myself to the genius of the Unity community. Please let me know what you think of the above approach, or if you would do something completely different.

Also, pointers to any resources where I could read more about this topic are much appreciated.

Thanks for your help!

clipping example.png (35.3 kB)
Comment

People who like this

0 Show 4
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 Tarlius · Jun 27, 2013 at 06:45 AM 0
Share

I'm having a similar problem and I think the "correct" way to do it will involve writing a shader and a second texture for the "depth" of the sprites.

Depending on your game, you might be able to get away with just moving the sprite backwards (ie: directly backwards from the camera) and clipping through the floor and fixing that with layers, but you'll have the same problem with "real" walls behind the sprite. However, this will work if in your game you only care about the floor and back walls (the green ones)

I'd be interested in any solution/workaround you came up with?

avatar image BJO · Oct 15, 2013 at 09:06 AM 0
Share

Hey Tarlius, an update on what I've learned/devised... First, it seems this is a non-trivial problem, and while we can't be the first to encounter it, there doesn't seem to be a lot that is publicly available.

The solution I came up with is to create a mesh that (at least to some degree) mimics the geometry of the sprite it contains. The complexity of the mesh is entirely dependent upon how the sprite interacts with its surroundings (does the sprite just sit in-front-of/behind other sprites, do they occupy the same space, etc...). You can then modify the z-value (from the camera's frame of reference) of the mesh's vertices such that you get proper clipping.

Hope this helps!

avatar image Tarlius · Oct 15, 2013 at 01:03 PM 1
Share

I actually did some reading about this myself and stumbled upon info from how they did it in the original Sims. They apparently had a separate texture that contained depth information for each pixel (quite similar to what I suggested, actually!)

I think messing around with the mesh is simpler though, and that was the workaround I implemented too, although it sounds like your implementation is less basic than mine was.

avatar image Jessy · Oct 15, 2013 at 02:15 PM 0
Share

I would model the object, but not store traditional UVs. You can construct your own texture coordinates in the shader, based on view space coordinates. Maybe use the UV coordinate for every vertex, and use them as a scalar for local to texture space.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Need a Solution For Ordering My Sprites Correctly 0 Answers

3d Collision for 2d objects 1 Answer

Making sprites just rotate and not tilt. 3 Answers

so uhh.. how do you make sprites for the 4.6 UI? 2 Answers

Sprite quality decreased after adding 3d object to 2d game 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