index.html 10 KB

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