animationNode.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. this.animationUpdate = function(time, duration){
  2. //console.log("DO on animation update")
  3. }
  4. this.translateBy = function(translation, duration){
  5. this.startTranslationSIM = this.position || goog.vec.Vec3.create();
  6. var deltaTranslation = this.translationFromValue( translation );
  7. this.stopTranslationSIM = goog.vec.Vec3.add(
  8. this.startTranslationSIM,
  9. deltaTranslation,
  10. goog.vec.Vec3.create()
  11. );
  12. if(duration > 0) {
  13. this.animationDuration = duration;
  14. this.animationUpdate = function(time, duration) {
  15. this.position = goog.vec.Vec3.lerp(
  16. this.startTranslationSIM, this.stopTranslationSIM,
  17. time >= duration ? 1 : time / duration,
  18. goog.vec.Vec3.create()
  19. );
  20. }
  21. this.animationPlay(0, duration);
  22. }
  23. else {
  24. this.position = this.stopTranslationSIM;
  25. } //@ sourceURL=node3.animation.translateBy.vwf
  26. }
  27. this.translateTo = function(translation, duration){
  28. this.startTranslationSIM = this.position || goog.vec.Vec3.create();
  29. this.stopTranslationSIM = this.translationFromValue( translation );
  30. if(duration > 0) {
  31. this.animationDuration = duration;
  32. this.animationUpdate = function(time, duration) {
  33. this.position = goog.vec.Vec3.lerp(
  34. this.startTranslationSIM, this.stopTranslationSIM,
  35. duration == 0 ? duration : time / duration,
  36. goog.vec.Vec3.create()
  37. );
  38. }
  39. this.animationPlay(0, duration);
  40. }
  41. else {
  42. this.position = this.stopTranslationSIM;
  43. } //@ sourceURL=node3.animation.translateTo.vwf
  44. }
  45. this.rotateBy = function(rotation, duration, frame) {
  46. let rotationValue = this.translationFromValue(rotation);
  47. let deltaQuaternion = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  48. (THREE.Math.degToRad(rotationValue[0])),
  49. (THREE.Math.degToRad(rotationValue[1])),
  50. (THREE.Math.degToRad(rotationValue[2])), 'YXZ'
  51. ));
  52. this.quaterniateBy( deltaQuaternion, duration, frame ); //@ sourceURL=node3.animation.rotateBy.vwf
  53. }
  54. this.rotateTo = function(rotation, duration){
  55. let rotationValue = this.translationFromValue(rotation);
  56. let stopQuaternion = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  57. (THREE.Math.degToRad(rotationValue[0])),
  58. (THREE.Math.degToRad(rotationValue[1])),
  59. (THREE.Math.degToRad(rotationValue[2])), 'YXZ'
  60. ));
  61. this.quaterniateTo( stopQuaternion, duration ); //@ sourceURL=node3.animation.rotateTo.vwf
  62. }
  63. this.quaterniateBy = function(quaternion, duration, frame) {
  64. this.startQuaternionSIM = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  65. (THREE.Math.degToRad(this.rotation[0])),
  66. (THREE.Math.degToRad(this.rotation[1])),
  67. (THREE.Math.degToRad(this.rotation[2])), 'YXZ'
  68. ));
  69. var deltaQuaternion = (new THREE.Quaternion).copy(quaternion);
  70. if ( ! frame || frame == "rotated" ) {
  71. this.stopQuaternionSIM = (new THREE.Quaternion()).multiplyQuaternions(deltaQuaternion, this.startQuaternionSIM);
  72. } else if ( frame == "scaled" ) {
  73. this.stopQuaternionSIM = (new THREE.Quaternion()).multiplyQuaternions(this.startQuaternionSIM, deltaQuaternion);
  74. }
  75. //this.stopQuaternionSIM = (new THREE.Quaternion()).copy(quaternion);
  76. if(duration > 0) {
  77. this.animationDuration = duration;
  78. this.animationUpdate = function(time, duration) {
  79. let q = new THREE.Quaternion();
  80. let e = new THREE.Euler();
  81. let step = (time >= duration) ? 1 : time / duration;
  82. THREE.Quaternion.slerp(this.startQuaternionSIM, this.stopQuaternionSIM, q, step || 0);
  83. let interp = e.setFromQuaternion(q, 'YXZ');
  84. this.rotation = [THREE.Math.radToDeg(interp.x), THREE.Math.radToDeg(interp.y), THREE.Math.radToDeg(interp.z)];
  85. }
  86. this.animationPlay(0, duration);
  87. }
  88. else {
  89. let eE = new THREE.Euler();
  90. let eQ = (new THREE.Quaternion).copy(this.stopQuaternionSIM);
  91. let interpE = eE.setFromQuaternion(eQ, 'YXZ');
  92. this.rotation = [THREE.Math.radToDeg(interpE.x),THREE.Math.radToDeg(interpE.y), THREE.Math.radToDeg(interpE.z)];
  93. //this.quaternion = this.stopQuaternionSIM;
  94. } //@ sourceURL=node3.animation.quaterniateBy.vwf
  95. }
  96. this.scaleBy = function(scale, duration){
  97. this.startScaleSIM = this.scale || goog.vec.Vec3.createFromValues( 1, 1, 1 );
  98. var deltaScale = this.translationFromValue( scale );
  99. this.stopScaleSIM = goog.vec.Vec3.createFromValues(
  100. this.startScaleSIM[0] * deltaScale[0],
  101. this.startScaleSIM[1] * deltaScale[1],
  102. this.startScaleSIM[2] * deltaScale[2]
  103. );
  104. if(duration > 0) {
  105. this.animationDuration = duration;
  106. this.animationUpdate = function(time, duration) {
  107. this.scale = goog.vec.Vec3.lerp( // TODO: should be geometric interpolation
  108. this.startScaleSIM, this.stopScaleSIM,
  109. duration == 0 ? duration : time / duration,
  110. goog.vec.Vec3.create()
  111. );
  112. }
  113. this.animationPlay(0, duration);
  114. }
  115. else {
  116. this.scale = this.stopScaleSIM;
  117. } //@ sourceURL=node3.animation.scaleBy.vwf
  118. }
  119. this.scaleTo = function(scale, duration){
  120. this.startScaleSIM = this.scale || goog.vec.Vec3.createFromValues( 1, 1, 1 );
  121. this.stopScaleSIM = this.translationFromValue( scale );
  122. if(duration > 0) {
  123. this.animationDuration = duration;
  124. this.animationUpdate = function(time, duration) {
  125. this.scale = goog.vec.Vec3.lerp( // TODO: should be geometric interpolation
  126. this.startScaleSIM, this.stopScaleSIM,
  127. duration == 0 ? duration : time / duration,
  128. goog.vec.Vec3.create()
  129. );
  130. }
  131. this.animationPlay(0, duration);
  132. }
  133. else {
  134. this.scale = this.stopScaleSIM;
  135. }//@ sourceURL=node3.animation.scaleTo.vwf
  136. }
  137. this.transformBy = function(transform, duration){
  138. var startTransform = this.transform || goog.vec.Vec3.create();
  139. var deltaTransform = this.transformFromValue( transform );
  140. // Left multiply by the delta
  141. var stopTransform = goog.vec.Mat4.multMat( deltaTransform, startTransform, goog.vec.Mat4.createFloat32() );
  142. this.transformTo( stopTransform, duration ); //@ sourceURL=node3.animation.transformBy.vwf
  143. }
  144. this.transformTo = function(transform, duration){
  145. var stopTransform = this.transformFromValue( transform ) || goog.vec.Mat4.createIdentity();
  146. if ( duration > 0 ) {
  147. // Calculate the start and stop translations
  148. this.startTranslationSIM = this.translation || goog.vec.Vec3.create();
  149. this.stopTranslationSIM = goog.vec.Vec3.create();
  150. goog.vec.Mat4.getColumn( stopTransform, 3, this.stopTranslationSIM );
  151. // Calculate the start and stop quaternion and scale
  152. this.startScaleSIM = this.scale || goog.vec.Vec3.createFromValues( 1, 1, 1 );
  153. this.stopScaleSIM = goog.vec.Vec3.create();
  154. this.startQuaternionSIM = this.quaternion || goog.vec.Quaternion.createFromValues( 0, 0, 0, 1 );
  155. this.stopQuaternionSIM = goog.vec.Quaternion.fromRotationMatrix4(
  156. this.unscaledTransform( stopTransform || goog.vec.Mat4.createIdentity(), this.stopScaleSIM, goog.vec.Mat4.create() ),
  157. goog.vec.Quaternion.create()
  158. );
  159. this.animationDuration = duration;
  160. // Call the appropriate functions to do the translation and quaterniation (that is totally a word)
  161. this.animationUpdate = function(time, duration) {
  162. this.translation = goog.vec.Vec3.lerp(
  163. this.startTranslationSIM, this.stopTranslationSIM,
  164. duration == 0 ? duration : time / duration,
  165. goog.vec.Vec3.create()
  166. );
  167. this.quaternion = goog.vec.Quaternion.slerp(
  168. this.startQuaternionSIM, this.stopQuaternionSIM,
  169. duration == 0 ? duration : time / duration,
  170. goog.vec.Quaternion.create()
  171. );
  172. this.scale = goog.vec.Vec3.lerp( // TODO: should be geometric interpolation
  173. this.startScaleSIM, this.stopScaleSIM,
  174. duration == 0 ? duration : time / duration,
  175. goog.vec.Vec3.create()
  176. );
  177. }
  178. this.animationPlay(0, duration);
  179. }
  180. else {
  181. this.transform = stopTransform;
  182. } //@ sourceURL=node3.animation.transformTo.vwf
  183. }
  184. this.worldTransformBy = function(transform, duration){
  185. var startWorldTransform = this.worldTransform || goog.vec.Mat4.create();
  186. var deltaTransform = this.transformFromValue( transform );
  187. // Left multiply by the delta
  188. var stopWorldTransform = goog.vec.Mat4.multMat( deltaTransform, startWorldTransform,
  189. goog.vec.Mat4.createFloat32() );
  190. this.worldTransformTo( stopWorldTransform, duration ) //@ sourceURL=node3.animation.worldTransformBy.vwf
  191. }
  192. this.worldTransformTo = function(transform, duration){
  193. var stopWorldTransform = this.transformFromValue( transform );
  194. var stopTransform;
  195. if ( this.parent && this.parent.worldTransform ) {
  196. // We need to find the local transform that will bring about the desired world transform
  197. // The math for this looks like -
  198. // (new worldTransform) = (parentWorldTransform) * (transform)
  199. // So, if we left multiply both sides by the inverse of the parentWorldTransform...
  200. // inv(parentWorldTransform) * (new worldTransform) = (transform)
  201. // Find the inverse parent worldTransform
  202. var inverseParentWorldTransform = goog.vec.Mat4.createFloat32();
  203. if ( goog.vec.Mat4.invert( this.parent.worldTransform, inverseParentWorldTransform ) ) {
  204. // Left multiply the new worldTransform by the inverse parent worldTransform
  205. stopTransform = goog.vec.Mat4.multMat( inverseParentWorldTransform, stopWorldTransform,
  206. goog.vec.Mat4.createFloat32() );
  207. }
  208. else {
  209. stopTransform = goog.vec.Mat4.createIdentity();
  210. this.logger.error( "Parent '" + this.parent.id + "' transform matrix is not invertible: " +
  211. this.parent.transform );
  212. }
  213. }
  214. else {
  215. stopTransform = stopWorldTransform;
  216. }
  217. this.transformTo( stopTransform, duration ); //@ sourceURL=node3.animation.worldTransformTo.vwf
  218. }