The fastest way would probably be extremely copmplicated.
But a very fast simple way would be to use some method which divides the world into a grid, or several grids of varying size. You can then dtermine which grid square a point is in simply by dividing it's position by the grid resolution and discarding the remainder. Ie, divide by 64, and use Floor().
Then you can examine only those points which are in the same region of the grid as the camera.
Granted, this will require three divides per point minimum, and three calls to Floor() but it avoids an IF statement to dtermine if a point's distance is less than that of the last point checked. I'm not sure if it will in fact be faster though.
I dunno if you realise it, but you don't need to do a square root to find the two closest points to the camera. All you need to do is this. This should be more than fast enough for even 100,000 points.
; Set these values as appropriate given the scale of your world. They should be larger than whatever the greatest distance you think a point will ever be from your camera.
Pd1# = 65536
Pd2# = 65536
For Point = 0 to Total_Points-1
; Get Point's xyz here.
D# = Px#*Px# + Py#*Py# + Pz#*Pz#
If D# < Pd1#
If D# < Pd2#
Pd2# = D#
P2 = Point
else
Pd1# = D#
P1 = Point
endif
endif
next
Now, you can just compute the distance of P1 and P2 from the camera normally if you need it. And this loop goes through all the points only once, so it's linear in speed which means it's pretty fast. And there's no square root.
Ps:
When I say Px#*Px#, what I was assuming was the camera is centered at 0,0,0. If it is not then you need to do (Cx#-Px#)*(Cx#-Px#) for X Y and Z.
But a very fast simple way would be to use some method which divides the world into a grid, or several grids of varying size. You can then dtermine which grid square a point is in simply by dividing it's position by the grid resolution and discarding the remainder. Ie, divide by 64, and use Floor().
Then you can examine only those points which are in the same region of the grid as the camera.
Granted, this will require three divides per point minimum, and three calls to Floor() but it avoids an IF statement to dtermine if a point's distance is less than that of the last point checked. I'm not sure if it will in fact be faster though.
I dunno if you realise it, but you don't need to do a square root to find the two closest points to the camera. All you need to do is this. This should be more than fast enough for even 100,000 points.
; Set these values as appropriate given the scale of your world. They should be larger than whatever the greatest distance you think a point will ever be from your camera.
Pd1# = 65536
Pd2# = 65536
For Point = 0 to Total_Points-1
; Get Point's xyz here.
D# = Px#*Px# + Py#*Py# + Pz#*Pz#
If D# < Pd1#
If D# < Pd2#
Pd2# = D#
P2 = Point
else
Pd1# = D#
P1 = Point
endif
endif
next
Now, you can just compute the distance of P1 and P2 from the camera normally if you need it. And this loop goes through all the points only once, so it's linear in speed which means it's pretty fast. And there's no square root.
Ps:
When I say Px#*Px#, what I was assuming was the camera is centered at 0,0,0. If it is not then you need to do (Cx#-Px#)*(Cx#-Px#) for X Y and Z.