animationNode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.x)),
  49. (THREE.Math.degToRad(rotationValue.y)),
  50. (THREE.Math.degToRad(rotationValue.z)), 'XYZ'
  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.x)),
  58. (THREE.Math.degToRad(rotationValue.y)),
  59. (THREE.Math.degToRad(rotationValue.z)), 'XYZ'
  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.x)),
  66. (THREE.Math.degToRad(this.rotation.y)),
  67. (THREE.Math.degToRad(this.rotation.z)), 'XYZ'
  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, 'XYZ');
  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, 'XYZ');
  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.quaterniateTo = function(quaternion, duration) {
  97. this.startQuaternionSIM = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  98. (THREE.Math.degToRad(this.rotation.x)),
  99. (THREE.Math.degToRad(this.rotation.y)),
  100. (THREE.Math.degToRad(this.rotation.z)), 'XYZ'
  101. ));
  102. this.stopQuaternionSIM = (new THREE.Quaternion).copy(quaternion);
  103. if(duration > 0) {
  104. this.animationDuration = duration;
  105. this.animationUpdate = function(time, duration) {
  106. let q = new THREE.Quaternion();
  107. let e = new THREE.Euler();
  108. let step = (time >= duration) ? 1 : time / duration;
  109. THREE.Quaternion.slerp(this.startQuaternionSIM, this.stopQuaternionSIM, q, step || 0);
  110. let interp = e.setFromQuaternion(q, 'XYZ');
  111. this.rotation = [THREE.Math.radToDeg(interp.x), THREE.Math.radToDeg(interp.y), THREE.Math.radToDeg(interp.z)];
  112. }
  113. this.animationPlay(0, duration);
  114. }
  115. else {
  116. let eE = new THREE.Euler();
  117. let eQ = (new THREE.Quaternion).copy(this.stopQuaternionSIM);
  118. let interpE = eE.setFromQuaternion(eQ, 'XYZ');
  119. this.rotation = [THREE.Math.radToDeg(interpE.x),THREE.Math.radToDeg(interpE.y), THREE.Math.radToDeg(interpE.z)];
  120. //this.quaternion = this.stopQuaternionSIM;
  121. } //@ sourceURL=node3.animation.quaterniateTo.vwf
  122. }
  123. this.scaleBy = function(scale, duration){
  124. this.startScaleSIM = this.scale || goog.vec.Vec3.createFromValues( 1, 1, 1 );
  125. var deltaScale = this.translationFromValue( scale );
  126. this.stopScaleSIM = goog.vec.Vec3.createFromValues(
  127. this.startScaleSIM[0] * deltaScale[0],
  128. this.startScaleSIM[1] * deltaScale[1],
  129. this.startScaleSIM[2] * deltaScale[2]
  130. );
  131. if(duration > 0) {
  132. this.animationDuration = duration;
  133. this.animationUpdate = function(time, duration) {
  134. this.scale = goog.vec.Vec3.lerp( // TODO: should be geometric interpolation
  135. this.startScaleSIM, this.stopScaleSIM,
  136. duration == 0 ? duration : time / duration,
  137. goog.vec.Vec3.create()
  138. );
  139. }
  140. this.animationPlay(0, duration);
  141. }
  142. else {
  143. this.scale = this.stopScaleSIM;
  144. } //@ sourceURL=node3.animation.scaleBy.vwf
  145. }
  146. this.scaleTo = function(scale, duration){
  147. this.startScaleSIM = this.scale || goog.vec.Vec3.createFromValues( 1, 1, 1 );
  148. this.stopScaleSIM = this.translationFromValue( scale );
  149. if(duration > 0) {
  150. this.animationDuration = duration;
  151. this.animationUpdate = function(time, duration) {
  152. this.scale = goog.vec.Vec3.lerp( // TODO: should be geometric interpolation
  153. this.startScaleSIM, this.stopScaleSIM,
  154. duration == 0 ? duration : time / duration,
  155. goog.vec.Vec3.create()
  156. );
  157. }
  158. this.animationPlay(0, duration);
  159. }
  160. else {
  161. this.scale = this.stopScaleSIM;
  162. }//@ sourceURL=node3.animation.scaleTo.vwf
  163. }
  164. this.transformBy = function(transform, duration){
  165. var startTransform = this.transform || goog.vec.Vec3.create();
  166. var deltaTransform = this.transformFromValue( transform );
  167. // Left multiply by the delta
  168. var stopTransform = goog.vec.Mat4.multMat( deltaTransform, startTransform, goog.vec.Mat4.createFloat32() );
  169. this.transformTo( stopTransform, duration ); //@ sourceURL=node3.animation.transformBy.vwf
  170. }
  171. this.transformTo = function(transform, duration){
  172. var stopTransform = this.transformFromValue( transform ) || goog.vec.Mat4.createIdentity();
  173. if ( duration > 0 ) {
  174. // Calculate the start and stop translations
  175. this.startTranslationSIM = this.translation || goog.vec.Vec3.create();
  176. this.stopTranslationSIM = goog.vec.Vec3.create();
  177. goog.vec.Mat4.getColumn( stopTransform, 3, this.stopTranslationSIM );
  178. // Calculate the start and stop quaternion and scale
  179. this.startScaleSIM = this.scale || goog.vec.Vec3.createFromValues( 1, 1, 1 );
  180. this.stopScaleSIM = goog.vec.Vec3.create();
  181. this.startQuaternionSIM = this.quaternion || goog.vec.Quaternion.createFromValues( 0, 0, 0, 1 );
  182. this.stopQuaternionSIM = goog.vec.Quaternion.fromRotationMatrix4(
  183. this.unscaledTransform( stopTransform || goog.vec.Mat4.createIdentity(), this.stopScaleSIM, goog.vec.Mat4.create() ),
  184. goog.vec.Quaternion.create()
  185. );
  186. this.animationDuration = duration;
  187. // Call the appropriate functions to do the translation and quaterniation (that is totally a word)
  188. this.animationUpdate = function(time, duration) {
  189. this.translation = goog.vec.Vec3.lerp(
  190. this.startTranslationSIM, this.stopTranslationSIM,
  191. duration == 0 ? duration : time / duration,
  192. goog.vec.Vec3.create()
  193. );
  194. this.quaternion = goog.vec.Quaternion.slerp(
  195. this.startQuaternionSIM, this.stopQuaternionSIM,
  196. duration == 0 ? duration : time / duration,
  197. goog.vec.Quaternion.create()
  198. );
  199. this.scale = goog.vec.Vec3.lerp( // TODO: should be geometric interpolation
  200. this.startScaleSIM, this.stopScaleSIM,
  201. duration == 0 ? duration : time / duration,
  202. goog.vec.Vec3.create()
  203. );
  204. }
  205. this.animationPlay(0, duration);
  206. }
  207. else {
  208. this.transform = stopTransform;
  209. } //@ sourceURL=node3.animation.transformTo.vwf
  210. }
  211. this.worldTransformBy = function(transform, duration){
  212. var startWorldTransform = this.worldTransform || goog.vec.Mat4.create();
  213. var deltaTransform = this.transformFromValue( transform );
  214. // Left multiply by the delta
  215. var stopWorldTransform = goog.vec.Mat4.multMat( deltaTransform, startWorldTransform,
  216. goog.vec.Mat4.createFloat32() );
  217. this.worldTransformTo( stopWorldTransform, duration ) //@ sourceURL=node3.animation.worldTransformBy.vwf
  218. }
  219. this.worldTransformTo = function(transform, duration){
  220. var stopWorldTransform = this.transformFromValue( transform );
  221. var stopTransform;
  222. if ( this.parent && this.parent.worldTransform ) {
  223. // We need to find the local transform that will bring about the desired world transform
  224. // The math for this looks like -
  225. // (new worldTransform) = (parentWorldTransform) * (transform)
  226. // So, if we left multiply both sides by the inverse of the parentWorldTransform...
  227. // inv(parentWorldTransform) * (new worldTransform) = (transform)
  228. // Find the inverse parent worldTransform
  229. var inverseParentWorldTransform = goog.vec.Mat4.createFloat32();
  230. if ( goog.vec.Mat4.invert( this.parent.worldTransform, inverseParentWorldTransform ) ) {
  231. // Left multiply the new worldTransform by the inverse parent worldTransform
  232. stopTransform = goog.vec.Mat4.multMat( inverseParentWorldTransform, stopWorldTransform,
  233. goog.vec.Mat4.createFloat32() );
  234. }
  235. else {
  236. stopTransform = goog.vec.Mat4.createIdentity();
  237. this.logger.error( "Parent '" + this.parent.id + "' transform matrix is not invertible: " +
  238. this.parent.transform );
  239. }
  240. }
  241. else {
  242. stopTransform = stopWorldTransform;
  243. }
  244. this.transformTo( stopTransform, duration ); //@ sourceURL=node3.animation.worldTransformTo.vwf
  245. }