Skip to content

Commit

Permalink
Fix for textures outside of res:// folder
Browse files Browse the repository at this point in the history
  • Loading branch information
demolke committed Dec 5, 2024
1 parent 9577662 commit 68cd8f0
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3954,8 +3954,8 @@ void GLTFDocument::_parse_image_save_image(Ref<GLTFState> p_state, const Vector<
Vector<uint8_t> img_data = p_image->get_data();
Dictionary generator_parameters;
String file_path;
// If resource_uri is outside of .godot/imported folder, use it.
if (!p_resource_uri.is_empty() && !p_resource_uri.begins_with("res://.godot/imported")) {
// If resource_uri is within res:// folder but outside of .godot/imported folder, use it.
if (!p_resource_uri.is_empty() && !p_resource_uri.begins_with("res://.godot/imported") && !p_resource_uri.begins_with("res://..")) {
file_path = p_resource_uri;
must_import = true;
must_write = !FileAccess::exists(file_path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.2.70",
"version":"2.0"
},
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
1
]
}
],
"nodes":[
{
"mesh":0,
"name":"mesh_instance_3d"
},
{
"children":[
0
],
"name":"_Node3D_6"
}
],
"materials":[
{
"name":"material",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.9999998807907104,
0.9999998807907104,
0.9999998807907104,
1
],
"baseColorTexture":{
"index":0
},
"metallicFactor":0
}
}
],
"meshes":[
{
"name":"Mesh_0",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
},
"indices":3,
"material":0
}
]
}
],
"textures":[
{
"sampler":0,
"source":0
}
],
"images":[
{
"mimeType":"image/png",
"name":"material_albedo000",
"uri":"../texture.png",
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":4,
"max":[
1,
0,
1
],
"min":[
-1,
0,
-1
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":4,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":4,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5123,
"count":6,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":48,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":48,
"byteOffset":48,
"target":34962
},
{
"buffer":0,
"byteLength":32,
"byteOffset":96,
"target":34962
},
{
"buffer":0,
"byteLength":12,
"byteOffset":128,
"target":34963
}
],
"samplers":[
{
"magFilter":9729,
"minFilter":9987
}
],
"buffers":[
{
"byteLength":140,
"uri":"data:application/octet-stream;base64,AACAPwAAAAAAAIA/AACAvwAAAAAAAIA/AACAPwAAAAAAAIC/AACAvwAAAAAAAIC/AAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAACAPwAAgD8AAAAAAAAAAAAAAAACAAEAAAACAAMAAQA="
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions modules/gltf/tests/test_gltf_images.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ TEST_CASE("[SceneTree][Node][Editor] Import GLTF from .godot/imported folder wit
memdelete(efs);
}

TEST_CASE("[SceneTree][Node][Editor] Import GLTF with texture outside of res:// directory") {
init("gltf_pointing_to_texture_outside_of_res_folder", "res://");

EditorFileSystem *efs = memnew(EditorFileSystem);
EditorResourcePreview *erp = memnew(EditorResourcePreview);

// Copy texture to the parent folder of res:// - i.e. to res://.. where we can't import from.
String oneup = TestUtils::get_temp_path("texture.png");
Error err;
Ref<FileAccess> output = FileAccess::open(oneup, FileAccess::WRITE, &err);
CHECK_MESSAGE(err == OK, "Unable to open texture file");
output->store_buffer(FileAccess::get_file_as_bytes("res://texture_source.png"));
output->close();

Node *loaded = gltf_import("res://gltf_pointing_to_texture_outside_of_res_folder.gltf");
Ref<Texture2D> texture = _check_texture(loaded);

// Imports of gltf with texture from outside of res:// folder should end up being copied to res://
CHECK_MESSAGE(texture->get_path() == "res://gltf_pointing_to_texture_outside_of_res_folder_material_albedo000.png", "Texture not parsed as resource.");

memdelete(loaded);
memdelete(erp);
memdelete(efs);
}

TEST_CASE("[SceneTree][Node][Editor] Import GLTF with embedded texture, check how it got extracted") {
init("gltf_embedded_texture", "res://");

Expand Down

0 comments on commit 68cd8f0

Please sign in to comment.