multi_group_common.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * central location for multi_group script, called by multi_group_JSON.html & multi_group_inline.html
  3. */
  4. var plane;
  5. function prep(scene){
  6. plane = scene.getMeshByID("Plane");
  7. plane.debug = true;
  8. // pretty important when there are multiple groups moving at the same time to pre-define your keys
  9. var leftGrp = plane.getShapeKeyGroup("LEFT");
  10. leftGrp.mirrorAxisOnY(); // mirror on Y, so bump can also be a depression, using negative end state ratios
  11. leftGrp.addDerivedKey("BASIS", "BUMP", -.2);
  12. leftGrp.addDerivedKey("BASIS", "BUMP", .2);
  13. var middleGrp = plane.getShapeKeyGroup("MIDDLE");
  14. middleGrp.mirrorAxisOnY(); // mirror on Y, so bump can also be a depression, using negative end state ratios
  15. middleGrp.addDerivedKey("BASIS", "BUMP", -.2);
  16. middleGrp.addDerivedKey("BASIS", "BUMP", .2);
  17. var rightGrp = plane.getShapeKeyGroup("RIGHT");
  18. rightGrp.mirrorAxisOnY(); // mirror on Y, so bump can also be a depression, using negative end state ratios
  19. rightGrp.addDerivedKey("BASIS", "BUMP", -.2);
  20. rightGrp.addDerivedKey("BASIS", "BUMP", .2);
  21. // testing of AutomatonEventSeriesAction, trigger on a pick
  22. var reset = [new BABYLON.ReferenceDeformation("LEFT" ,"BUMP", "BASIS", 1, 0, 1),
  23. new BABYLON.ReferenceDeformation("RIGHT" ,"BUMP", "BASIS", 1, 0, 1),
  24. new BABYLON.ReferenceDeformation("MIDDLE" ,"BUMP", "BASIS", 1, 0, 1),
  25. ];
  26. var resetSeries = new BABYLON.AutomatonEventSeries(reset);
  27. var resetAction = new BABYLON.AutomatonEventSeriesAction(BABYLON.ActionManager.OnPickTrigger, plane, resetSeries);
  28. plane.actionManager = new BABYLON.ActionManager(scene);
  29. plane.actionManager.registerAction(resetAction);
  30. }
  31. function left() {
  32. boing("LEFT");
  33. }
  34. function middle() {
  35. boing("MIDDLE");
  36. }
  37. function right() {
  38. boing("RIGHT");
  39. }
  40. function boing(group){
  41. /**
  42. * sub-class of ReferenceDeformation, where the referenceStateName is Fixed to "BASIS"
  43. * @param {string} shapeKeyGroupName - Used by Automaton to place in the correct ShapeKeyGroup queue(s).
  44. * @param {string} endStateName - Name of state key to deform to
  45. * @param {number} milliDuration - The number of milli seconds the deformation is to be completed in
  46. * @param {number} millisBefore - Fixed wait period, once a syncPartner (if any) is also ready (default 0)
  47. * @param {number} endStateRatio - ratio of the end state to be obtained from reference state: -1 (mirror) to 1 (default 1)
  48. * @param {Vector3} movePOV - Mesh movement relative to its current position/rotation to be performed at the same time (default null)
  49. * right-up-forward
  50. * @param {Vector3} rotatePOV - Incremental Mesh rotation to be performed at the same time (default null)
  51. * flipBack-twirlClockwise-tiltRight
  52. * @param {Pace} pace - Any Object with the function: getCompletionMilestone(currentDurationRatio) (default Pace.LINEAR)
  53. */
  54. // Shape end- flip back
  55. // Key dur- state twirl clockwise
  56. // Group State ation wait ratio right-up-forward tilt right pace
  57. var stretch = [new BABYLON.Deformation(group ,"BUMP" , 750, 0, 1.0),
  58. new BABYLON.Deformation(group ,"BUMP" , 150, 100, -.2)
  59. ];
  60. var vibrate = [new BABYLON.Deformation(group ,"BUMP" , 75, 0, .2),
  61. new BABYLON.Deformation(group ,"BUMP" , 75, 0, -.2),
  62. ];
  63. var reset = [new BABYLON.ReferenceDeformation(group ,"BUMP", "BASIS", 50, 0, 1),
  64. ];
  65. plane.queueEventSeries(new BABYLON.AutomatonEventSeries(stretch));
  66. plane.queueEventSeries(new BABYLON.AutomatonEventSeries(vibrate, 3, 0.8));
  67. plane.queueEventSeries(new BABYLON.AutomatonEventSeries(reset));
  68. }
  69. function drumming() {
  70. var dur = 75;
  71. // note right "BUMP" is in the opposite direction of left "BUMP", so down is > 0
  72. var rightDown = new BABYLON.ReferenceDeformation("RIGHT", "BASIS", "BUMP", dur, 300, .2); // starts too fast, & each subsequent down also needs to wait
  73. var rightLastDown = new BABYLON.ReferenceDeformation("RIGHT", "BASIS", "BUMP", dur, 300, .2); // in sync with left, but delay for it after both are started
  74. var rightUp = new BABYLON.ReferenceDeformation("RIGHT", "BASIS", "BUMP", dur, 0, -.2);
  75. var rightHorizontal = new BABYLON.ReferenceDeformation("RIGHT", "BUMP", "BASIS", dur, 0, 1);
  76. var rightStall = new BABYLON.ReferenceDeformation("RIGHT", "BUMP", "BASIS", 1, 150, 1); // same as rightHorizontal, so nothing happens (less CPU to wait)
  77. var leftDown = new BABYLON.ReferenceDeformation("LEFT" , "BASIS", "BUMP", dur, 0, -.2);
  78. var leftUp = new BABYLON.ReferenceDeformation("LEFT" , "BASIS", "BUMP", dur, 0, .2);
  79. var leftHorizontal = new BABYLON.ReferenceDeformation("LEFT" , "BUMP", "BASIS", dur, 0, 1);
  80. // make last down beats a sync pair
  81. leftDown .setSyncPartner(rightLastDown);
  82. rightLastDown.setSyncPartner(leftDown );
  83. var series = [
  84. // even though left is first in the series, sync will delay all lefts till rightLastDown is ready
  85. leftDown , leftUp , leftHorizontal,
  86. rightDown , rightUp, rightHorizontal,
  87. rightDown , rightUp, rightHorizontal,
  88. rightDown , rightUp, rightHorizontal,
  89. rightLastDown, rightUp, rightHorizontal, rightStall
  90. ];
  91. plane.queueEventSeries(new BABYLON.AutomatonEventSeries(series, 3));
  92. }
  93. function conflict() {
  94. // all three start at the same time, use delays for demo
  95. var series = [new BABYLON.Deformation("MIDDLE", "BUMP", 500, 1600, 1.0),
  96. new BABYLON.Deformation("RIGHT" , "BUMP", 500, 0, 1.0),
  97. new BABYLON.Deformation("LEFT" , "BUMP", 500, 0, 1.0),
  98. // functions and Actions run on the queue of the first series, in this case 'MIDDLE'
  99. function(){
  100. window.alert("Overlapping Shape Key Groups can exist, but it is up to the application programmer to manage, unlike here.\n\nAction test: Pick mesh to reset");
  101. }
  102. ];
  103. plane.queueEventSeries(new BABYLON.AutomatonEventSeries(series));
  104. }
  105. function pausePlay() {
  106. console.log("Requesting " + (BABYLON.Automaton.isSystemPaused() ? "resume" : "pause"));
  107. // test Automation system wide pause-play
  108. if (BABYLON.Automaton.isSystemPaused()) BABYLON.Automaton.resumeSystem();
  109. else BABYLON.Automaton.pauseSystem();
  110. }