index.html 9.7 KB

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