index.html 9.8 KB

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