Skip to content

Commit

Permalink
To keep performance, load gltf binary as compressed texture
Browse files Browse the repository at this point in the history
Uses COMPRESSION_MODE_BASIS_UNIVERSAL.
  • Loading branch information
fire committed Jun 28, 2022
1 parent caa9ec8 commit 7ecee28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
28 changes: 20 additions & 8 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "gltf_state.h"
#include "gltf_texture.h"

#include "core/config/project_settings.h"
#include "core/crypto/crypto_core.h"
#include "core/error/error_macros.h"
#include "core/io/dir_access.h"
Expand All @@ -69,6 +70,7 @@
#include "scene/resources/mesh.h"
#include "scene/resources/multimesh.h"
#include "scene/resources/surface_tool.h"
#include "scene/resources/texture.h"

#include "modules/modules_enabled.gen.h" // For csg, gridmap.

Expand Down Expand Up @@ -3131,6 +3133,7 @@ Error GLTFDocument::_parse_images(Ref<GLTFState> state, const String &p_base_pat
Ref<Texture2D> texture = ResourceLoader::load(uri);
if (texture.is_valid()) {
state->images.push_back(texture);
state->source_images.push_back(texture->get_image());
continue;
} else if (mimetype == "image/png" || mimetype == "image/jpeg") {
// Fallback to loading as byte array.
Expand Down Expand Up @@ -3202,11 +3205,11 @@ Error GLTFDocument::_parse_images(Ref<GLTFState> state, const String &p_base_pat
state->images.push_back(Ref<Texture2D>());
continue;
}

Ref<ImageTexture> t;
Ref<PortableCompressedTexture2D> t;
t.instantiate();
t->create_from_image(img);

state->source_images.push_back(img->duplicate());
img->generate_mipmaps();
t->create_from_image(img, PortableCompressedTexture2D::COMPRESSION_MODE_BASIS_UNIVERSAL);
state->images.push_back(t);
}

Expand Down Expand Up @@ -3266,13 +3269,22 @@ GLTFTextureIndex GLTFDocument::_set_texture(Ref<GLTFState> state, Ref<Texture2D>
return gltf_texture_i;
}

Ref<Texture2D> GLTFDocument::_get_texture(Ref<GLTFState> state, const GLTFTextureIndex p_texture) {
Ref<Texture2D> GLTFDocument::_get_texture(Ref<GLTFState> state, const GLTFTextureIndex p_texture, bool p_is_normal) {
ERR_FAIL_INDEX_V(p_texture, state->textures.size(), Ref<Texture2D>());
const GLTFImageIndex image = state->textures[p_texture]->get_src_image();

ERR_FAIL_INDEX_V(image, state->images.size(), Ref<Texture2D>());

return state->images[image];
if (!p_is_normal) {
return state->images[image];
}
Ref<PortableCompressedTexture2D> portable_texture;
portable_texture.instantiate();
ERR_FAIL_INDEX_V(image, state->source_images.size(), Ref<Texture2D>());
Ref<Image> new_img = state->source_images[image];
ERR_FAIL_NULL_V(new_img, Ref<Texture2D>());
new_img->generate_mipmaps(true);
portable_texture->create_from_image(new_img, PortableCompressedTexture2D::COMPRESSION_MODE_BASIS_UNIVERSAL, true);
return portable_texture;
}

Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
Expand Down Expand Up @@ -3654,7 +3666,7 @@ Error GLTFDocument::_parse_materials(Ref<GLTFState> state) {
if (d.has("normalTexture")) {
const Dictionary &bct = d["normalTexture"];
if (bct.has("index")) {
material->set_texture(BaseMaterial3D::TEXTURE_NORMAL, _get_texture(state, bct["index"]));
material->set_texture(BaseMaterial3D::TEXTURE_NORMAL, _get_texture(state, bct["index"], true));
material->set_feature(BaseMaterial3D::FEATURE_NORMAL_MAPPING, true);
}
if (bct.has("scale")) {
Expand Down
2 changes: 1 addition & 1 deletion modules/gltf/gltf_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class GLTFDocument : public Resource {
const String &p_name);
GLTFTextureIndex _set_texture(Ref<GLTFState> state, Ref<Texture2D> p_texture);
Ref<Texture2D> _get_texture(Ref<GLTFState> state,
const GLTFTextureIndex p_texture);
const GLTFTextureIndex p_texture, bool p_is_normal_texture = false);
Error _parse_json(const String &p_path, Ref<GLTFState> state);
Error _parse_glb(Ref<FileAccess> f, Ref<GLTFState> state);
void _compute_node_heights(Ref<GLTFState> state);
Expand Down
2 changes: 2 additions & 0 deletions modules/gltf/gltf_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class GLTFState : public Resource {
String scene_name;
Vector<int> root_nodes;
Vector<Ref<GLTFTexture>> textures;

Vector<Ref<Texture2D>> images;
Vector<Ref<Image>> source_images;

Vector<Ref<GLTFSkin>> skins;
Vector<Ref<GLTFCamera>> cameras;
Expand Down

0 comments on commit 7ecee28

Please sign in to comment.