test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, 16, 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. scene.beginAnimation(spaceDek3, 0, 100, true, 1.0);
  110. });
  111. // Animations
  112. var alpha = 0;
  113. var frameCount = 0;
  114. var reloop = 0;
  115. var startDate = new Date();
  116. var count = 0;
  117. scene.registerBeforeRender(function () {
  118. box.rotation.y += 0.01;
  119. cylinder.rotation.x += 0.01;
  120. sphere.rotation.y += 0.02;
  121. // box3.scaling.y = 1 + Math.cos(alpha);
  122. torus.rotation.z += 0.01;
  123. alpha += 0.01;
  124. if (spaceDek) {
  125. spaceDek.rotation.y += 0.01;
  126. }
  127. if (spaceDek2) {
  128. spaceDek2.rotation.y -= 0.01;
  129. }
  130. if (spaceDek3) {
  131. spaceDek3.rotation.y -= 0.01;
  132. }
  133. if (torus.intersectsMesh(box)) {
  134. material2.alpha = 1;
  135. torus.scaling = new BABYLON.Vector3(2, 2, 2);
  136. } else {
  137. material2.alpha = 0.5;
  138. torus.scaling = new BABYLON.Vector3(1, 1, 1);
  139. }
  140. // Sprites
  141. frameCount++;
  142. if (frameCount > 3) {
  143. frameCount = 0;
  144. reloop++;
  145. for (var index = 0; index < spriteManager.sprites.length; index++) {
  146. var sprite = spriteManager.sprites[index];
  147. sprite.position.x -= 0.1 * sprite.dir;
  148. if (reloop > 20) {
  149. sprite.dir *= -1;
  150. sprite.invertU = !sprite.invertU;
  151. }
  152. }
  153. if (reloop > 20) {
  154. reloop = 0;
  155. }
  156. }
  157. // Update dynamic texture
  158. var diff = (new Date() - startDate);
  159. if (diff > 200) {
  160. startDate = new Date();
  161. var textureContext = planeTexture.getContext();
  162. var size = planeTexture.getSize();
  163. var text = count.toString();
  164. textureContext.clearRect(0, 0, size.width, size.height);
  165. textureContext.font = "bold 120px Calibri";
  166. var textSize = textureContext.measureText(text);
  167. textureContext.fillStyle = "white";
  168. textureContext.fillText(text, (size.width - textSize.width) / 2, (size.height - 120) / 2);
  169. planeTexture.update();
  170. count++;
  171. }
  172. // Background
  173. background0.texture.uOffset += 0.001;
  174. });
  175. return scene;
  176. };