How to add uv1 of mesh using script?

I’m trying to add uv1 through script for the meshes that do not have a uv1 channel. I check the vertex buffer for uv1, and if uv1 is not present. I manually try to add them via script. But I’m not getting results.

mesh.setUvs(1, uv1data);

How I can add the uv1 using script for the meshes that do not have UV1 by default, I’m importing the meshes on runtime and I have to add uv1 to them manually.

Hi @PC_Coder,

I haven’t done this myself but I think you also need to update the vertex/index buffers for the new mesh data to get uploaded to the GPU:

mesh.setUvs(1,uv1data);
mesh.update();

Check these two engine examples on how they do this:

1 Like

If you only do this

mesh.setUvs(1,uv1data);
mesh.update();

your mesh will only have uv1 stream and nothing else.

You need to first get existing streams from mesh. Say

var pos = [];
mesh.getPositions(pos);
var nrm = [];
mesh.getNormals(nrm);
var uv0 = [];
mesh.getUvs(0, uv0);

and then rebuild the mesh with those and additional streams

mesh.clear();
mesh.setPositions(pos);
mesh.setNormals(nrm);
mesh.setUvs(0, uv0);
mesh.setUvs(1, uv1);
mesh.update();
4 Likes

Thanks, it solved the problem. I can see the UV1 now but on the other hand, it has messed up I think my geometrical data, I see half cube on screen in shape (half triangle), why position or vertices are not working properly now?

Why I dont see full cube?

you probably need to call getIndices and setIndices as well.

1 Like

Thanks its working now :slight_smile:

1 Like