index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /// <reference path="../../dist/preview release/babylon.d.ts"/>
  2. var Test = (function () {
  3. function Test(canvasId) {
  4. var _this = this;
  5. var canvas = document.getElementById(canvasId);
  6. this.engine = new BABYLON.Engine(canvas, true);
  7. BABYLONDEVTOOLS.Loader.debugShortcut(this.engine);
  8. this.scene = null;
  9. window.addEventListener("resize", function () {
  10. _this.engine.resize();
  11. });
  12. this._run();
  13. }
  14. Test.prototype._run = function () {
  15. var _this = this;
  16. this._initScene();
  17. this.scene.debugLayer.show({
  18. popup: false,
  19. parentElement: document.getElementById('inspector'),
  20. newColors: {
  21. backgroundColor: '#eee',
  22. backgroundColorLighter: '#fff',
  23. backgroundColorLighter2: '#fff',
  24. backgroundColorLighter3: '#fff',
  25. color: '#333'
  26. }
  27. });
  28. this.scene.executeWhenReady(function () {
  29. _this.engine.runRenderLoop(function () {
  30. _this.scene.render();
  31. });
  32. });
  33. };
  34. Test.prototype._initScene = function () {
  35. var scene = new BABYLON.Scene(this.engine);
  36. var canvas = scene.getEngine().getRenderingCanvas();
  37. var sceneRoot = new BABYLON.TransformNode("abstractmesh");
  38. var tn = new BABYLON.TransformNode("transform node");
  39. // Our built-in 'ground' shape. Params: name, width, depth, subdivs, scene
  40. var ground = BABYLON.Mesh.CreateGround("node_damagedHelmet_-6514", 6, 6, 2, scene);
  41. ground.parent = tn;
  42. let num = 5;
  43. let angStep = 6.283185307 / num;
  44. let rad = 2;
  45. let p = sceneRoot;
  46. for (let i = 0; i < num; i++) {
  47. // Our built-in 'sphere' shape. Params: name, subdivs, size, scene
  48. let sphere = BABYLON.Mesh.CreateSphere(`sphere${i}`, 16, 2, scene);
  49. // Move the sphere upward 1/2 its height
  50. sphere.position.y = 0.2;
  51. sphere.position.x = Math.sin(i * angStep) * rad;
  52. sphere.position.z = Math.cos(i * angStep) * rad;
  53. sphere.parent = p;
  54. p = sphere;
  55. }
  56. let t = 0;
  57. scene.registerBeforeRender(() => {
  58. ground.rotation.y += 0.01;
  59. ground.position.y = Math.cos(t += 0.01);
  60. });
  61. scene.createDefaultCameraOrLight(true);
  62. scene.activeCamera.attachControl(canvas);
  63. this.scene = scene;
  64. };
  65. return Test;
  66. }());