test.js 8.3 KB

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