How to draw an object at great distance without flickering

After two days into using Unity and programming full stop I am rather pleased to have come up with the following. It draws a randomly determined star prefab. However, upon viewing the more common m class stars (the smallest) just flicker even when the camera is not moving. Is there a way to solve this or should I somehow just draw a single pixel for each star and then somehow dump a big model over the top of that pixel when the camera gets closer to that specific star? Please bear in mind only 2 days scripting means I need a simple explanation :-) Thanks in advance.

``````var randomSeed : float = 1;
var starPrefab_a : GameObject;
var starPrefab_b : GameObject;
var starPrefab_f : GameObject;
var starPrefab_g : GameObject;
var starPrefab_k : GameObject;
var starPrefab_m : GameObject;
var starPrefab_o : GameObject;

function Start() {
for (i = 0; i < 2000; i++) {

var star_class = Random.Range(0,100);

starPlot = Vector3((PseudoRandom()*30000)-15000, (PseudoRandom()*30000)-15000, (PseudoRandom()*30000)-15000);

if (star_class <=1) {
Instantiate(starPrefab_o, starPlot, Quaternion.identity);
} else if (star_class <=2) {
Instantiate(starPrefab_b, starPlot, Quaternion.identity);
} else if (star_class <=3) {
Instantiate(starPrefab_a, starPlot, Quaternion.identity);
} else if (star_class <=6) {
Instantiate(starPrefab_f, starPlot, Quaternion.identity);
} else if (star_class <=12) {
Instantiate(starPrefab_g, starPlot, Quaternion.identity);
} else if (star_class <=24) {
Instantiate(starPrefab_k, starPlot, Quaternion.identity);
} else if (star_class <=100) {
Instantiate(starPrefab_m, starPlot, Quaternion.identity);
}
}
}

function PseudoRandom() {
randomSeed = (randomSeed*9301 + 49297) % 233280;
return randomSeed / 233280;
}

``````

This looks like it's simply because the objects size is less than one pixel on the screen. As a result, sometimes the object is deemed visible, other times not.

One way around this is to turn up the Anti Aliasing settings, but this has a significant performance cost.

Another method might be to use a different technique to draw the stars, which makes better use of the normal type of filtering which happens within textures. I've experimented with using particle systems as starfields before, like this:

• Create a new particle system
• Remove the "Particle Animator" component from it
• adjust the settings like so:
• One Shot: yes
• Ellipsoid size: X:950, Y:950, Z:950
• Min Emitter Range: 1
• Min Size: 5
• Max Size: 10
• Min Emission: 10000
• Max Emission: 10000

I picked 950 for the ellipsoid size, as the default camera far clip plane is 1000, so this is near the extremeties of what your cam will render. If you've changed your cam far clip plane, adjust this so that it's near (but comfortably within) you camera's far clip plane.

Setting Min Emitter Range to '1' makes it so that the particles are only placed around the outside of the ellipsoid, and not in the volume within.

You would then want to move this particle system with the camera every frame (but don't parent it, because you don't want to inherit the cam's rotation, only follow the position). You could use a configurable joint to do this, if you put a rigidbody on the camera, or you could do it with a tiny script, containing something like:

``````transform.position = Camera.main.transform.position;

``````

Alternatively, if you want to be able to fly 'through' a cluster of stars, rather than have them act like a 'skybox' as describe above, simply set Min Emitter Range to 0 instead of 1, and don't move the particle system at all when the camera moves!

It's hard to say without knowing any details, but I'd guess "great distance" means you're running out of z-buffer precision. Try increasing your camera's near clip plane as much as possible.

On a side note, you don't need your PseudoRandom function, since you can set Random.seed and get the same functionality natively.

As I suspected and Duck highlighted the simple answer is that the objects are too small to sometimes be rendered. I have got around this by doing the following:

The camera is in a cube 2000 units along x,y,z. This cube is the centre of a big cube layered of same sized cubes total dimensions 11 constituent cubes along x,y,z. In the centre cube and the next layer all objects are drawn. In the next layer the smallest star is no longer drawn at all and the other stars are now represented by a billboard facing the camera. This strategy is then continued (the next smallest stars being no longer drawn at all) to the outer layer where only they biggest stars are represented by a billboard. Wrapped around this is Duck's particle idea. The trick now is moving the camera from one box to the next and the making that the centre but I think I know how to do this.

Thanks for all the help from Duck as I no longer need my dodgy skybox and Eric5h5 for the original post on the forums a few years back that got me started on plotting my stars randomly but with a controllable predictability.