• 3D
  • How to move UVs programmatically

I have this funny object here:

Its made up of two billboards that represent the same cube from different angles.

I only want to see one cube at a time depending on my camera's viewing angle. I'd like to manipulate the UVs to toggle the cubes on and off as my camera moves.

I assume doing this in the shader is a bit faster than in script.

How can I access and modify the UV data? I don't know how UV data is stored or what it looks like. I assume the coordinates of each vertex of the UV is stored in an array? How can I get this array? Also, I have two cubes. Each cube has four vertices in the UV map. How can I tell which vertex belongs to which cube?

I assume to hide a cube I can somehow set the four vertices to 0, 0, 0, 0?

9 days later

Manipulating UV's is easiest using a custom shader, but I don't think that's a good option for you. If you just want to show/hide an object, you can call the show() and hide() functions on it.

Yeah, I'm not sure what's going on in that image. What is it you're trying to do?

To answer my own question, incase anyone else stumbles upon this: To be clear there is only one object, but its made up of intersecting billboards. So calling show()/hide() would always show or hide both billboards.

To hide only one of the billboards, in the shader I looked at UV.x which represent the percent of distance left to right on the UV texture. I have my UV texture split into 2. The UV of one billboard is in the first half of the texture and the UV of the other billboard is in the second half of the texture. So I can do this: if (UV.x < .5) { discard; } to hide the first billboard and if (UV.x > .5) { discard; } to hide the second billboard.

Watch out tho, if I recall correct, discard is deprecated and might not be there in future versions.

@Megalomaniak thanks for the heads up

@Megalomaniak said: Watch out tho, if I recall correct, discard is deprecated and might not be there in future versions.

I don't remember hearing about discard being deprecated. However, it should be kept in mind that discarding large amounts of pixels can be slow (even if those pixels aren't rendered).

I see. Maybe adjusting alpha would be faster than discard? If it were an alpha scissor it should be pretty fast.

Yeah, I tried googling to double check and I may have been thinking of gl alpha test, where using discard instead might be the solution.

@cybereality said: I see. Maybe adjusting alpha would be faster than discard? If it were an alpha scissor it should be pretty fast.

I believe alpha scissor uses discard internally.

I see, interesting. But since these are whole sprites being hidden (not just pixels) I think discard is not the right method. Simply using visible should work better, no?

Maybe it would be faster, if in GDScript, I changed all four corners of the unwanted billboard UV to (0,0)? Not sure how to do that though