- Home /
How Would I Find Out if a Player can See a Certain Face of a Mesh?
Hey people! I'm making a game that is somewhat like a voxel game, except that the blocks are not on a 3D grid. They can be anywhere in the y axis. Now, if I want to optimize this game, how would I go about checking to see if a face of a block is visible to the player? Is the best way to just look through the list of 50-100 blocks to find one that would hide the face? Also, I cannot use raycasts because more than one object might make a face covered.
Thanks!
I don't see why you still couldn't use raycasts? It will hit any object that may be blocking, assu$$anonymous$$g the physics layers are set up correctly. I'm also curious what you are trying to optimize for.
To answer in $$anonymous$$ath terms though, you could use a dot product of the player's Forward vector and the vector corresponding to the face's normal. Or it seems like Vector3.Angle would do something similar.
simply use a raycast, as you guessed. that's perfectly correct and an everyday thing to do. (as callen has already said, it's no problem (why would it be?) that more than one item might be in the way.)
The reason I didn't want to use raycasts was because multiple blocks together might block a face ins$$anonymous$$d of just one big one. Could raycasts still do this somehow?
I don't think I understand the problem you're having... If you need to find all of the objects covering your target, you could do multiple raycasts (say, aimed at each vertex of the face in question). But from my reading of your question, you don't need that do you?
So I guess I'm confused as to whether you want to know just the Boolean of "can the player see this face?" or the more involved case of "which object (or set of objects) stand between my player and this face?"
For either case, I think raycasts are still the way to go. But as I said, I'm curious what optimization this will give you, and if it couldn't be done another way.
Sorry I can't explain this well. I'll try explaining my situation using a picture:
__ __
| | | |
| | |_|
|_| |_|
$$anonymous$$y problem is that I want to hide any faces that are not necessary to render, but there is no grid on the y axis in the game. Each "block" could be a completely different height, and multiple blocks could cover the face of a taller block, as I hopefully showed in the picture. In that case, the tall block's covered face does not need to be rendered.
Again, I don't know how I can explain this situation in a few words because the whole game is quite a complex concept.
EDIT: $$anonymous$$y picture did not turn out the best, but hopefully you can still understand what I'm saying.
Answer by callen · Dec 29, 2015 at 03:36 PM
If I'm reading the correctly, the only reason for this problem is to determine whether or not to render a face?
Do you realize that the engine can do this for you? I highly suspect that Unity's implementation of Occlusion Culling will outperform yours, (hit the link for set up details), especially because there is likely some overhead with enabling/disabling renderers manually, which the engine can internally optimize out.
You may be trying something more complex which you did not describe, but it looks like you're trying to do Occlusion Culling, so use Unity's implementation for maximum performance.
Callen has absolutely hit the nail on the head.
It's quite common on this site that folks ask questions, and the answer is actually "oh that is totally done for you, it's the whole point of a game engine!"
For example quite often you get people asking "I'd like to simulate what happens when two blocks hit each other -- and one bounces the other along!" Of course that is the whole entire raison d'etre of a game engine.
Exactly as Callen says, DevLuke, I think it's the case you're just mistaken DevLuke and thankfully, any ordinary rendering engine completely does this for you.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
So what's Mesh.UploadMeshData do exactly? 3 Answers
Is it possible to calculate batch count of each material ? 0 Answers
C# basic perfomance question 2 Answers