AnimationObject3d.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package min3d.animation;
  2. import min3d.core.FacesBufferedList;
  3. import min3d.core.Object3d;
  4. import min3d.core.TextureList;
  5. import min3d.core.Vertices;
  6. public class AnimationObject3d extends Object3d {
  7. private int numFrames;
  8. private KeyFrame[] frames;
  9. private int currentFrameIndex;
  10. private long startTime;
  11. private long currentTime;
  12. private boolean isPlaying;
  13. private float interpolation;
  14. private float fps = 70;
  15. private boolean updateVertices = true;
  16. private String currentFrameName;
  17. private int loopStartIndex;
  18. private boolean loop = false;
  19. public AnimationObject3d(int $maxVertices, int $maxFaces, int $numFrames) {
  20. super($maxVertices, $maxFaces);
  21. this.numFrames = $numFrames;
  22. this.frames = new KeyFrame[numFrames];
  23. this.currentFrameIndex = 0;
  24. this.isPlaying = false;
  25. this.interpolation = 0;
  26. this._animationEnabled = true;
  27. }
  28. public AnimationObject3d(Vertices $vertices, FacesBufferedList $faces, TextureList $textures, KeyFrame[] $frames)
  29. {
  30. super($vertices, $faces, $textures);
  31. numFrames = $frames.length;
  32. frames = $frames;
  33. }
  34. public int getCurrentFrame() {
  35. return currentFrameIndex;
  36. }
  37. public void addFrame(KeyFrame frame) {
  38. frames[currentFrameIndex++] = frame;
  39. }
  40. public void setFrames(KeyFrame[] frames) {
  41. this.frames = frames;
  42. }
  43. public void play() {
  44. startTime = System.currentTimeMillis();
  45. isPlaying = true;
  46. currentFrameName = null;
  47. loop = false;
  48. }
  49. public void play(String name) {
  50. currentFrameIndex = 0;
  51. currentFrameName = name;
  52. for (int i = 0; i < numFrames; i++) {
  53. if (frames[i].getName().equals(name))
  54. {
  55. loopStartIndex = currentFrameIndex = i;
  56. break;
  57. }
  58. }
  59. startTime = System.currentTimeMillis();
  60. isPlaying = true;
  61. }
  62. public void play(String name, boolean loop) {
  63. this.loop = loop;
  64. play(name);
  65. }
  66. public void stop() {
  67. isPlaying = false;
  68. currentFrameIndex = 0;
  69. }
  70. public void pause() {
  71. isPlaying = false;
  72. }
  73. public void update() {
  74. if (!isPlaying || !updateVertices)
  75. return;
  76. currentTime = System.currentTimeMillis();
  77. KeyFrame currentFrame = frames[currentFrameIndex];
  78. KeyFrame nextFrame = frames[(currentFrameIndex + 1) % numFrames];
  79. if(currentFrameName != null && !currentFrameName.equals(currentFrame.getName()))
  80. {
  81. if(!loop)
  82. stop();
  83. else
  84. currentFrameIndex = loopStartIndex;
  85. return;
  86. }
  87. float[] currentVerts = currentFrame.getVertices();
  88. float[] nextVerts = nextFrame.getVertices();
  89. float[] currentNormals = currentFrame.getNormals();
  90. float[] nextNormals = nextFrame.getNormals();
  91. int numVerts = currentVerts.length;
  92. float[] interPolatedVerts = new float[numVerts];
  93. float[] interPolatedNormals = new float[numVerts];
  94. for (int i = 0; i < numVerts; i += 3) {
  95. interPolatedVerts[i] = currentVerts[i] + interpolation * (nextVerts[i] - currentVerts[i]);
  96. interPolatedVerts[i + 1] = currentVerts[i + 1] + interpolation * (nextVerts[i + 1] - currentVerts[i + 1]);
  97. interPolatedVerts[i + 2] = currentVerts[i + 2] + interpolation * (nextVerts[i + 2] - currentVerts[i + 2]);
  98. interPolatedNormals[i] = currentNormals[i] + interpolation * (nextNormals[i] - currentNormals[i]);
  99. interPolatedNormals[i + 1] = currentNormals[i + 1] + interpolation * (nextNormals[i + 1] - currentNormals[i + 1]);
  100. interPolatedNormals[i + 2] = currentNormals[i + 2] + interpolation * (nextNormals[i + 2] - currentNormals[i + 2]);
  101. }
  102. interpolation += fps * (currentTime - startTime) / 1000;
  103. vertices().overwriteNormals(interPolatedNormals);
  104. vertices().overwriteVerts(interPolatedVerts);
  105. if (interpolation > 1) {
  106. interpolation = 0;
  107. currentFrameIndex++;
  108. if (currentFrameIndex >= numFrames)
  109. currentFrameIndex = 0;
  110. }
  111. startTime = System.currentTimeMillis();
  112. }
  113. public float getFps() {
  114. return fps;
  115. }
  116. public void setFps(float fps) {
  117. this.fps = fps;
  118. }
  119. public Object3d clone(boolean cloneData)
  120. {
  121. Vertices v = cloneData ? _vertices.clone() : _vertices;
  122. FacesBufferedList f = cloneData ? _faces.clone() : _faces;
  123. //KeyFrame[] fr = cloneData ? getClonedFrames() : frames;
  124. AnimationObject3d clone = new AnimationObject3d(v, f, _textures, frames);
  125. clone.position().x = position().x;
  126. clone.position().y = position().y;
  127. clone.position().z = position().z;
  128. clone.rotation().x = rotation().x;
  129. clone.rotation().y = rotation().y;
  130. clone.rotation().z = rotation().z;
  131. clone.scale().x = scale().x;
  132. clone.scale().y = scale().y;
  133. clone.scale().z = scale().z;
  134. clone.setFps(fps);
  135. clone.animationEnabled(animationEnabled());
  136. return clone;
  137. }
  138. public KeyFrame[] getClonedFrames()
  139. {
  140. int len = frames.length;
  141. KeyFrame[] cl = new KeyFrame[len];
  142. for(int i=0; i<len; i++)
  143. {
  144. cl[i] = frames[i].clone();
  145. }
  146. return cl;
  147. }
  148. public boolean getUpdateVertices() {
  149. return updateVertices;
  150. }
  151. public void setUpdateVertices(boolean updateVertices) {
  152. this.updateVertices = updateVertices;
  153. }
  154. }