My goal is to generates vertices equally distributed over any mesh. How do I plan on doing this? In a geometry shader with a triangle typology as input, I will generate x vertices based on the surface area of the triangle compared to the total surface area of the object.
For example:
Triangle 1 Surface Area = 2
Total Object Surface Area = 100
=> ratio = 2/100 = 0.02
Total amount of vertices to generate: 1000
=> vertices to generate in this triangle: 0.02 * 1000 => 20.
Then I would need random values to apply the following equations to generate the points inside the triangle:
The problem lies with the random values. I know random isn’t a thing in a geometry shader or at least not as easy to achieve as in C#.
How can I get random numbers?
I know I could perhaps use a noise map, but how does that exactly work then? I know I can use the UV coords from one of the 3 input vertices to get a value from it, but as in the example above, I need 20 different random values.
Should I calculate my random values outside the shader, in C# code and then pass them to the shader?
Perhaps use a custom buffer?
What’s the best way to get random numbers inside a geometry shader? Or should I do it another way?
Is the question “more than one random value within each execution”, or “a random value for each execution”?
If you need a random number for each execution, @Przemyslaw_Zaworski answered that already.
The primitive ID is unique for each execution of the geometry shader (ie: each triangle) per mesh.
If you need multiple random numbers within each execution, just call your rand function multiple times, either with the output random value as the seed, or the original seed plus some arbitrary value.
It was multiple random values within each execution. It think your solution will do.
I added the random position I created using the random in the next random function. But using the actual random as input seems easier.
Thanks both
This is the result of random vertex distribution used as hair roots:
Now I wanna figure out how to get the correct UV at the random point location, because there are white hair roots in black colored uv as shown in picture below:
Basically, I want white hair roots in white parts of the texture, and black hair roots in black parts of the texture. I did some googling first and found out I could perhaps interpolate between the UVs of the triangle the point is generated in: