# PointsCloudSystem: Unequal distribution of points

**URL:** https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981
**Category:** Bugs
**Created:** [March 22, 2024, 9:08pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981 "2024-03-22T21:08:02Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![hcschuetz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hcschuetz/32/46493_2.png) [@hcschuetz](https://forum.babylonjs.com/u/hcschuetz)
#### Post date: [March 22, 2024, 9:08pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/1 "2024-03-22T21:08:03Z")

</div>

I see several issues with PointsCloudSystem.

Example:

> **[Babylon.js Playground](https://playground.babylonjs.com/#UI95UC%232446)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

1. Points are unevenly distributed **within** a triangle. I haven’t tried it, but I am pretty sure that this can be easily fixed by changing [this line](https://github.com/BabylonJS/Babylon.js/blob/ec187b04b424df562cb6ff9d0aa1b6a47b9fd8eb/packages/dev/core/src/Particles/pointsCloudSystem.ts#L402) to “`lamda = Math.sqrt(Scalar.RandomRange(0, 1));`”.  
(See also the Wikipedia page on [triangular distributions](https://en.wikipedia.org/wiki/Triangular_distribution#Generating_triangular-distributed_random_variates).)

2. If the mesh contains many small triangles, points are unevenly distributed **across** triangles.  
To see this, change the the number of segments in the playground example from 1 to 32.  
Then we get no particles around the south pole but a high density around the north pole.  
This is due to some hacky/broken logic in method `_calculateDensity(...)`:  
First the density for the small triangles around the poles is floored to 0 (line 603) and near the end of the method (line 615) the `extraPoints` are distributed over the first few triangles, which happen to be around the north pole.  
A cleaner solution would work like this:  
Whenever the division in line 603 produces a non-integer quotient, we assign the floor or the ceiling of that quotient, with a probability given by the fractional part.  
Example: Assume the quotient is 4.56. Then we assign 4 points for sure and an additional point with a probability of 56%.  
A straight-forward implementation of this approach (where the probabilistic decisions for different triangles are independent) may lead to a total number of points that is a little to high or a little too low. But the approach can be refined to avoid this.  
(As an alternative, we could give up the requirement that the given number of points must be obeyed precisely. The given number would just be the expected mean number of points. We could use a Poisson distribution with that number as its parameter. The advantage would be that a Poisson distribution can be broken down from the mesh to the triangles very easily. In our example above we would simply draw a [Poisson-distributed random variable](https://en.wikipedia.org/wiki/Poisson_distribution#Computational_methods) with parameter 4.56. )

**If you are interested, I would volunteer to implement fixes for issues 1 and 2.**

And while we are at this class already, I have some more remarks (which are, however, not bugs):

1. The computation of **triangle areas** in lines 590 to 597 can be simplified. The area is just half the length of the cross product of vec0 and vec1.
2. When I move the camera closer to my mesh I would have expected the particles to grow. Similarly when moving the camera away, the particles should IMHO be rendered smaller. But actually the **particles keep their “rendered size”**. This makes the “intuitive density” unnaturally dependent on the camera distance. Did I miss a flag for changing this behavior? Or is the current behavior actually intended?
3. The code for **filling a volume** also feels quite hacky. But admittedly I do not have a truly better solution.  
One approach would be to just randomly fill the mesh’s bounding box with points and to drop those points that are outside the mesh (recognizable by an odd number of intersections between the mesh and the line from the point to some fixed “central point”). This would probably work quite well for compact closed shapes but would be inefficient for other shapes. (Actually intersection detection is what GPUs are good at.)

So as I wrote above, I could fix issues 1 and 2 if there is interest in having this fixed. And I would also like to know what you think about issues 3, 4, and 5.

Regards,  
Heribert

---

<div class="post-metadata">

### Author: ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)
#### Post date: [March 22, 2024, 9:17pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/2 "2024-03-22T21:17:22Z")

</div>

The point cloud was develop by a great person who unfortunately is no more with us.

I’m sure he would be so happy if the community could continue his work.

So by all means: please send a PR, I’ll be happy to review and merge it:)

---

<div class="post-metadata">

### Author: ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)
#### Post date: [March 22, 2024, 9:18pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/3 "2024-03-22T21:18:11Z")

</div>

Regarding 4: yes this is the expected behavior but I understand your point as well. We could think about adding that feature if someone is motivated 🙂

---

<div class="post-metadata">

### Author: ![hcschuetz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hcschuetz/32/46493_2.png) [@hcschuetz](https://forum.babylonjs.com/u/hcschuetz)
#### Post date: [March 23, 2024, 3:22am UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/4 "2024-03-23T03:22:25Z")

</div>

> [@Deltakosh](#):
>
> So by all means: please send a PR, I’ll be happy to review and merge it:)

OK, I started with issue 1. Got a fork of Babylon on Github and a clone on my machine. Got the development environment to run on my machine, made my 1-line change, opened [http://localhost:1337/#UI95UC#2446](http://localhost:1337/#UI95UC#2446), found that it looks better than in the playground.

(BTW, [Babylon.js docs](https://doc.babylonjs.com/contribute/toBabylon/HowToContribute#debug-a-playground-snippet) says that playground examples can be tested at “localhost:1338#…”. That did not work for me. I had to use port 1337 for this. Is this a mistake in the docs?)

Committed, merged with an upstream change, pushed, created a pull request: [fix in PointsCloudSystem: uniform distribution in triangle by hcschuetz · Pull Request #14900 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/14900).

The snapshot test under [https://playground.babylonjs.com/?snapshot=refs/pull/14900/merge#UI95UC#2446](https://playground.babylonjs.com/?snapshot=refs/pull/14900/merge#UI95UC#2446) also looks good.

Now I am having two problems with this pull request. Could you please give me some advice?

(a) bjsplat (a robot?) asked me to label my PR with “bug” (or some other values that do not apply). [Github documentation](https://docs.github.com/en/issues/using-labels-and-milestones-to-track-work/managing-labels#applying-a-label) tells me to click “Labels” in the right sidebar and then to select a label. But unfortunately nothing happens when I click “Labels” in the right sidebar of the PR. This is apparently not an active element. Do you have any idea?

(b) Two of the automatic tests were not successful. But the details only say “Bash exited with code ‘1’”. Not so helpful. Can I ignore this?

(One more note: I temporarily switched the PR to “draft”, hoping that this might solve problem (a). It didn’t. But could that be the reason for the test failure (b)?)

Anyway, could you have a look at the PR? Then I’ll work on issue 2 tomorrow.

---

<div class="post-metadata">

### Author: ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)
#### Post date: [March 23, 2024, 3:38am UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/5 "2024-03-23T03:38:53Z")

</div>

I will review it on Monday 🙂  
@RaananW for the doc error:)

Regarding the label this is for me, I will do it

The visualization test is this one: [Playwright Test Report](https://babylonsnapshots.z22.web.core.windows.net/refs/pull/14900/merge/testResults/webgl2playwright/index.html#?testId=f30d275d57169c9bf0ff-a77bf7fe22fe3ae62166)

This seems unrelated to your PR (any clue @RaananW ?)

---

<div class="post-metadata">

### Author: ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)
#### Post date: [March 23, 2024, 6:51am UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/6 "2024-03-23T06:51:19Z")

</div>

The test passed, so i don’t see an issue there. Most have been a temporary asset loading issue, because i don’t remember this test in the flaky list. Is it new?

---

<div class="post-metadata">

### Author: ![hcschuetz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hcschuetz/32/46493_2.png) [@hcschuetz](https://forum.babylonjs.com/u/hcschuetz)
#### Post date: [March 23, 2024, 5:37pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/7 "2024-03-23T17:37:25Z")

</div>

So here is the PR for issue 2:

> <https://github.com/BabylonJS/Babylon.js/pull/14902>
>
> See issue 2 in https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribut…ion-of-points/48981
> 
> usage example: https://playground.babylonjs.com/#UI95UC#2447

It fixes the problem that there are no particles around the south pole and to many particles near the north pole in this example:

> **[Babylon.js Playground](https://playground.babylonjs.com/#UI95UC%232447)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Here is the fixed view:  
[https://babylonsnapshots.z22.web.core.windows.net/refs/pull/14902/merge/index.html#UI95UC#2447](https://babylonsnapshots.z22.web.core.windows.net/refs/pull/14902/merge/index.html#UI95UC#2447)

This time the automated tests succeeded.

---

<div class="post-metadata">

### Author: ![hcschuetz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hcschuetz/32/46493_2.png) [@hcschuetz](https://forum.babylonjs.com/u/hcschuetz)
#### Post date: [March 23, 2024, 6:02pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/8 "2024-03-23T18:02:45Z")

</div>

…aaand another PR for issue 3. This one is not a bug fix, just a code simplification. I don’t know your policy regarding mere cleanup changes.

> <https://github.com/BabylonJS/Babylon.js/pull/14903>
>
> See issue 3 in https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribut…ion-of-points/48981
> 
> No semantic change.
> 
> Usage example: https://playground.babylonjs.com/#UI95UC#2447

This PR might conflict the one for issue 2. So I would re-base it once the PR for issue 2 is merged.

And a side note: I am quite impressed by your development and project-management environment. In particular the automatically created playground clones for pull requests are really cool!

---

<div class="post-metadata">

### Author: ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)
#### Post date: [March 25, 2024, 4:16pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/9 "2024-03-25T16:16:18Z")

</div>

All merged! you rock! thanks a ton for these updates!

Much appreciated

---

<div class="post-metadata">

### Author: ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)
#### Post date: [March 25, 2024, 4:16pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/10 "2024-03-25T16:16:37Z")

</div>

> [@hcschuetz](#):
>
> I am quite impressed by your development and project-management environment. In particular the automatically created playground clones for pull requests are really cool!

All credits to @RaananW

---

<div class="post-metadata">

### Author: ![hcschuetz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hcschuetz/32/46493_2.png) [@hcschuetz](https://forum.babylonjs.com/u/hcschuetz)
#### Post date: [May 8, 2025, 1:53pm UTC](https://forum.babylonjs.com/t/pointscloudsystem-unequal-distribution-of-points/48981/11 "2025-05-08T13:53:14Z")

</div>

Just for the record, no action required:

[Today I learned](https://phanpy.social/#/mastodon.social/s/114472001783809879?view=full) that distributions like the ones discussed in this thread are actually called “[hyperuniform](https://en.wikipedia.org/wiki/Hyperuniformity)”.
