Fix model export

This commit is contained in:
James Lambert 2023-11-19 15:34:15 -07:00
parent 67d99e1bd3
commit 0f5d6b29f6
4 changed files with 60 additions and 19 deletions

View file

@ -48,6 +48,27 @@ aiMesh* copyMesh(aiMesh* mesh) {
} }
} }
result->mNumBones = mesh->mNumBones;
result->mBones = new aiBone*[mesh->mNumBones];
for (unsigned i = 0; i < mesh->mNumBones; ++i) {
aiBone* newBone = new aiBone;
aiBone* oldBone = mesh->mBones[i];
newBone->mArmature = oldBone->mArmature;
newBone->mName = oldBone->mName;
newBone->mNode = oldBone->mNode;
newBone->mNumWeights = oldBone->mNumWeights;
newBone->mOffsetMatrix = oldBone->mOffsetMatrix;
newBone->mWeights = new aiVertexWeight[oldBone->mNumWeights];
for (unsigned weightIndex = 0; weightIndex < oldBone->mNumWeights; ++weightIndex) {
newBone->mWeights[weightIndex] = oldBone->mWeights[weightIndex];
}
result->mBones[i] = newBone;
}
result->mName = mesh->mName; result->mName = mesh->mName;
return result; return result;
@ -234,8 +255,6 @@ std::shared_ptr<ExtendedMesh> ExtendedMesh::Transform(const aiMatrix4x4& transfo
aiMatrix3x3 inverseRotation = rotationOnly; aiMatrix3x3 inverseRotation = rotationOnly;
inverseRotation.Inverse(); inverseRotation.Inverse();
for (unsigned i = 0; i < result->mMesh->mNumVertices; ++i) { for (unsigned i = 0; i < result->mMesh->mNumVertices; ++i) {
result->mMesh->mVertices[i] = transform * result->mMesh->mVertices[i]; result->mMesh->mVertices[i] = transform * result->mMesh->mVertices[i];
@ -250,6 +269,10 @@ std::shared_ptr<ExtendedMesh> ExtendedMesh::Transform(const aiMatrix4x4& transfo
} }
} }
for (unsigned i = 0; i < result->mMesh->mNumBones; ++i) {
result->mMesh->mBones[i]->mOffsetMatrix = result->mMesh->mBones[i]->mOffsetMatrix * inverseTransform;
}
result->RecalcBB(); result->RecalcBB();
return result; return result;

View file

