• 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 arlinon · Nov 08, 2013 at 12:31 PM · 2dpositioningtextureing

Two quads, same position, which one is seen ahead?

Hi all, lets say I have two quads at the same position with a sprite in them as texture. Is there any way you can identify which one will be seen on top of the other from an orthographic camera?

Comment
Add comment · Show 3
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 arlinon · Nov 09, 2013 at 11:16 AM 0
Share

I don't know whether this question would be better in another thread but, what ways do people use for 2d games with objects getting on top of other? In starcraft 1 you could make units go on top of each other though they were doing it reluctantly lol. Any idea what they use there?

avatar image Bunny83 · Nov 09, 2013 at 03:59 PM 0
Share

Q&A sites like UnityAnswers don't have "threads". We have questions and answers to that questions and for stuff that's not an answer we have comments. There is only one question which might have multiple answers. Here on the Qato design it's not that well seperated since the question looks almost like the answers below.

I've converted your answer into a comment.

Well, starcraft is actually a 2d game. Essentially in (fake) isometric 2D views you usually don't use a depth buffer (those has been invented mainly for 3D). In an isometric view you can simply say everything with a greater y coordinate is below things with lower y coordinate. So if you draw things from top to bottom they should appear correctly (using shaders without depth testing).

Such games usually have multiple passes, so static geometry is drawn first, so all units will appear on top of it.

Back the old days you also don't redraw the whole screen all the time. You usually just update the areas where something has changed. This was done because we don't had great graphics hardware ;)

If you create such a game in Unity you would simply create the game as a 2.5D game (so using 3D objects / sprites and view them at a 45° angle). Objects that are literally behind other objects will be displayed correctly when using the depth buffer. You would only use the x-z plane for the gameplay and keep most stuff at y == 0.

Of course if you like you can still doing everything in pure 2D, but that's usually much more work

avatar image arlinon · Nov 10, 2013 at 03:57 PM 0
Share

Great information, you rock! So I think I am just going to write a script that arranges objects that intersect, changing their z position a little, putting them front or behind.

Are there any posts you can suggest about doing everything in pure 2D with unity?

1 Reply

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

Answer by Bunny83 · Nov 08, 2013 at 12:50 PM

No, because if they have the same position you will experience something called z-fighting. This happens because the GPU uses a depth-buffer in which it stores a depth value for each pixel. Due to floating point inaccuracy two objects that are at the same point (or almost at the same) will be "fighting" for each pixel. Some will belong to the one some to the other.

**edit*

If you use a shader that doesn't write to the depth buffer (like most transparent shaders) the order is given simply by the render order. The Render order might depend on the shader you use on each object and by the internal render order which can't be changed directly. Shaders can define a (Render)Queue tag value to partially control the order in which they are drawn.

If the two quads are part of the same mesh, it only depends on the order of the triangles within the Mesh itself. Again, keep in mind that's only true for shaders that don't use the depth buffer. All other shaders are based on the depth buffer.

Comment
Add comment · Show 3 · 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 Owen-Reynolds · Nov 08, 2013 at 03:51 PM 0
Share

...and for the last para, tri-order, Optimize may swap those around.

avatar image arlinon · Nov 08, 2013 at 05:40 PM 0
Share

Thanks for the answer. Could you elaborate on the order of triangles and why it affects z-fighting?

avatar image Bunny83 · Nov 08, 2013 at 10:14 PM 0
Share

Z-fighting only happens when the depth buffer is used. For each rendered fragment the GPU performs a depth test. If two primitives are at the same depth level it's kind of random who will win the fight.

The order of triangles are irrelevant when the depth buffer is used, only shaders that don't perform a depth test are affected by the render order. It's like painting, what you draw last will be on top.

However if the shader does use the depth buffer some pixels might belong to the thing drawn first and some to the thing drawn last.

$$anonymous$$eep in $$anonymous$$d there are shaders that do perform the depth test, but don't write to the depth buffer (most transparent shaders do this). That way the engine will render all opaque geometry which will be written to the depth buffer. When the transparent geometry is drawn it performs a depth test but the shader won't update the depth value. So transparent shaders can have z-fighting with opaque geometry, but not with other transparent geometry. In this case it depends solely on the render order.

That's why transparent geometry is sorted from far to near, however it's only sorted per object. Transparency sorting is a great problem on it's own which doesn't have a clear solution. So Unity only sorts per object so if triangles of the same mesh are overlapping, they are drawn in the order they appear in the triangle array.

See this thread on the forum for more information.

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

17 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

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Positioning sprite perfectly in camera view. 0 Answers

Root motion lost in transitions 1 Answer

Why do my child object and parent object have the exact same absolute position? 4 Answers

How Do I Align Game Objects Evenly In 2D Space? 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