index.html 9.8 KB

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