- Home /
Find closest vertices to camera.
I am working on a simple LOD system for my randomly generated spherical terrain and have everything worked out except for how to find the 9 closest vertices to the camera. Is there any way to easily implement this?
oh, that's a "sphere". further your camera is always equatorial?
yeah just keep all the longitudes and get them from that
if the camera moves around just use ordinary old culindrical projection and draw a square
It is a 3D cube with its vertices mapped to a sphere. I don't have lighting on my shader so you can,t make out the perspective of it. Here it is with a per vertex shader with lighting.
yeah if it's a sphere keep the info lat/long with each vert
it's absolutely trivial to then get the verts nearest a satellite, or solve other similar problems
generally whenever you fool about with a sphere ... you keep a Handy List of lat/long for each vert
(I suppose even more generally, whenever you fool with some trivial surface that has a really clear projection to another set of coordinates, you almost always need that other set of coordinates, on hand, for one reason or another!)
it goes without saying: performance is almost never relevant so obviously just brute force it first!
Answer by whydoidoit · May 17, 2013 at 09:19 PM
Using Linq:
using System.Linq;
...
var pos = Camera.main.transform.position;
var closest = mesh.vertices.OrderBy(v=>(v - pos).sqrMagnitude).Take(9).ToArray();
But it won't be that fast if you have a lot of vertices!
If you have a lot of vertices and the mesh isnt changing then cache it when it's built otherwise mesh.vertices makes a new array every time.