From 9248182a0b09730954e8ba06204e794dff002a7d Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 2 Nov 2022 20:46:56 +0100 Subject: [PATCH] assimp: Support texture transforms --- pandatool/src/assimp/assimpLoader.cxx | 37 ++++++++++++++++++++++++--- pandatool/src/assimp/assimpLoader.h | 3 ++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/pandatool/src/assimp/assimpLoader.cxx b/pandatool/src/assimp/assimpLoader.cxx index e2c0ff63d5..5725bf85a8 100644 --- a/pandatool/src/assimp/assimpLoader.cxx +++ b/pandatool/src/assimp/assimpLoader.cxx @@ -35,6 +35,8 @@ #include "animBundleNode.h" #include "animChannelMatrixXfmTable.h" #include "pvector.h" +#include "cmath.h" +#include "deg_2_rad.h" #include "pandaIOSystem.h" #include "pandaLogger.h" @@ -285,7 +287,8 @@ load_texture(size_t index) { * Converts an aiMaterial into a RenderState. */ void AssimpLoader:: -load_texture_stage(const aiMaterial &mat, const aiTextureType &ttype, CPT(TextureAttrib) &tattr) { +load_texture_stage(const aiMaterial &mat, const aiTextureType &ttype, + CPT(TextureAttrib) &tattr, CPT(TexMatrixAttrib) &tmattr) { aiString path; aiTextureMapping mapping; unsigned int uvindex; @@ -402,6 +405,30 @@ load_texture_stage(const aiMaterial &mat, const aiTextureType &ttype, CPT(Textur } tattr = DCAST(TextureAttrib, tattr->add_on_stage(stage, ptex)); + + // Is there a texture transform? + aiUVTransform transform; + if (AI_SUCCESS == mat.Get(AI_MATKEY_UVTRANSFORM(ttype, i), transform)) { + // Reconstruct the original origin from the glTF file. + PN_stdfloat rcos, rsin; + csincos(-transform.mRotation, &rsin, &rcos); + transform.mTranslation.x -= (0.5 * transform.mScaling.x) * (-rcos + rsin + 1); + transform.mTranslation.y -= ((0.5 * transform.mScaling.y) * (rsin + rcos - 1)) + 1 - transform.mScaling.y; + + LMatrix3 matrix = + LMatrix3::translate_mat(0, -1) * + LMatrix3::scale_mat(transform.mScaling.x, transform.mScaling.y) * + LMatrix3::rotate_mat(rad_2_deg(-transform.mRotation)) * + LMatrix3::translate_mat(transform.mTranslation.x, 1 + transform.mTranslation.y); + + CPT(TransformState) cstate = + TransformState::make_mat3(matrix); + + CPT(RenderAttrib) new_attr = (tmattr == nullptr) + ? TexMatrixAttrib::make(stage, std::move(cstate)) + : tmattr->add_stage(stage, std::move(cstate)); + tmattr = DCAST(TexMatrixAttrib, std::move(new_attr)); + } } } } @@ -477,11 +504,15 @@ load_material(size_t index) { // And let's not forget the textures! CPT(TextureAttrib) tattr = DCAST(TextureAttrib, TextureAttrib::make()); - load_texture_stage(mat, aiTextureType_DIFFUSE, tattr); - load_texture_stage(mat, aiTextureType_LIGHTMAP, tattr); + CPT(TexMatrixAttrib) tmattr; + load_texture_stage(mat, aiTextureType_DIFFUSE, tattr, tmattr); + load_texture_stage(mat, aiTextureType_LIGHTMAP, tattr, tmattr); if (tattr->get_num_on_stages() > 0) { state = state->add_attrib(tattr); } + if (tmattr != nullptr) { + state = state->add_attrib(tmattr); + } _mat_states[index] = state; } diff --git a/pandatool/src/assimp/assimpLoader.h b/pandatool/src/assimp/assimpLoader.h index 3133fee377..da60c36a3f 100644 --- a/pandatool/src/assimp/assimpLoader.h +++ b/pandatool/src/assimp/assimpLoader.h @@ -72,7 +72,8 @@ private: const aiNode *find_node(const aiNode &root, const aiString &name); void load_texture(size_t index); - void load_texture_stage(const aiMaterial &mat, const aiTextureType &ttype, CPT(TextureAttrib) &tattr); + void load_texture_stage(const aiMaterial &mat, const aiTextureType &ttype, + CPT(TextureAttrib) &tattr, CPT(TexMatrixAttrib) &tmattr); void load_material(size_t index); void create_joint(Character *character, CharacterJointBundle *bundle, PartGroup *parent, const aiNode &node); void create_anim_channel(const aiAnimation &anim, AnimBundle *bundle, AnimGroup *parent, const aiNode &node);