最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Struggling to make a C# Script to find chunk render priority in Minecraft-esque Unity game - Stack Overflow

programmeradmin0浏览0评论

I've been following a tutorial on how to make a voxel game in Unity. I've followed it through but it seems the tutorial came to an end after it described how to do very basic rendering.

The way my game renders now is rendering all the chunks at once. This is horrible for performance; so I did some research on how other voxel games render their chunks. Turns out that they use something called chunk priority:

  1. Find the location of the player
  2. Draw a ray/find the distance between the player and the nearest chunk to them (that isn't the one they're already in)
  3. Put the unrendered chunks in a list, closest-to-farthest (sorted by distance)
  4. Render.

(Do note, I'm not using any sort of frustrum culling for the chunks. EX: Minecraft renders the chunks you're looking towards (in your FOV) first. My project doesn't need that, yet.)

However, I'm having a problem with step 3 above. In unity, I can't find a way to find the 2D distance* between player and chunk. My theory was as follows:

  1. Create a sphere/cube collider of certain size that follows the player (for render distance)
  2. New chunks will enter the Collider and activate Collider.OnTriggerEnter in an attached script, saving them to a list for the next step
  3. Cast a ray between the player and the chunks in the list, finding which ones are closest and adding them to a list closest-to-farthest
  4. Render chunks closest-to-farthest.

When I tried implementing this it failed. For some reason, when I added a collider to my player and another to the chunks, the option to use the chunk's collider as a trigger activator was greyed out.

According to unity, my chunk's have to have convex meshes to use them with the collider, but that would hinder gameplay quite a lot since individual blocks lose their collision meshes.

If you have any ideas to help me I'd be greatly appreciative.

*(vertical distance doesn't matter with this rendering system)

I've been following a tutorial on how to make a voxel game in Unity. I've followed it through but it seems the tutorial came to an end after it described how to do very basic rendering.

The way my game renders now is rendering all the chunks at once. This is horrible for performance; so I did some research on how other voxel games render their chunks. Turns out that they use something called chunk priority:

  1. Find the location of the player
  2. Draw a ray/find the distance between the player and the nearest chunk to them (that isn't the one they're already in)
  3. Put the unrendered chunks in a list, closest-to-farthest (sorted by distance)
  4. Render.

(Do note, I'm not using any sort of frustrum culling for the chunks. EX: Minecraft renders the chunks you're looking towards (in your FOV) first. My project doesn't need that, yet.)

However, I'm having a problem with step 3 above. In unity, I can't find a way to find the 2D distance* between player and chunk. My theory was as follows:

  1. Create a sphere/cube collider of certain size that follows the player (for render distance)
  2. New chunks will enter the Collider and activate Collider.OnTriggerEnter in an attached script, saving them to a list for the next step
  3. Cast a ray between the player and the chunks in the list, finding which ones are closest and adding them to a list closest-to-farthest
  4. Render chunks closest-to-farthest.

When I tried implementing this it failed. For some reason, when I added a collider to my player and another to the chunks, the option to use the chunk's collider as a trigger activator was greyed out.

According to unity, my chunk's have to have convex meshes to use them with the collider, but that would hinder gameplay quite a lot since individual blocks lose their collision meshes.

If you have any ideas to help me I'd be greatly appreciative.

*(vertical distance doesn't matter with this rendering system)

Share Improve this question edited Mar 6 at 9:30 SpicyCatGames 1,7481 gold badge18 silver badges34 bronze badges asked Mar 3 at 23:55 Anthony RimshasAnthony Rimshas 111 bronze badge 1
  • 1 Well, for a minecraft voxel style, why not primarily use box colliders. Second, whos voxel thing have you followed? not all are equal tbh – BugFinder Commented Mar 4 at 0:11
Add a comment  | 

1 Answer 1

Reset to default 0

The whole idea of chunks in all frameworks I've encountered so far, incl. Minecraft is to have them in a spatial grid. Especially so that calculations like these become trivial.

The framework you use most likely provides you some coordinates for every chunk in the world. Either in real world X & Y of the chunks corner, or as Chunk X and Y which you have to multiply with the size of a chunk. Minecraft shows all that in the debug screen.

With those numbers using a sphere collider on the player is absolute overkill. It's just good ol' pythagoras to get distance to the player.

Do you actually use a chunk based voxel framework that does not have that? Does it use chunks of different sizes somehow?

EDIT: Achieving collision for the player with the voxel terrain for regular gameplay however is a whole other story and quite a beast to do efficiently. Hope the tutorial you follow covers that as it's not a beginner topic.

发布评论

评论列表(0)

  1. 暂无评论