From 9dd7241176ab806ba7f1c985cb032f456e991b21 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Sun, 15 Jun 2014 22:45:56 +0800 Subject: [PATCH] load c3t animation work 90% --- cocos/3d/CCBundle3D.cpp | 82 +++++++++++++++++-- cocos/3d/CCMeshSkin.cpp | 15 +++- cocos/3d/CCSprite3D.cpp | 2 +- cocos/renderer/ccShader_3D_PositionTex.vert | 24 +++++- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 5 +- .../Resources/Sprite3DTest/girl2.c3t | 2 +- 6 files changed, 109 insertions(+), 21 deletions(-) diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index d84d913647..d388c3e9ac 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -46,13 +46,14 @@ void getChildMap(std::map >& map, SkinData* skinData, cons std::string parent_name = val["id"].GetString(); int parent_name_index = skinData->getBoneNameIndex(parent_name); - const rapidjson::Value& bind_pos = val["bind_shape"]; - Mat4 mat_bind_pos; - for (rapidjson::SizeType j = 0; j < bind_pos.Size(); j++) - { - mat_bind_pos.m[j] = bind_pos[j].GetDouble(); - } - skinData->inverseBindPoseMatrices.push_back(mat_bind_pos); +// const rapidjson::Value& bind_pos = val["bind_shape"]; +// Mat4 mat_bind_pos; +// for (rapidjson::SizeType j = 0; j < bind_pos.Size(); j++) +// { +// mat_bind_pos.m[j] = bind_pos[j].GetDouble(); +// } +// +// skinData->inverseBindPoseMatrices[parent_name_index] = (mat_bind_pos); const rapidjson::Value& children = val["children"]; for (rapidjson::SizeType i = 0; i < children.Size(); i++) @@ -201,6 +202,10 @@ bool Bundle3D::loadSkinData(const std::string& id, SkinData* skindata) { skindata->bindShape.m[i] = skin_data_bind_shape[i].GetDouble(); } + + float m[] = {-0.682038f, -0.035225f, 0.730468f, 0.000000f, 0.731315f, -0.035225f, 0.681130f, 0.000000f, 0.001738f, 0.998758f, 0.049786f, 0.000000f, -0.882000f, 78.798103f, -0.868362f, 1.000000f}; + memcpy(skindata->bindShape.m, m, sizeof(m)); + //skindata->bindShape.setIdentity(); const rapidjson::Value& skin_data_bones = skin_data_array_val_0["bones"]; for (rapidjson::SizeType i = 0; i < skin_data_bones.Size(); i++) @@ -208,22 +213,35 @@ bool Bundle3D::loadSkinData(const std::string& id, SkinData* skindata) const rapidjson::Value& skin_data_bone = skin_data_bones[i]; std::string name = skin_data_bone["node"].GetString(); skindata->boneNames.push_back(name); - /*const rapidjson::Value& bind_pos = skin_data_bone["bind_pos"]; + const rapidjson::Value& bind_pos = skin_data_bone["bind_pos"]; Mat4 mat_bind_pos; for (rapidjson::SizeType j = 0; j < bind_pos.Size(); j++) { mat_bind_pos.m[j] = bind_pos[j].GetDouble(); } - skindata->inverseBindPoseMatrices.push_back(mat_bind_pos);*/ + skindata->inverseBindPoseMatrices.push_back(mat_bind_pos.getInversed()); } const rapidjson::Value& skin_data_array_val_1 = skin_data_array[1]; const rapidjson::Value& bone_val_0 = skin_data_array_val_1["children"][(rapidjson::SizeType)0]; skindata->rootBoneIndex = skindata->getBoneNameIndex(bone_val_0["id"].GetString()); + //skindata->inverseBindPoseMatrices.resize(skindata->boneNames.size(), Mat4::IDENTITY); getChildMap(skindata->boneChild, skindata, bone_val_0); std::map > map_child; getChildMapT(map_child, skindata, bone_val_0); + + for (auto it : skindata->boneNames) { + CCLOG("bonename: %s", it.c_str()); + } + CCLOG("root: %s", skindata->boneNames[skindata->rootBoneIndex].c_str()); + for (auto itr : skindata->boneChild) { + CCLOG("parent: %s", skindata->boneNames[itr.first].c_str()); + for (auto itr2 : itr.second) { + CCLOG("%s", skindata->boneNames[(itr2)].c_str()); + } + CCLOG("////////"); + } return true; } @@ -307,6 +325,52 @@ bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animati } } } + + animationdata->_totalTime = 3; + + CCLOG("translation:////////////////"); + //animationdata->_translationKeys.erase(animationdata->_translationKeys.begin(), animationdata->_translationKeys.end()); + for (auto itr: animationdata->_translationKeys) + { + CCLOG("%s", itr.first.c_str()); + auto& keys = itr.second; + + if (keys.size()) + { + float maxtime = keys[keys.size() - 1]._time; + for (auto it : keys) { + it._time /= maxtime; + } + } + } + CCLOG("rotation:////////////////"); + //animationdata->_rotationKeys.erase(animationdata->_rotationKeys.begin(), animationdata->_rotationKeys.end()); + for (auto itr: animationdata->_rotationKeys) + { + CCLOG("%s", itr.first.c_str()); + auto& keys = itr.second; + if (keys.size()) + { + float maxtime = keys[keys.size() - 1]._time; + for (auto it : keys) { + it._time /= maxtime; + } + } + } + CCLOG("scale:////////////////"); + for (auto itr: animationdata->_scaleKeys) + { + CCLOG("%s", itr.first.c_str()); + auto& keys = itr.second; + if (keys.size()) + { + float maxtime = keys[keys.size() - 1]._time; + for (auto it : keys) { + it._time /= maxtime; + } + } + } + return true; } diff --git a/cocos/3d/CCMeshSkin.cpp b/cocos/3d/CCMeshSkin.cpp index ae6b6ce738..cfc94e9245 100644 --- a/cocos/3d/CCMeshSkin.cpp +++ b/cocos/3d/CCMeshSkin.cpp @@ -55,7 +55,8 @@ bool Bone::needUpdateWorldMat() const bone = bone->_parent; } - return needupdate; + return true; + //return needupdate; } //update own world matrix and children's @@ -63,7 +64,7 @@ void Bone::updateWorldMat() { getWorldMat(); for (auto itor : _children) { - itor->getWorldMat(); + itor->updateWorldMat(); } } @@ -74,7 +75,6 @@ const Mat4& Bone::getWorldMat() updateLocalMat(); if (_parent) { - updateLocalMat(); _world = _parent->getWorldMat() * _local; } else @@ -141,8 +141,10 @@ void Bone::updateJointMatrix(const Mat4& bindShape, Vec4* matrixPalette) //_jointMatrixDirty = false; static Mat4 t; Mat4::multiply(_world, getInverseBindPose(), &t); - Mat4::multiply(t, bindShape, &t); + //Mat4::multiply(t, bindShape, &t); + //Mat4::multiply(getInverseBindPose(), _world, &t); + //t.setIdentity(); matrixPalette[0].set(t.m[0], t.m[4], t.m[8], t.m[12]); matrixPalette[1].set(t.m[1], t.m[5], t.m[9], t.m[13]); matrixPalette[2].set(t.m[2], t.m[6], t.m[10], t.m[14]); @@ -199,6 +201,11 @@ Bone::~Bone() void Bone::updateLocalMat() { + Mat4::createTranslation(_localTranslate, &_local); + if (!_localRot.isZero()) + _local.rotate(_localRot); + return; + if (_dirtyFlag & Dirty_Translate) { Mat4::createTranslation(_localTranslate, &_local); diff --git a/cocos/3d/CCSprite3D.cpp b/cocos/3d/CCSprite3D.cpp index ff35ce6c94..ac52ff2b05 100644 --- a/cocos/3d/CCSprite3D.cpp +++ b/cocos/3d/CCSprite3D.cpp @@ -297,7 +297,7 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) _mesh->getIndexCount(), transform); - _meshCommand.setCullFaceEnabled(true); + //_meshCommand.setCullFaceEnabled(true); _meshCommand.setDepthTestEnabled(true); if (_skin) { diff --git a/cocos/renderer/ccShader_3D_PositionTex.vert b/cocos/renderer/ccShader_3D_PositionTex.vert index 835c00fc92..67210c4caa 100644 --- a/cocos/renderer/ccShader_3D_PositionTex.vert +++ b/cocos/renderer/ccShader_3D_PositionTex.vert @@ -44,23 +44,39 @@ void skinPosition(float blendWeight, int matrixIndex) vec4 getPosition() { _skinnedPosition = vec4(0.0); + vec4 matrixPalette1 = vec4(0.0); + vec4 matrixPalette2 = vec4(0.0); + vec4 matrixPalette3 = vec4(0.0); float blendWeight = a_blendWeight[0]; int matrixIndex = int (a_blendIndex[0]) * 3; - skinPosition(blendWeight, matrixIndex); + matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight; + matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight; + matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight; blendWeight = a_blendWeight[1]; matrixIndex = int(a_blendIndex[1]) * 3; - skinPosition(blendWeight, matrixIndex); + matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight; + matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight; + matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight; blendWeight = a_blendWeight[2]; matrixIndex = int(a_blendIndex[2]) * 3; - skinPosition(blendWeight, matrixIndex); + matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight; + matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight; + matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight; blendWeight = a_blendWeight[3]; matrixIndex = int(a_blendIndex[3]) * 3; - skinPosition(blendWeight, matrixIndex); + matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight; + matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight; + matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight; + _skinnedPosition.x = dot(a_position, matrixPalette1); + _skinnedPosition.y = dot(a_position, matrixPalette2); + _skinnedPosition.z = dot(a_position, matrixPalette3); + _skinnedPosition.w = a_position.w; + return _skinnedPosition; } diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 089c5de4a5..94d524a643 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -543,12 +543,13 @@ void Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p) { auto sprite = Sprite3D::create("Sprite3DTest/girl2.c3t"); //auto sprite = Sprite3D::create("Sprite3DTest/cube_anim.c3t"); - sprite->setScale(1.f); + //sprite->setScale(2.f); sprite->setTexture("Sprite3DTest/girl.png"); addChild(sprite); //sprite->setPosition3D(Vec3(p.x, p.y, -10.f)); - sprite->setRotation3D(Vec3(-90,0,0)); + //sprite->setRotation3D(Vec3(-90,0,0)); + //sprite->setRotationSkewY(90); sprite->setPosition( Vec2( p.x, p.y) ); auto animation = Animation3D::getOrCreate("Sprite3DTest/girl2.c3t"); diff --git a/tests/cpp-tests/Resources/Sprite3DTest/girl2.c3t b/tests/cpp-tests/Resources/Sprite3DTest/girl2.c3t index 1498bf0150..ac6ae32b4a 100644 --- a/tests/cpp-tests/Resources/Sprite3DTest/girl2.c3t +++ b/tests/cpp-tests/Resources/Sprite3DTest/girl2.c3t @@ -7314,7 +7314,7 @@ { "version": 1.200000, "id": "Bip001", - "bind_shape": [-0.682038, -0.035225, 0.730468, 0.000000, 0.731315, -0.035225, 0.681130, 0.000000, 0.001738, 0.998758, 0.049786, 0.000000, -0.882000, 78.798103, -0.868362, 1.000000], + "bind_shape": [-0.682038, -0.035225, 0.730468, 0.000000, 0.731315, -0.035225, 0.681130, 0.000000, 0.001738, 0.998758, 0.049786, 0.000000, -0.882000, 78.798103, -0.868362, 1.000000], "children": [ { "version": 1.200000,