animationNode.js 11 KB

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