Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This question was closed Aug 01, 2015 at 10:27 AM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by heyx3 · Jul 29, 2015 at 02:48 AM · c#camerameshgraphics

Problems using OnRenderObject() and Graphics.DrawMeshNow()

I'm working on a 2D voxel game where the voxel geometry is split into 8x8 "chunks". Originally, I just had one GameObject (with a MeshFilter/MeshRenderer) for each chunk and let Unity handle everything itself.

This worked just fine, but it wasn't scaling well when I started playing with larger grids, so I'm implementing my own system where a single object/script generates all the chunk meshes and manually draws a small number of them based on the area visible to the camera. However, with this new system, my meshes are not appearing anywhere in the scene! They worked with the old method, so I know the issue isn't with the mesh/material itself, and thanks to Debug.Log() I know that this code is all being executed as expected, so the problem must lie with how I'm using the voxel material and the "Graphics.DrawMeshNow()" method.

The rendering code is as follows:

 void OnRenderObject()
 {
     //For reference:
     //"Chunks" is the 2D array of chunks. Each chunk object stores its own mesh.
     //"VoxelBlockMat" is a simple material for rendering the chunks.
     //"Chunk.Size" is the number of voxels per chunk (8).
 
     //Figure out which chunks are in view.
     Camera cam = Camera.current;
     Vector2 orthoHalf = new Vector2(cam.orthographicSize * cam.pixelWidth / cam.pixelHeight,
                                     cam.orthographicSize);
     Vector3 camPos = cam.transform.position;
     Vector2 viewMin = new Vector2(camPos.x - orthoHalf.x, camPos.y - orthoHalf.y),
             viewMax = viewMin + (orthoHalf * 2.0f);
     Vector2i viewMinI = new Vector2i((int)viewMin.x, (int)viewMin.y),
              viewMaxI = new Vector2i((int)viewMax.x + 1, (int)viewMax.y + 1);
 
     Vector2i chunkMinI = viewMinI / Chunk.Size,
              chunkMaxI = viewMaxI / Chunk.Size;
 
     //Render those chunks.
     VoxelBlockMat.SetPass(0);
     for (int y = chunkMinI.y; y <= chunkMaxI.y && y < Chunks.GetLength(1); ++y)
     {
         if (y > 0)
         {
             for (int x = chunkMinI.x; x <= chunkMaxI.x && x < Chunks.GetLength(0); ++x)
             {
                 if (x > 0)
                 {
                     //The mesh stores the world position of the voxels, so no world transform is needed.
                     Graphics.DrawMeshNow(Chunks[x, y].VoxelMesh, Vector3.zero, Quaternion.identity);
                 }
             }
         }
     }
 }
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

  • Sort: 
avatar image
0
Best Answer

Answer by heyx3 · Jul 29, 2015 at 05:00 AM

Fixed the problem myself! Apparently my mesh wasn't set up correctly, which is confusing because it worked just fine with the old method...but anyway, the problem wasn't related to this rendering code.

Comment
Add comment · 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
0

Answer by Bunny83 · Jul 29, 2015 at 03:27 AM

If i draw the default cube mesh with the default shader i can see a black cube at the origin. Keep in mind that Graphics.DrawMeshNow doesn't supports lighting. It's a low level rendering method.

Also keep in mind that currently you draw all chunks at the same position. If you haven't placed the vertices at world space coordiates you will get strange results.

Do you use a custom shader or any of the built in?

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 · Jul 29, 2015 at 03:30 AM 0
Share

Uhm, you use an orthographic camera? o.O

Have you checked the sceneview when in playmode?

avatar image heyx3 · Jul 29, 2015 at 03:33 AM 0
Share

As I mentioned before, all vertices are defined in world-space. This is a custom material for my voxels. This is a 2D game, so I don't use Unity's lighting system; I just need the $$anonymous$$VP matrix for the material (I assume that's still set automatically when I use Draw$$anonymous$$eshNow()?). The material takes in points and builds them into quads in the geometry shader, then just samples from a tilemap texture for the color.

avatar image heyx3 · Jul 29, 2015 at 03:43 AM 0
Share

Nothing shows up in the scene view either.

avatar image Bunny83 · Jul 29, 2015 at 04:24 AM 0
Share

As i said i tried it and i can see the cube in the sceneview as well as in the gameview. I even see it in the preview area. Try exposing one of your meshes in a public variable at runtime and double click it in the inspector. You should see the mesh preview at the bottom. If you can't see anything (after you rotate a bit) your mesh is messed up.

Without more details this is a dead-end since it works for me "out of the box". If it doesn't work for you there's something about your project we don't know.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Fixing mesh normals 1 Answer

Does anyone know how to change the Mesh in the SkinnedMeshRenderer? 1 Answer

Distribute terrain in zones 3 Answers

Mesh Position at double gameobject's position 1 Answer

Placing 3Dtext with Perspective Camera 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