@ -1,5 +1,7 @@
#include "ZSorter.h" #include "ZSorter.h"
#include <vector>
struct SingleFace { struct SingleFace {
aiFace* face; aiFace* face;
std::pair<Bone*, Bone*> bonePair; std::pair<Bone*, Bone*> bonePair;
@ -124,31 +126,41 @@ RenderChunk renderChunksRebuildFromFaces(const aiScene* scene, std::vector<Singl
newAiMesh->mNumBones = 0; newAiMesh->mNumBones = 0;
if (start->bonePair.first) { newAiMesh->mNumBones = source->mMesh->mNumBones;
newAiMesh->mNumBones = boneHeirarchy.GetBoneCount();
if (newAiMesh->mNumBones) {
newAiMesh->mBones = new aiBone*[newAiMesh->mNumBones]; newAiMesh->mBones = new aiBone*[newAiMesh->mNumBones];
for (unsigned i = 0; i < newAiMesh->mNumBones; ++i) { for (unsigned i = 0; i < newAiMesh->mNumBones; ++i) {
aiBone* bone = new aiBone; aiBone* bone = new aiBone;
aiBone* srcBone = source->mMesh->mBones[i];
bone->mArmature = NULL; bone->mArmature = srcBone->mArmature;
bone->mName = start->bonePair.first->GetName(); bone->mName = srcBone->mName;
bone->mNode = NULL; bone->mNode = srcBone->mNode;
bone->mOffsetMatrix = srcBone->mOffsetMatrix;
if (start->bonePair.first->GetName() == boneHeirarchy.BoneByIndex(i)->GetName()) { std::vector<unsigned> attachedIndices;
bone->mNode = scene->mRootNode->FindNode(start->bonePair.first->GetName().c_str());
bone->mOffsetMatrix = (settings.CreateCollisionTransform() * aiBuildOffsetMatrix(bone->mNode)).Inverse();
bone->mNumWeights = newAiMesh->mNumVertices; std::set<unsigned> includedBefore;
bone->mWeights = new aiVertexWeight[newAiMesh->mNumVertices]; for (unsigned weightIndex = 0; weightIndex < srcBone->mNumWeights; ++weightIndex) {
includedBefore.insert(srcBone->mWeights[weightIndex].mVertexId);
}
for (unsigned index = 0; index < newAiMesh->mNumVertices; ++index) { for (unsigned vertexIndex = 0; vertexIndex < newAiMesh->mNumVertices; ++vertexIndex) {
bone->mWeights[index].mVertexId = index; unsigned sourceIndex = mesh.sourceIndex[vertexIndex];
bone->mWeights[index].mWeight = 1.0f;
if (includedBefore.find(sourceIndex) != includedBefore.end()) {
attachedIndices.push_back(vertexIndex);
} }
} else { }
bone->mNumWeights = 0;
bone->mNumWeights = attachedIndices.size();
bone->mWeights = new aiVertexWeight[attachedIndices.size()];
for (unsigned weightIndex = 0; weightIndex < attachedIndices.size(); ++weightIndex) {
bone->mWeights[weightIndex].mVertexId = attachedIndices[weightIndex];
bone->mWeights[weightIndex].mWeight = 1.0f;
} }
newAiMesh->mBones[i] = bone; newAiMesh->mBones[i] = bone;

View file

@ -22,12 +22,13 @@
struct Transform gGunTransform = { struct Transform gGunTransform = {
{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 0.0f, 0.0f},
{1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 1.0f},
}; };
void portalGunInit(struct PortalGun* portalGun, struct Transform* at){ void portalGunInit(struct PortalGun* portalGun, struct Transform* at){
skArmatureInit(&portalGun->armature, &portal_gun_v_portalgun_armature); skArmatureInit(&portalGun->armature, &portal_gun_v_portalgun_armature);
skAnimatorInit(&portalGun->animator, portal_gun_v_portalgun_armature.numberOfBones);
portalGun->portalGunVisible = 0; portalGun->portalGunVisible = 0;
portalGun->shootAnimationTimer = 0.0; portalGun->shootAnimationTimer = 0.0;
portalGun->shootTotalAnimationTimer = 0.0; portalGun->shootTotalAnimationTimer = 0.0;
@ -37,6 +38,8 @@ void portalGunInit(struct PortalGun* portalGun, struct Transform* at){
portalTrailInit(&portalGun->projectiles[0].trail); portalTrailInit(&portalGun->projectiles[0].trail);
portalTrailInit(&portalGun->projectiles[1].trail); portalTrailInit(&portalGun->projectiles[1].trail);
skAnimatorRunClip(&portalGun->animator, &portal_gun_v_portalgun_Armature_pickup_clip, 0.0f, SKAnimatorFlagsLoop);
} }
#define PORTAL_PROJECTILE_RADIUS 0.15f #define PORTAL_PROJECTILE_RADIUS 0.15f
@ -120,7 +123,7 @@ void portalGunRenderReal(struct PortalGun* portalGun, struct RenderState* render
} }
u16 perspectiveNormalize; u16 perspectiveNormalize;
guPerspective(&matrix[1], &perspectiveNormalize, fromCamera->fov, getAspect(), 0.05f * SCENE_SCALE, 2.0f * SCENE_SCALE, 1.0f); guPerspective(&matrix[1], &perspectiveNormalize, fromCamera->fov, getAspect(), 0.05f * SCENE_SCALE, 4.0f * SCENE_SCALE, 1.0f);
gSPMatrix(renderState->dl++, &matrix[1], G_MTX_PROJECTION | G_MTX_LOAD | G_MTX_NOPUSH); gSPMatrix(renderState->dl++, &matrix[1], G_MTX_PROJECTION | G_MTX_LOAD | G_MTX_NOPUSH);
gSPLookAt(renderState->dl++, &gLookAt); gSPLookAt(renderState->dl++, &gLookAt);
@ -149,6 +152,7 @@ void portalGunUpdatePosition(struct PortalGun* portalGun, struct Player* player)
} }
void portalGunUpdate(struct PortalGun* portalGun, struct Player* player) { void portalGunUpdate(struct PortalGun* portalGun, struct Player* player) {
skAnimatorUpdate(&portalGun->animator, portalGun->armature.pose, FIXED_DELTA_TIME);
portalGunUpdatePosition(portalGun, player); portalGunUpdatePosition(portalGun, player);
if (player->flags & (PlayerHasFirstPortalGun | PlayerHasSecondPortalGun)) { if (player->flags & (PlayerHasFirstPortalGun | PlayerHasSecondPortalGun)) {

View file

@ -12,6 +12,7 @@
#include "../effects/portal_trail.h" #include "../effects/portal_trail.h"
#include "../scene/camera.h" #include "../scene/camera.h"
#include "../sk64/skelatool_armature.h" #include "../sk64/skelatool_armature.h"
#include "../sk64/skelatool_animator.h"
struct PortalGunProjectile { struct PortalGunProjectile {
struct Ray positionDirection; struct Ray positionDirection;
@ -30,6 +31,7 @@ struct PortalGunProjectile {
struct PortalGun { struct PortalGun {
struct SKArmature armature; struct SKArmature armature;
struct SKAnimator animator;
int portalGunVisible; int portalGunVisible;
float shootAnimationTimer; float shootAnimationTimer;
float shootTotalAnimationTimer; float shootTotalAnimationTimer;