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 c1913219 · Dec 01, 2013 at 05:33 PM · meshgraphicsdrawcallsdrawingdrawmesh

Graphics.DrawMesh not working

I tried rendering a cube with Graphics.DrawMesh on Unity 4.3, but nothing seems to happen. I have an empty scene with a camera looking at the origin. The following script is attached to the camera:

 using UnityEngine;
 public class DrawMesh : MonoBehaviour 
 {    /* 5_ _ _6
       /|    /|
     1/_|_ 2/ |      
     | 4|_ _|_|7        
     | /    | / 
     |/_ _ _|/ 
     0      3 */
     Mesh mesh;

     void Start() {
         mesh = new Mesh();
 
         Vector3[] vertices  = new Vector3[] {
             new Vector3(-1, -1, -1),
             new Vector3(-1, +1, -1),
             new Vector3(+1, +1, -1),
             new Vector3(+1, -1, -1),
             
             new Vector3(-1, -1, +1),
             new Vector3(-1, +1, +1),
             new Vector3(+1, +1, +1),
             new Vector3(+1, -1, +1)};
         
         int[] triangles = new int[] {
             0, 1, 2,    2, 3, 0,
             1, 5, 6,    6, 2, 1,
             3, 2, 6,    6, 7, 3,
             4, 5, 1,    1, 0, 4,
             0, 3, 7,    7, 4, 0,
             7, 6, 5,    5, 4, 7};
 
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
     }
 
     void Update () {
         Graphics.DrawMeshNow(mesh, Vector3.zero, Quaternion.identity);
     }
 }

What am I doing wrong?

Project attachment

drawmesttest.zip (22.2 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

2 Replies

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

Answer by Bunny83 · Dec 04, 2013 at 02:28 PM

Graphics.DrawMesh should work pretty well if you:

  • actually use Graphics.DrawMesh and not Graphics.DrawMeshNow which is a totally different function.

  • have Unity pro since this is a pro feature. *It used to be a pro only feature.

DrawMeshNow will draw the mesh immediately. If you use this outside of a rendering callback it has no effect since it will get cleared when the actual rendering starts (which is at the end of each frame). DrawMesh on the other hand "registers" the mesh to be drawn this frame and it's rendered when it's time to do so.

edit
ps: You are aware of the fact that you can't draw a cube with 8 vertices? Your "cube" will look like a lowpoly sphere. Each vertex can only have one normal vector. If you share all vertices like you did, RecalculateNormals will average the normal between all 3 faces that meet at the vertex. The difference is this:

cube normals

To render a cube like a cube you need at least 24 vertices or it will look like this:

smoothshaded cube

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 Bunny83 · Dec 04, 2013 at 03:32 PM 0
Share

In this answer i've created a frustum mesh for a cameras frustum. If you replace the 8 corners with your cube corners it should work ;) I have the same ordering as you have, but my code duplicates the vertices for each face. So ins$$anonymous$$d of my local "v" array you could use your "vertices" array and would get a cube. If you use RecalculateNormals you can also omit the normal generation ;)

avatar image c1913219 · Dec 04, 2013 at 06:37 PM 1
Share

I got it working once I used non-obsolete overload of the Draw$$anonymous$$esh function. Using an obsolete version gives a warining that Draw$$anonymous$$eshNow should be used ins$$anonymous$$d, which is kind of misleading.

However, Unity Pro is not required. You might want to correct that part from your answer. Thanks for your help.

avatar image Bunny83 · Dec 04, 2013 at 07:30 PM 0
Share

@c1913219:
Well, it used to be required ;) The whole Graphics class was pro only when it has been introduced. Back then there was a note on the Graphics page that it's pro only. Now only a few things from this class requires pro. I've changed my answer. Thanks.

avatar image AbleArcher · Feb 08, 2018 at 11:23 PM 0
Share

Can you point us to documentation that says that Draw$$anonymous$$eshNow will have no effect outside of a rendering callback?

avatar image
0

Answer by diegzumillo · Dec 01, 2013 at 05:59 PM

I'm a javascript user but, at the end of your triangles definition you have "7[};][1]" that looks odd. Try

 int[] triangles = new int[] {
          0, 1, 2, 2, 3, 0,
          1, 5, 6,    6, 2, 1,
          3, 2, 6, 6, 7, 3,
          4, 5, 1, 1, 0, 4,
          0, 3, 7, 7, 4, 0,
          7, 6, 5, 5, 4, 7}; 
Comment
Add comment · Show 1 · 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 c1913219 · Dec 01, 2013 at 10:15 PM 0
Share

$$anonymous$$ust have typoed something there. $$anonymous$$y bad, but it isn't in the actual project and is not the problem.

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

19 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

Related Questions

Changing draw order of meshes in Graphics.DrawMesh() 1 Answer

Game work slow in other devices.. 1 Answer

650+ DrawCalls, Mesh.DrawVBO 1 Answer

Will Unity support hardware mesh instancing? 0 Answers

Populate people in city with DrawMeshInstancedIndirect? 2 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