TextureTransform: set material transform only when the extension is provided.

pull/2764/head
Kim Kulling 2019-11-16 08:08:57 +01:00
parent 4ad2368116
commit 2eed8b1820
3 changed files with 1001 additions and 1039 deletions

View File

@ -686,9 +686,12 @@ namespace glTF2
unsigned int index;
unsigned int texCoord = 0;
bool textureTransformSupported = false;
struct TextureTransformExt {
float offset[2];
float rotation;
float scale[2];
} TextureTransformExt_t;
};
struct NormalTextureInfo : TextureInfo

View File

@ -803,25 +803,26 @@ namespace {
inline void SetTextureProperties(Asset& r, Value* prop, TextureInfo& out) {
if (r.extensionsUsed.KHR_texture_transform) {
if (Value *extensions = FindObject(*prop, "extensions")) {
out.textureTransformSupported = true;
if (Value *pKHR_texture_transform = FindObject(*extensions, "KHR_texture_transform")) {
if (Value *array = FindArray(*pKHR_texture_transform, "offset")) {
out.offset[0] = (*array)[0].GetFloat();
out.offset[1] = (*array)[1].GetFloat();
out.TextureTransformExt_t.offset[0] = (*array)[0].GetFloat();
out.TextureTransformExt_t.offset[1] = (*array)[1].GetFloat();
} else {
out.offset[0] = 0;
out.offset[1] = 0;
out.TextureTransformExt_t.offset[0] = 0;
out.TextureTransformExt_t.offset[1] = 0;
}
if (!ReadMember(*pKHR_texture_transform, "rotation", out.rotation)) {
out.rotation = 0;
if (!ReadMember(*pKHR_texture_transform, "rotation", out.TextureTransformExt_t.rotation)) {
out.TextureTransformExt_t.rotation = 0;
}
if (Value *array = FindArray(*pKHR_texture_transform, "scale")) {
out.scale[0] = (*array)[0].GetFloat();
out.scale[1] = (*array)[1].GetFloat();
out.TextureTransformExt_t.scale[0] = (*array)[0].GetFloat();
out.TextureTransformExt_t.scale[1] = (*array)[1].GetFloat();
} else {
out.scale[0] = 1;
out.scale[1] = 1;
out.TextureTransformExt_t.scale[0] = 1;
out.TextureTransformExt_t.scale[1] = 1;
}
}
}

View File

@ -43,18 +43,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef ASSIMP_BUILD_NO_GLTF_IMPORTER
#include "glTF2/glTF2Importer.h"
#include "PostProcessing/MakeVerboseFormat.h"
#include "glTF2/glTF2Asset.h"
#include "glTF2/glTF2AssetWriter.h"
#include "PostProcessing/MakeVerboseFormat.h"
#include <assimp/CreateAnimMesh.h>
#include <assimp/StringComparison.h>
#include <assimp/StringUtils.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/ai_assert.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/importerdesc.h>
#include <assimp/CreateAnimMesh.h>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/Importer.hpp>
#include <memory>
#include <unordered_map>
@ -91,11 +91,11 @@ static const aiImporterDesc desc = {
"gltf glb"
};
glTF2Importer::glTF2Importer()
: BaseImporter()
, meshOffsets()
, embeddedTexIdxs()
, mScene( NULL ) {
glTF2Importer::glTF2Importer() :
BaseImporter(),
meshOffsets(),
embeddedTexIdxs(),
mScene(NULL) {
// empty
}
@ -103,13 +103,11 @@ glTF2Importer::~glTF2Importer() {
// empty
}
const aiImporterDesc* glTF2Importer::GetInfo() const
{
const aiImporterDesc *glTF2Importer::GetInfo() const {
return &desc;
}
bool glTF2Importer::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool /* checkSig */) const
{
bool glTF2Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const {
const std::string &extension = GetExtension(pFile);
if (extension != "gltf" && extension != "glb")
@ -125,8 +123,7 @@ bool glTF2Importer::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool
return false;
}
static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode)
{
static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) {
switch (gltfWrapMode) {
case SamplerWrap::Mirrored_Repeat:
return aiTextureMapMode_Mirror;
@ -180,22 +177,19 @@ static void CopyValue(const glTF2::vec4& v, aiQuaternion& out)
o.a4 = v[12]; o.b4 = v[13]; o.c4 = v[14]; o.d4 = v[15];
}*/
inline void SetMaterialColorProperty(Asset& /*r*/, vec4& prop, aiMaterial* mat, const char* pKey, unsigned int type, unsigned int idx)
{
inline void SetMaterialColorProperty(Asset & /*r*/, vec4 &prop, aiMaterial *mat, const char *pKey, unsigned int type, unsigned int idx) {
aiColor4D col;
CopyValue(prop, col);
mat->AddProperty(&col, 1, pKey, type, idx);
}
inline void SetMaterialColorProperty(Asset& /*r*/, vec3& prop, aiMaterial* mat, const char* pKey, unsigned int type, unsigned int idx)
{
inline void SetMaterialColorProperty(Asset & /*r*/, vec3 &prop, aiMaterial *mat, const char *pKey, unsigned int type, unsigned int idx) {
aiColor4D col;
glTFCommon::CopyValue(prop, col);
mat->AddProperty(&col, 1, pKey, type, idx);
}
inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& /*r*/, glTF2::TextureInfo prop, aiMaterial* mat, aiTextureType texType, unsigned int texSlot = 0)
{
inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset & /*r*/, glTF2::TextureInfo prop, aiMaterial *mat, aiTextureType texType, unsigned int texSlot = 0) {
if (prop.texture && prop.texture->source) {
aiString uri(prop.texture->source->uri);
@ -206,13 +200,15 @@ inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset&
uri.length = 1 + ASSIMP_itoa10(uri.data + 1, MAXLEN - 1, texIdx);
}
if (prop.textureTransformSupported) {
aiUVTransform transform;
transform.mTranslation.x = prop.offset[0];
transform.mTranslation.y = prop.offset[0];
transform.mRotation = prop.rotation;
transform.mScaling.x = prop.scale[0];
transform.mScaling.y = prop.scale[1];
transform.mTranslation.x = prop.TextureTransformExt_t.offset[0];
transform.mTranslation.y = prop.TextureTransformExt_t.offset[0];
transform.mRotation = prop.TextureTransformExt_t.rotation;
transform.mScaling.x = prop.TextureTransformExt_t.scale[0];
transform.mScaling.y = prop.TextureTransformExt_t.scale[1];
mat->AddProperty(&transform, 1, _AI_MATKEY_UVTRANSFORM_BASE, texType, texSlot);
}
mat->AddProperty(&uri, AI_MATKEY_TEXTURE(texType, texSlot));
mat->AddProperty(&prop.texCoord, 1, _AI_MATKEY_GLTF_TEXTURE_TEXCOORD_BASE, texType, texSlot);
@ -242,8 +238,7 @@ inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset&
}
}
inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& r, glTF2::NormalTextureInfo& prop, aiMaterial* mat, aiTextureType texType, unsigned int texSlot = 0)
{
inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset &r, glTF2::NormalTextureInfo &prop, aiMaterial *mat, aiTextureType texType, unsigned int texSlot = 0) {
SetMaterialTextureProperty(embeddedTexIdxs, r, (glTF2::TextureInfo)prop, mat, texType, texSlot);
if (prop.texture && prop.texture->source) {
@ -251,8 +246,7 @@ inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset&
}
}
inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& r, glTF2::OcclusionTextureInfo& prop, aiMaterial* mat, aiTextureType texType, unsigned int texSlot = 0)
{
inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset &r, glTF2::OcclusionTextureInfo &prop, aiMaterial *mat, aiTextureType texType, unsigned int texSlot = 0) {
SetMaterialTextureProperty(embeddedTexIdxs, r, (glTF2::TextureInfo)prop, mat, texType, texSlot);
if (prop.texture && prop.texture->source) {
@ -260,8 +254,7 @@ inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset&
}
}
static aiMaterial* ImportMaterial(std::vector<int>& embeddedTexIdxs, Asset& r, Material& mat)
{
static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, Material &mat) {
aiMaterial *aimat = new aiMaterial();
if (!mat.name.empty()) {
@ -319,8 +312,7 @@ static aiMaterial* ImportMaterial(std::vector<int>& embeddedTexIdxs, Asset& r, M
return aimat;
}
void glTF2Importer::ImportMaterials(glTF2::Asset& r)
{
void glTF2Importer::ImportMaterials(glTF2::Asset &r) {
const unsigned int numImportedMaterials = unsigned(r.materials.Size());
Material defaultMaterial;
@ -333,24 +325,20 @@ void glTF2Importer::ImportMaterials(glTF2::Asset& r)
}
}
static inline void SetFace(aiFace& face, int a)
{
static inline void SetFace(aiFace &face, int a) {
face.mNumIndices = 1;
face.mIndices = new unsigned int[1];
face.mIndices[0] = a;
}
static inline void SetFace(aiFace& face, int a, int b)
{
static inline void SetFace(aiFace &face, int a, int b) {
face.mNumIndices = 2;
face.mIndices = new unsigned int[2];
face.mIndices[0] = a;
face.mIndices[1] = b;
}
static inline void SetFace(aiFace& face, int a, int b, int c)
{
static inline void SetFace(aiFace &face, int a, int b, int c) {
face.mNumIndices = 3;
face.mIndices = new unsigned int[3];
face.mIndices[0] = a;
@ -359,8 +347,7 @@ static inline void SetFace(aiFace& face, int a, int b, int c)
}
#ifdef ASSIMP_BUILD_DEBUG
static inline bool CheckValidFacesIndices(aiFace* faces, unsigned nFaces, unsigned nVerts)
{
static inline bool CheckValidFacesIndices(aiFace *faces, unsigned nFaces, unsigned nVerts) {
for (unsigned i = 0; i < nFaces; ++i) {
for (unsigned j = 0; j < faces[i].mNumIndices; ++j) {
unsigned idx = faces[i].mIndices[j];
@ -372,8 +359,7 @@ static inline bool CheckValidFacesIndices(aiFace* faces, unsigned nFaces, unsign
}
#endif // ASSIMP_BUILD_DEBUG
void glTF2Importer::ImportMeshes(glTF2::Asset& r)
{
void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
std::vector<aiMesh *> meshes;
unsigned int k = 0;
@ -414,7 +400,6 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
case PrimitiveMode_TRIANGLE_FAN:
aim->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
break;
}
Mesh::Primitive::Attributes &attr = prim.attributes;
@ -516,7 +501,6 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
}
}
aiFace *faces = 0;
size_t nFaces = 0;
@ -580,13 +564,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < nFaces; ++i) {
//The ordering is to ensure that the triangles are all drawn with the same orientation
if ((i + 1) % 2 == 0)
{
if ((i + 1) % 2 == 0) {
//For even n, vertices n + 1, n, and n + 2 define triangle n
SetFace(faces[i], data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2));
}
else
{
} else {
//For odd n, vertices n, n+1, and n+2 define triangle n
SetFace(faces[i], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2));
}
@ -602,8 +583,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
}
break;
}
}
else { // no indices provided so directly generate from counts
} else { // no indices provided so directly generate from counts
// use the already determined count as it includes checks
unsigned int count = aim->mNumVertices;
@ -662,13 +642,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < nFaces; ++i) {
//The ordering is to ensure that the triangles are all drawn with the same orientation
if ((i+1) % 2 == 0)
{
if ((i + 1) % 2 == 0) {
//For even n, vertices n + 1, n, and n + 2 define triangle n
SetFace(faces[i], i + 1, i, i + 2);
}
else
{
} else {
//For odd n, vertices n, n+1, and n+2 define triangle n
SetFace(faces[i], i, i + 1, i + 2);
}
@ -694,11 +671,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
if (prim.material) {
aim->mMaterialIndex = prim.material.GetIndex();
}
else {
} else {
aim->mMaterialIndex = mScene->mNumMaterials - 1;
}
}
}
@ -707,8 +682,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
CopyVector(meshes, mScene->mMeshes, mScene->mNumMeshes);
}
void glTF2Importer::ImportCameras(glTF2::Asset& r)
{
void glTF2Importer::ImportCameras(glTF2::Asset &r) {
if (!r.cameras.Size()) return;
mScene->mNumCameras = r.cameras.Size();
@ -740,8 +714,7 @@ void glTF2Importer::ImportCameras(glTF2::Asset& r)
}
}
void glTF2Importer::ImportLights(glTF2::Asset& r)
{
void glTF2Importer::ImportLights(glTF2::Asset &r) {
if (!r.lights.Size())
return;
@ -753,18 +726,19 @@ void glTF2Importer::ImportLights(glTF2::Asset& r)
aiLight *ail = mScene->mLights[i] = new aiLight();
switch (light.type)
{
switch (light.type) {
case Light::Directional:
ail->mType = aiLightSource_DIRECTIONAL; break;
ail->mType = aiLightSource_DIRECTIONAL;
break;
case Light::Point:
ail->mType = aiLightSource_POINT; break;
ail->mType = aiLightSource_POINT;
break;
case Light::Spot:
ail->mType = aiLightSource_SPOT; break;
ail->mType = aiLightSource_SPOT;
break;
}
if (ail->mType != aiLightSource_POINT)
{
if (ail->mType != aiLightSource_POINT) {
ail->mDirection = aiVector3D(0.0f, 0.0f, -1.0f);
ail->mUp = aiVector3D(0.0f, 1.0f, 0.0f);
}
@ -774,14 +748,11 @@ void glTF2Importer::ImportLights(glTF2::Asset& r)
CopyValue(colorWithIntensity, ail->mColorDiffuse);
CopyValue(colorWithIntensity, ail->mColorSpecular);
if (ail->mType == aiLightSource_DIRECTIONAL)
{
if (ail->mType == aiLightSource_DIRECTIONAL) {
ail->mAttenuationConstant = 1.0;
ail->mAttenuationLinear = 0.0;
ail->mAttenuationQuadratic = 0.0;
}
else
{
} else {
//in PBR attenuation is calculated using inverse square law which can be expressed
//using assimps equation: 1/(att0 + att1 * d + att2 * d*d) with the following parameters
//this is correct equation for the case when range (see
@ -795,8 +766,7 @@ void glTF2Importer::ImportLights(glTF2::Asset& r)
ail->mAttenuationQuadratic = 1.0;
}
if (ail->mType == aiLightSource_SPOT)
{
if (ail->mType == aiLightSource_SPOT) {
ail->mAngleInnerCone = light.innerConeAngle;
ail->mAngleOuterCone = light.outerConeAngle;
}
@ -806,8 +776,7 @@ void glTF2Importer::ImportLights(glTF2::Asset& r)
static void GetNodeTransform(aiMatrix4x4 &matrix, const glTF2::Node &node) {
if (node.matrix.isPresent) {
CopyValue(node.matrix.value, matrix);
}
else {
} else {
if (node.translation.isPresent) {
aiVector3D trans;
CopyValue(node.translation.value, trans);
@ -832,8 +801,7 @@ static void GetNodeTransform(aiMatrix4x4& matrix, const glTF2::Node& node) {
}
}
static void BuildVertexWeightMapping(Mesh::Primitive& primitive, std::vector<std::vector<aiVertexWeight>>& map)
{
static void BuildVertexWeightMapping(Mesh::Primitive &primitive, std::vector<std::vector<aiVertexWeight>> &map) {
Mesh::Primitive::Attributes &attr = primitive.attributes;
if (attr.weight.empty() || attr.joint.empty()) {
return;
@ -844,12 +812,18 @@ static void BuildVertexWeightMapping(Mesh::Primitive& primitive, std::vector<std
size_t num_vertices = attr.weight[0]->count;
struct Weights { float values[4]; };
struct Weights {
float values[4];
};
Weights *weights = nullptr;
attr.weight[0]->ExtractData(weights);
struct Indices8 { uint8_t values[4]; };
struct Indices16 { uint16_t values[4]; };
struct Indices8 {
uint8_t values[4];
};
struct Indices16 {
uint16_t values[4];
};
Indices8 *indices8 = nullptr;
Indices16 *indices16 = nullptr;
if (attr.joint[0]->GetElementSize() == 4) {
@ -880,13 +854,11 @@ static void BuildVertexWeightMapping(Mesh::Primitive& primitive, std::vector<std
delete[] indices16;
}
static std::string GetNodeName(const Node& node)
{
static std::string GetNodeName(const Node &node) {
return node.name.empty() ? node.id : node.name;
}
aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>& meshOffsets, glTF2::Ref<glTF2::Node>& ptr)
{
aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &meshOffsets, glTF2::Ref<glTF2::Node> &ptr) {
Node &node = *ptr;
aiNode *ainode = new aiNode(GetNodeName(node));
@ -988,8 +960,7 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>&
//range is optional - see https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
//it is added to meta data of parent node, because there is no other place to put it
if (node.light->range.isPresent)
{
if (node.light->range.isPresent) {
ainode->mMetaData = aiMetadata::Alloc(1);
ainode->mMetaData->Set(0, "PBR_LightRange", node.light->range.value);
}
@ -998,8 +969,7 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>&
return ainode;
}
void glTF2Importer::ImportNodes(glTF2::Asset& r)
{
void glTF2Importer::ImportNodes(glTF2::Asset &r) {
if (!r.scene) return;
std::vector<Ref<Node>> rootNodes = r.scene->nodes;
@ -1008,8 +978,7 @@ void glTF2Importer::ImportNodes(glTF2::Asset& r)
unsigned int numRootNodes = unsigned(rootNodes.size());
if (numRootNodes == 1) { // a single root node: use it
mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]);
}
else if (numRootNodes > 1) { // more than one root node: create a fake root
} else if (numRootNodes > 1) { // more than one root node: create a fake root
aiNode *root = new aiNode("ROOT");
root->mChildren = new aiNode *[numRootNodes];
for (unsigned int i = 0; i < numRootNodes; ++i) {
@ -1019,18 +988,14 @@ void glTF2Importer::ImportNodes(glTF2::Asset& r)
}
mScene->mRootNode = root;
}
//if (!mScene->mRootNode) {
// mScene->mRootNode = new aiNode("EMPTY");
//}
}
struct AnimationSamplers {
AnimationSamplers()
: translation(nullptr)
, rotation(nullptr)
, scale(nullptr)
, weight(nullptr) {
AnimationSamplers() :
translation(nullptr),
rotation(nullptr),
scale(nullptr),
weight(nullptr) {
// empty
}
@ -1040,8 +1005,7 @@ struct AnimationSamplers {
Animation::Sampler *weight;
};
aiNodeAnim* CreateNodeAnim(glTF2::Asset& r, Node& node, AnimationSamplers& samplers)
{
aiNodeAnim *CreateNodeAnim(glTF2::Asset &r, Node &node, AnimationSamplers &samplers) {
aiNodeAnim *anim = new aiNodeAnim();
anim->mNodeName = GetNodeName(node);
@ -1120,8 +1084,7 @@ aiNodeAnim* CreateNodeAnim(glTF2::Asset& r, Node& node, AnimationSamplers& sampl
return anim;
}
aiMeshMorphAnim* CreateMeshMorphAnim(glTF2::Asset& r, Node& node, AnimationSamplers& samplers)
{
aiMeshMorphAnim *CreateMeshMorphAnim(glTF2::Asset &r, Node &node, AnimationSamplers &samplers) {
aiMeshMorphAnim *anim = new aiMeshMorphAnim();
anim->mName = GetNodeName(node);
@ -1157,8 +1120,7 @@ aiMeshMorphAnim* CreateMeshMorphAnim(glTF2::Asset& r, Node& node, AnimationSampl
return anim;
}
std::unordered_map<unsigned int, AnimationSamplers> GatherSamplers(Animation& anim)
{
std::unordered_map<unsigned int, AnimationSamplers> GatherSamplers(Animation &anim) {
std::unordered_map<unsigned int, AnimationSamplers> samplers;
for (unsigned int c = 0; c < anim.channels.size(); ++c) {
Animation::Channel &channel = anim.channels[c];
@ -1183,8 +1145,7 @@ std::unordered_map<unsigned int, AnimationSamplers> GatherSamplers(Animation& an
return samplers;
}
void glTF2Importer::ImportAnimations(glTF2::Asset& r)
{
void glTF2Importer::ImportAnimations(glTF2::Asset &r) {
if (!r.scene) return;
mScene->mNumAnimations = r.animations.Size();
@ -1286,8 +1247,7 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r)
}
}
void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)
{
void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
embeddedTexIdxs.resize(r.images.Size(), -1);
int numEmbeddedTexs = 0;
@ -1332,8 +1292,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)
}
}
void glTF2Importer::InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
{
void glTF2Importer::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
// clean all member arrays
meshOffsets.clear();
embeddedTexIdxs.clear();
@ -1366,4 +1325,3 @@ void glTF2Importer::InternReadFile(const std::string& pFile, aiScene* pScene, IO
}
#endif // ASSIMP_BUILD_NO_GLTF_IMPORTER