index.html 10 KB

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