index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  38. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, 1.0, 110, new BABYLON.Vector3(0, -20, 0), scene);
  39. camera.attachControl(canvas, true);
  40. var camera1 = new BABYLON.ArcRotateCamera("Camera1", 1.58, 1.2, 110, BABYLON.Vector3.Zero(), scene);
  41. var camera2 = new BABYLON.ArcRotateCamera("Camera2", Math.PI / 3, .8, 40, BABYLON.Vector3.Zero(), scene);
  42. var camera3 = new BABYLON.ArcRotateCamera("Camera3", -Math.PI / 5, 1.0, 70, BABYLON.Vector3.Zero(), scene);
  43. var camera4 = new BABYLON.ArcRotateCamera("Camera4", -Math.PI / 6, 1.3, 110, BABYLON.Vector3.Zero(), scene);
  44. camera1.layerMask = 2;
  45. camera2.layerMask = 2;
  46. camera3.layerMask = 2;
  47. camera4.layerMask = 2;
  48. /*
  49. camera1.parent = camera;
  50. camera2.parent = camera;
  51. camera3.parent = camera;
  52. camera4.parent = camera;
  53. */
  54. //Sounds
  55. var jump = new BABYLON.Sound("Jump", "/assets/sounds/jump.wav", scene);
  56. var explosion = new BABYLON.Sound("Explosion", "/assets/sounds/explosion.wav", scene);
  57. jump.setVolume(0.1);
  58. window.addEventListener("keydown", function (evt) {
  59. if (evt.keyCode === 32) {
  60. jump.play();
  61. }
  62. });
  63. var sphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 10.0, 9.0, scene);
  64. var sphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 2.0, 9.0, scene);//Only two segments
  65. var sphere3 = BABYLON.Mesh.CreateSphere("Sphere3", 10.0, 9.0, scene);
  66. var sphere4 = BABYLON.Mesh.CreateSphere("Sphere4", 10.0, 9.0, scene);
  67. var sphere5 = BABYLON.Mesh.CreateSphere("Sphere5", 10.0, 9.0, scene);
  68. var sphere6 = BABYLON.Mesh.CreateSphere("Sphere6", 10.0, 9.0, scene);
  69. sphere1.position.x = 40;
  70. sphere2.position.x = 25;
  71. sphere3.position.x = 10;
  72. sphere4.position.x = -5;
  73. sphere5.position.x = -20;
  74. sphere6.position.x = -35;
  75. var rt1 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  76. scene.customRenderTargets.push(rt1);
  77. rt1.activeCamera = camera1;
  78. rt1.renderList = scene.meshes;
  79. var rt2 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  80. scene.customRenderTargets.push(rt2);
  81. rt2.activeCamera = camera2;
  82. rt2.renderList = scene.meshes;
  83. var rt3 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  84. scene.customRenderTargets.push(rt3);
  85. rt3.activeCamera = camera3;
  86. rt3.renderList = scene.meshes;
  87. var rt4 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  88. scene.customRenderTargets.push(rt4);
  89. rt4.activeCamera = camera4;
  90. rt4.renderList = scene.meshes;
  91. var mon1 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  92. mon1.position = new BABYLON.Vector3(-8.8, -7.8, 25)
  93. // mon1.showBoundingBox = true;
  94. var mon1mat = new BABYLON.StandardMaterial("texturePlane", scene);
  95. mon1mat.diffuseTexture = rt1;
  96. mon1.material = mon1mat;
  97. mon1.parent = camera;
  98. mon1.layerMask = 1;
  99. var mon2 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  100. mon2.position = new BABYLON.Vector3(-2.9, -7.8, 25)
  101. // mon2.showBoundingBox = true;
  102. var mon2mat = new BABYLON.StandardMaterial("texturePlane", scene);
  103. mon2mat.diffuseTexture = rt2;
  104. mon2.material = mon2mat;
  105. mon2.parent = camera;
  106. mon2.layerMask = 1;
  107. var mon3 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  108. mon3.position = new BABYLON.Vector3(2.9, -7.8, 25)
  109. // mon3.showBoundingBox = true;
  110. var mon3mat = new BABYLON.StandardMaterial("texturePlane", scene);
  111. mon3mat.diffuseTexture = rt3;
  112. mon3.material = mon3mat;
  113. mon3.parent = camera;
  114. mon3.layerMask = 1;
  115. var mon4 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  116. mon4.position = new BABYLON.Vector3(8.8, -7.8, 25)
  117. // mon4.showBoundingBox = true;
  118. var mon4mat = new BABYLON.StandardMaterial("texturePlane", scene);
  119. mon4mat.diffuseTexture = rt4;
  120. mon4.material = mon4mat;
  121. mon4.parent = camera;
  122. mon4.layerMask = 1;
  123. var butback = BABYLON.MeshBuilder.CreatePlane("plane", { width: 25, height: 6 }, scene);
  124. butback.position = new BABYLON.Vector3(0, -8.2, 26)
  125. // butback.showBoundingBox = true;
  126. var butbackmat = new BABYLON.StandardMaterial("texturePlane", scene);
  127. butbackmat.diffuseColor = BABYLON.Color3.Black();
  128. butback.material = butbackmat;
  129. butback.parent = camera;
  130. butback.layerMask = 1;
  131. var plane = BABYLON.Mesh.CreatePlane("plane", 120, scene);
  132. plane.position.y = -5;
  133. plane.rotation.x = Math.PI / 2;
  134. var materialSphere1 = new BABYLON.StandardMaterial("texture1", scene);
  135. materialSphere1.wireframe = true;
  136. //Creation of a red material with alpha
  137. var materialSphere2 = new BABYLON.StandardMaterial("texture2", scene);
  138. materialSphere2.diffuseColor = new BABYLON.Color3(1, 0, 0); //Red
  139. materialSphere2.alpha = 0.3;
  140. //Creation of a material with an image texture
  141. var materialSphere3 = new BABYLON.StandardMaterial("texture3", scene);
  142. materialSphere3.diffuseTexture = new BABYLON.Texture("/assets/textures/amiga.jpg", scene);
  143. //Creation of a material with translated texture
  144. var materialSphere4 = new BABYLON.StandardMaterial("texture4", scene);
  145. materialSphere4.diffuseTexture = new BABYLON.Texture("/assets/textures/floor.png", scene);
  146. materialSphere4.diffuseTexture.vOffset = 0.1;//Vertical offset of 10%
  147. materialSphere4.diffuseTexture.uOffset = 0.4;//Horizontal offset of 40%
  148. //Creation of a material with an alpha texture
  149. var materialSphere5 = new BABYLON.StandardMaterial("texture5", scene);
  150. materialSphere5.diffuseTexture = new BABYLON.Texture("/assets/textures/rock.png", scene);
  151. materialSphere5.diffuseTexture.hasAlpha = true;//Has an alpha
  152. //Creation of a material and show all the faces
  153. var materialSphere6 = new BABYLON.StandardMaterial("texture6", scene);
  154. materialSphere6.diffuseTexture = new BABYLON.Texture("/assets/textures/grass.png", scene);
  155. materialSphere6.diffuseTexture.hasAlpha = true;//Have an alpha
  156. materialSphere6.backFaceCulling = false;//Show all the faces of the element
  157. //Creation of a repeated textured material
  158. var materialPlane = new BABYLON.StandardMaterial("texturePlane", scene);
  159. materialPlane.diffuseTexture = new BABYLON.Texture("/assets/textures/mixMap.png", scene);
  160. materialPlane.diffuseTexture.uScale = 5.0;
  161. materialPlane.diffuseTexture.vScale = 5.0;
  162. materialPlane.backFaceCulling = false;
  163. //Apply the materials to meshes
  164. sphere1.material = materialSphere1;
  165. sphere2.material = materialSphere2;
  166. sphere3.material = materialSphere3;
  167. sphere4.material = materialSphere4;
  168. sphere5.material = materialSphere5;
  169. sphere6.material = materialSphere6;
  170. plane.material = materialPlane;
  171. new BABYLON.Mesh('mesh_without_geometry', scene);
  172. var d = 50;
  173. var cubes = new Array();
  174. for (var i = 0; i < 360; i += 20) {
  175. var r = BABYLON.Tools.ToRadians(i);
  176. var b = BABYLON.Mesh.CreateBox("Box #" + i / 20, 5, scene, false);
  177. b.position.x = Math.cos(r) * d;
  178. b.position.z = Math.sin(r) * d;
  179. cubes.push(b);
  180. }
  181. //Other meshes
  182. var box1 = BABYLON.MeshBuilder.CreateBox("box1", {size: 1}, scene);
  183. var box2 = BABYLON.MeshBuilder.CreateBox("box2", {size: 1}, scene);
  184. box2.position.x = 1.5;
  185. box2.parent = box1;
  186. var box3 = BABYLON.MeshBuilder.CreateBox("box3", {size: 1}, scene);
  187. box3.position.x = 1.5;
  188. box3.parent = box2;
  189. var box4 = BABYLON.MeshBuilder.CreateBox("box4", {size: 1}, scene);
  190. box4.position.x = 1.5;
  191. box4.parent = box3;
  192. // gui
  193. var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
  194. var picker = new BABYLON.GUI.ColorPicker();
  195. picker.height = "150px";
  196. picker.width = "150px";
  197. picker.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  198. var checkbox = new BABYLON.GUI.Checkbox();
  199. checkbox.width = "20px";
  200. checkbox.height = "20px";
  201. var slider = new BABYLON.GUI.Slider();
  202. slider.minimum = 0;
  203. slider.maximum = 2 * Math.PI;
  204. slider.value = 0;
  205. slider.height = "20px";
  206. slider.width = "200px";
  207. var line = new BABYLON.GUI.Line();
  208. line.x1 = 10;
  209. line.y1 = 10;
  210. line.x2 = 1000;
  211. line.y2 = 500;
  212. var panel = new BABYLON.GUI.StackPanel();
  213. var button = BABYLON.GUI.Button.CreateSimpleButton("but", "Click Me");
  214. button.width = 0.2;
  215. button.height = "40px";
  216. button.color = "white";
  217. button.background = "green";
  218. panel.addControl(button);
  219. var button2 = BABYLON.GUI.Button.CreateSimpleButton("but2", "Click Me also!");
  220. button2.width = 0.2;
  221. button2.height = "40px";
  222. button2.color = "white";
  223. button2.background = "green";
  224. panel.addControl(button2);
  225. var ellipse1 = new BABYLON.GUI.Ellipse();
  226. ellipse1.width = "100px"
  227. ellipse1.height = "100px";
  228. ellipse1.color = "Orange";
  229. ellipse1.thickness = 4;
  230. ellipse1.background = "green";
  231. advancedTexture.addControl(ellipse1);
  232. advancedTexture.addControl(panel);
  233. advancedTexture.addControl(picker);
  234. advancedTexture.addControl(checkbox);
  235. advancedTexture.addControl(slider);
  236. advancedTexture.addControl(line);
  237. advancedTexture.addControl(checkbox);
  238. this.scene = scene;
  239. };
  240. return Test;
  241. }());