index.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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(20, 20, 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 shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  91. shadowCaster3.setEnabled(false);
  92. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  93. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  94. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  95. shadowGenerator.usePoissonSampling = true;
  96. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  97. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  98. shadowGenerator2.usePoissonSampling = true;
  99. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  100. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  101. shadowGenerator3.usePoissonSampling = true;
  102. // Register a render loop to repeatedly render the scene
  103. engine.runRenderLoop(function () {
  104. scene.render();
  105. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  106. shadowCaster.rotation.x += 0.01;
  107. shadowCaster.rotation.y += 0.01;
  108. shadowCaster2.rotation.x += 0.01;
  109. shadowCaster2.rotation.y += 0.01;
  110. shadowCaster3.rotation.x += 0.01;
  111. shadowCaster3.rotation.y += 0.01;
  112. });
  113. // Resize
  114. window.addEventListener("resize", function () {
  115. engine.resize();
  116. });
  117. // Fog
  118. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  119. scene.fogDensity = 0.01;
  120. // Create shaders
  121. var std = new BABYLON.StandardMaterial("std", scene);
  122. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  123. std.diffuseTexture.uScale = 5;
  124. std.diffuseTexture.vScale = 5;
  125. var simple = new BABYLON.SimpleMaterial("simple", scene);
  126. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  127. simple.diffuseTexture.uScale = 5;
  128. simple.diffuseTexture.vScale = 5;
  129. var water = new BABYLON.WaterMaterial("water", scene);
  130. water.backFaceCulling = false;
  131. water.enableRenderTargets(false);
  132. water.bumpTexture = new BABYLON.Texture("textures/waterbump.png", scene);
  133. water.windForce = -45;
  134. water.waveHeight = 1.3;
  135. water.addToRenderList(skybox);
  136. water.addToRenderList(shadowCaster);
  137. water.addToRenderList(shadowCaster2);
  138. water.addToRenderList(shadowCaster3);
  139. var fire = new BABYLON.FireMaterial("fire", scene);
  140. fire.diffuseTexture = new BABYLON.Texture("textures/fire/diffuse.png", scene);
  141. fire.distortionTexture = new BABYLON.Texture("textures/fire/distortion.png", scene);
  142. fire.opacityTexture = new BABYLON.Texture("textures/fire/opacity.png", scene);
  143. // Default to std
  144. var currentMaterial = std;
  145. sphere.material = std;
  146. sphere.receiveShadows = true;
  147. //UI
  148. var gui = new dat.GUI();
  149. var options = {
  150. material: "standard",
  151. mesh: "sphere",
  152. hemisphericLight: true,
  153. pointLight: false,
  154. directionalLight: false,
  155. castShadows: false,
  156. spotLight: false,
  157. fog: false,
  158. skybox: false
  159. }
  160. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire']).onFinishChange(function () {
  161. water.enableRenderTargets(false);
  162. switch (options.material) {
  163. case "simple":
  164. currentMaterial = simple;
  165. break;
  166. case "water":
  167. currentMaterial = water;
  168. water.enableRenderTargets(true);
  169. skybox.setEnabled(true);
  170. break;
  171. case "fire":
  172. currentMaterial = fire;
  173. break;
  174. default:
  175. currentMaterial = std;
  176. break;
  177. }
  178. currentMesh.material = currentMaterial;
  179. });
  180. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  181. currentMesh.setEnabled(false);
  182. switch (options.mesh) {
  183. case "sphere":
  184. currentMesh = sphere;
  185. break;
  186. case "knot":
  187. currentMesh = knot;
  188. break;
  189. case "plane":
  190. currentMesh = plane;
  191. break;
  192. case "ground":
  193. currentMesh = ground;
  194. break;
  195. case "rabbit":
  196. currentMesh = rabbit;
  197. break;
  198. }
  199. currentMesh.setEnabled(true);
  200. currentMesh.receiveShadows = true;
  201. currentMesh.material = currentMaterial;
  202. water.mesh = currentMesh;
  203. });
  204. var f1 = gui.addFolder('lights');
  205. f1.add(options, 'hemisphericLight').onChange(function () {
  206. hemisphericLight.setEnabled(options.hemisphericLight);
  207. });
  208. f1.add(options, 'pointLight').onChange(function () {
  209. pointLight.setEnabled(options.pointLight);
  210. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  211. });
  212. f1.add(options, 'spotLight').onChange(function () {
  213. spotLight.setEnabled(options.spotLight);
  214. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  215. });
  216. f1.add(options, 'directionalLight').onChange(function () {
  217. directionalLight.setEnabled(options.directionalLight);
  218. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  219. });
  220. f1.add(options, 'castShadows').onChange(function () {
  221. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  222. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  223. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  224. });
  225. gui.add(options, 'fog').onChange(function () {
  226. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  227. });
  228. gui.add(options, 'skybox').onChange(function() {
  229. skybox.setEnabled(options.skybox);
  230. });
  231. });
  232. }
  233. </script>
  234. </body>
  235. </html>