index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /// <reference path="../../dist/preview release/babylon.d.ts"/>
  2. /// <reference path="../../dist/preview release/canvas2D/babylon.canvas2D.d.ts"/>
  3. var Test = (function () {
  4. function Test(canvasId) {
  5. var _this = this;
  6. var canvas = document.getElementById(canvasId);
  7. this.engine = new BABYLON.Engine(canvas, true);
  8. BABYLONDEVTOOLS.Loader.debugShortcut(this.engine);
  9. this.scene = null;
  10. window.addEventListener("resize", function () {
  11. _this.engine.resize();
  12. });
  13. this._run();
  14. }
  15. Test.prototype._run = function () {
  16. var _this = this;
  17. this._initScene();
  18. this.scene.debugLayer.show({
  19. popup: false,
  20. parentElement: document.getElementById('inspector'),
  21. newColors: {
  22. backgroundColor: '#eee',
  23. backgroundColorLighter: '#fff',
  24. backgroundColorLighter2: '#fff',
  25. backgroundColorLighter3: '#fff',
  26. color: '#333'
  27. }
  28. });
  29. this.scene.executeWhenReady(function () {
  30. _this._initGame();
  31. _this.engine.runRenderLoop(function () {
  32. _this.scene.render();
  33. });
  34. });
  35. };
  36. Test.prototype._initScene = function () {
  37. var scene = new BABYLON.Scene(this.engine);
  38. var canvas = scene.getEngine().getRenderingCanvas();
  39. var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  40. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, 1.0, 110, new BABYLON.Vector3(0, -20, 0), scene);
  41. camera.attachControl(canvas, true);
  42. var camera1 = new BABYLON.ArcRotateCamera("Camera1", 1.58, 1.2, 110, BABYLON.Vector3.Zero(), scene);
  43. var camera2 = new BABYLON.ArcRotateCamera("Camera2", Math.PI / 3, .8, 40, BABYLON.Vector3.Zero(), scene);
  44. var camera3 = new BABYLON.ArcRotateCamera("Camera3", -Math.PI / 5, 1.0, 70, BABYLON.Vector3.Zero(), scene);
  45. var camera4 = new BABYLON.ArcRotateCamera("Camera4", -Math.PI / 6, 1.3, 110, BABYLON.Vector3.Zero(), scene);
  46. camera1.layerMask = 2;
  47. camera2.layerMask = 2;
  48. camera3.layerMask = 2;
  49. camera4.layerMask = 2;
  50. /*
  51. camera1.parent = camera;
  52. camera2.parent = camera;
  53. camera3.parent = camera;
  54. camera4.parent = camera;
  55. */
  56. //Sounds
  57. var jump = new BABYLON.Sound("Jump", "/assets/sounds/jump.wav", scene);
  58. var explosion = new BABYLON.Sound("Explosion", "/assets/sounds/explosion.wav", scene);
  59. jump.setVolume(0.1);
  60. window.addEventListener("keydown", function (evt) {
  61. if (evt.keyCode === 32) {
  62. jump.play();
  63. }
  64. });
  65. var sphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 10.0, 9.0, scene);
  66. var sphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 2.0, 9.0, scene);//Only two segments
  67. var sphere3 = BABYLON.Mesh.CreateSphere("Sphere3", 10.0, 9.0, scene);
  68. var sphere4 = BABYLON.Mesh.CreateSphere("Sphere4", 10.0, 9.0, scene);
  69. var sphere5 = BABYLON.Mesh.CreateSphere("Sphere5", 10.0, 9.0, scene);
  70. var sphere6 = BABYLON.Mesh.CreateSphere("Sphere6", 10.0, 9.0, scene);
  71. sphere1.position.x = 40;
  72. sphere2.position.x = 25;
  73. sphere3.position.x = 10;
  74. sphere4.position.x = -5;
  75. sphere5.position.x = -20;
  76. sphere6.position.x = -35;
  77. var rt1 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  78. scene.customRenderTargets.push(rt1);
  79. rt1.activeCamera = camera1;
  80. rt1.renderList = scene.meshes;
  81. var rt2 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  82. scene.customRenderTargets.push(rt2);
  83. rt2.activeCamera = camera2;
  84. rt2.renderList = scene.meshes;
  85. var rt3 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  86. scene.customRenderTargets.push(rt3);
  87. rt3.activeCamera = camera3;
  88. rt3.renderList = scene.meshes;
  89. var rt4 = new BABYLON.RenderTargetTexture("depth", 1024, scene, true, true);
  90. scene.customRenderTargets.push(rt4);
  91. rt4.activeCamera = camera4;
  92. rt4.renderList = scene.meshes;
  93. var mon1 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  94. mon1.position = new BABYLON.Vector3(-8.8, -7.8, 25)
  95. // mon1.showBoundingBox = true;
  96. var mon1mat = new BABYLON.StandardMaterial("texturePlane", scene);
  97. mon1mat.diffuseTexture = rt1;
  98. mon1.material = mon1mat;
  99. mon1.parent = camera;
  100. mon1.layerMask = 1;
  101. var mon2 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  102. mon2.position = new BABYLON.Vector3(-2.9, -7.8, 25)
  103. // mon2.showBoundingBox = true;
  104. var mon2mat = new BABYLON.StandardMaterial("texturePlane", scene);
  105. mon2mat.diffuseTexture = rt2;
  106. mon2.material = mon2mat;
  107. mon2.parent = camera;
  108. mon2.layerMask = 1;
  109. var mon3 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  110. mon3.position = new BABYLON.Vector3(2.9, -7.8, 25)
  111. // mon3.showBoundingBox = true;
  112. var mon3mat = new BABYLON.StandardMaterial("texturePlane", scene);
  113. mon3mat.diffuseTexture = rt3;
  114. mon3.material = mon3mat;
  115. mon3.parent = camera;
  116. mon3.layerMask = 1;
  117. var mon4 = BABYLON.Mesh.CreatePlane("plane", 5, scene);
  118. mon4.position = new BABYLON.Vector3(8.8, -7.8, 25)
  119. // mon4.showBoundingBox = true;
  120. var mon4mat = new BABYLON.StandardMaterial("texturePlane", scene);
  121. mon4mat.diffuseTexture = rt4;
  122. mon4.material = mon4mat;
  123. mon4.parent = camera;
  124. mon4.layerMask = 1;
  125. var butback = BABYLON.MeshBuilder.CreatePlane("plane", { width: 25, height: 6 }, scene);
  126. butback.position = new BABYLON.Vector3(0, -8.2, 26)
  127. // butback.showBoundingBox = true;
  128. var butbackmat = new BABYLON.StandardMaterial("texturePlane", scene);
  129. butbackmat.diffuseColor = BABYLON.Color3.Black();
  130. butback.material = butbackmat;
  131. butback.parent = camera;
  132. butback.layerMask = 1;
  133. var plane = BABYLON.Mesh.CreatePlane("plane", 120, scene);
  134. plane.position.y = -5;
  135. plane.rotation.x = Math.PI / 2;
  136. var materialSphere1 = new BABYLON.StandardMaterial("texture1", scene);
  137. materialSphere1.wireframe = true;
  138. //Creation of a red material with alpha
  139. var materialSphere2 = new BABYLON.StandardMaterial("texture2", scene);
  140. materialSphere2.diffuseColor = new BABYLON.Color3(1, 0, 0); //Red
  141. materialSphere2.alpha = 0.3;
  142. //Creation of a material with an image texture
  143. var materialSphere3 = new BABYLON.StandardMaterial("texture3", scene);
  144. materialSphere3.diffuseTexture = new BABYLON.Texture("/assets/textures/amiga.jpg", scene);
  145. //Creation of a material with translated texture
  146. var materialSphere4 = new BABYLON.StandardMaterial("texture4", scene);
  147. materialSphere4.diffuseTexture = new BABYLON.Texture("/assets/textures/floor.png", scene);
  148. materialSphere4.diffuseTexture.vOffset = 0.1;//Vertical offset of 10%
  149. materialSphere4.diffuseTexture.uOffset = 0.4;//Horizontal offset of 40%
  150. //Creation of a material with an alpha texture
  151. var materialSphere5 = new BABYLON.StandardMaterial("texture5", scene);
  152. materialSphere5.diffuseTexture = new BABYLON.Texture("/assets/textures/rock.png", scene);
  153. materialSphere5.diffuseTexture.hasAlpha = true;//Has an alpha
  154. //Creation of a material and show all the faces
  155. var materialSphere6 = new BABYLON.StandardMaterial("texture6", scene);
  156. materialSphere6.diffuseTexture = new BABYLON.Texture("/assets/textures/grass.png", scene);
  157. materialSphere6.diffuseTexture.hasAlpha = true;//Have an alpha
  158. materialSphere6.backFaceCulling = false;//Show all the faces of the element
  159. //Creation of a repeated textured material
  160. var materialPlane = new BABYLON.StandardMaterial("texturePlane", scene);
  161. materialPlane.diffuseTexture = new BABYLON.Texture("/assets/textures/mixMap.png", scene);
  162. materialPlane.diffuseTexture.uScale = 5.0;
  163. materialPlane.diffuseTexture.vScale = 5.0;
  164. materialPlane.backFaceCulling = false;
  165. //Apply the materials to meshes
  166. sphere1.material = materialSphere1;
  167. sphere2.material = materialSphere2;
  168. sphere3.material = materialSphere3;
  169. sphere4.material = materialSphere4;
  170. sphere5.material = materialSphere5;
  171. sphere6.material = materialSphere6;
  172. plane.material = materialPlane;
  173. var d = 50;
  174. var cubes = new Array();
  175. for (var i = 0; i < 360; i += 20) {
  176. var r = BABYLON.Tools.ToRadians(i);
  177. var b = BABYLON.Mesh.CreateBox("Box #" + i / 20, 5, scene, false);
  178. b.position.x = Math.cos(r) * d;
  179. b.position.z = Math.sin(r) * d;
  180. cubes.push(b);
  181. }
  182. var canvas = new BABYLON.ScreenSpaceCanvas2D(scene, {
  183. id: "ScreenCanvas",
  184. cachingStrategy: BABYLON.Canvas2D.CACHESTRATEGY_TOPLEVELGROUPS
  185. });
  186. i = 0;
  187. for (var _i = 0, cubes_1 = cubes; _i < cubes_1.length; _i++) {
  188. var cube = cubes_1[_i];
  189. new BABYLON.Group2D({
  190. parent: canvas, id: "GroupTag #" + i, width: 80, height: 40, trackNode: cube, origin: BABYLON.Vector2.Zero(),
  191. children: [
  192. new BABYLON.Rectangle2D({
  193. id: "firstRect", width: 80, height: 26, x: 0, y: 0, origin: BABYLON.Vector2.Zero(), border: "#FFFFFFFF", fill: "#808080FF", children: [
  194. new BABYLON.Text2D(cube.name, { marginAlignment: "h: center, v:center", fontName: "bold 12px Arial" })
  195. ]
  196. })
  197. ]
  198. });
  199. ++i;
  200. }
  201. this.scene = scene;
  202. };
  203. Test.prototype._initGame = function () {
  204. this._createCanvas();
  205. };
  206. /**
  207. * Create the canvas2D
  208. */
  209. Test.prototype._createCanvas = function () {
  210. // object hierarchy g1 -> g2 -> rect
  211. // when cachingStrategy is 1 or 2 - everything is rendered
  212. // when it is 3 - only direct children of g1 are rendered
  213. var canvas = new BABYLON.ScreenSpaceCanvas2D(this.scene,
  214. {
  215. id: "ScreenCanvas",
  216. cachingStrategy: BABYLON.Canvas2D.CACHESTRATEGY_DONTCACHE
  217. }); // 1
  218. // cachingStrategy: BABYLON.Canvas2D.CACHESTRATEGY_TOPLEVELGROUPS }); // 2
  219. // cachingStrategy: BABYLON.Canvas2D.CACHESTRATEGY_ALLGROUPS }); // 3
  220. canvas.createCanvasProfileInfoCanvas();
  221. // parent group
  222. var g1 = new BABYLON.Group2D({
  223. parent: canvas, id: "G1",
  224. x: 50, y: 50, size: new BABYLON.Size(60, 60)
  225. });
  226. // just to see it
  227. let frame1 = new BABYLON.Rectangle2D({
  228. parent: g1,
  229. x: 0, y: 0, size: g1.size, border: "#FF0000FF"
  230. });
  231. // child group
  232. let g2 = new BABYLON.Group2D({
  233. parent: g1, id: "G2",
  234. x: 10, y: 10, size: new BABYLON.Size(40, 40)
  235. });
  236. // just to see it
  237. let frame2 = new BABYLON.Rectangle2D({ parent: g2, x: 0, y: 0, size: g2.size, border: "#0000FFFF" });
  238. let rect = new BABYLON.Rectangle2D({
  239. parent: g2, x: 10, y: 10, size: new BABYLON.Size(20, 20),
  240. fill: "#00FF00FF"
  241. });
  242. return canvas;
  243. };
  244. return Test;
  245. }());