index.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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(0,-1, 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 knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  58. knot.setEnabled(false);
  59. var currentMesh = sphere;
  60. // Rabbit
  61. var rabbit;
  62. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  63. rabbit = newMeshes[1];
  64. rabbit.setEnabled(false);
  65. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  66. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  67. // Shadow caster
  68. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  69. shadowCaster.setEnabled(false);
  70. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  71. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  72. shadowCaster2.setEnabled(false);
  73. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  74. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  75. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  76. shadowGenerator.usePoissonSampling = true;
  77. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  78. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  79. shadowGenerator2.usePoissonSampling = true;
  80. // Register a render loop to repeatedly render the scene
  81. engine.runRenderLoop(function () {
  82. scene.render();
  83. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  84. shadowCaster.rotation.x += 0.01;
  85. shadowCaster.rotation.y += 0.01;
  86. shadowCaster2.rotation.x += 0.01;
  87. shadowCaster2.rotation.y += 0.01;
  88. });
  89. // Resize
  90. window.addEventListener("resize", function () {
  91. engine.resize();
  92. });
  93. // Fog
  94. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  95. scene.fogDensity = 0.01;
  96. // Create shaders
  97. var std = new BABYLON.StandardMaterial("std", scene);
  98. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  99. std.diffuseTexture.uScale = 5;
  100. std.diffuseTexture.vScale = 5;
  101. var simple = new BABYLON.SimpleMaterial("simple", scene);
  102. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  103. simple.diffuseTexture.uScale = 5;
  104. simple.diffuseTexture.vScale = 5;
  105. // Default to std
  106. var currentMaterial = std;
  107. sphere.material = std;
  108. sphere.receiveShadows = true;
  109. //UI
  110. var gui = new dat.GUI();
  111. var options = {
  112. material: "standard",
  113. mesh: "sphere",
  114. hemisphericLight: true,
  115. pointLight: false,
  116. directionalLight: false,
  117. castShadows: false,
  118. spotLight: false,
  119. fog: false
  120. }
  121. gui.add(options, 'material', ['standard', 'simple']).onFinishChange(function () {
  122. switch (options.material) {
  123. case "simple":
  124. currentMaterial = simple;
  125. break;
  126. default:
  127. currentMaterial = std;
  128. break;
  129. }
  130. currentMesh.material = currentMaterial;
  131. });
  132. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'rabbit']).onFinishChange(function () {
  133. currentMesh.setEnabled(false);
  134. switch (options.mesh) {
  135. case "sphere":
  136. currentMesh = sphere;
  137. break;
  138. case "knot":
  139. currentMesh = knot;
  140. break;
  141. case "plane":
  142. currentMesh = plane;
  143. break;
  144. case "rabbit":
  145. currentMesh = rabbit;
  146. break;
  147. }
  148. currentMesh.setEnabled(true);
  149. currentMesh.receiveShadows = true;
  150. currentMesh.material = currentMaterial;
  151. });
  152. var f1 = gui.addFolder('lights');
  153. f1.add(options, 'hemisphericLight').onChange(function () {
  154. hemisphericLight.setEnabled(options.hemisphericLight);
  155. });
  156. f1.add(options, 'pointLight').onChange(function () {
  157. pointLight.setEnabled(options.pointLight);
  158. });
  159. f1.add(options, 'spotLight').onChange(function () {
  160. spotLight.setEnabled(options.spotLight);
  161. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  162. });
  163. f1.add(options, 'directionalLight').onChange(function () {
  164. directionalLight.setEnabled(options.directionalLight);
  165. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  166. });
  167. f1.add(options, 'castShadows').onChange(function () {
  168. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  169. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  170. });
  171. gui.add(options, 'fog').onChange(function () {
  172. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  173. });
  174. });
  175. }
  176. </script>
  177. </body>
  178. </html>