Giter Club home page Giter Club logo

Comments (4)

xkyl-yhw avatar xkyl-yhw commented on September 5, 2024

Never Mind. I figure it out. I use blender to calculate the tangent vector which contain in the fbx file. I recommod to add a new option to calculate the tangent vector.

from ufbx.

bqqbarbhg avatar bqqbarbhg commented on September 5, 2024

Yeah I considered adding tangent calculation but it's actually very complicated to get right, and in practice most people will want to use mikktspace to stay compatible with other software.

from ufbx.

xkyl-yhw avatar xkyl-yhw commented on September 5, 2024

I highly recommend to add the method of tangent calculate. I try to use the mikktspace. Refer to https://www.turais.de/using-mikktspace-in-your-project/, I build the conection between mikktspace and ufbx_mesh. Here is my code.

`
#include "calc_mikktspace_tangent.h"
#include
#include <fmt/format.h>

CalcMikkTSpaceTangent::CalcMikkTSpaceTangent()
{
iface.m_getNumFaces = get_num_faces;
iface.m_getNumVerticesOfFace = get_num_vertices_of_face;

iface.m_getNormal = get_normal;
iface.m_getPosition = get_position;
iface.m_getTexCoord = get_tex_coords;
iface.m_setTSpaceBasic = set_tspace_basic;

context.m_pInterface = &iface;

}

void CalcMikkTSpaceTangent::calc(ufbx_mesh* mesh)
{
context.m_pUserData = mesh;

genTangSpaceDefault(&this->context);

}

int CalcMikkTSpaceTangent::get_vertex_index (const SMikkTSpaceContext* context, int iFace, int iVert)
{
ufbx_mesh* _mesh = static_cast<ufbx_mesh*> (context->m_pUserData);

auto face_size = get_num_vertices_of_face(context, iFace);

auto indices_index = (iFace * face_size) + iVert;

int index = _mesh->vertex_indices[indices_index];

return index;

}

int CalcMikkTSpaceTangent::get_num_faces (const SMikkTSpaceContext* context)
{
ufbx_mesh* _mesh = static_cast<ufbx_mesh*> (context->m_pUserData);
float f_size = (float)_mesh->num_indices / 3.f;
int i_size = (int)_mesh->num_indices / 3;

//assert((f_size - (float)i_size) == 0.f);

return i_size;

}

int CalcMikkTSpaceTangent::get_num_vertices_of_face (const SMikkTSpaceContext* context, int iFace)
{
return 3;
}

void CalcMikkTSpaceTangent::get_position (const SMikkTSpaceContext* context, float outpos[], int iFace, int iVert)
{
ufbx_mesh* _mesh = static_cast<ufbx_mesh*> (context->m_pUserData);

auto index = get_vertex_index(context, iFace, iVert);
auto vertex = _mesh->vertex_position[index];

outpos[0] = vertex.x;
outpos[1] = vertex.y;
outpos[2] = vertex.z;

}

void CalcMikkTSpaceTangent::get_normal (const SMikkTSpaceContext* context, float outnormal[], int iFace, int iVert)
{
ufbx_mesh* _mesh = static_cast<ufbx_mesh*> (context->m_pUserData);

auto index = get_vertex_index(context, iFace, iVert);
auto normal = _mesh->vertex_normal[index];

outnormal[0] = normal.x;
outnormal[1] = normal.y;
outnormal[2] = normal.z;

}

void CalcMikkTSpaceTangent::get_tex_coords (const SMikkTSpaceContext* context, float outuv[], int iFace, int iVert)
{
ufbx_mesh* _mesh = static_cast<ufbx_mesh*> (context->m_pUserData);

auto index = get_vertex_index(context, iFace, iVert);
auto uv = _mesh->vertex_uv[index];

outuv[0] = uv.x;
outuv[1] = uv.y;

}

void CalcMikkTSpaceTangent::set_tspace_basic (const SMikkTSpaceContext* context, const float tangentu[], float fSign, int iFace, int iVert)
{
ufbx_mesh* _mesh = static_cast<ufbx_mesh*> (context->m_pUserData);

auto index = get_vertex_index(context, iFace, iVert);
auto* tangent = &_mesh->vertex_tangent[index];

tangent->x = tangentu[0];
tangent->y = tangentu[1];
tangent->z = tangentu[2];

}
`

Here are two problem.

  1. The result didn't go well, the normal is total wrong.
  2. Only the fbx which had calculate the tangent in blender can override tangent. That why I think should add the support between the ufbx and mikktspace.

from ufbx.

bqqbarbhg avatar bqqbarbhg commented on September 5, 2024

Hmm, adding callbacks to external mikktspace would be an option for sure.

It looks like it doesn't support N-gons:

static int GenerateInitialVerticesIndexList(STriInfo pTriInfos[], int piTriList_out[], const SMikkTSpaceContext * pContext, const int iNrTrianglesIn)
{
	int iTSpacesOffs = 0, f=0, t=0;
	int iDstTriIndex = 0;
	for (f=0; f<pContext->m_pInterface->m_getNumFaces(pContext); f++)
	{
		const int verts = pContext->m_pInterface->m_getNumVerticesOfFace(pContext, f);
		if (verts!=3 && verts!=4) continue;

This will make integrating it a lot harder as ufbx would need to create some sort of temporary face mapping with triangulated N-gons.

from ufbx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.