index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. this.scene.executeWhenReady(function () {
  20. _this._initGame();
  21. _this.engine.runRenderLoop(function () {
  22. _this.scene.render();
  23. });
  24. });
  25. };
  26. Test.prototype._initScene = function () {
  27. var scene = new BABYLON.Scene(this.engine);
  28. var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 4, Math.PI / 2.5, 200, BABYLON.Vector3.Zero(), scene);
  29. camera.attachControl(this.engine.getRenderingCanvas(), true);
  30. camera.minZ = 0.1;
  31. // Lights
  32. var light0 = new BABYLON.PointLight("Omni0", new BABYLON.Vector3(0, 10, 0), scene);
  33. var light1 = new BABYLON.PointLight("Omni1", new BABYLON.Vector3(0, -10, 0), scene);
  34. var light2 = new BABYLON.PointLight("Omni2", new BABYLON.Vector3(10, 0, 0), scene);
  35. var light3 = new BABYLON.DirectionalLight("Dir0", new BABYLON.Vector3(1, -1, 0), scene);
  36. var material = new BABYLON.StandardMaterial("kosh", scene);
  37. var sphere = BABYLON.Mesh.CreateSphere("Sphere", 16, 3, scene);
  38. // Creating light sphere
  39. var lightSphere0 = BABYLON.Mesh.CreateSphere("Sphere0", 16, 0.5, scene);
  40. var lightSphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 16, 0.5, scene);
  41. var lightSphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 16, 0.5, scene);
  42. lightSphere0.material = new BABYLON.StandardMaterial("red", scene);
  43. lightSphere0.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
  44. lightSphere0.material.specularColor = new BABYLON.Color3(0, 0, 0);
  45. lightSphere0.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
  46. lightSphere1.material = new BABYLON.StandardMaterial("green", scene);
  47. lightSphere1.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
  48. lightSphere1.material.specularColor = new BABYLON.Color3(0, 0, 0);
  49. lightSphere1.material.emissiveColor = new BABYLON.Color3(0, 1, 0);
  50. lightSphere2.material = new BABYLON.StandardMaterial("blue", scene);
  51. lightSphere2.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
  52. lightSphere2.material.specularColor = new BABYLON.Color3(0, 0, 0);
  53. lightSphere2.material.emissiveColor = new BABYLON.Color3(0, 0, 1);
  54. // Sphere material
  55. material.diffuseColor = new BABYLON.Color3(1, 1, 1);
  56. sphere.material = material;
  57. // Lights colors
  58. light0.diffuse = new BABYLON.Color3(1, 0, 0);
  59. light0.specular = new BABYLON.Color3(1, 0, 0);
  60. light1.diffuse = new BABYLON.Color3(0, 1, 0);
  61. light1.specular = new BABYLON.Color3(0, 1, 0);
  62. light2.diffuse = new BABYLON.Color3(0, 0, 1);
  63. light2.specular = new BABYLON.Color3(0, 0, 1);
  64. light3.diffuse = new BABYLON.Color3(1, 1, 1);
  65. light3.specular = new BABYLON.Color3(1, 1, 1);
  66. BABYLON.Effect.ShadersStore["customVertexShader"] = `precision highp float;
  67. attribute vec3 position;
  68. attribute vec2 uv;
  69. uniform mat4 worldViewProjection;
  70. varying vec2 vUV;
  71. varying vec3 vPos;
  72. void main(){vec4 p = vec4( position, 1. );
  73. gl_Position = worldViewProjection * vec4(position, 1.0);
  74. vUV = uv;
  75. }`;
  76. BABYLON.Effect.ShadersStore["customFragmentShader"] = 'precision highp float;varying vec3 vPos;uniform vec3 color;void main(){gl_FragColor=vec4(mix(color,vPos,.05),1.);}';
  77. var shaderMaterial = new BABYLON.ShaderMaterial("shader", scene, {
  78. vertex: "custom",
  79. fragment: "custom",
  80. }, {
  81. attributes: ["position", "normal", "uv"],
  82. uniforms: ["world", "worldView", "worldViewProjection", "view", "projection"]
  83. });
  84. sphere.material = shaderMaterial;
  85. // Animations
  86. var alpha = 0;
  87. scene.beforeRender = function () {
  88. light0.position = new BABYLON.Vector3(10 * Math.sin(alpha), 0, 10 * Math.cos(alpha));
  89. light1.position = new BABYLON.Vector3(10 * Math.sin(alpha), 0, -10 * Math.cos(alpha));
  90. light2.position = new BABYLON.Vector3(10 * Math.cos(alpha), 0, 10 * Math.sin(alpha));
  91. lightSphere0.position = light0.position;
  92. lightSphere1.position = light1.position;
  93. lightSphere2.position = light2.position;
  94. alpha += 0.01;
  95. };
  96. // Create texture panel
  97. let panel = BABYLON.Mesh.CreatePlane('plane', 10, scene);
  98. let tex = new BABYLON.StandardMaterial('panelMat', scene);
  99. tex.diffuseTexture = new BABYLON.Texture('/assets/textures/amiga.jpg', scene);
  100. panel.material = tex;
  101. this.scene = scene;
  102. };
  103. Test.prototype._initGame = function () {
  104. this._createCanvas();
  105. };
  106. /**
  107. * Create the canvas2D
  108. */
  109. Test.prototype._createCanvas = function () {
  110. // object hierarchy g1 -> g2 -> rect
  111. // when cachingStrategy is 1 or 2 - everything is rendered
  112. // when it is 3 - only direct children of g1 are rendered
  113. var canvas = new BABYLON.ScreenSpaceCanvas2D(this.scene,
  114. {
  115. id: "ScreenCanvas",
  116. cachingStrategy: BABYLON.Canvas2D.CACHESTRATEGY_DONTCACHE
  117. }); // 1
  118. // cachingStrategy: BABYLON.Canvas2D.CACHESTRATEGY_TOPLEVELGROUPS }); // 2
  119. // cachingStrategy: BABYLON.Canvas2D.CACHESTRATEGY_ALLGROUPS }); // 3
  120. canvas.createCanvasProfileInfoCanvas();
  121. // parent group
  122. var g1 = new BABYLON.Group2D({
  123. parent: canvas, id: "G1",
  124. x: 50, y: 50, size: new BABYLON.Size(60, 60)
  125. });
  126. // just to see it
  127. let frame1 = new BABYLON.Rectangle2D({
  128. parent: g1,
  129. x: 0, y: 0, size: g1.size, border: "#FF0000FF"
  130. });
  131. // child group
  132. let g2 = new BABYLON.Group2D({
  133. parent: g1, id: "G2",
  134. x: 10, y: 10, size: new BABYLON.Size(40, 40)
  135. });
  136. // just to see it
  137. let frame2 = new BABYLON.Rectangle2D({ parent: g2, x: 0, y: 0, size: g2.size, border: "#0000FFFF" });
  138. let rect = new BABYLON.Rectangle2D({
  139. parent: g2, x: 10, y: 10, size: new BABYLON.Size(20, 20),
  140. fill: "#00FF00FF"
  141. });
  142. return canvas;
  143. };
  144. return Test;
  145. }());