index.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Shaders Library</title>
  5. <script src="dat.gui.min.js"></script>
  6. <script src="../../babylon.max.js"></script>
  7. <script src="../dist/babylon.simpleMaterial.js"></script>
  8. <style>
  9. html, body {
  10. width: 100%;
  11. height: 100%;
  12. padding: 0;
  13. margin: 0;
  14. overflow: hidden;
  15. }
  16. #renderCanvas {
  17. width: 100%;
  18. height: 100%;
  19. }
  20. #fps {
  21. position: absolute;
  22. background-color: black;
  23. border: 2px solid red;
  24. text-align: center;
  25. font-size: 16px;
  26. color: white;
  27. top: 15px;
  28. left: 10px;
  29. width: 60px;
  30. height: 20px;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="fps">0</div>
  36. <canvas id="renderCanvas"></canvas>
  37. <script>
  38. if (BABYLON.Engine.isSupported()) {
  39. var canvas = document.getElementById("renderCanvas");
  40. var engine = new BABYLON.Engine(canvas, true);
  41. var divFps = document.getElementById("fps");
  42. var scene = new BABYLON.Scene(engine);
  43. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  44. camera.attachControl(canvas, true);
  45. // Lights
  46. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  47. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(100, 100, 10), scene);
  48. pointLight.setEnabled(false);
  49. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(1, 0.2, 0), scene);
  50. directionalLight.setEnabled(false);
  51. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  52. spotLight.setEnabled(false);
  53. // Create meshes
  54. var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
  55. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  56. plane.setEnabled(false);
  57. var currentMesh = sphere;
  58. // Rabbit
  59. var rabbit;
  60. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  61. rabbit = newMeshes[1];
  62. rabbit.setEnabled(false);
  63. rabbit.scaling = new BABYLON.Vector3(0.4, 0.4, 0.4);
  64. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  65. // Shadow caster
  66. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  67. shadowCaster.setEnabled(false);
  68. shadowCaster.position = new BABYLON.Vector3(-20, -10, 0);
  69. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  70. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  71. shadowGenerator.usePoissonSampling = true;
  72. // Register a render loop to repeatedly render the scene
  73. var angle = 0;
  74. engine.runRenderLoop(function () {
  75. scene.render();
  76. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  77. shadowCaster.position.x = 20 * Math.sin(angle);
  78. shadowCaster.position.y = 20 * Math.cos(angle);
  79. shadowCaster.position.z = 20 * Math.cos(angle);
  80. shadowCaster.rotation.x += 0.01;
  81. shadowCaster.rotation.y += 0.01;
  82. angle += 0.01;
  83. });
  84. // Resize
  85. window.addEventListener("resize", function () {
  86. engine.resize();
  87. });
  88. // Fog
  89. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  90. scene.fogDensity = 0.01;
  91. // Create shaders
  92. var std = new BABYLON.StandardMaterial("std", scene);
  93. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  94. std.diffuseTexture.uScale = 5;
  95. std.diffuseTexture.vScale = 5;
  96. var simple = new BABYLON.SimpleMaterial("simple", scene);
  97. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  98. simple.diffuseTexture.uScale = 5;
  99. simple.diffuseTexture.vScale = 5;
  100. // Default to std
  101. var currentMaterial = std;
  102. sphere.material = std;
  103. //UI
  104. var gui = new dat.GUI();
  105. var options = {
  106. shader: "standard",
  107. mesh: "sphere",
  108. hemisphericLight: true,
  109. pointLight: false,
  110. directionalLight: false,
  111. castShadows: false,
  112. spotLight: false,
  113. fog: false
  114. }
  115. gui.add(options, 'shader', ['standard', 'simple']).onFinishChange(function () {
  116. switch (options.shader) {
  117. case "simple":
  118. currentMaterial = simple;
  119. break;
  120. default:
  121. currentMaterial = std;
  122. break;
  123. }
  124. currentMesh.material = currentMaterial;
  125. });
  126. gui.add(options, 'mesh', ['sphere', 'plane', 'rabbit']).onFinishChange(function () {
  127. currentMesh.setEnabled(false);
  128. switch (options.mesh) {
  129. case "sphere":
  130. currentMesh = sphere;
  131. break;
  132. case "plane":
  133. currentMesh = plane;
  134. break;
  135. case "rabbit":
  136. currentMesh = rabbit;
  137. break;
  138. }
  139. currentMesh.setEnabled(true);
  140. currentMesh.material = currentMaterial;
  141. });
  142. gui.add(options, 'hemisphericLight').onChange(function () {
  143. hemisphericLight.setEnabled(options.hemisphericLight);
  144. });
  145. gui.add(options, 'pointLight').onChange(function () {
  146. pointLight.setEnabled(options.pointLight);
  147. });
  148. gui.add(options, 'spotLight').onChange(function () {
  149. spotLight.setEnabled(options.spotLight);
  150. });
  151. var f1 = gui.addFolder('directionalLight');
  152. f1.add(options, 'directionalLight').onChange(function () {
  153. directionalLight.setEnabled(options.directionalLight);
  154. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  155. });
  156. f1.add(options, 'castShadows').onChange(function () {
  157. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  158. currentMesh.receiveShadows = true;
  159. });
  160. gui.add(options, 'fog').onChange(function () {
  161. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  162. });
  163. });
  164. }
  165. </script>
  166. </body>
  167. </html>