test.js 8.0 KB

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