automaton_common.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * central location for automaton script, called by automaton_JSON.html & automaton_inline.html
  3. */
  4. var scene; // for later camera jiggle in reset
  5. var cloth;
  6. var originalPos;
  7. var originalRot;
  8. var inAnim = false;
  9. // a custom non-linear Pace completionRatios durationRatios
  10. var hiccup = new BABYLON.Pace([.1, .8, .6, 1.0],[.25, .6, .8, 1.0]);
  11. // The creation of Deformations & Event Series need only be done once; requires no actual mesh; queue as often as you wish to run
  12. /**
  13. * Deformation is a sub-class of ReferenceDeformation, where the referenceStateName is Fixed to "BASIS"
  14. * @param {string} shapeKeyGroupName - Used by Automaton to place in the correct ShapeKeyGroup queue(s).
  15. * @param {string} endStateName - Name of state key to deform to
  16. * @param {number} milliDuration - The number of milli seconds the deformation is to be completed in
  17. * @param {number} millisBefore - Fixed wait period, once a syncPartner (if any) is also ready (default 0)
  18. * @param {number} endStateRatio - ratio of the end state to be obtained from reference state: -1 (mirror) to 1 (default 1)
  19. * @param {Vector3} movePOV - Mesh movement relative to its current position/rotation to be performed at the same time (default null)
  20. * right-up-forward
  21. * @param {Vector3} rotatePOV - Incremental Mesh rotation to be performed at the same time (default null)
  22. * flipBack-twirlClockwise-tiltRight
  23. * @param {Pace} pace - Any Object with the function: getCompletionMilestone(currentDurationRatio) (default Pace.LINEAR)
  24. */
  25. // Shape end- flip back -
  26. // Key dur- state twirl clockwise -
  27. // Group State ation wait ratio right-up-forward tilt right pace
  28. var stretching = [new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 900, 500, 0.9),
  29. new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 1500, 0, -0.1, null , null , hiccup)
  30. ];// illustrates the millisBefore parameter & the non-linear pace
  31. var hardFlap = [new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 800, 0, 0.1),
  32. new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 300, 0, -0.2, new BABYLON.Vector3( 0, 2, 0))
  33. ];// when your horizontal, up is really up; not all deformations need the same movePOV
  34. var away = [new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 200, 0, 0.3, new BABYLON.Vector3( 0, 1.5, 3.3)),
  35. new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 400, 0, -0.2, new BABYLON.Vector3( 0, 1.5, 6.7)),
  36. ];// climbing forward; series repeat acceleration applied when queued to avoid jerk start
  37. // forward velocity: (3.3 + 6.7) / (200 + 400) = 0.016666 units / milli
  38. var bankRight = [new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 750, 0, 0.1, new BABYLON.Vector3(-2, 0, 16), new BABYLON.Vector3(0, .4, .2)),
  39. new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 750, 0, -0.2, new BABYLON.Vector3(-2, 0, 16), new BABYLON.Vector3(0, .4, .2))
  40. ];// twirl clockwise while tilting right; going left while on your right side is really up
  41. // forward velocity: (16 + 16) / (750 + 750) = 0.021333 units / milli
  42. var backStretch = [new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 450, 0, 0.3, new BABYLON.Vector3( 0, 0, 12)),
  43. new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 450, 0, -0.2, new BABYLON.Vector3( 0, 0, 12))
  44. ];// need to make range (0.3 to -0.2), same as away, so can be seen so far away from camera
  45. // forward velocity: (12 + 12) / (450 + 450) = 0.026666 units / milli
  46. var turnRight = [new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 450, 0, -0.1, new BABYLON.Vector3( 3, 0, 24), new BABYLON.Vector3(0, .6, 0)),
  47. new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 450, 0, -0.2, new BABYLON.Vector3( 3, 0, 24), new BABYLON.Vector3(0, .6, 0))
  48. ];// twirl without aditional tilt; going right which starts to make it go down;
  49. // forward velocity: (24 + 24) / (450 + 450) = 0.053333 units / milli
  50. var tiltToHoriz = [new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 250, 0, 0.3, new BABYLON.Vector3( 0, -1, 8), new BABYLON.Vector3(0, 0, -.2)),
  51. new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 250, 0, -0.1, new BABYLON.Vector3( 0, -1, 8), new BABYLON.Vector3(0, 0, -.2))
  52. ];// reverse the tilt from 'transRight' and 'bankRight'; down hill
  53. // forward velocity: (8 + 8) / (250 + 250) = 0.032 units / milli
  54. var woosh = [new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 400, 0 , 0.3, new BABYLON.Vector3( 12, -1, 25)),
  55. new BABYLON.Deformation("ENTIRE MESH", "DRAPED", 400, 0, -0.1, new BABYLON.Vector3( 12, -1, 25))
  56. ];// cross over right / down hill; eat your heart out Roddenberry
  57. // forward velocity: (25 + 25) / (400 + 400) = 0.0625 units / milli
  58. // using the version of Deformation which does not default on the reference state, here "DRAPED", to going back to 'BASIS'
  59. var reset = [new BABYLON.ReferenceDeformation("ENTIRE MESH", "DRAPED", "BASIS", 1, 0, 1),
  60. function(){
  61. cloth.position = originalPos;
  62. cloth.rotation = originalRot;
  63. scene.activeCamera._getViewMatrix(); // jiggle camera to re-lock on target, also done by ShapeKeyGroup.incrementallyDeform()
  64. var report = cloth.getTrackingReport();
  65. window.alert(report);
  66. console.log(report);
  67. setInAnim(false);
  68. }
  69. ];
  70. /**
  71. * @param {Array} _eventSeries - Elements must either be a ReferenceDeformation, Action, or function. Min # of Deformations: 1
  72. * @param {number} _nRepeats - Number of times to run through series elements. There is sync across runs. (Default 1)
  73. * @param {number} _initialWallclockProrating - The factor to multiply the duration of a Deformation before passing to a
  74. * ShapeKeyGroup. Amount is decreased or increased across repeats, so that it is 1 for the final repeat.
  75. * Facilitates acceleration when > 1, & deceleration when < 1. (Default 1)
  76. * @param {string} _debug - Write progress messages to console when true (Default false)
  77. */
  78. // first run
  79. // Series nRepeats prorating debug
  80. var stretchSeries = new BABYLON.AutomatonEventSeries(stretching , 2);
  81. var hardFlapSeries = new BABYLON.AutomatonEventSeries(hardFlap , 4);
  82. var awaySeries = new BABYLON.AutomatonEventSeries(away , 5 , 2.0 , true); // demo extra message
  83. var bankRightSeries = new BABYLON.AutomatonEventSeries(bankRight , 3 , 0.021333 / 0.016666);
  84. var backStretchSeries = new BABYLON.AutomatonEventSeries(backStretch, 2 , 0.026666 / 0.021333);
  85. var turnRightSeries = new BABYLON.AutomatonEventSeries(turnRight , 2 , 0.053333 / 0.026666);
  86. var tiltToHorizSeries = new BABYLON.AutomatonEventSeries(tiltToHoriz, 3);
  87. var wooshSeries = new BABYLON.AutomatonEventSeries(woosh , 3);
  88. var resetSeries = new BABYLON.AutomatonEventSeries(reset);
  89. function prep(sceneArg){
  90. scene = sceneArg;
  91. cloth = scene.getMeshByID("Cloth");
  92. cloth.debug = true;
  93. originalPos = cloth.position.clone();
  94. originalRot = cloth.rotation.clone();
  95. var entireGrp = cloth.getShapeKeyGroup("ENTIRE MESH");
  96. entireGrp.mirrorAxisOnY(); // mirror on Y, so wings flapping up past horizontal created, using negative end state ratios
  97. // set test to false to try to compare performance in final reports
  98. if (1 === 1){
  99. entireGrp.addDerivedKey("BASIS", "DRAPED", -0.2);
  100. entireGrp.addDerivedKey("BASIS", "DRAPED", -0.1);
  101. entireGrp.addDerivedKey("BASIS", "DRAPED", 0.1);
  102. entireGrp.addDerivedKey("BASIS", "DRAPED", 0.3);
  103. entireGrp.addDerivedKey("BASIS", "DRAPED", 0.9);
  104. }
  105. }
  106. function setInAnim(inAnimArg){inAnim = inAnimArg;}
  107. function pausePlay() {
  108. console.log("Requesting " + (cloth.isPaused() ? "resume" : "pause"));
  109. // test instance pause-play
  110. if (cloth.isPaused()) cloth.resumePlay();
  111. else cloth.pausePlay();
  112. }
  113. function queueAnimation(){
  114. if (inAnim){
  115. console.log("queueAnimation while in progress ignored.");
  116. return;
  117. }
  118. setInAnim(true);
  119. // the following calls return immediately, due to being put on the appropriate ShapeKeyGroup(s) queues
  120. cloth.queueEventSeries(stretchSeries);
  121. cloth.queueEventSeries(hardFlapSeries);
  122. cloth.queueEventSeries(awaySeries);
  123. cloth.queueEventSeries(bankRightSeries);
  124. cloth.queueEventSeries(backStretchSeries);
  125. cloth.queueEventSeries(turnRightSeries);
  126. cloth.queueEventSeries(tiltToHorizSeries);
  127. cloth.queueEventSeries(wooshSeries);
  128. cloth.queueEventSeries(resetSeries);
  129. }