index.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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: 1, depth:30 }, scene);
  58. plane.setEnabled(false);
  59. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  60. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  61. ground.setEnabled(false);
  62. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  63. knot.setEnabled(false);
  64. // Skybox
  65. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  66. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  67. skyboxMaterial.backFaceCulling = false;
  68. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  69. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  70. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  71. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  72. skyboxMaterial.disableLighting = true;
  73. skybox.material = skyboxMaterial;
  74. skybox.setEnabled(false);
  75. var currentMesh = sphere;
  76. // Rabbit
  77. var rabbit;
  78. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  79. rabbit = newMeshes[1];
  80. rabbit.setEnabled(false);
  81. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  82. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  83. // Shadow caster
  84. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  85. shadowCaster.setEnabled(false);
  86. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  87. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  88. shadowCaster2.setEnabled(false);
  89. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  90. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  91. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  92. shadowGenerator.usePoissonSampling = true;
  93. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  94. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  95. shadowGenerator2.usePoissonSampling = true;
  96. // Register a render loop to repeatedly render the scene
  97. engine.runRenderLoop(function () {
  98. scene.render();
  99. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  100. shadowCaster.rotation.x += 0.01;
  101. shadowCaster.rotation.y += 0.01;
  102. shadowCaster2.rotation.x += 0.01;
  103. shadowCaster2.rotation.y += 0.01;
  104. });
  105. // Resize
  106. window.addEventListener("resize", function () {
  107. engine.resize();
  108. });
  109. // Fog
  110. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  111. scene.fogDensity = 0.01;
  112. // Create shaders
  113. var std = new BABYLON.StandardMaterial("std", scene);
  114. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  115. std.diffuseTexture.uScale = 5;
  116. std.diffuseTexture.vScale = 5;
  117. var simple = new BABYLON.SimpleMaterial("simple", scene);
  118. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  119. simple.diffuseTexture.uScale = 5;
  120. simple.diffuseTexture.vScale = 5;
  121. var water = new BABYLON.WaterMaterial("water", scene);
  122. water.backFaceCulling = false;
  123. water.enableRenderTargets(false);
  124. water.bumpTexture = new BABYLON.Texture("textures/waterbump.png", scene);
  125. water.windForce = -45;
  126. water.waveHeight = 1.3;
  127. water.addToRenderList(skybox);
  128. water.addToRenderList(shadowCaster);
  129. water.addToRenderList(shadowCaster2);
  130. var fire = new BABYLON.FireMaterial("fire", scene);
  131. fire.diffuseTexture = new BABYLON.Texture("textures/fire/diffuse.png", scene);
  132. fire.distortionTexture = new BABYLON.Texture("textures/fire/distortion.png", scene);
  133. fire.opacityTexture = new BABYLON.Texture("textures/fire/opacity.png", scene);
  134. // Default to std
  135. var currentMaterial = std;
  136. sphere.material = std;
  137. sphere.receiveShadows = true;
  138. //UI
  139. var gui = new dat.GUI();
  140. var options = {
  141. material: "standard",
  142. mesh: "sphere",
  143. hemisphericLight: true,
  144. pointLight: false,
  145. directionalLight: false,
  146. castShadows: false,
  147. spotLight: false,
  148. fog: false,
  149. skybox: false
  150. }
  151. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire']).onFinishChange(function () {
  152. water.enableRenderTargets(false);
  153. switch (options.material) {
  154. case "simple":
  155. currentMaterial = simple;
  156. break;
  157. case "water":
  158. currentMaterial = water;
  159. water.enableRenderTargets(true);
  160. skybox.setEnabled(true);
  161. break;
  162. case "fire":
  163. currentMaterial = fire;
  164. break;
  165. default:
  166. currentMaterial = std;
  167. break;
  168. }
  169. currentMesh.material = currentMaterial;
  170. });
  171. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  172. currentMesh.setEnabled(false);
  173. switch (options.mesh) {
  174. case "sphere":
  175. currentMesh = sphere;
  176. break;
  177. case "knot":
  178. currentMesh = knot;
  179. break;
  180. case "plane":
  181. currentMesh = plane;
  182. break;
  183. case "ground":
  184. currentMesh = ground;
  185. break;
  186. case "rabbit":
  187. currentMesh = rabbit;
  188. break;
  189. }
  190. currentMesh.setEnabled(true);
  191. currentMesh.receiveShadows = true;
  192. currentMesh.material = currentMaterial;
  193. water.mesh = currentMesh;
  194. });
  195. var f1 = gui.addFolder('lights');
  196. f1.add(options, 'hemisphericLight').onChange(function () {
  197. hemisphericLight.setEnabled(options.hemisphericLight);
  198. });
  199. f1.add(options, 'pointLight').onChange(function () {
  200. pointLight.setEnabled(options.pointLight);
  201. });
  202. f1.add(options, 'spotLight').onChange(function () {
  203. spotLight.setEnabled(options.spotLight);
  204. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  205. });
  206. f1.add(options, 'directionalLight').onChange(function () {
  207. directionalLight.setEnabled(options.directionalLight);
  208. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  209. });
  210. f1.add(options, 'castShadows').onChange(function () {
  211. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  212. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  213. });
  214. gui.add(options, 'fog').onChange(function () {
  215. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  216. });
  217. gui.add(options, 'skybox').onChange(function() {
  218. skybox.setEnabled(options.skybox);
  219. });
  220. });
  221. }
  222. </script>
  223. </body>
  224. </html>