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 Andrew_Kenady · Mar 27, 2014 at 04:50 PM · meshgraphicsdrawingorderdrawmesh

Changing draw order of meshes in Graphics.DrawMesh()

Hi! I'm working on a game where we need to be able to draw things with 3D lines, and I've come across a road block. I'm using the Graphics.DrawMesh() function to draw each line to the screen (each line is its own procedurally generated mesh). However, I can't figure out what the best way is to modify the order in which they draw.

Here's the Draw method for a drawing:

 public void Draw()
 {
     for (int i = 0; i < _lineMeshes.Count; i++)
     {
         Graphics.DrawMesh(_lineMeshes[i], transform.localToWorldMatrix, _lineMaterials[i], 0);
     }
 }

My first instinct was to reverse the order of the For loop, but that didn't change anything. Changing the Z position of the meshes didn't change anything, either. The issue is, every mesh that is created later is drawn later to the screen. Here's an image showing the problem: alt text

"1" was the first mesh created, "2" second, and so on. I'd like this order to be reversed.

The shader on the lines is as follows (not sure if it helps, but maybe it's part of the problem):

 Shader "Custom/LineShader" 
 {
     Properties 
     {
         _Color ("Main Color", Color) = (0,0,0,1)
     }
     
     SubShader {
 
         Pass 
         {
             Blend SrcAlpha OneMinusSrcAlpha 
             ZWrite Off
             Color [_Color]
             Lighting Off
             Cull Off
         }
         
         Pass 
         {
             Blend SrcAlpha OneMinusSrcAlpha 
             ZWrite Off
             Color [_Color]
             Lighting Off
             Cull Off  
         } 
     }
     
     FallBack "VertexLit"
 } 

Any help is appreciated! Thanks in advance

lines.png (2.7 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

1 Reply

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

Answer by supernat · Mar 27, 2014 at 04:59 PM

The shader helps. You turned ZWrite off in the shader. If you leave z write on and change the Z value of the mesh (assuming you are looking down the Z axis), that will allow the Z value to control the order. I'm guessing DrawMesh() probably just queues the meshes up into a buffer and is not guaranteed to keep the order that you call DrawMesh in, which is unfortunate.

EDIT: Hmmm...I should clarify a couple of things. Z Write has Zero to do with the Z axis (can I call you Zorro just for fun?). Z Write is the Z Depth in the final view-projection matrix, i.e. after after everything has been transformed into the screen space, it's the Z Depth into the screen. So you could draw your numbers along the Y axis, along the X axis, and then the Y and X values would control the depth. Just wanted to clarify.

Another thought came to me. You could use the Sorting Layers and Sorting Order to order the draw calls. You can assume the default sorting layer and would only need to set the sorting order for each mesh, but I don't know the API for that off hand, and it's not listed in the Unity reference.

The other thing I thought of is that you may want the numbers to draw on top of everything else in the world, hence is why you set the Z write off. In that case, if you can't control the order in which the numbers are drawn using sorting layers, you must leave ZWrite on, and you need to render the numbers on a separate layer and with a separate camera, similar to how we would do UI. The second camera would clear the depth buffer then draw the meshes in that layer. There are several unity answers on how to render UI to a second camera if you're interested.

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 Andrew_Kenady · Mar 27, 2014 at 06:25 PM 0
Share

Thanks for the input! I'll try to work with what you've told me to fix the issue... Basically, the idea is that the player draws his own character which is used in-game, so we need the lines to draw on top of each other, but also be present in 3d space and be obscured by other objects. Thanks! :)

avatar image supernat · Mar 27, 2014 at 07:19 PM 0
Share

You'll want to think through that then. It sounds like you definitely want ZWrite on, but that means everything in the scene will need to be ordered in the Z axis (if that's what you use) and you'll want to use only 1 camera in that case. You can probably just decrement the Z value each time the user clicks and releases the mouse so that each draw is closer. It'll need to be really small amounts though so that their models can run around in the environment without leaving gaps big enough for other objects to slip through. But not too small, because then your Z resolution will cause Z fighting. To avoid that, make sure you set the near clip plane on the camera as far away as you can and the far clip plane as close as you can.

avatar image Andrew_Kenady · Mar 28, 2014 at 02:49 PM 0
Share

Thanks again :) we ended up going with a solution that changes the z position very slightly with every line. I wish we could have gotten a solution that would have just reversed the default behavior, but this will work for what we need it to (hopefully!).

avatar image supernat · Mar 28, 2014 at 03:39 PM 0
Share

Well the thing is, if you want to turn ZWrite off, you pretty much only have 1 choice. Everything you draw with ZWrite off must be sorted and drawn in order (I'm sure we could figure a way to get that to work but surprised Draw$$anonymous$$esh didn't). The problem then is you can't just draw anything else in the environment unless it too is sorted or unless you are okay with the stuff you draw always being on top. It sounds like you made the logically correct choice because you want your drawings to move around in the environment and be occluded by other objects. It sounds like a cool game/application, good luck with it!

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

21 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

Related Questions

Graphics.DrawMesh not working 2 Answers

How to attach a mesh created with Graphics.DrawMesh to a GameObject? 1 Answer

DrawMesh always drawn before gameobjects 1 Answer

OnWillRenderObject without renderer? 1 Answer

650+ DrawCalls, Mesh.DrawVBO 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