2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2024-02-23 21:30:05 +00:00
|
|
|
Copyright (c) 2006-2024, assimp team
|
2018-01-28 18:42:05 +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
|
2015-05-19 03:48:29 +00:00
|
|
|
following 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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file GenUVCoords step */
|
|
|
|
|
|
|
|
#include "ComputeUVMappingProcess.h"
|
2023-05-03 22:00:52 +00:00
|
|
|
#include "Geometry/GeometryUtils.h"
|
2015-05-19 03:48:29 +00:00
|
|
|
#include "ProcessHelper.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/Exceptional.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
const static aiVector3D base_axis_y(0.0, 1.0, 0.0);
|
|
|
|
const static aiVector3D base_axis_x(1.0, 0.0, 0.0);
|
|
|
|
const static aiVector3D base_axis_z(0.0, 0.0, 1.0);
|
|
|
|
const static ai_real angle_epsilon = ai_real(0.95);
|
|
|
|
} // namespace
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
2023-05-03 22:00:52 +00:00
|
|
|
bool ComputeUVMappingProcess::IsActive(unsigned int pFlags) const {
|
|
|
|
return (pFlags & aiProcess_GenUVCoords) != 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Find the first empty UV channel in a mesh
|
2023-05-03 22:00:52 +00:00
|
|
|
inline unsigned int FindEmptyUVChannel(aiMesh *mesh) {
|
|
|
|
for (unsigned int m = 0; m < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++m)
|
2023-04-16 16:20:14 +00:00
|
|
|
if (!mesh->mTextureCoords[m]) {
|
|
|
|
return m;
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("Unable to compute UV coordinates, no free UV slot found");
|
2015-05-19 03:57:13 +00:00
|
|
|
return UINT_MAX;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Try to remove UV seams
|
2023-05-03 22:00:52 +00:00
|
|
|
void RemoveUVSeams(aiMesh *mesh, aiVector3D *out) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// TODO: just a very rough algorithm. I think it could be done
|
|
|
|
// much easier, but I don't know how and am currently too tired to
|
|
|
|
// to think about a better solution.
|
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
const static ai_real LOWER_LIMIT = ai_real(0.1);
|
|
|
|
const static ai_real UPPER_LIMIT = ai_real(0.9);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
const static ai_real LOWER_EPSILON = ai_real(10e-3);
|
|
|
|
const static ai_real UPPER_EPSILON = ai_real(1.0 - 10e-3);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int fidx = 0; fidx < mesh->mNumFaces; ++fidx) {
|
|
|
|
const aiFace &face = mesh->mFaces[fidx];
|
2023-04-16 16:20:14 +00:00
|
|
|
if (face.mNumIndices < 3) {
|
|
|
|
continue; // triangles and polygons only, please
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-04 07:04:19 +00:00
|
|
|
unsigned int smallV = face.mNumIndices, large = smallV;
|
2015-05-19 03:57:13 +00:00
|
|
|
bool zero = false, one = false, round_to_zero = false;
|
|
|
|
|
|
|
|
// Check whether this face lies on a UV seam. We can just guess,
|
|
|
|
// but the assumption that a face with at least one very small
|
|
|
|
// on the one side and one very large U coord on the other side
|
|
|
|
// lies on a UV seam should work for most cases.
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int n = 0; n < face.mNumIndices; ++n) {
|
|
|
|
if (out[face.mIndices[n]].x < LOWER_LIMIT) {
|
2021-03-04 07:04:19 +00:00
|
|
|
smallV = n;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// If we have a U value very close to 0 we can't
|
|
|
|
// round the others to 0, too.
|
|
|
|
if (out[face.mIndices[n]].x <= LOWER_EPSILON)
|
|
|
|
zero = true;
|
2023-05-03 22:00:52 +00:00
|
|
|
else
|
|
|
|
round_to_zero = true;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
if (out[face.mIndices[n]].x > UPPER_LIMIT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
large = n;
|
|
|
|
|
|
|
|
// If we have a U value very close to 1 we can't
|
|
|
|
// round the others to 1, too.
|
|
|
|
if (out[face.mIndices[n]].x >= UPPER_EPSILON)
|
|
|
|
one = true;
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
if (smallV != face.mNumIndices && large != face.mNumIndices) {
|
|
|
|
for (unsigned int n = 0; n < face.mNumIndices; ++n) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// If the u value is over the upper limit and no other u
|
|
|
|
// value of that face is 0, round it to 0
|
|
|
|
if (out[face.mIndices[n]].x > UPPER_LIMIT && !zero)
|
2016-07-15 05:38:29 +00:00
|
|
|
out[face.mIndices[n]].x = 0.0;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// If the u value is below the lower limit and no other u
|
|
|
|
// value of that face is 1, round it to 1
|
|
|
|
else if (out[face.mIndices[n]].x < LOWER_LIMIT && !one)
|
2016-07-15 05:38:29 +00:00
|
|
|
out[face.mIndices[n]].x = 1.0;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// The face contains both 0 and 1 as UV coords. This can occur
|
|
|
|
// for faces which have an edge that lies directly on the seam.
|
|
|
|
// Due to numerical inaccuracies one U coord becomes 0, the
|
|
|
|
// other 1. But we do still have a third UV coord to determine
|
|
|
|
// to which side we must round to.
|
2023-05-03 22:00:52 +00:00
|
|
|
else if (one && zero) {
|
|
|
|
if (round_to_zero && out[face.mIndices[n]].x >= UPPER_EPSILON)
|
2016-07-15 05:38:29 +00:00
|
|
|
out[face.mIndices[n]].x = 0.0;
|
2015-05-19 03:57:13 +00:00
|
|
|
else if (!round_to_zero && out[face.mIndices[n]].x <= LOWER_EPSILON)
|
2016-07-15 05:38:29 +00:00
|
|
|
out[face.mIndices[n]].x = 1.0;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2023-05-03 22:00:52 +00:00
|
|
|
void ComputeUVMappingProcess::ComputeSphereMapping(aiMesh *mesh, const aiVector3D &axis, aiVector3D *out) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiVector3D center, min, max;
|
|
|
|
FindMeshCenter(mesh, center, min, max);
|
|
|
|
|
|
|
|
// If the axis is one of x,y,z run a faster code path. It's worth the extra effort ...
|
|
|
|
// currently the mapping axis will always be one of x,y,z, except if the
|
|
|
|
// PretransformVertices step is used (it transforms the meshes into worldspace,
|
|
|
|
// thus changing the mapping axis)
|
2023-05-03 22:00:52 +00:00
|
|
|
if (axis * base_axis_x >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// For each point get a normalized projection vector in the sphere,
|
|
|
|
// get its longitude and latitude and map them to their respective
|
|
|
|
// UV axes. Problems occur around the poles ... unsolvable.
|
|
|
|
//
|
|
|
|
// The spherical coordinate system looks like this:
|
|
|
|
// x = cos(lon)*cos(lat)
|
|
|
|
// y = sin(lon)*cos(lat)
|
|
|
|
// z = sin(lat)
|
|
|
|
//
|
|
|
|
// Thus we can derive:
|
|
|
|
// lat = arcsin (z)
|
|
|
|
// lon = arctan (y/x)
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D diff = (mesh->mVertices[pnt] - center).Normalize();
|
|
|
|
out[pnt] = aiVector3D((std::atan2(diff.z, diff.y) + AI_MATH_PI_F) / AI_MATH_TWO_PI_F,
|
|
|
|
(std::asin(diff.x) + AI_MATH_HALF_PI_F) / AI_MATH_PI_F, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
} else if (axis * base_axis_y >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ... just the same again
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D diff = (mesh->mVertices[pnt] - center).Normalize();
|
|
|
|
out[pnt] = aiVector3D((std::atan2(diff.x, diff.z) + AI_MATH_PI_F) / AI_MATH_TWO_PI_F,
|
|
|
|
(std::asin(diff.y) + AI_MATH_HALF_PI_F) / AI_MATH_PI_F, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
} else if (axis * base_axis_z >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// ... just the same again
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D diff = (mesh->mVertices[pnt] - center).Normalize();
|
|
|
|
out[pnt] = aiVector3D((std::atan2(diff.y, diff.x) + AI_MATH_PI_F) / AI_MATH_TWO_PI_F,
|
|
|
|
(std::asin(diff.z) + AI_MATH_HALF_PI_F) / AI_MATH_PI_F, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// slower code path in case the mapping axis is not one of the coordinate system axes
|
2023-05-03 22:00:52 +00:00
|
|
|
else {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMatrix4x4 mTrafo;
|
2023-05-03 22:00:52 +00:00
|
|
|
aiMatrix4x4::FromToMatrix(axis, base_axis_y, mTrafo);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// again the same, except we're applying a transformation now
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D diff = ((mTrafo * mesh->mVertices[pnt]) - center).Normalize();
|
|
|
|
out[pnt] = aiVector3D((std::atan2(diff.y, diff.x) + AI_MATH_PI_F) / AI_MATH_TWO_PI_F,
|
|
|
|
(std::asin(diff.z) + AI_MATH_HALF_PI_F) / AI_MATH_PI_F, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now find and remove UV seams. A seam occurs if a face has a tcoord
|
|
|
|
// close to zero on the one side, and a tcoord close to one on the
|
|
|
|
// other side.
|
2023-05-03 22:00:52 +00:00
|
|
|
RemoveUVSeams(mesh, out);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2023-05-03 22:00:52 +00:00
|
|
|
void ComputeUVMappingProcess::ComputeCylinderMapping(aiMesh *mesh, const aiVector3D &axis, aiVector3D *out) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiVector3D center, min, max;
|
|
|
|
|
|
|
|
// If the axis is one of x,y,z run a faster code path. It's worth the extra effort ...
|
|
|
|
// currently the mapping axis will always be one of x,y,z, except if the
|
|
|
|
// PretransformVertices step is used (it transforms the meshes into worldspace,
|
|
|
|
// thus changing the mapping axis)
|
2023-05-03 22:00:52 +00:00
|
|
|
if (axis * base_axis_x >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
FindMeshCenter(mesh, center, min, max);
|
2016-07-15 05:38:29 +00:00
|
|
|
const ai_real diff = max.x - min.x;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// If the main axis is 'z', the z coordinate of a point 'p' is mapped
|
|
|
|
// directly to the texture V axis. The other axis is derived from
|
|
|
|
// the angle between ( p.x - c.x, p.y - c.y ) and (1,0), where
|
|
|
|
// 'c' is the center point of the mesh.
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D &pos = mesh->mVertices[pnt];
|
|
|
|
aiVector3D &uv = out[pnt];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
uv.y = (pos.x - min.x) / diff;
|
2023-05-03 22:00:52 +00:00
|
|
|
uv.x = (std::atan2(pos.z - center.z, pos.y - center.y) + (ai_real)AI_MATH_PI) / (ai_real)AI_MATH_TWO_PI;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
} else if (axis * base_axis_y >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
FindMeshCenter(mesh, center, min, max);
|
2016-07-15 05:38:29 +00:00
|
|
|
const ai_real diff = max.y - min.y;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// just the same ...
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D &pos = mesh->mVertices[pnt];
|
|
|
|
aiVector3D &uv = out[pnt];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
uv.y = (pos.y - min.y) / diff;
|
2023-05-03 22:00:52 +00:00
|
|
|
uv.x = (std::atan2(pos.x - center.x, pos.z - center.z) + (ai_real)AI_MATH_PI) / (ai_real)AI_MATH_TWO_PI;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
} else if (axis * base_axis_z >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
FindMeshCenter(mesh, center, min, max);
|
2016-07-15 05:38:29 +00:00
|
|
|
const ai_real diff = max.z - min.z;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// just the same ...
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D &pos = mesh->mVertices[pnt];
|
|
|
|
aiVector3D &uv = out[pnt];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
uv.y = (pos.z - min.z) / diff;
|
2023-05-03 22:00:52 +00:00
|
|
|
uv.x = (std::atan2(pos.y - center.y, pos.x - center.x) + (ai_real)AI_MATH_PI) / (ai_real)AI_MATH_TWO_PI;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// slower code path in case the mapping axis is not one of the coordinate system axes
|
|
|
|
else {
|
|
|
|
aiMatrix4x4 mTrafo;
|
2023-05-03 22:00:52 +00:00
|
|
|
aiMatrix4x4::FromToMatrix(axis, base_axis_y, mTrafo);
|
|
|
|
FindMeshCenterTransformed(mesh, center, min, max, mTrafo);
|
2016-07-15 05:38:29 +00:00
|
|
|
const ai_real diff = max.y - min.y;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// again the same, except we're applying a transformation now
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D pos = mTrafo * mesh->mVertices[pnt];
|
|
|
|
aiVector3D &uv = out[pnt];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
uv.y = (pos.y - min.y) / diff;
|
2023-05-03 22:00:52 +00:00
|
|
|
uv.x = (std::atan2(pos.x - center.x, pos.z - center.z) + (ai_real)AI_MATH_PI) / (ai_real)AI_MATH_TWO_PI;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now find and remove UV seams. A seam occurs if a face has a tcoord
|
|
|
|
// close to zero on the one side, and a tcoord close to one on the
|
|
|
|
// other side.
|
2023-05-03 22:00:52 +00:00
|
|
|
RemoveUVSeams(mesh, out);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2023-05-03 22:00:52 +00:00
|
|
|
void ComputeUVMappingProcess::ComputePlaneMapping(aiMesh *mesh, const aiVector3D &axis, aiVector3D *out) {
|
|
|
|
ai_real diffu, diffv;
|
2015-05-19 03:57:13 +00:00
|
|
|
aiVector3D center, min, max;
|
|
|
|
|
|
|
|
// If the axis is one of x,y,z run a faster code path. It's worth the extra effort ...
|
|
|
|
// currently the mapping axis will always be one of x,y,z, except if the
|
|
|
|
// PretransformVertices step is used (it transforms the meshes into worldspace,
|
|
|
|
// thus changing the mapping axis)
|
2023-05-03 22:00:52 +00:00
|
|
|
if (axis * base_axis_x >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
FindMeshCenter(mesh, center, min, max);
|
|
|
|
diffu = max.z - min.z;
|
|
|
|
diffv = max.y - min.y;
|
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D &pos = mesh->mVertices[pnt];
|
|
|
|
out[pnt].Set((pos.z - min.z) / diffu, (pos.y - min.y) / diffv, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
} else if (axis * base_axis_y >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
FindMeshCenter(mesh, center, min, max);
|
|
|
|
diffu = max.x - min.x;
|
|
|
|
diffv = max.z - min.z;
|
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D &pos = mesh->mVertices[pnt];
|
|
|
|
out[pnt].Set((pos.x - min.x) / diffu, (pos.z - min.z) / diffv, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
} else if (axis * base_axis_z >= angle_epsilon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
FindMeshCenter(mesh, center, min, max);
|
2019-09-23 09:22:20 +00:00
|
|
|
diffu = max.x - min.x;
|
|
|
|
diffv = max.y - min.y;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
|
|
|
const aiVector3D &pos = mesh->mVertices[pnt];
|
|
|
|
out[pnt].Set((pos.x - min.x) / diffu, (pos.y - min.y) / diffv, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// slower code path in case the mapping axis is not one of the coordinate system axes
|
2023-05-03 22:00:52 +00:00
|
|
|
else {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMatrix4x4 mTrafo;
|
2023-05-03 22:00:52 +00:00
|
|
|
aiMatrix4x4::FromToMatrix(axis, base_axis_y, mTrafo);
|
|
|
|
FindMeshCenterTransformed(mesh, center, min, max, mTrafo);
|
2015-05-19 03:57:13 +00:00
|
|
|
diffu = max.x - min.x;
|
|
|
|
diffv = max.z - min.z;
|
|
|
|
|
|
|
|
// again the same, except we're applying a transformation now
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int pnt = 0; pnt < mesh->mNumVertices; ++pnt) {
|
2015-05-19 03:57:13 +00:00
|
|
|
const aiVector3D pos = mTrafo * mesh->mVertices[pnt];
|
2023-05-03 22:00:52 +00:00
|
|
|
out[pnt].Set((pos.x - min.x) / diffu, (pos.z - min.z) / diffv, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldn't be necessary to remove UV seams ...
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2023-05-03 22:00:52 +00:00
|
|
|
void ComputeUVMappingProcess::ComputeBoxMapping(aiMesh *, aiVector3D *) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("Mapping type currently not implemented");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2023-05-03 22:00:52 +00:00
|
|
|
void ComputeUVMappingProcess::Execute(aiScene *pScene) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("GenUVCoordsProcess begin");
|
2015-05-19 03:57:13 +00:00
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT)
|
|
|
|
throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
|
|
|
|
|
|
|
|
std::list<MappingInfo> mappingStack;
|
|
|
|
|
|
|
|
/* Iterate through all materials and search for non-UV mapped textures
|
|
|
|
*/
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int i = 0; i < pScene->mNumMaterials; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mappingStack.clear();
|
2023-05-03 22:00:52 +00:00
|
|
|
aiMaterial *mat = pScene->mMaterials[i];
|
|
|
|
for (unsigned int a = 0; a < mat->mNumProperties; ++a) {
|
|
|
|
aiMaterialProperty *prop = mat->mProperties[a];
|
|
|
|
if (!::strcmp(prop->mKey.data, "$tex.mapping")) {
|
|
|
|
aiTextureMapping &mapping = *((aiTextureMapping *)prop->mData);
|
|
|
|
if (aiTextureMapping_UV != mapping) {
|
|
|
|
if (!DefaultLogger::isNullLogger()) {
|
2016-01-06 14:35:25 +00:00
|
|
|
ai_snprintf(buffer, 1024, "Found non-UV mapped texture (%s,%u). Mapping type: %s",
|
2023-05-03 22:00:52 +00:00
|
|
|
aiTextureTypeToString((aiTextureType)prop->mSemantic), prop->mIndex,
|
|
|
|
MappingTypeToString(mapping));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_INFO(buffer);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aiTextureMapping_OTHER == mapping)
|
|
|
|
continue;
|
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
MappingInfo info(mapping);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Get further properties - currently only the major axis
|
2023-05-03 22:00:52 +00:00
|
|
|
for (unsigned int a2 = 0; a2 < mat->mNumProperties; ++a2) {
|
|
|
|
aiMaterialProperty *prop2 = mat->mProperties[a2];
|
2015-05-19 03:57:13 +00:00
|
|
|
if (prop2->mSemantic != prop->mSemantic || prop2->mIndex != prop->mIndex)
|
|
|
|
continue;
|
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
if (!::strcmp(prop2->mKey.data, "$tex.mapaxis")) {
|
|
|
|
info.axis = *((aiVector3D *)prop2->mData);
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
unsigned int idx(99999999);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check whether we have this mapping mode already
|
2023-05-03 22:00:52 +00:00
|
|
|
std::list<MappingInfo>::iterator it = std::find(mappingStack.begin(), mappingStack.end(), info);
|
|
|
|
if (mappingStack.end() != it) {
|
2015-05-19 03:57:13 +00:00
|
|
|
idx = (*it).uv;
|
2023-05-03 22:00:52 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
/* We have found a non-UV mapped texture. Now
|
2023-05-03 22:00:52 +00:00
|
|
|
* we need to find all meshes using this material
|
|
|
|
* that we can compute UV channels for them.
|
|
|
|
*/
|
|
|
|
for (unsigned int m = 0; m < pScene->mNumMeshes; ++m) {
|
|
|
|
aiMesh *mesh = pScene->mMeshes[m];
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int outIdx = 0;
|
2023-05-03 22:00:52 +00:00
|
|
|
if (mesh->mMaterialIndex != i || (outIdx = FindEmptyUVChannel(mesh)) == UINT_MAX ||
|
|
|
|
!mesh->mNumVertices) {
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate output storage
|
2023-05-03 22:00:52 +00:00
|
|
|
aiVector3D *p = mesh->mTextureCoords[outIdx] = new aiVector3D[mesh->mNumVertices];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2023-05-03 22:00:52 +00:00
|
|
|
switch (mapping) {
|
2015-05-19 03:57:13 +00:00
|
|
|
case aiTextureMapping_SPHERE:
|
2023-05-03 22:00:52 +00:00
|
|
|
ComputeSphereMapping(mesh, info.axis, p);
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
case aiTextureMapping_CYLINDER:
|
2023-05-03 22:00:52 +00:00
|
|
|
ComputeCylinderMapping(mesh, info.axis, p);
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
case aiTextureMapping_PLANE:
|
2023-05-03 22:00:52 +00:00
|
|
|
ComputePlaneMapping(mesh, info.axis, p);
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
case aiTextureMapping_BOX:
|
2023-05-03 22:00:52 +00:00
|
|
|
ComputeBoxMapping(mesh, p);
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
}
|
2023-05-03 22:00:52 +00:00
|
|
|
if (m && idx != outIdx) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_WARN("UV index mismatch. Not all meshes assigned to "
|
2023-05-03 22:00:52 +00:00
|
|
|
"this material have equal numbers of UV channels. The UV index stored in "
|
|
|
|
"the material structure does therefore not apply for all meshes. ");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
idx = outIdx;
|
|
|
|
}
|
|
|
|
info.uv = idx;
|
|
|
|
mappingStack.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the material property list
|
|
|
|
mapping = aiTextureMapping_UV;
|
2023-05-03 22:00:52 +00:00
|
|
|
((aiMaterial *)mat)->AddProperty(&idx, 1, AI_MATKEY_UVWSRC(prop->mSemantic, prop->mIndex));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("GenUVCoordsProcess finished");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|