actions.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, new BABYLON.Vector3(0, 0, 0), scene);
  4. camera.setPosition(new BABYLON.Vector3(20, 200, 400));
  5. camera.attachControl(canvas, true);
  6. camera.lowerBetaLimit = 0.1;
  7. camera.upperBetaLimit = (Math.PI / 2) * 0.99;
  8. camera.lowerRadiusLimit = 150;
  9. scene.clearColor = new BABYLON.Color3(0, 0, 0);
  10. var light1 = new BABYLON.PointLight("omni", new BABYLON.Vector3(0, 50, 0), scene);
  11. var light2 = new BABYLON.PointLight("omni", new BABYLON.Vector3(0, 50, 0), scene);
  12. var light3 = new BABYLON.PointLight("omni", new BABYLON.Vector3(0, 50, 0), scene);
  13. light1.diffuse = BABYLON.Color3.Red();
  14. light2.diffuse = BABYLON.Color3.Green();
  15. light3.diffuse = BABYLON.Color3.Blue();
  16. // Define states
  17. light1.state = "on";
  18. light2.state = "on";
  19. light3.state = "on";
  20. // Ground
  21. var ground = BABYLON.Mesh.CreateGround("ground", 1000, 1000, 1, scene, false);
  22. var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
  23. groundMaterial.specularColor = BABYLON.Color3.Black();
  24. ground.material = groundMaterial;
  25. // Boxes
  26. var redBox = BABYLON.Mesh.CreateBox("red", 20, scene);
  27. var redMat = new BABYLON.StandardMaterial("ground", scene);
  28. redMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  29. redMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  30. redMat.emissiveColor = BABYLON.Color3.Red();
  31. redBox.material = redMat;
  32. redBox.position.x -= 100;
  33. var greenBox = BABYLON.Mesh.CreateBox("green", 20, scene);
  34. var greenMat = new BABYLON.StandardMaterial("ground", scene);
  35. greenMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  36. greenMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  37. greenMat.emissiveColor = BABYLON.Color3.Green();
  38. greenBox.material = greenMat;
  39. greenBox.position.z -= 100;
  40. var blueBox = BABYLON.Mesh.CreateBox("blue", 20, scene);
  41. var blueMat = new BABYLON.StandardMaterial("ground", scene);
  42. blueMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  43. blueMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  44. blueMat.emissiveColor = BABYLON.Color3.Blue();
  45. blueBox.material = blueMat;
  46. blueBox.position.x += 100;
  47. // Sphere
  48. var sphere = BABYLON.Mesh.CreateSphere("sphere", 16, 20, scene);
  49. var sphereMat = new BABYLON.StandardMaterial("ground", scene);
  50. sphereMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  51. sphereMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  52. sphereMat.emissiveColor = BABYLON.Color3.Purple();
  53. sphere.material = sphereMat;
  54. sphere.position.z += 100;
  55. // Rotating donut
  56. var donut = BABYLON.Mesh.CreateTorus("donut", 20, 8, 16, scene);
  57. // On pick interpolations
  58. var prepareButton = function (mesh, color, light) {
  59. var goToColorAction = new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnPickTrigger, light, "diffuse", color, 1000, null, true);
  60. mesh.actionManager = new BABYLON.ActionManager(scene);
  61. mesh.actionManager.registerAction(
  62. new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnPickTrigger, light, "diffuse", BABYLON.Color3.Black(), 1000))
  63. .then(new BABYLON.CombineAction(BABYLON.ActionManager.NothingTrigger, [ // Then is used to add a child action used alternatively with the root action.
  64. goToColorAction, // First click: root action. Second click: child action. Third click: going back to root action and so on...
  65. new BABYLON.SetValueAction(BABYLON.ActionManager.NothingTrigger, mesh.material, "wireframe", false)
  66. ]));
  67. mesh.actionManager.registerAction(new BABYLON.SetValueAction(BABYLON.ActionManager.OnPickTrigger, mesh.material, "wireframe", true))
  68. .then(new BABYLON.DoNothingAction());
  69. mesh.actionManager.registerAction(new BABYLON.SetStateAction(BABYLON.ActionManager.OnPickTrigger, light, "off"))
  70. .then(new BABYLON.SetStateAction(BABYLON.ActionManager.OnPickTrigger, light, "on"));
  71. }
  72. prepareButton(redBox, BABYLON.Color3.Red(), light1);
  73. prepareButton(greenBox, BABYLON.Color3.Green(), light2);
  74. prepareButton(blueBox, BABYLON.Color3.Blue(), light3);
  75. // Conditions
  76. sphere.actionManager = new BABYLON.ActionManager(scene);
  77. var condition1 = new BABYLON.StateCondition(sphere.actionManager, light1, "off");
  78. var condition2 = new BABYLON.StateCondition(sphere.actionManager, light1, "on");
  79. sphere.actionManager.registerAction(new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnLeftPickTrigger, camera, "alpha", 0, 500, condition1));
  80. sphere.actionManager.registerAction(new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnLeftPickTrigger, camera, "alpha", Math.PI, 500, condition2));
  81. // Over/Out
  82. var makeOverOut = function (mesh) {
  83. mesh.actionManager.registerAction(new BABYLON.SetValueAction(BABYLON.ActionManager.OnPointerOutTrigger, mesh.material, "emissiveColor", mesh.material.emissiveColor));
  84. mesh.actionManager.registerAction(new BABYLON.SetValueAction(BABYLON.ActionManager.OnPointerOverTrigger, mesh.material, "emissiveColor", BABYLON.Color3.White()));
  85. mesh.actionManager.registerAction(new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnPointerOutTrigger, mesh, "scaling", new BABYLON.Vector3(1, 1, 1), 150));
  86. mesh.actionManager.registerAction(new BABYLON.InterpolateValueAction(BABYLON.ActionManager.OnPointerOverTrigger, mesh, "scaling", new BABYLON.Vector3(1.1, 1.1, 1.1), 150));
  87. }
  88. makeOverOut(redBox);
  89. makeOverOut(greenBox);
  90. makeOverOut(blueBox);
  91. makeOverOut(sphere);
  92. // scene's actions
  93. scene.actionManager = new BABYLON.ActionManager(scene);
  94. var rotate = function (mesh) {
  95. scene.actionManager.registerAction(new BABYLON.IncrementValueAction(BABYLON.ActionManager.OnEveryFrameTrigger, mesh, "rotation.y", 0.01));
  96. }
  97. rotate(redBox);
  98. rotate(greenBox);
  99. rotate(blueBox);
  100. // Intersections
  101. donut.actionManager = new BABYLON.ActionManager(scene);
  102. donut.actionManager.registerAction(new BABYLON.SetValueAction(
  103. { trigger: BABYLON.ActionManager.OnIntersectionEnterTrigger, parameter: sphere },
  104. donut, "scaling", new BABYLON.Vector3(1.2, 1.2, 1.2)));
  105. donut.actionManager.registerAction(new BABYLON.SetValueAction(
  106. { trigger: BABYLON.ActionManager.OnIntersectionExitTrigger, parameter: sphere }
  107. , donut, "scaling", new BABYLON.Vector3(1, 1, 1)));
  108. // Animations
  109. var alpha = 0;
  110. scene.registerBeforeRender(function () {
  111. donut.position.x = 100 * Math.cos(alpha);
  112. donut.position.y = 5;
  113. donut.position.z = 100 * Math.sin(alpha);
  114. alpha += 0.01;
  115. });
  116. return scene;
  117. }