Skip to content

Commit 9f4738c

Browse files
committed
[GLTF] Export Sound/Texture anim data
It now writes the information intot he "extras" section, allowing other tools to deal with that information.
1 parent 8b58f3a commit 9f4738c

4 files changed

Lines changed: 57 additions & 17 deletions

File tree

src/Animation.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Animation::Animation(const MMDAnimation& anim)
4242

4343
mtnFrame++;
4444
keyFrame++;
45+
keyFrameTime = (keyFrame - 1) / 20.0f;
4546
}
4647

4748
for (uint32_t node = 0; node < momentumData.size(); node++)
@@ -79,9 +80,8 @@ void Animation::updateData(const Axis axis, uint32_t node)
7980

8081
auto oldPos = nodeData[node].data[axis].back();
8182
auto frameCount = keyFrame - oldMomentum.first;
82-
auto timeCode = (keyFrame - 1) / 20.0f;
8383

84-
nodeData[node].data[axis].push_back({ timeCode, oldPos.second + (oldMomentum.second * frameCount) });
84+
nodeData[node].data[axis].push_back({ keyFrameTime, oldPos.second + (oldMomentum.second * frameCount) });
8585
}
8686

8787
bool KeyframeInstruction::run(Animation& anim)
@@ -102,7 +102,7 @@ bool KeyframeInstruction::run(Animation& anim)
102102

103103
bool LoopStartInstruction::run(Animation& anim)
104104
{
105-
if (loopCount == 0 || loopCount == 0xFF) anim.endlessStart = ((anim.keyFrame - 1) / 20.0f);
105+
if (loopCount == 0 || loopCount == 0xFF) anim.endlessStart = anim.keyFrameTime;
106106

107107
anim.jumpbackIndex = anim.currentIndex;
108108
anim.loopCount = loopCount;
@@ -130,13 +130,15 @@ bool PlaySoundInstruction::run(Animation& anim)
130130
{
131131
if (anim.mtnFrame != timecode) return false;
132132

133+
anim.sound.emplace_back(anim.keyFrameTime, vabId, soundId);
133134
return true;
134135
}
135136

136137
bool TextureInstruction::run(Animation& anim)
137138
{
138139
if (anim.mtnFrame != timecode) return false;
139140

141+
anim.texture.emplace_back(anim.keyFrameTime, srcX, srcY, destX, destY, width, height);
140142
return true;
141143
}
142144

src/Animation.hpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,35 @@ struct AnimNodeData
144144
AnimNodeData(const Position& pos);
145145
};
146146

147-
typedef std::map<Axis, std::pair<uint32_t, float>> MomentumData;
147+
struct TextureAnimation
148+
{
149+
float time;
150+
uint16_t srcX;
151+
uint16_t srcY;
152+
uint16_t destX;
153+
uint16_t destY;
154+
uint16_t width;
155+
uint16_t height;
156+
};
157+
158+
struct SoundAnimation
159+
{
160+
float time;
161+
uint16_t vabId;
162+
uint16_t soundId;
163+
};
148164

149165
class Animation
150166
{
167+
using MomentumData = std::map<Axis, std::pair<uint32_t, float>>;
168+
151169
private:
152170
std::vector<AnimNodeData> nodeData;
153171
std::vector<MomentumData> momentumData;
154172

155-
uint32_t mtnFrame = 1; // used by MTN data, can jump
156-
uint32_t keyFrame = 1; // used by keyframe data, is continuious
173+
uint32_t mtnFrame = 1; // used by MTN data, can jump
174+
uint32_t keyFrame = 1; // used by keyframe data, is continuious
175+
float keyFrameTime = 0.0f;
157176

158177
uint32_t currentIndex = 0;
159178
uint32_t jumpbackIndex = 0;
@@ -172,6 +191,8 @@ class Animation
172191
public:
173192
// start of endless looping, as time code
174193
float endlessStart = -1;
194+
std::vector<SoundAnimation> sound;
195+
std::vector<TextureAnimation> texture;
175196

176197
public:
177198
Animation(const MMDAnimation& anim);

src/GLTF.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,34 @@ void GLTFExporter::buildAnimations()
444444
}
445445

446446
tinygltf::Value::Object extras;
447-
extras.emplace("endlessStart", std::to_string(data.endlessStart));
447+
tinygltf::Value::Array soundArray;
448+
tinygltf::Value::Array textureArray;
449+
450+
for (auto s : data.sound)
451+
{
452+
tinygltf::Value::Object sound;
453+
sound.emplace("time", s.time);
454+
sound.emplace("vabId", s.vabId);
455+
sound.emplace("soundId", s.soundId);
456+
soundArray.emplace_back(sound);
457+
}
458+
459+
for (auto t : data.texture)
460+
{
461+
tinygltf::Value::Object texture;
462+
texture.emplace("time", t.time);
463+
texture.emplace("srxX", t.srcX);
464+
texture.emplace("srcY", t.srcY);
465+
texture.emplace("destX", t.destX);
466+
texture.emplace("destY", t.destY);
467+
texture.emplace("width", t.width);
468+
texture.emplace("height", t.height);
469+
textureArray.emplace_back(texture);
470+
}
471+
472+
if (data.endlessStart != -1) extras.emplace("endlessStart", std::to_string(data.endlessStart));
473+
if (!data.sound.empty()) extras.emplace("sounds", soundArray);
474+
if (!data.texture.empty()) extras.emplace("textures", textureArray);
448475
anim.extras = tinygltf::Value(extras);
449476
push(model.animations, anim);
450477
}
@@ -485,14 +512,6 @@ GLTFExporter::GLTFExporter(Model& mmd, AbstractTIM& tim)
485512
buildMeshEntries();
486513
buildAnimations();
487514
buildTexture();
488-
/*
489-
tinygltf::TinyGLTF gltf;
490-
gltf.WriteGltfSceneToFile(&model,
491-
"MUGE.gltf",
492-
true, // embedImages
493-
true, // embedBuffers
494-
true, // pretty print
495-
false); // write binary*/
496515
}
497516

498517
void GLTFExporter::save(const std::string& filename)

src/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ int main()
8787

8888
// TODO support for multiple images (that one arena)
8989
// TODO support for images being used by multiple models
90-
// TODO playSound animation data into glTF
91-
// TODO changeTexture animation data into glTF
9290

9391
std::cout << "Done" << std::endl;
9492
return 0;

0 commit comments

Comments
 (0)