Is there a better way to find the color at a given uv

This is how I do it at the moment.

  1. Find address of texture image
  2. Load image into a dynamicTexture.
  3. Take a uv.x and uv.y and using imageData get r, g, b, a color values for dynamicTexture canvas.

Reason to take a mesh and create a pointcloud from the mesh where each point in the cloud is color matched to the material used on the mesh as in https://www.babylonjs-playground.com/#W92WT0#1

EDIT Corrected PG

For each facet a new internal point in the triangle is created and the uv value for that point is calculated which is then mapped to the image to find the color of the pixel at that point.

Issue is that with the current method you need to find the address of the material the mesh uses to load into a dynamicTexture and the dynamic texture itself is an extra step.

Is there a better way?

Not sure that you need a Dynamic Texture.

Where are the color data ? in the image file. So you need to load the image file.
How is the image applied to the mesh ? according to the UV data. So you need the UVs.
I think then it’s enough.
You just have to access the image data directly from the loaded image (can’t remember the way, but Google may know) at the right UV coordinates.
No need for instanciating an intermediate and useless texture, imho.

Just by curiosity …
Can’t we directly apply a texture to a point cloud if this one has a defined UV array data ?
Maybe this could work directly like it works for standard meshes …?

[quote=“JohnK, post:4, topic:3805”]
Thank you for your pointers @jerome, I have just realised how I totally messed up. The first way was with the PG in my first post which I have now edited with correct one.

In trying out different things I wrongly saved a version of a PG that mixed up two different methods. Like you I though there must be a way with just uvs but the second way I messed up was to push three values at a time into the uv array instead of two. You can see the wrong result I had by looking at #2 in my PGs for this thread.

Here is the result I was after https://www.babylonjs-playground.com/#W92WT0#3 with just uv values

and finally using the material url from the mesh https://www.babylonjs-playground.com/#W92WT0#4

2 Likes

awsome result :slight_smile:
congratulations

1 Like