index.html 10 KB

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