I created a 3DS Max exporter that exports mesh and animation data including skinned animations. In addition the exporter features the capability of exporting mesh only or animation only data which can be specified in an XML settings file when bulk exporting. Additionally, the exporter features transformation decomposition of animation data into scale, rotation and translation data. As a further optimization, the data is quantized reducing each frame’s transformation data to 20 bytes.
The exporter produces the required data format for my Character Animation System which leverages the aforementioned optimizations when blending transformations.
Source Code
An excerpt from the exporter: the function in which the animation data is extracted and quantized.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
/* file War3Exporter.cpp author Warsam Osman brief ExportAnimations definition */ void War3Exporter::ExportAnimations( Object& theMesh, IGameNode* pNode ) { int timeStep = 4800 / 30; theMesh.m_animation.reserve( ( m_SceneEndTime - m_SceneStartTime ) / timeStep ); GMatrix toParent; toParent.SetIdentity(); IGameNode* parent = pNode->GetNodeParent(); TimeValue sceneEndTime = m_SceneEndTime; if( m_exportType == MESH_ONLY ) { //Export only first frame sceneEndTime = m_SceneStartTime + timeStep; } //Quantization of transformations //First loop through the animation data and get the transformation bounds AABB3 translationBounds; AABB3 scaleBounds; Vec4f rotationBoundMin; Vec4f rotationBoundMax; for( int t = m_SceneStartTime; t < sceneEndTime; t += timeStep ) { if (parent) toParent = parent->GetWorldTM(t).Inverse(); Matrix44 tm = pNode->GetWorldTM(t) * toParent; //Extract translation Vec3f positionv = tm.GetTranslation(); translationBounds.add( positionv ); //Extract scale //Gramm Schmidt rotation to desired basis handedness Matrix44 gs = Orthonormalize( tm ); Vec3f scalev = ExtractRotationScale( tm, gs ); scaleBounds.add( scalev ); //Extract rotation Quaternion rotv( gs ); rotv.Normalize(); if (rotv.w < rotationBoundMin[0]) rotationBoundMin[0] = rotv.w; if (rotv.w > rotationBoundMax[0]) rotationBoundMax[0] = rotv.w; if (rotv.x < rotationBoundMin[1]) rotationBoundMin[1] = rotv.x; if (rotv.x > rotationBoundMax[1]) rotationBoundMax[1] = rotv.x; if (rotv.y < rotationBoundMin[2]) rotationBoundMin[2] = rotv.y; if (rotv.y > rotationBoundMax[2]) rotationBoundMax[2] = rotv.y; if (rotv.z < rotationBoundMin[3]) rotationBoundMin[3] = rotv.z; if (rotv.z > rotationBoundMax[3]) rotationBoundMax[3] = rotv.z; } theMesh.scaleBounds = scaleBounds; theMesh.translationBounds = translationBounds; theMesh.rotationBoundMin = rotationBoundMin; theMesh.rotationBoundMax = rotationBoundMax; const Vec3f translationDimensions = translationBounds.size(); const Vec3f scaleDimensions = scaleBounds.size(); const Vec4f rotationDimension = rotationBoundMax - rotationBoundMin; //Loop through scene again and use the transformation bounds to quantize transformations for( int t = m_SceneStartTime; t < sceneEndTime; t += timeStep ) { Transforms trans; if (parent) toParent = parent->GetWorldTM(t).Inverse(); Matrix44 tm = pNode->GetWorldTM(t) * toParent; Vec3f transaltionVec3f, scaleVec3f; Vec4f rotationVec4f; //Extract translation transaltionVec3f = tm.GetTranslation(); //Extract scale Matrix44 gs = Orthonormalize( tm ); scaleVec3f = ExtractRotationScale( tm, gs ); //Extract rotation rotationVec4f = Quaternion( gs ); //Quantize for( int i = 0; i < 4; ++i ) { const float rotationInDimension = RangeMap<float,float>( rotationVec4f[i], rotationBoundMin[i], rotationBoundMax[i], 0.f, rotationDimension[i]); trans.rotation[i] = RangeMap<float,unsigned short>( rotationInDimension, 0.f, rotationDimension[i], 0, MAXWORD); } for( int i = 0; i < 3; ++i ) { const float translationInDimension = RangeMap<float,float>( transaltionVec3f[i], theMesh.translationBounds.m_min[i], theMesh.translationBounds.m_max[i], 0.f, translationDimensions[i]); trans.translation[i] = RangeMap<float,unsigned short>( translationInDimension, 0.f, translationDimensions[i], 0, MAXWORD); const float scaleInDimension = RangeMap<float,float>( scaleVec3f[i], theMesh.scaleBounds.m_min[i], theMesh.scaleBounds.m_max[i], 0.f, scaleDimensions[i]); trans.scale[i] = RangeMap<float,unsigned short>( scaleInDimension, 0.f, scaleDimensions[i], 0, MAXWORD); } //Verify data by reverse quantization: Vec3f transTemp, scaleTemp; Vec4f rotTemp; const float invMaxWord = static_cast< float >( 1.0f / MAXWORD ); for( int i = 0; i < 3; ++i ) { const float transDimOverMaxWord = translationDimensions[i]*invMaxWord; const float trans16TimesDimOverWord = trans.translation[i] * transDimOverMaxWord; transTemp[i] = trans16TimesDimOverWord + theMesh.translationBounds.m_min[i]; const float scaleDimOverMaxWord = scaleDimensions[i]*invMaxWord; const float scale16TimesDimOverWord = trans.scale[i] * scaleDimOverMaxWord; scaleTemp[i] = scale16TimesDimOverWord + theMesh.scaleBounds.m_min[i]; } for( int i = 0; i < 4; ++i ) { const float rotDimOverMaxWord = rotationDimension[i]*invMaxWord; const float rot16TimesDimOverWord = trans.rotation[i] * rotDimOverMaxWord; rotTemp[i] = rot16TimesDimOverWord + theMesh.rotationBoundMin[i]; } //Ok if within desired percentage const float MINIMUM_ACCEPTABLE_ERROR = 0.01f; if( fabsf((transTemp - transaltionVec3f).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR ) throw("Quantized translation not within MINIMUM_ACCEPTABLE_ERROR"); if( fabsf((scaleTemp - scaleVec3f ).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR ) throw("Quantized scale not within MINIMUM_ACCEPTABLE_ERROR"); if( fabsf((rotTemp - rotationVec4f).CalcLength()) > MINIMUM_ACCEPTABLE_ERROR ) throw("Quantized rotation not within MINIMUM_ACCEPTABLE_ERROR"); theMesh.numFrames++; theMesh.m_animation.push_back( trans ); } if( m_exportType == MESH_ONLY ) return; //Optimization - collapse to single if all frames the same if( theMesh.numFrames > 1 ) { bool staticAnimation = true; for( size_t i = 0; i < theMesh.numFrames; ++i ) { if( theMesh.m_animation[0] != theMesh.m_animation[i] ) { staticAnimation = false; break; } } if( staticAnimation ) { theMesh.m_animation.erase(theMesh.m_animation.begin()+1, theMesh.m_animation.end()); theMesh.numFrames = 1; } } } |