basic elements.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 8, 50, BABYLON.Vector3.Zero(), scene);
  4. camera.attachControl(canvas, true);
  5. var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  6. //Creation of a box
  7. //(name of the box, size, scene)
  8. var box = BABYLON.Mesh.CreateBox("box", 6.0, scene);
  9. //Creation of a sphere
  10. //(name of the sphere, segments, diameter, scene)
  11. var sphere = BABYLON.Mesh.CreateSphere("sphere", 10.0, 10.0, scene);
  12. //Creation of a plan
  13. //(name of the plane, size, scene)
  14. var plan = BABYLON.Mesh.CreatePlane("plane", 10.0, scene);
  15. //Creation of a cylinder
  16. //(name, height, diameter, tessellation, scene, updatable)
  17. var cylinder = BABYLON.Mesh.CreateCylinder("cylinder", 3, 3, 3, 6, 1, scene, false);
  18. // Creation of a torus
  19. // (name, diameter, thickness, tessellation, scene, updatable)
  20. var torus = BABYLON.Mesh.CreateTorus("torus", 5, 1, 10, scene, false);
  21. // Creation of a knot
  22. // (name, radius, tube, radialSegments, tubularSegments, p, q, scene, updatable)
  23. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 2, 0.5, 128, 64, 2, 3, scene);
  24. // Creation of a lines mesh
  25. var lines = BABYLON.Mesh.CreateLines("lines", [
  26. new BABYLON.Vector3(-10, 0, 0),
  27. new BABYLON.Vector3(10, 0, 0),
  28. new BABYLON.Vector3(0, 0, -10),
  29. new BABYLON.Vector3(0, 0, 10)
  30. ], scene);
  31. // Creation of a ribbon
  32. // let's first create many paths along a maths exponential function as an example
  33. var exponentialPath = function (p) {
  34. var path = [];
  35. for (var i = -10; i < 10; i++) {
  36. path.push(new BABYLON.Vector3(p, i, Math.sin(p / 3) * 5 * Math.exp(-(i - p) * (i - p) / 60) + i / 3));
  37. }
  38. return path;
  39. };
  40. // let's populate arrayOfPaths with all these different paths
  41. var arrayOfPaths = [];
  42. for (var p = 0; p < 20; p++) {
  43. arrayOfPaths[p] = exponentialPath(p);
  44. }
  45. // (name, array of paths, closeArray, closePath, offset, scene)
  46. var ribbon = BABYLON.Mesh.CreateRibbon("ribbon", arrayOfPaths, false, false, 0, scene);
  47. // Moving elements
  48. box.position = new BABYLON.Vector3(-10, 0, 0); // Using a vector
  49. sphere.position = new BABYLON.Vector3(0, 10, 0); // Using a vector
  50. plan.position.z = 10; // Using a single coordinate component
  51. cylinder.position.z = -10;
  52. torus.position.x = 10;
  53. knot.position.y = -10;
  54. ribbon.position = new BABYLON.Vector3(-10, -10, 20);
  55. return scene;
  56. }