test_gltf_images.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**************************************************************************/
  2. /* test_gltf_images.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_GLTF_IMAGES_H
  31. #define TEST_GLTF_IMAGES_H
  32. #include "test_gltf.h"
  33. #ifdef TOOLS_ENABLED
  34. #include "editor/editor_file_system.h"
  35. #include "editor/editor_paths.h"
  36. #include "scene/resources/image_texture.h"
  37. namespace TestGltf {
  38. Ref<Texture2D> _check_texture(Node *p_node) {
  39. MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(p_node->find_child("mesh_instance_3d", true, true));
  40. Ref<StandardMaterial3D> material = mesh_instance_3d->get_active_material(0);
  41. Ref<Texture2D> texture = material->get_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO);
  42. CHECK_MESSAGE(texture->get_size().x == 2, "Texture width not correct.");
  43. CHECK_MESSAGE(texture->get_size().y == 2, "Texture height not correct.");
  44. // Check if the loaded texture pixels are exactly as we expect.
  45. for (int x = 0; x < 2; ++x) {
  46. for (int y = 0; y < 2; ++y) {
  47. Color c = texture->get_image()->get_pixel(x, y);
  48. CHECK_MESSAGE(c == Color(x, y, y), "Texture content is incorrect.");
  49. }
  50. }
  51. return texture;
  52. }
  53. TEST_CASE("[SceneTree][Node] Export GLTF with external texture and import") {
  54. init("gltf_images_external_export_import");
  55. // Setup scene.
  56. Ref<ImageTexture> original_texture;
  57. original_texture.instantiate();
  58. Ref<Image> image;
  59. image.instantiate();
  60. image->initialize_data(2, 2, false, Image::FORMAT_RGBA8);
  61. for (int x = 0; x < 2; ++x) {
  62. for (int y = 0; y < 2; ++y) {
  63. image->set_pixel(x, y, Color(x, y, y));
  64. }
  65. }
  66. original_texture->set_image(image);
  67. Ref<StandardMaterial3D> original_material;
  68. original_material.instantiate();
  69. original_material->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, original_texture);
  70. original_material->set_name("material");
  71. Ref<PlaneMesh> original_meshdata;
  72. original_meshdata.instantiate();
  73. original_meshdata->set_name("planemesh");
  74. original_meshdata->surface_set_material(0, original_material);
  75. MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);
  76. original_mesh_instance->set_mesh(original_meshdata);
  77. original_mesh_instance->set_name("mesh_instance_3d");
  78. Node3D *original = memnew(Node3D);
  79. SceneTree::get_singleton()->get_root()->add_child(original);
  80. original->add_child(original_mesh_instance);
  81. original->set_owner(SceneTree::get_singleton()->get_root());
  82. original_mesh_instance->set_owner(SceneTree::get_singleton()->get_root());
  83. // Convert to GLFT and back.
  84. Node *loaded = gltf_export_then_import(original, "gltf_images");
  85. _check_texture(loaded);
  86. memdelete(original_mesh_instance);
  87. memdelete(original);
  88. memdelete(loaded);
  89. }
  90. TEST_CASE("[SceneTree][Node][Editor] Import GLTF from .godot/imported folder with external texture") {
  91. init("gltf_placed_in_dot_godot_imported", "res://.godot/imported");
  92. EditorFileSystem *efs = memnew(EditorFileSystem);
  93. EditorResourcePreview *erp = memnew(EditorResourcePreview);
  94. Node *loaded = gltf_import("res://.godot/imported/gltf_placed_in_dot_godot_imported.gltf");
  95. Ref<Texture2D> texture = _check_texture(loaded);
  96. // In-editor imports of gltf and texture from .godot/imported folder should end up in res:// if extract_path is defined.
  97. CHECK_MESSAGE(texture->get_path() == "res://gltf_placed_in_dot_godot_imported_material_albedo000.png", "Texture not parsed as resource.");
  98. memdelete(loaded);
  99. memdelete(erp);
  100. memdelete(efs);
  101. }
  102. TEST_CASE("[SceneTree][Node][Editor] Import GLTF with texture outside of res:// directory") {
  103. init("gltf_pointing_to_texture_outside_of_res_folder", "res://");
  104. EditorFileSystem *efs = memnew(EditorFileSystem);
  105. EditorResourcePreview *erp = memnew(EditorResourcePreview);
  106. // Copy texture to the parent folder of res:// - i.e. to res://.. where we can't import from.
  107. String oneup = TestUtils::get_temp_path("texture.png");
  108. Error err;
  109. Ref<FileAccess> output = FileAccess::open(oneup, FileAccess::WRITE, &err);
  110. CHECK_MESSAGE(err == OK, "Unable to open texture file.");
  111. output->store_buffer(FileAccess::get_file_as_bytes("res://texture_source.png"));
  112. output->close();
  113. Node *loaded = gltf_import("res://gltf_pointing_to_texture_outside_of_res_folder.gltf");
  114. Ref<Texture2D> texture = _check_texture(loaded);
  115. // Imports of gltf with texture from outside of res:// folder should end up being copied to res://
  116. CHECK_MESSAGE(texture->get_path() == "res://gltf_pointing_to_texture_outside_of_res_folder_material_albedo000.png", "Texture not parsed as resource.");
  117. memdelete(loaded);
  118. memdelete(erp);
  119. memdelete(efs);
  120. }
  121. TEST_CASE("[SceneTree][Node][Editor] Import GLTF with embedded texture, check how it got extracted") {
  122. init("gltf_embedded_texture", "res://");
  123. EditorFileSystem *efs = memnew(EditorFileSystem);
  124. EditorResourcePreview *erp = memnew(EditorResourcePreview);
  125. Node *loaded = gltf_import("res://embedded_texture.gltf");
  126. Ref<Texture2D> texture = _check_texture(loaded);
  127. // In-editor imports of texture embedded in file should end up with a resource.
  128. CHECK_MESSAGE(texture->get_path() == "res://embedded_texture_material_albedo000.png", "Texture not parsed as resource.");
  129. memdelete(loaded);
  130. memdelete(erp);
  131. memdelete(efs);
  132. }
  133. } //namespace TestGltf
  134. #endif // TOOLS_ENABLED
  135. #endif // TEST_GLTF_IMAGES_H