API Reference · Math utilities
Scatter
public static class ScatterRepeatable randomness, indexed rather than drawn.
Scattering is everywhere in a scene — where the trees go, which way each piece of a wreck flies, how far each blade of grass leans, what size the fourth star from the left is — and Random is the wrong instrument for nearly all of it. A Random is a sequence: it answers "what is next", which means the answer for item 40 depends on having asked about items 0 to 39 first, in that order, from that instance. So the arrangement cannot be reproduced without replaying the whole draw, an object has to be kept alive to hold the position in it, and two things reading the same scatter have to agree on who asks first.
What a scene actually wants is a function: give it an index and it gives back the same value every time, in any order, from anywhere, with nothing stored in between. Then a starfield is a loop, a wreck breaking up the same way on every loop of a film is free rather than something to arrange, and one fragment's behaviour can be worked out without touching the other two hundred.
for (var i = 0; i < count; i++)
stars[i] = Scatter.Direction(i) * float.Lerp(900f, 1000f, Scatter.Value(i));channel — every member takes one — is what gives an index more than one answer. A tree at index 12 wants a position, a height and a lean, and those are channels 0, 1 and 2 of index 12 rather than three parallel arrays or one index multiplied by three. Adding a channel later does not disturb the ones already there, which matters when the arrangement has been art-directed.The three members draw on separate internal streams, so Direction(i) and Value(i) at the same index and channel are uncorrelated. Multiplying one by the other is the commonest thing anybody does with them, and it would fail quietly and look almost right if they were two views of the same number.
This is a hash, not a generator: no seeding, no state, no thread affinity, no allocation, and identical results on every platform and every run. It is not suitable for anything that needs to be unpredictable.
One thing to know about the name, which the demo walked into the first time it used this. Scatter is a verb as well as a noun, so a class with a method of its own called Scatter cannot reach this one by its plain name — the method wins the lookup, and the compiler's complaint is about a method "not valid in the given context" rather than about the name. Qualifying it as Ava3D.Scatter works; renaming the method is usually better, as it was there.
Methods
| Member | Description |
|---|---|
| A repeatable unit vector, spread evenly over the sphere. Evenly is the part worth having. The obvious construction — a random longitude and a random latitude — is not even: it crowds the poles, because the rings of latitude near them are short and still get their full share of points. Picking the height uniformly instead and taking the ring radius from it is, which is Archimedes' hat-box theorem and costs exactly the same arithmetic. The difference is plain the moment a few hundred of these are drawn as a starfield. |
| A repeatable point spread evenly through the unit ball — the interior, not the surface. Multiply by a radius for a cloud, or by a The radius is a cube root for the same reason the height above is uniform. A radius drawn flat from 0..1 puts half the points inside half the radius, which is an eighth of the volume, so the cloud comes out dense in the middle and thin at the edge. Blown outward as debris that reads as a solid core with a haze around it rather than as a shell of fragments. |
| A repeatable number in 0..1 from an index and a channel. |