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
1
Question by Hoeloe · Feb 25, 2016 at 12:19 AM · spritedepthclipping

2D sprites in a 3D environment - undistorted without clipping

So I'm trying to create an environment with a 2D sprite in a 3D, perspective environment.

The issue I've come across is that 2D sprites drawn as flat textured quads in the scene become distorted. To solve this, I have a simple line of code that rotates the sprite quad to face the camera plane:

 sprite.LookAt(Vector3.ProjectOnPlane(transform.position + Camera.main.transform.position, Camera.main.transform.forward) - Camera.main.transform.position);

This works, but produces an issue with the depth buffer. The sprite appears to be flat on the ground to the viewer, but is in fact leaning backwards at a fairly steep angle. This causes the sprite to clip through the geometry, as seen in this image:

Sprite clips through geometry

The obvious solution to this issue is to stretch the sprite rather than rotate it, in order to counteract the perspective projection, but this also has problems, in that the stretched sprite would inevitably be extremely tall, and the environment may contain overlapping layers, which could easily clip through the stretched sprite.

My attempts so far have been to disable the depth buffer and force transparent rendering, but neither of these have made any difference whatsoever. The ideal solutions for me would be to either:

  • Force the sprite to be sorted at the object level rather than per pixel, including shadow casting.

  • Use a custom shader that produces custom calculations for the depth buffer, including shadow casting.

  • Directly draw the sprite programatically at a specified depth.

Any of these solutions would produce what I'm looking for, but there may be others I haven't considered. Can anyone help with this?

3d-sprite-depth-issue.png (112.4 kB)
Comment
Add comment · Show 8
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 Jessespike · Feb 25, 2016 at 03:34 AM 0
Share

I suppose you could rewrite the default Sprite shader to be aligned with the view plane. Replace the commented line with the new code. As explained in this billboard shader article.

     //OUT.vertex = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, IN.vertex);

     OUT.vertex = mul(UNITY_$$anonymous$$ATRIX_P,
         mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$V, float4(0.0, 0.0, 0.0, 1.0))
         + float4(IN.vertex.x, IN.vertex.y, 0.0, 0.0));

Actually, shadows won't work on Sprites, so you'll need take that into account. $$anonymous$$aybe try that code snippet in a Standard shader ins$$anonymous$$d.

avatar image Hoeloe Jessespike · Feb 25, 2016 at 08:46 AM 0
Share

Changing the vertices in the shader won't help the problem. If I move the vertices, all I end up with is exactly the same problem I have now. The issue is that in order to maintain the sprite proportions, I need to rotate or scale the sprite - i.e. move the vertices - in 3D space. This ends up with the sprite clipping through the geometry. Just moving that logic into the shader won't help that.

avatar image Hoeloe · Feb 25, 2016 at 07:00 PM 0
Share

So I've been doing this in a shader and trying to write to the depth buffer. So far I've managed to write to the depth buffer using the float dep:DEPTH; notation, but I can't seem to get the object depth working. When I do this:

 o.dep=i.pos.z;

I get exactly what you see above (where i.pos is the translated vertex location). However when I use:

 o.dep=i.objpos.z;

Where objpos is defined as:

 float4 objpos : POSITION1;
 
 objpos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, float4(0, 0, 0, 1));

in the vertex shader, my sprite renders behind everything else in the scene.

avatar image Hoeloe Hoeloe · Feb 25, 2016 at 07:10 PM 0
Share

$$anonymous$$ore info. I have deter$$anonymous$$ed that these two lines of code, absolutely adjacent to each other, produce different results:

             struct v2f {
                 float4 pos : POSITION0;
             float4 objpos : POSITION1;
             };
                 o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
                 o.objpos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);

avatar image Hoeloe Hoeloe · Feb 25, 2016 at 08:11 PM 0
Share

$$anonymous$$ore stuff! I've got the depth buffer sort of behaving by adding a constant offset to the depth of my sprite. This works well enough for my needs.

I'm now working on shadow casting and... well this is the best I can get:

http://i.imgur.com/$$anonymous$$iuRRY$$anonymous$$.png

As you can see, shadows are interacting with the cut out sections of the sprite, which is obviously undesirable. This effect is achieved by using a Fallback to an existing shader, which adds the shadow passes from that shader onto the custom passes. Unfortunately it seems I'm missing something vital in order for this to work properly.

Show more comments
avatar image airwick · Mar 07, 2016 at 11:11 AM 0
Share

Can you show how you managed to solve the clipping by "adding a constant offset to the depth". I'm dealing with the same issue.

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.

Follow this Question

Answers Answers and Comments

47 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

Related Questions

clipping issue with sprites in fixed camera scene 0 Answers

Sprite clips into 3D environment; any way to render the sprite? 1 Answer

Inverse depth masking 0 Answers

Sprites rendered in the wrong order 1 Answer

Game view goes crazy (clear flags)? 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