index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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", "test/jump.wav", scene);
  56. var explosion = new BABYLON.Sound("Explosion", "test/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. // Skybox
  64. var skybox = BABYLON.Mesh.CreateBox("skyBox", 500.0, scene);
  65. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  66. skyboxMaterial.backFaceCulling = false;
  67. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("/playground/textures/TropicalSunnyDay", scene);
  68. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  69. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  70. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  71. skyboxMaterial.disableLighting = true;
  72. skybox.material = skyboxMaterial;
  73. var sphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 10.0, 9.0, scene);
  74. var sphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 2.0, 9.0, scene);//Only two segments
  75. var sphere3 = BABYLON.Mesh.CreateSphere("Sphere3", 10.0, 9.0, scene);
  76. var sphere4 = BABYLON.Mesh.CreateSphere("Sphere4", 10.0, 9.0, scene);
  77. var sphere5 = BABYLON.Mesh.CreateSphere("Sphere5", 10.0, 9.0, scene);
  78. var sphere6 = BABYLON.Mesh.CreateSphere("Sphere6", 10.0, 9.0, scene);
  79. sphere1.position.x = 40;
  80. sphere2.position.x = 25;
  81. sphere3.position.x = 10;
  82. sphere4.position.x = -5;
  83. sphere5.position.x = -20;
  84. sphere6.position.x = -35;
  85. var rt1 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  86. scene.customRenderTargets.push(rt1);
  87. rt1.activeCamera = camera1;
  88. rt1.renderList = scene.meshes;
  89. var rt2 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  90. scene.customRenderTargets.push(rt2);
  91. rt2.activeCamera = camera2;
  92. rt2.renderList = scene.meshes;
  93. var rt3 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  94. scene.customRenderTargets.push(rt3);
  95. rt3.activeCamera = camera3;
  96. rt3.renderList = scene.meshes;
  97. var rt4 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  98. scene.customRenderTargets.push(rt4);
  99. rt4.activeCamera = camera4;
  100. rt4.renderList = scene.meshes;
  101. var mon1 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  102. mon1.position = new BABYLON.Vector3(-8.8, -7.8, 25)
  103. // mon1.showBoundingBox = true;
  104. var mon1mat = new BABYLON.StandardMaterial("texturePlane", scene);
  105. mon1mat.diffuseTexture = rt1;
  106. mon1.material = mon1mat;
  107. mon1.parent = camera;
  108. mon1.layerMask = 1;
  109. var mon2 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  110. mon2.position = new BABYLON.Vector3(-2.9, -7.8, 25)
  111. // mon2.showBoundingBox = true;
  112. var mon2mat = new BABYLON.StandardMaterial("texturePlane", scene);
  113. mon2mat.diffuseTexture = rt2;
  114. mon2.material = mon2mat;
  115. mon2.parent = camera;
  116. mon2.layerMask = 1;
  117. var mon3 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  118. mon3.position = new BABYLON.Vector3(2.9, -7.8, 25)
  119. // mon3.showBoundingBox = true;
  120. var mon3mat = new BABYLON.StandardMaterial("texturePlane", scene);
  121. mon3mat.diffuseTexture = rt3;
  122. mon3.material = mon3mat;
  123. mon3.parent = camera;
  124. mon3.layerMask = 1;
  125. var mon4 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  126. mon4.position = new BABYLON.Vector3(8.8, -7.8, 25)
  127. // mon4.showBoundingBox = true;
  128. var mon4mat = new BABYLON.StandardMaterial("texturePlane", scene);
  129. mon4mat.diffuseTexture = rt4;
  130. mon4.material = mon4mat;
  131. mon4.parent = camera;
  132. mon4.layerMask = 1;
  133. var butback = BABYLON.MeshBuilder.CreatePlane("plane", { width: 25, height: 6 }, scene);
  134. butback.position = new BABYLON.Vector3(0, -8.2, 26)
  135. // butback.showBoundingBox = true;
  136. var butbackmat = new BABYLON.StandardMaterial("texturePlane", scene);
  137. butbackmat.diffuseColor = BABYLON.Color3.Black();
  138. butback.material = butbackmat;
  139. butback.parent = camera;
  140. butback.layerMask = 1;
  141. var plane = BABYLON.Mesh.CreatePlane("plane", 120, scene);
  142. plane.position.y = -5;
  143. plane.rotation.x = Math.PI / 2;
  144. var materialSphere1 = new BABYLON.StandardMaterial("texture1", scene);
  145. materialSphere1.wireframe = true;
  146. //Creation of a red material with alpha
  147. var materialSphere2 = new BABYLON.StandardMaterial("texture2", scene);
  148. materialSphere2.diffuseColor = new BABYLON.Color3(1, 0, 0); //Red
  149. materialSphere2.alpha = 0.3;
  150. //Creation of a material with an image texture
  151. var materialSphere3 = new BABYLON.StandardMaterial("texture3", scene);
  152. materialSphere3.diffuseTexture = new BABYLON.Texture("/playground/textures/amiga.jpg", scene);
  153. //Creation of a material with translated texture
  154. var materialSphere4 = new BABYLON.StandardMaterial("texture4", scene);
  155. materialSphere4.diffuseTexture = new BABYLON.Texture("/playground/textures/floor.png", scene);
  156. materialSphere4.diffuseTexture.vOffset = 0.1;//Vertical offset of 10%
  157. materialSphere4.diffuseTexture.uOffset = 0.4;//Horizontal offset of 40%
  158. //Creation of a material with an alpha texture
  159. var materialSphere5 = new BABYLON.StandardMaterial("texture5", scene);
  160. materialSphere5.diffuseTexture = new BABYLON.Texture("/playground/textures/rock.png", scene);
  161. materialSphere5.diffuseTexture.hasAlpha = true;//Has an alpha
  162. //Creation of a material and show all the faces
  163. var materialSphere6 = new BABYLON.StandardMaterial("texture6", scene);
  164. materialSphere6.diffuseTexture = new BABYLON.Texture("/playground/textures/grass.png", scene);
  165. materialSphere6.diffuseTexture.hasAlpha = true;//Have an alpha
  166. materialSphere6.backFaceCulling = false;//Show all the faces of the element
  167. //Creation of a repeated textured material
  168. var materialPlane = new BABYLON.StandardMaterial("texturePlane", scene);
  169. materialPlane.diffuseTexture = new BABYLON.Texture("/playground/textures/mixMap.png", scene);
  170. materialPlane.diffuseTexture.uScale = 5.0;
  171. materialPlane.diffuseTexture.vScale = 5.0;
  172. materialPlane.backFaceCulling = false;
  173. //Apply the materials to meshes
  174. sphere1.material = materialSphere1;
  175. sphere2.material = materialSphere2;
  176. sphere3.material = materialSphere3;
  177. sphere4.material = materialSphere4;
  178. sphere5.material = materialSphere5;
  179. sphere6.material = materialSphere6;
  180. plane.material = materialPlane;
  181. new BABYLON.Mesh('mesh_without_geometry', scene);
  182. var d = 50;
  183. var cubes = new Array();
  184. for (var i = 0; i < 360; i += 20) {
  185. var r = BABYLON.Tools.ToRadians(i);
  186. var b = BABYLON.Mesh.CreateBox("Box #" + i / 20, 5, scene, false);
  187. b.position.x = Math.cos(r) * d;
  188. b.position.z = Math.sin(r) * d;
  189. cubes.push(b);
  190. }
  191. scene.getMeshByName("Box #6").scaling.x = 2;
  192. //Other meshes, to contrôl mesh hierarchy
  193. var box1 = BABYLON.MeshBuilder.CreateBox("box1", {size: 1}, scene);
  194. var box2 = BABYLON.MeshBuilder.CreateBox("box2", {size: 1}, scene);
  195. box2.position.x = 1.5;
  196. box2.parent = box1;
  197. var box3 = BABYLON.MeshBuilder.CreateBox("box3", {size: 1}, scene);
  198. box3.position.x = 1.5;
  199. box3.parent = box2;
  200. var box4 = BABYLON.MeshBuilder.CreateBox("box4", {size: 1}, scene);
  201. box4.position.x = 1.5;
  202. box4.parent = box3;
  203. window.addEventListener("keydown", function (evt) {
  204. sphere1.dispose();
  205. var sphere7 = BABYLON.Mesh.CreateSphere("Sphere7", 10.0, 9.0, scene);
  206. sphere7.position.x = 40;
  207. });
  208. var sphere_1 = BABYLON.Mesh.CreateSphere("_sphere_1", 16, 2, scene);
  209. var assets_mesh = new BABYLON.AbstractMesh("assets_mesh", scene);
  210. var sphere_2 = BABYLON.Mesh.CreateSphere("_sphere_2", 3, 2, scene);
  211. var sphere_3 = BABYLON.Mesh.CreateSphere("_sphere_3", 2, 2, scene);
  212. var scene_mesh = new BABYLON.AbstractMesh;
  213. scene_mesh.name="scene_mesh";
  214. sphere_1.parent = assets_mesh;
  215. sphere_2.parent = assets_mesh;
  216. sphere_3.parent = assets_mesh;
  217. for (var i=0; i<10 ; i++){
  218. var inst = sphere_1.createInstance("C_" + i + "clone");
  219. inst.isVisible = true;
  220. inst.setEnabled = true;
  221. inst.parent = scene_mesh;
  222. inst.position.x = i*2;
  223. inst.refreshBoundingInfo();
  224. inst.computeWorldMatrix();
  225. }
  226. // to test reflection prob texture handling
  227. var probe = new BABYLON.ReflectionProbe("probe", 512, scene);
  228. probe.renderList.push(sphere1);
  229. // gui
  230. var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
  231. var picker = new BABYLON.GUI.ColorPicker();
  232. picker.height = "150px";
  233. picker.width = "150px";
  234. picker.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  235. var checkbox = new BABYLON.GUI.Checkbox();
  236. checkbox.width = "20px";
  237. checkbox.height = "20px";
  238. var slider = new BABYLON.GUI.Slider();
  239. slider.minimum = 0;
  240. slider.maximum = 2 * Math.PI;
  241. slider.value = 0;
  242. slider.height = "20px";
  243. slider.width = "200px";
  244. var line = new BABYLON.GUI.Line();
  245. line.x1 = 10;
  246. line.y1 = 10;
  247. line.x2 = 1000;
  248. line.y2 = 500;
  249. var panel = new BABYLON.GUI.StackPanel();
  250. var button = BABYLON.GUI.Button.CreateSimpleButton("but", "Click Me");
  251. button.width = 0.2;
  252. button.height = "40px";
  253. button.color = "white";
  254. button.background = "green";
  255. panel.addControl(button);
  256. var button2 = BABYLON.GUI.Button.CreateSimpleButton("but2", "Click Me also!");
  257. button2.width = 0.2;
  258. button2.height = "40px";
  259. button2.color = "white";
  260. button2.background = "green";
  261. panel.addControl(button2);
  262. var ellipse1 = new BABYLON.GUI.Ellipse();
  263. ellipse1.width = "100px"
  264. ellipse1.height = "100px";
  265. ellipse1.color = "Orange";
  266. ellipse1.thickness = 4;
  267. ellipse1.background = "green";
  268. advancedTexture.addControl(ellipse1);
  269. advancedTexture.addControl(panel);
  270. advancedTexture.addControl(picker);
  271. advancedTexture.addControl(checkbox);
  272. advancedTexture.addControl(slider);
  273. advancedTexture.addControl(line);
  274. advancedTexture.addControl(checkbox);
  275. this.scene = scene;
  276. };
  277. return Test;
  278. }());