FrameAnimation.hx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package h3d.anim;
  2. import h3d.anim.Animation;
  3. class FrameObject extends AnimatedObject {
  4. public var frames : haxe.ds.Vector<h3d.Matrix>;
  5. public var alphas : haxe.ds.Vector<Float>;
  6. public var uvs : haxe.ds.Vector<Float>;
  7. override function clone() : AnimatedObject {
  8. var o = new FrameObject(objectName);
  9. o.frames = frames;
  10. o.alphas = alphas;
  11. o.uvs = uvs;
  12. return o;
  13. }
  14. }
  15. class FrameAnimation extends Animation {
  16. var syncFrame : Int;
  17. public function new(name,frame,sampling) {
  18. super(name,frame,sampling);
  19. syncFrame = -1;
  20. }
  21. public function addCurve( objName, frames ) {
  22. var f = new FrameObject(objName);
  23. f.frames = frames;
  24. objects.push(f);
  25. }
  26. public function addAlphaCurve( objName, alphas ) {
  27. var f = new FrameObject(objName);
  28. f.alphas = alphas;
  29. objects.push(f);
  30. }
  31. public function addUVCurve( objName, uvs ) {
  32. var f = new FrameObject(objName);
  33. f.uvs = uvs;
  34. objects.push(f);
  35. }
  36. inline function getFrames() : Array<FrameObject> {
  37. return cast objects;
  38. }
  39. override function initInstance() {
  40. super.initInstance();
  41. for( a in getFrames() )
  42. if( a.alphas != null && (a.targetObject == null || !a.targetObject.isMesh()) )
  43. throw a.objectName + " should be a mesh";
  44. }
  45. override function clone(?a:Animation) {
  46. if( a == null )
  47. a = new FrameAnimation(name, frameCount, sampling);
  48. super.clone(a);
  49. return a;
  50. }
  51. @:access(h3d.scene.Skin)
  52. override function sync( decompose = false ) {
  53. if( decompose ) throw "Decompose not supported on Frame Animation";
  54. var frame = Std.int(frame);
  55. if( frame < 0 ) frame = 0 else if( frame >= frameCount ) frame = frameCount - 1;
  56. if( frame == syncFrame )
  57. return;
  58. syncFrame = frame;
  59. for( o in getFrames() ) {
  60. if( o.alphas != null ) {
  61. var mat = o.targetObject.toMesh().material;
  62. if( mat.blendMode == Normal ) mat.blendMode = Alpha;
  63. mat.color.w = o.alphas[frame];
  64. } else if( o.targetSkin != null ) {
  65. o.targetSkin.currentRelPose[o.targetJoint] = o.frames[frame];
  66. o.targetSkin.jointsUpdated = true;
  67. } else
  68. o.targetObject.defaultTransform = o.frames[frame];
  69. }
  70. }
  71. }