diff --git a/example/animation/animation.dart b/example/animation/animation.dart index 429e234..7b758eb 100644 --- a/example/animation/animation.dart +++ b/example/animation/animation.dart @@ -168,20 +168,20 @@ List extractTicks(List data) { return out; } -List extractValueVec3(List data) { - List out = new List(data.length); +List extractValueVec3(List data) { + List out = new List(data.length); for (int i = 0; i < data.length; i++) { var p = data[i]['value']; - out[i] = new VM.Vector4(p[0], p[1], p[2], 1.0); + out[i] = new VM.Vector3(p[0], p[1], p[2]); } return out; } -List extractValueVec4(List data) { - List out = new List(data.length); +List extractValueQuaternion(List data) { + List out = new List(data.length); for (int i = 0; i < data.length; i++) { var p = data[i]['value']; - out[i] = new VM.Vector4(p[0], p[1], p[2], p[3]); + out[i] = new VM.Quaternion(p[0], p[1], p[2], p[3]); } return out; } @@ -216,7 +216,7 @@ SkeletonAnimation ReadAnim( BoneAnimation ba = new BoneAnimation(name, index, extractTicks(positions), extractValueVec3(positions), - extractTicks(rotations), extractValueVec4(rotations), + extractTicks(rotations), extractValueQuaternion(rotations), extractTicks(scales), extractValueVec3(scales)); sa.InsertBone(ba); } diff --git a/lib/src/animation/skeleton_animation.dart b/lib/src/animation/skeleton_animation.dart index 43411ce..59baa4a 100644 --- a/lib/src/animation/skeleton_animation.dart +++ b/lib/src/animation/skeleton_animation.dart @@ -94,15 +94,11 @@ class BoneAnimation { final int boneIndex; List _positionTimes; - List _positionValues; + List _positionValues; List _rotationTimes; - List _rotationValues; + List _rotationValues; List _scaleTimes; - List _scaleValues; - - final VM.Matrix4 _positionMatrix = new VM.Matrix4.zero(); - final VM.Matrix4 _rotationMatrix = new VM.Matrix4.zero(); - final VM.Matrix4 _scaleMatrix = new VM.Matrix4.zero(); + List _scaleValues; /// Construct bone animation with [boneName]. Animation key frames /// will be loaded from [positions], [rotations], and [scales]. @@ -116,14 +112,18 @@ class BoneAnimation { this._scaleTimes, this._scaleValues) { if (_positionTimes == null || _positionTimes.length == 0) { - setNoPositionAnimation(); + _positionTimes = [0]; + _positionValues = [new VM.Vector3(0.0, 0.0, 0.0)]; } if (_rotationTimes == null || _rotationTimes.length == 0) { - setNoRotationAnimation(); + _rotationTimes = [0]; + _rotationValues = [new VM.Quaternion(0.0, 0.0, 0.0, 1.0)]; } + if (_scaleTimes == null || _scaleTimes.length == 0) { - setNoScaleAnimation(); + _scaleTimes = [0]; + _scaleValues = [new VM.Vector3(1.0, 1.0, 1.0)]; } assert(_rotationTimes.length > 0); @@ -134,24 +134,6 @@ class BoneAnimation { assert(_scaleTimes.length == _scaleValues.length); } - /// Makes bone have no position animation. - void setNoPositionAnimation() { - _positionTimes = [0]; - _positionValues = [new VM.Vector4(0.0, 0.0, 0.0, 0.1)]; - } - - /// Makes bone have no rotation animation. - void setNoRotationAnimation() { - _rotationTimes = [0]; - _rotationValues = [new VM.Vector4(0.0, 0.0, 0.0, 1.0)]; - } - - /// Makes bone have no scale animation. - void setNoScaleAnimation() { - _scaleTimes = [0]; - _scaleValues = [new VM.Vector4(1.0, 1.0, 1.0, 1.0)]; - } - static int _findTime(List timeList, int t) { for (int i = 0; i < timeList.length - 1; i++) { if (t < timeList[i + 1]) { @@ -161,72 +143,19 @@ class BoneAnimation { return 0; } - void _buildScaledMatrixAtTick(int t) { - final int scaleIndex = _findTime(_scaleTimes, t); + /// Set [boneMatrix] to correspond to bone animation at time [t]. + /// Does not interpolate between key frames. + void setBoneMatrixAtTick(int tick, VM.Matrix4 boneMatrix) { + final int scaleIndex = _findTime(_scaleTimes, tick); assert(scaleIndex >= 0); - _scaleMatrix[0] = _scaleValues[scaleIndex].x; - _scaleMatrix[5] = _scaleValues[scaleIndex].y; - _scaleMatrix[10] = _scaleValues[scaleIndex].z; - _scaleMatrix[15] = 1.0; - } - - void _buildPositionMatrixAtTick(int t) { - final int positionIndex = _findTime(_positionTimes, t); + final int positionIndex = _findTime(_positionTimes, tick); assert(positionIndex >= 0); - _positionMatrix[0] = 1.0; - _positionMatrix[5] = 1.0; - _positionMatrix[10] = 1.0; - _positionMatrix[12] = _positionValues[positionIndex].x; - _positionMatrix[13] = _positionValues[positionIndex].y; - _positionMatrix[14] = _positionValues[positionIndex].z; - _positionMatrix[15] = 1.0; - } - - void _buildRotationMatrixAtTick(int t) { - final int rotationIndex = _findTime(_rotationTimes, t); + final int rotationIndex = _findTime(_rotationTimes, tick); assert(rotationIndex >= 0); - - double x = _rotationValues[rotationIndex].x; - double y = _rotationValues[rotationIndex].y; - double z = _rotationValues[rotationIndex].z; - double w = _rotationValues[rotationIndex].w; - double x2 = x + x; - double y2 = y + y; - double z2 = z + z; - - double xx = x * x2; - double xy = x * y2; - double xz = x * z2; - double yy = y * y2; - double yz = y * z2; - double zz = z * z2; - double wx = w * x2; - double wy = w * y2; - double wz = w * z2; - - _rotationMatrix[0] = 1.0 - (yy + zz); - _rotationMatrix[1] = xy + wz; - _rotationMatrix[2] = xz - wy; - _rotationMatrix[4] = xy - wz; - _rotationMatrix[5] = 1.0 - (xx + zz); - _rotationMatrix[6] = yz + wx; - _rotationMatrix[8] = xz + wy; - _rotationMatrix[9] = yz - wx; - _rotationMatrix[10] = 1.0 - (xx + yy); - _rotationMatrix[15] = 1.0; - } - - /// Set [boneMatrix] to correspond to bone animation at time [t]. - /// Does not interpolate between key frames. - void setBoneMatrixAtTick(int t, VM.Matrix4 boneMatrix) { - _buildRotationMatrixAtTick(t); - _buildScaledMatrixAtTick(t); - _buildPositionMatrixAtTick(t); - // boneMatrix = _positionMatrix * _scaleMatrix * _rotationMatrix - // TODO: use "multiply into" if available - boneMatrix.setFrom(_positionMatrix); - boneMatrix.multiply(_scaleMatrix); - boneMatrix.multiply(_rotationMatrix); + VM.Vector3 s = _scaleValues[scaleIndex]; + VM.Vector3 t = _positionValues[positionIndex]; + VM.Quaternion r = _rotationValues[rotationIndex]; + boneMatrix.setFromTranslationRotationScale(t, r, s); } }