2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2020-01-20 13:53:12 +00:00
|
|
|
Copyright (c) 2006-2020, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-05-19 03:48:29 +00:00
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
/** @file Implementation of the post processing step to calculate
|
2015-05-19 03:48:29 +00:00
|
|
|
* tangents and bitangents for all imported meshes
|
|
|
|
*/
|
|
|
|
|
|
|
|
// internal headers
|
|
|
|
#include "CalcTangentsProcess.h"
|
|
|
|
#include "ProcessHelper.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/TinyFormatter.h>
|
|
|
|
#include <assimp/qnan.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2020-06-23 19:05:42 +00:00
|
|
|
CalcTangentsProcess::CalcTangentsProcess() :
|
|
|
|
configMaxAngle(AI_DEG_TO_RAD(45.f)), configSourceUV(0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to do here
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
2020-06-23 19:05:42 +00:00
|
|
|
CalcTangentsProcess::~CalcTangentsProcess() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to do here
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
2020-06-23 19:05:42 +00:00
|
|
|
bool CalcTangentsProcess::IsActive(unsigned int pFlags) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return (pFlags & aiProcess_CalcTangentSpace) != 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
2020-06-23 19:05:42 +00:00
|
|
|
void CalcTangentsProcess::SetupProperties(const Importer *pImp) {
|
|
|
|
ai_assert(nullptr != pImp);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get the current value of the property
|
2020-06-23 19:05:42 +00:00
|
|
|
configMaxAngle = pImp->GetPropertyFloat(AI_CONFIG_PP_CT_MAX_SMOOTHING_ANGLE, 45.f);
|
|
|
|
configMaxAngle = std::max(std::min(configMaxAngle, 45.0f), 0.0f);
|
2015-05-19 03:57:13 +00:00
|
|
|
configMaxAngle = AI_DEG_TO_RAD(configMaxAngle);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
configSourceUV = pImp->GetPropertyInteger(AI_CONFIG_PP_CT_TEXTURE_CHANNEL_INDEX, 0);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
2020-06-23 19:05:42 +00:00
|
|
|
void CalcTangentsProcess::Execute(aiScene *pScene) {
|
|
|
|
ai_assert(nullptr != pScene);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("CalcTangentsProcess begin");
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
bool bHas = false;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
|
|
|
|
if (ProcessMesh(pScene->mMeshes[a], a)) bHas = true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (bHas) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_INFO("CalcTangentsProcess finished. Tangents have been calculated");
|
2015-05-19 03:48:29 +00:00
|
|
|
} else {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("CalcTangentsProcess finished");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-04-05 20:02:02 +00:00
|
|
|
// Calculates tangents and bi-tangents for the given mesh
|
2020-06-23 19:05:42 +00:00
|
|
|
bool CalcTangentsProcess::ProcessMesh(aiMesh *pMesh, unsigned int meshIndex) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// we assume that the mesh is still in the verbose vertex format where each face has its own set
|
|
|
|
// of vertices and no vertices are shared between faces. Sadly I don't know any quick test to
|
|
|
|
// assert() it here.
|
2015-05-19 03:48:29 +00:00
|
|
|
// assert( must be verbose, dammit);
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (pMesh->mTangents) // this implies that mBitangents is also there
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the mesh consists of lines and/or points but not of
|
|
|
|
// triangles or higher-order polygons the normal vectors
|
|
|
|
// are undefined.
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_INFO("Tangents are undefined for line and point meshes");
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// what we can check, though, is if the mesh has normals and texture coordinates. That's a requirement
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pMesh->mNormals == nullptr) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("Failed to compute tangents; need normals");
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
if (configSourceUV >= AI_MAX_NUMBER_OF_TEXTURECOORDS || !pMesh->mTextureCoords[configSourceUV]) {
|
|
|
|
ASSIMP_LOG_ERROR((Formatter::format("Failed to compute tangents; need UV data in channel"), configSourceUV));
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const float angleEpsilon = 0.9999f;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<bool> vertexDone(pMesh->mNumVertices, false);
|
2015-05-19 03:57:13 +00:00
|
|
|
const float qnan = get_qnan();
|
|
|
|
|
|
|
|
// create space for the tangents and bitangents
|
|
|
|
pMesh->mTangents = new aiVector3D[pMesh->mNumVertices];
|
|
|
|
pMesh->mBitangents = new aiVector3D[pMesh->mNumVertices];
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiVector3D *meshPos = pMesh->mVertices;
|
|
|
|
const aiVector3D *meshNorm = pMesh->mNormals;
|
|
|
|
const aiVector3D *meshTex = pMesh->mTextureCoords[configSourceUV];
|
|
|
|
aiVector3D *meshTang = pMesh->mTangents;
|
|
|
|
aiVector3D *meshBitang = pMesh->mBitangents;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// calculate the tangent and bitangent for every face
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < pMesh->mNumFaces; a++) {
|
|
|
|
const aiFace &face = pMesh->mFaces[a];
|
|
|
|
if (face.mNumIndices < 3) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// There are less than three indices, thus the tangent vector
|
|
|
|
// is not defined. We are finished with these vertices now,
|
|
|
|
// their tangent vectors are set to qnan.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < face.mNumIndices; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int idx = face.mIndices[i];
|
2020-06-23 19:05:42 +00:00
|
|
|
vertexDone[idx] = true;
|
|
|
|
meshTang[idx] = aiVector3D(qnan);
|
|
|
|
meshBitang[idx] = aiVector3D(qnan);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// triangle or polygon... we always use only the first three indices. A polygon
|
|
|
|
// is supposed to be planar anyways....
|
|
|
|
// FIXME: (thom) create correct calculation for multi-vertex polygons maybe?
|
|
|
|
const unsigned int p0 = face.mIndices[0], p1 = face.mIndices[1], p2 = face.mIndices[2];
|
|
|
|
|
|
|
|
// position differences p1->p2 and p1->p3
|
|
|
|
aiVector3D v = meshPos[p1] - meshPos[p0], w = meshPos[p2] - meshPos[p0];
|
|
|
|
|
|
|
|
// texture offset p1->p2 and p1->p3
|
|
|
|
float sx = meshTex[p1].x - meshTex[p0].x, sy = meshTex[p1].y - meshTex[p0].y;
|
2015-05-19 03:48:29 +00:00
|
|
|
float tx = meshTex[p2].x - meshTex[p0].x, ty = meshTex[p2].y - meshTex[p0].y;
|
2015-05-19 03:57:13 +00:00
|
|
|
float dirCorrection = (tx * sy - ty * sx) < 0.0f ? -1.0f : 1.0f;
|
2015-05-19 03:48:29 +00:00
|
|
|
// when t1, t2, t3 in same position in UV space, just use default UV direction.
|
2020-06-23 19:05:42 +00:00
|
|
|
if (sx * ty == sy * tx) {
|
|
|
|
sx = 0.0;
|
|
|
|
sy = 1.0;
|
|
|
|
tx = 1.0;
|
|
|
|
ty = 0.0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// tangent points in the direction where to positive X axis of the texture coord's would point in model space
|
|
|
|
// bitangent's points along the positive Y axis of the texture coord's, respectively
|
|
|
|
aiVector3D tangent, bitangent;
|
|
|
|
tangent.x = (w.x * sy - v.x * ty) * dirCorrection;
|
2015-05-19 03:48:29 +00:00
|
|
|
tangent.y = (w.y * sy - v.y * ty) * dirCorrection;
|
|
|
|
tangent.z = (w.z * sy - v.z * ty) * dirCorrection;
|
|
|
|
bitangent.x = (w.x * sx - v.x * tx) * dirCorrection;
|
|
|
|
bitangent.y = (w.y * sx - v.y * tx) * dirCorrection;
|
|
|
|
bitangent.z = (w.z * sx - v.z * tx) * dirCorrection;
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// store for every vertex of that face
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int b = 0; b < face.mNumIndices; ++b) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int p = face.mIndices[b];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// project tangent and bitangent into the plane formed by the vertex' normal
|
|
|
|
aiVector3D localTangent = tangent - meshNorm[p] * (tangent * meshNorm[p]);
|
|
|
|
aiVector3D localBitangent = bitangent - meshNorm[p] * (bitangent * meshNorm[p]);
|
2020-06-23 19:05:42 +00:00
|
|
|
localTangent.NormalizeSafe();
|
|
|
|
localBitangent.NormalizeSafe();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// reconstruct tangent/bitangent according to normal and bitangent/tangent when it's infinite or NaN.
|
|
|
|
bool invalid_tangent = is_special_float(localTangent.x) || is_special_float(localTangent.y) || is_special_float(localTangent.z);
|
|
|
|
bool invalid_bitangent = is_special_float(localBitangent.x) || is_special_float(localBitangent.y) || is_special_float(localBitangent.z);
|
|
|
|
if (invalid_tangent != invalid_bitangent) {
|
|
|
|
if (invalid_tangent) {
|
|
|
|
localTangent = meshNorm[p] ^ localBitangent;
|
2019-08-17 00:04:42 +00:00
|
|
|
localTangent.NormalizeSafe();
|
2015-05-19 03:48:29 +00:00
|
|
|
} else {
|
2015-05-19 03:52:10 +00:00
|
|
|
localBitangent = localTangent ^ meshNorm[p];
|
2019-08-17 00:04:42 +00:00
|
|
|
localBitangent.NormalizeSafe();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and write it into the mesh.
|
2020-06-23 19:05:42 +00:00
|
|
|
meshTang[p] = localTangent;
|
|
|
|
meshBitang[p] = localBitangent;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// create a helper to quickly find locally close vertices among the vertex array
|
|
|
|
// FIX: check whether we can reuse the SpatialSort of a previous step
|
2020-06-23 19:05:42 +00:00
|
|
|
SpatialSort *vertexFinder = nullptr;
|
|
|
|
SpatialSort _vertexFinder;
|
2020-02-16 14:37:20 +00:00
|
|
|
float posEpsilon = 10e-6f;
|
2020-06-23 19:05:42 +00:00
|
|
|
if (shared) {
|
|
|
|
std::vector<std::pair<SpatialSort, float>> *avf;
|
|
|
|
shared->GetProperty(AI_SPP_SPATIAL_SORT, avf);
|
|
|
|
if (avf) {
|
|
|
|
std::pair<SpatialSort, float> &blubb = avf->operator[](meshIndex);
|
2015-05-19 03:57:13 +00:00
|
|
|
vertexFinder = &blubb.first;
|
2020-06-23 19:05:42 +00:00
|
|
|
posEpsilon = blubb.second;
|
|
|
|
;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!vertexFinder) {
|
|
|
|
_vertexFinder.Fill(pMesh->mVertices, pMesh->mNumVertices, sizeof(aiVector3D));
|
2015-05-19 03:57:13 +00:00
|
|
|
vertexFinder = &_vertexFinder;
|
|
|
|
posEpsilon = ComputePositionEpsilon(pMesh);
|
|
|
|
}
|
|
|
|
std::vector<unsigned int> verticesFound;
|
|
|
|
|
2016-10-15 09:05:57 +00:00
|
|
|
const float fLimit = std::cos(configMaxAngle);
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector<unsigned int> closeVertices;
|
|
|
|
|
|
|
|
// in the second pass we now smooth out all tangents and bitangents at the same local position
|
|
|
|
// if they are not too far off.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < pMesh->mNumVertices; a++) {
|
|
|
|
if (vertexDone[a])
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiVector3D &origPos = pMesh->mVertices[a];
|
|
|
|
const aiVector3D &origNorm = pMesh->mNormals[a];
|
|
|
|
const aiVector3D &origTang = pMesh->mTangents[a];
|
|
|
|
const aiVector3D &origBitang = pMesh->mBitangents[a];
|
|
|
|
closeVertices.resize(0);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// find all vertices close to that position
|
2020-06-23 19:05:42 +00:00
|
|
|
vertexFinder->FindPositions(origPos, posEpsilon, verticesFound);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
closeVertices.reserve(verticesFound.size() + 5);
|
|
|
|
closeVertices.push_back(a);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// look among them for other vertices sharing the same normal and a close-enough tangent/bitangent
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int b = 0; b < verticesFound.size(); b++) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int idx = verticesFound[b];
|
2020-06-23 19:05:42 +00:00
|
|
|
if (vertexDone[idx])
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
2020-06-23 19:05:42 +00:00
|
|
|
if (meshNorm[idx] * origNorm < angleEpsilon)
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
2020-06-23 19:05:42 +00:00
|
|
|
if (meshTang[idx] * origTang < fLimit)
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
2020-06-23 19:05:42 +00:00
|
|
|
if (meshBitang[idx] * origBitang < fLimit)
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// it's similar enough -> add it to the smoothing group
|
2020-06-23 19:05:42 +00:00
|
|
|
closeVertices.push_back(idx);
|
2015-05-19 03:57:13 +00:00
|
|
|
vertexDone[idx] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// smooth the tangents and bitangents of all vertices that were found to be close enough
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D smoothTangent(0, 0, 0), smoothBitangent(0, 0, 0);
|
|
|
|
for (unsigned int b = 0; b < closeVertices.size(); ++b) {
|
|
|
|
smoothTangent += meshTang[closeVertices[b]];
|
|
|
|
smoothBitangent += meshBitang[closeVertices[b]];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
smoothTangent.Normalize();
|
|
|
|
smoothBitangent.Normalize();
|
|
|
|
|
|
|
|
// and write it back into all affected tangents
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int b = 0; b < closeVertices.size(); ++b) {
|
|
|
|
meshTang[closeVertices[b]] = smoothTangent;
|
|
|
|
meshBitang[closeVertices[b]] = smoothBitangent;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|