Skip to content

Instantly share code, notes, and snippets.

@monxa
Last active November 6, 2023 13:28
Show Gist options
  • Save monxa/5166ad028c466e42007c8e55944db308 to your computer and use it in GitHub Desktop.
Save monxa/5166ad028c466e42007c8e55944db308 to your computer and use it in GitHub Desktop.
inverse uv scaling for compressed mesh data in godot 4.2
// Adjusts UV and UV2 vectors based on provided uv_scale
void adjust_uv_scale(input_data &input, godot::Vector4 uv_scale) {
// If the uv_scale is an empty/default vector, no adjustment is necessary.
if (uv_scale == godot::Vector4())
return;
// Adjust UV coordinates
for (auto &uv : input.uvs) {
// The UV is shifted from a [0.5,1] range to [0,0.5] and then scaled according to the uv_scale.x and uv_scale.y.
// see: godot/servers/rendering_server.cpp:718 (line number might change obviously)
uv = (uv - godot::Vector2(0.5, 0.5)) * godot::Vector2(uv_scale.x, uv_scale.y);
}
// Adjust UV2 coordinates
for (auto &uv2 : input.uv2s) {
// The UV2 is adjusted in the same manner as UV.
// see: servers/rendering_server.cpp:745 (line number might change obviously)
uv2 = (uv2 - godot::Vector2(0.5, 0.5)) * godot::Vector2(uv_scale.x, uv_scale.y);
}
}
@monxa
Copy link
Author

monxa commented Nov 6, 2023

If defined, uv scale can be accessed with:
godot::RenderingServer::get_singleton()->mesh_get_surface(mesh.get_rid(), surface_index)["uv_scale"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment