index.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. <script src="../dist/babylon.waterMaterial.js"></script>
  9. <script src="../dist/babylon.fireMaterial.js"></script>
  10. <style>
  11. html, body {
  12. width: 100%;
  13. height: 100%;
  14. padding: 0;
  15. margin: 0;
  16. overflow: hidden;
  17. }
  18. #renderCanvas {
  19. width: 100%;
  20. height: 100%;
  21. }
  22. #fps {
  23. position: absolute;
  24. background-color: black;
  25. border: 2px solid red;
  26. text-align: center;
  27. font-size: 16px;
  28. color: white;
  29. top: 15px;
  30. left: 10px;
  31. width: 60px;
  32. height: 20px;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div id="fps">0</div>
  38. <canvas id="renderCanvas"></canvas>
  39. <script>
  40. if (BABYLON.Engine.isSupported()) {
  41. var canvas = document.getElementById("renderCanvas");
  42. var engine = new BABYLON.Engine(canvas, true);
  43. var divFps = document.getElementById("fps");
  44. var scene = new BABYLON.Scene(engine);
  45. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  46. camera.attachControl(canvas, true);
  47. // Lights
  48. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  49. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(100, 100, 10), scene);
  50. pointLight.setEnabled(false);
  51. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  52. directionalLight.setEnabled(false);
  53. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  54. spotLight.setEnabled(false);
  55. // Create meshes
  56. var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
  57. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 0.1, depth:30 }, scene);
  58. plane.setEnabled(false);
  59. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  60. knot.setEnabled(false);
  61. // Skybox
  62. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  63. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  64. skyboxMaterial.backFaceCulling = false;
  65. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  66. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  67. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  68. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  69. skyboxMaterial.disableLighting = true;
  70. skybox.material = skyboxMaterial;
  71. skybox.setEnabled(false);
  72. var currentMesh = sphere;
  73. // Rabbit
  74. var rabbit;
  75. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  76. rabbit = newMeshes[1];
  77. rabbit.setEnabled(false);
  78. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  79. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  80. // Shadow caster
  81. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  82. shadowCaster.setEnabled(false);
  83. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  84. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  85. shadowCaster2.setEnabled(false);
  86. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  87. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  88. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  89. shadowGenerator.usePoissonSampling = true;
  90. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  91. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  92. shadowGenerator2.usePoissonSampling = true;
  93. // Register a render loop to repeatedly render the scene
  94. engine.runRenderLoop(function () {
  95. scene.render();
  96. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  97. shadowCaster.rotation.x += 0.01;
  98. shadowCaster.rotation.y += 0.01;
  99. shadowCaster2.rotation.x += 0.01;
  100. shadowCaster2.rotation.y += 0.01;
  101. });
  102. // Resize
  103. window.addEventListener("resize", function () {
  104. engine.resize();
  105. });
  106. // Fog
  107. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  108. scene.fogDensity = 0.01;
  109. // Create shaders
  110. var std = new BABYLON.StandardMaterial("std", scene);
  111. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  112. std.diffuseTexture.uScale = 5;
  113. std.diffuseTexture.vScale = 5;
  114. var simple = new BABYLON.SimpleMaterial("simple", scene);
  115. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  116. simple.diffuseTexture.uScale = 5;
  117. simple.diffuseTexture.vScale = 5;
  118. var water = new BABYLON.WaterMaterial("water", scene, currentMesh);
  119. water.enableRenderTargets(false);
  120. water.bumpTexture = new BABYLON.Texture("textures/waterbump.png", scene);
  121. var fire = new BABYLON.FireMaterial("fire", scene);
  122. fire.diffuseTexture = new BABYLON.Texture("textures/fire/diffuse.png", scene);
  123. fire.distortionTexture = new BABYLON.Texture("textures/fire/distortion.png", scene);
  124. fire.opacityTexture = new BABYLON.Texture("textures/fire/opacity.png", scene);
  125. // Default to std
  126. var currentMaterial = std;
  127. sphere.material = std;
  128. sphere.receiveShadows = true;
  129. //UI
  130. var gui = new dat.GUI();
  131. var options = {
  132. material: "standard",
  133. mesh: "sphere",
  134. hemisphericLight: true,
  135. pointLight: false,
  136. directionalLight: false,
  137. castShadows: false,
  138. spotLight: false,
  139. fog: false,
  140. skybox: false
  141. }
  142. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire']).onFinishChange(function () {
  143. water.enableRenderTargets(false);
  144. switch (options.material) {
  145. case "simple":
  146. currentMaterial = simple;
  147. break;
  148. case "water":
  149. currentMaterial = water;
  150. water.enableRenderTargets(true);
  151. break;
  152. case "fire":
  153. currentMaterial = fire;
  154. break;
  155. default:
  156. currentMaterial = std;
  157. break;
  158. }
  159. currentMesh.material = currentMaterial;
  160. });
  161. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'rabbit']).onFinishChange(function () {
  162. currentMesh.setEnabled(false);
  163. switch (options.mesh) {
  164. case "sphere":
  165. currentMesh = sphere;
  166. break;
  167. case "knot":
  168. currentMesh = knot;
  169. break;
  170. case "plane":
  171. currentMesh = plane;
  172. break;
  173. case "rabbit":
  174. currentMesh = rabbit;
  175. break;
  176. }
  177. currentMesh.setEnabled(true);
  178. currentMesh.receiveShadows = true;
  179. currentMesh.material = currentMaterial;
  180. water.mesh = currentMesh;
  181. });
  182. var f1 = gui.addFolder('lights');
  183. f1.add(options, 'hemisphericLight').onChange(function () {
  184. hemisphericLight.setEnabled(options.hemisphericLight);
  185. });
  186. f1.add(options, 'pointLight').onChange(function () {
  187. pointLight.setEnabled(options.pointLight);
  188. });
  189. f1.add(options, 'spotLight').onChange(function () {
  190. spotLight.setEnabled(options.spotLight);
  191. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  192. });
  193. f1.add(options, 'directionalLight').onChange(function () {
  194. directionalLight.setEnabled(options.directionalLight);
  195. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  196. });
  197. f1.add(options, 'castShadows').onChange(function () {
  198. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  199. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  200. });
  201. gui.add(options, 'fog').onChange(function () {
  202. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  203. });
  204. gui.add(options, 'skybox').onChange(function() {
  205. skybox.setEnabled(options.skybox);
  206. });
  207. });
  208. }
  209. </script>
  210. </body>
  211. </html>