index.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. // Lights
  63. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  64. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  65. pointLight.setEnabled(false);
  66. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  67. directionalLight.setEnabled(false);
  68. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  69. spotLight.setEnabled(false);
  70. // Create meshes
  71. var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
  72. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  73. plane.setEnabled(false);
  74. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  75. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  76. ground.setEnabled(false);
  77. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  78. knot.setEnabled(false);
  79. var heightMap = BABYLON.Mesh.CreateGroundFromHeightMap("heightMap", "textures/heightMap.png", 100, 100, 100, 0, 10, scene, false);
  80. heightMap.setEnabled(false);
  81. // Skybox
  82. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  83. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  84. skyboxMaterial.backFaceCulling = false;
  85. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  86. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  87. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  88. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  89. skyboxMaterial.disableLighting = true;
  90. skybox.material = skyboxMaterial;
  91. skybox.setEnabled(false);
  92. var currentMesh = sphere;
  93. // Rabbit
  94. var rabbit;
  95. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  96. rabbit = newMeshes[1];
  97. rabbit.setEnabled(false);
  98. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  99. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  100. // Shadow caster
  101. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  102. shadowCaster.setEnabled(false);
  103. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  104. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  105. shadowCaster2.setEnabled(false);
  106. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  107. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  108. shadowCaster3.setEnabled(false);
  109. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  110. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  111. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  112. shadowGenerator.usePoissonSampling = true;
  113. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  114. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  115. shadowGenerator2.usePoissonSampling = true;
  116. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  117. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  118. shadowGenerator3.usePoissonSampling = true;
  119. // Register a render loop to repeatedly render the scene
  120. engine.runRenderLoop(function () {
  121. scene.render();
  122. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  123. shadowCaster.rotation.x += 0.01;
  124. shadowCaster.rotation.y += 0.01;
  125. shadowCaster2.rotation.x += 0.01;
  126. shadowCaster2.rotation.y += 0.01;
  127. shadowCaster3.rotation.x += 0.01;
  128. shadowCaster3.rotation.y += 0.01;
  129. });
  130. // Resize
  131. window.addEventListener("resize", function () {
  132. engine.resize();
  133. });
  134. // Fog
  135. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  136. scene.fogDensity = 0.01;
  137. // Create shaders
  138. var std = new BABYLON.StandardMaterial("std", scene);
  139. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  140. std.diffuseTexture.uScale = 5;
  141. std.diffuseTexture.vScale = 5;
  142. // Lava
  143. var lava = prepareLava();
  144. var simple = new BABYLON.SimpleMaterial("simple", scene);
  145. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  146. simple.diffuseTexture.uScale = 5;
  147. simple.diffuseTexture.vScale = 5;
  148. var normal = prepareNormal();
  149. var fur = prepareFur();
  150. var water = prepareWater();
  151. water.addToRenderList(skybox);
  152. water.addToRenderList(shadowCaster);
  153. water.addToRenderList(shadowCaster2);
  154. water.addToRenderList(shadowCaster3);
  155. var fire = prepareFire();
  156. var terrain = prepareTerrain();
  157. var pbr = preparePBR();
  158. var triPlanar = prepareTriPlanar();
  159. // Default to std
  160. var currentMaterial = std;
  161. sphere.material = std;
  162. sphere.receiveShadows = true;
  163. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava', 'normal', 'terrain', 'pbr', 'fur', 'triPlanar']).onFinishChange(function () {
  164. water.enableRenderTargets(false);
  165. switch (options.material) {
  166. case "simple":
  167. currentMaterial = simple;
  168. break;
  169. case "water":
  170. currentMaterial = water;
  171. water.enableRenderTargets(true);
  172. skybox.setEnabled(true);
  173. break;
  174. case "fire":
  175. currentMaterial = fire;
  176. break;
  177. case "lava":
  178. currentMaterial = lava;
  179. break;
  180. case "normal":
  181. currentMaterial = normal;
  182. break;
  183. case "terrain":
  184. currentMaterial = terrain;
  185. break;
  186. case "pbr":
  187. currentMaterial = pbr;
  188. break;
  189. case "fur":
  190. currentMaterial = fur;
  191. break;
  192. case "triPlanar":
  193. currentMaterial = triPlanar;
  194. break;
  195. default:
  196. currentMaterial = std;
  197. break;
  198. }
  199. currentMesh.material = currentMaterial;
  200. window.enableMaterial(options.material);
  201. });
  202. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'heightMap', 'rabbit']).onFinishChange(function () {
  203. currentMesh.setEnabled(false);
  204. switch (options.mesh) {
  205. case "sphere":
  206. currentMesh = sphere;
  207. break;
  208. case "knot":
  209. currentMesh = knot;
  210. break;
  211. case "plane":
  212. currentMesh = plane;
  213. break;
  214. case "ground":
  215. currentMesh = ground;
  216. break;
  217. case "heightMap":
  218. currentMesh = heightMap;
  219. break;
  220. case "rabbit":
  221. currentMesh = rabbit;
  222. break;
  223. }
  224. currentMesh.setEnabled(true);
  225. currentMesh.receiveShadows = true;
  226. currentMesh.material = currentMaterial;
  227. water.mesh = currentMesh;
  228. });
  229. var f1 = gui.addFolder('lights');
  230. f1.add(options, 'hemisphericLight').onChange(function () {
  231. hemisphericLight.setEnabled(options.hemisphericLight);
  232. });
  233. f1.add(options, 'pointLight').onChange(function () {
  234. pointLight.setEnabled(options.pointLight);
  235. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  236. });
  237. f1.add(options, 'spotLight').onChange(function () {
  238. spotLight.setEnabled(options.spotLight);
  239. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  240. });
  241. f1.add(options, 'directionalLight').onChange(function () {
  242. directionalLight.setEnabled(options.directionalLight);
  243. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  244. });
  245. f1.add(options, 'castShadows').onChange(function () {
  246. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  247. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  248. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  249. });
  250. gui.add(options, 'fog').onChange(function () {
  251. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  252. });
  253. gui.add(options, 'skybox').onChange(function() {
  254. skybox.setEnabled(options.skybox);
  255. });
  256. });
  257. }
  258. </script>
  259. </body>
  260. </html>