I’m trying to get the area of two figures, but the result is slightly delayed and I can’t get it right away and use it somehow in the code. I need to somehow wait for it to work, after completing `onAfterUnbindObservable’. How can this be achieved?

Is it also possible to somehow do the whole pixel counting process differently? Or, at least without re-creating rtt?

Advance thanks)

You can’t get the RTT pixels right away because it must be rendered first. Also, the materials will have to be compiled before they can be used to render the sphere in the RTT (this process is asynchronous).

There’s really no way around doing it after the scene has been rendered. Also, note that `readPixels`

is asynchronous and you will have some delay because of that (one or several frames delay).

Thanks for the answer!

Is there then some other way to get the area of the figure? It’s already essentially rendered. Or some way to compare two figures. In my case, I have two flat polygons and I need to know which one has more pixels.

What you can do is projecting the vertices of your polygon with something like:

```
const p = BABYLON.Vector3.Project(vertex, mesh.getWorldMatrix(), scene.getTransformMatrix(), { x:0, y:0, width: engine.getRenderWidth(), height: engine.getRenderHeight() });
```

and then use some maths formula to compute the area of this 2D polygon (you can throw away the z coordinate of p, you only need the x and y coordinates).

2 Likes