test.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. var CreateTestScene = function (engine) {
  2. var scene = new BABYLON.Scene(engine);
  3. //var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 10, BABYLON.Vector3.Zero(), scene);
  4. var camera = new BABYLON.FreeCamera("Camera", new BABYLON.Vector3(0, 0, -10), scene);
  5. var camera2 = new BABYLON.FreeCamera("Camera", new BABYLON.Vector3(0, 0, -10), scene);
  6. var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 100, 50), scene);
  7. var material = new BABYLON.StandardMaterial("leaves", scene);
  8. var material2 = new BABYLON.StandardMaterial("kosh transparent", scene);
  9. var material3 = new BABYLON.StandardMaterial("kosh", scene);
  10. var planeMaterial = new BABYLON.StandardMaterial("plane material", scene);
  11. var box = BABYLON.Mesh.CreateBox("Box", 1.0, scene);
  12. var cylinder = BABYLON.Mesh.CreateCylinder("Cylinder", 2, 0.8, 0, 32, scene);
  13. var torus = BABYLON.Mesh.CreateTorus("Torus", 1.0, 0.5, 16, scene);
  14. var sphere = BABYLON.Mesh.CreateSphere("Sphere", 16, 3, scene);
  15. var plane = BABYLON.Mesh.CreatePlane("plane", 3, scene);
  16. camera.viewport = new BABYLON.Viewport(0.5, 0, 0.5, 1.0);
  17. camera2.viewport = new BABYLON.Viewport(0, 0, 0.5, 1.0);
  18. scene.activeCameras.push(camera);
  19. scene.activeCameras.push(camera2);
  20. //material.diffuseColor = new BABYLON.Color3(0, 0, 1);
  21. material.diffuseTexture = new BABYLON.Texture("Assets/Tree.png", scene);
  22. material.diffuseTexture.hasAlpha = true;
  23. material.backFaceCulling = false;
  24. material2.diffuseTexture = new BABYLON.Texture("Assets/kosh.jpg", scene);
  25. material2.alpha = 0.5;
  26. material2.backFaceCulling = false;
  27. material3.diffuseTexture = new BABYLON.Texture("Assets/kosh.jpg", scene);
  28. planeMaterial.backFaceCulling = false;
  29. var planeTexture = new BABYLON.DynamicTexture("dynamic texture", 512, scene, true);
  30. planeTexture.hasAlpha = true;
  31. planeMaterial.diffuseTexture = planeTexture;
  32. plane.billboardMode = BABYLON.Mesh.BILLBOARDMODE_ALL;
  33. box.material = material;
  34. cylinder.material = material3;
  35. torus.material = material2;
  36. sphere.material = material;
  37. plane.material = planeMaterial;
  38. cylinder.position.z += 13;
  39. cylinder.renderingGroupId = 1;
  40. torus.position.x -= 3;
  41. torus.parent = sphere;
  42. sphere.position.z = 3;
  43. plane.position = new BABYLON.Vector3(0, 7, 0);
  44. cylinder.parent = camera2;
  45. // Particles
  46. var particleSystem = new BABYLON.ParticleSystem("particles", 4000, scene);
  47. particleSystem.particleTexture = new BABYLON.Texture("Assets/Flare.png", scene);
  48. particleSystem.minAngularSpeed = -0.5;
  49. particleSystem.maxAngularSpeed = 0.5;
  50. particleSystem.minSize = 0.5;
  51. particleSystem.maxSize = 1.0;
  52. particleSystem.minLifeTime = 0.5;
  53. particleSystem.maxLifeTime = 1.0;
  54. particleSystem.emitter = torus;
  55. particleSystem.emitRate = 300;
  56. particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
  57. particleSystem.minEmitBox = new BABYLON.Vector3(0, 0.1, 0);
  58. particleSystem.maxEmitBox = new BABYLON.Vector3(0, -0.1, 0);
  59. particleSystem.gravity = new BABYLON.Vector3(0, -0.5, 0);
  60. particleSystem.start();
  61. // Mirror
  62. var mirror = BABYLON.Mesh.CreateBox("Mirror", 1.0, scene);
  63. mirror.scaling = new BABYLON.Vector3(100.0, 0.01, 100.0);
  64. mirror.material = new BABYLON.StandardMaterial("mirror", scene);
  65. mirror.material.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  66. mirror.material.reflectionTexture = new BABYLON.MirrorTexture("mirror", 512, scene, true);
  67. mirror.material.reflectionTexture.mirrorPlane = new BABYLON.Plane(0, -1.0, 0, -5.0);
  68. mirror.material.reflectionTexture.renderList = [box, cylinder, torus, sphere];
  69. mirror.material.reflectionTexture.level = 0.5;
  70. mirror.position = new BABYLON.Vector3(0, -5.0, 0);
  71. // Sprites
  72. var spriteManager = new BABYLON.SpriteManager("MonsterA", "Assets/Player.png", 500, 64, scene);
  73. for (var index = 0; index < 500; index++) {
  74. var sprite = new BABYLON.Sprite("toto", spriteManager);
  75. sprite.position.y = -4.5;
  76. sprite.position.z = Math.random() * 20 - 10;
  77. sprite.position.x = Math.random() * 20 - 10;
  78. sprite.dir = Math.round(Math.random()) * 2 - 1;
  79. sprite.invertU = (sprite.dir < 0);
  80. sprite.playAnimation(0, 9, true, 100);
  81. sprite.color = new BABYLON.Color4(1, 0, 0, 1);
  82. }
  83. // Backgrounds
  84. var background0 = new BABYLON.Layer("back0", "Assets/Layer0_0.png", scene);
  85. var background1 = new BABYLON.Layer("back1", "Assets/Layer1_0.png", scene);
  86. var foreground = new BABYLON.Layer("back0", "Assets/Layer2_0.png", scene, true, new BABYLON.Color4(1, 0, 0, 1));
  87. // Import
  88. var spaceDek;
  89. BABYLON.SceneLoader.ImportMesh("Vaisseau", "Scenes/SpaceDek/", "SpaceDek.babylon", scene, function (newMeshes, particleSystems) {
  90. spaceDek = newMeshes[0];
  91. for (var index = 0; index < newMeshes.length; index++) {
  92. mirror.material.reflectionTexture.renderList.push(newMeshes[index]);
  93. }
  94. spaceDek.position = new BABYLON.Vector3(0, 20, 0);
  95. spaceDek.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  96. });
  97. var spaceDek2;
  98. var spaceDek3;
  99. BABYLON.SceneLoader.ImportMesh("Vaisseau", "Scenes/SpaceDek/", "SpaceDek.babylon", scene, function (newMeshes) {
  100. spaceDek2 = newMeshes[0];
  101. spaceDek2.name = "Vaisseau 2";
  102. for (var index = 0; index < newMeshes.length; index++) {
  103. mirror.material.reflectionTexture.renderList.push(newMeshes[index]);
  104. }
  105. spaceDek2.position = new BABYLON.Vector3(40, 20, 0);
  106. spaceDek2.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  107. // Clone
  108. spaceDek3 = spaceDek2.clone("Vaisseau 3");
  109. spaceDek3.position = new BABYLON.Vector3(-50, 20, 0);
  110. spaceDek3.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  111. mirror.material.reflectionTexture.renderList.push(spaceDek3);
  112. var children = spaceDek3.getDescendants();
  113. for (var index = 0; index < children.length; index++) {
  114. mirror.material.reflectionTexture.renderList.push(children[index]);
  115. }
  116. spaceDek3.material = spaceDek2.material.clone("Vaisseau 3 mat");
  117. spaceDek3.material.emissiveColor = new BABYLON.Color3(1.0, 0, 0);
  118. spaceDek3.infiniteDistance = true;
  119. scene.beginAnimation(spaceDek3, 0, 100, true, 1.0);
  120. });
  121. // Animations
  122. var alpha = 0;
  123. var frameCount = 0;
  124. var reloop = 0;
  125. var startDate = new Date();
  126. var count = 0;
  127. scene.registerBeforeRender(function () {
  128. box.rotation.y += 0.01;
  129. // cylinder.rotation.x += 0.01;
  130. sphere.rotation.y += 0.02;
  131. // box3.scaling.y = 1 + Math.cos(alpha);
  132. torus.rotation.z += 0.01;
  133. alpha += 0.01;
  134. if (spaceDek) {
  135. spaceDek.rotation.y += 0.01;
  136. }
  137. if (spaceDek2) {
  138. spaceDek2.rotation.y -= 0.01;
  139. }
  140. if (spaceDek3) {
  141. spaceDek3.rotation.y -= 0.01;
  142. }
  143. if (torus.intersectsMesh(box, true)) {
  144. material2.alpha = 1;
  145. torus.scaling = new BABYLON.Vector3(2, 2, 2);
  146. } else {
  147. material2.alpha = 0.5;
  148. torus.scaling = new BABYLON.Vector3(1, 1, 1);
  149. }
  150. // Sprites
  151. frameCount++;
  152. if (frameCount > 3) {
  153. frameCount = 0;
  154. reloop++;
  155. for (var index = 0; index < spriteManager.sprites.length; index++) {
  156. var sprite = spriteManager.sprites[index];
  157. sprite.position.x -= 0.1 * sprite.dir;
  158. if (reloop > 20) {
  159. sprite.dir *= -1;
  160. sprite.invertU = !sprite.invertU;
  161. }
  162. }
  163. if (reloop > 20) {
  164. reloop = 0;
  165. }
  166. }
  167. // Update dynamic texture
  168. var diff = (new Date() - startDate);
  169. if (diff > 200) {
  170. startDate = new Date();
  171. var textureContext = planeTexture.getContext();
  172. var size = planeTexture.getSize();
  173. var text = count.toString();
  174. textureContext.clearRect(0, 0, size.width, size.height);
  175. textureContext.font = "bold 120px Calibri";
  176. var textSize = textureContext.measureText(text);
  177. textureContext.fillStyle = "white";
  178. textureContext.fillText(text, (size.width - textSize.width) / 2, (size.height - 120) / 2);
  179. planeTexture.update();
  180. count++;
  181. }
  182. // Background
  183. background0.texture.uOffset += 0.001;
  184. });
  185. return scene;
  186. };