index.html 11 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. <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>
  51. if (BABYLON.Engine.isSupported()) {
  52. var canvas = document.getElementById("renderCanvas");
  53. var engine = new BABYLON.Engine(canvas, true);
  54. var divFps = document.getElementById("fps");
  55. var scene = new BABYLON.Scene(engine);
  56. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  57. camera.attachControl(canvas, true);
  58. // Lights
  59. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  60. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  61. pointLight.setEnabled(false);
  62. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  63. directionalLight.setEnabled(false);
  64. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  65. spotLight.setEnabled(false);
  66. // Create meshes
  67. var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
  68. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  69. plane.setEnabled(false);
  70. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  71. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  72. ground.setEnabled(false);
  73. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  74. knot.setEnabled(false);
  75. // Skybox
  76. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  77. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  78. skyboxMaterial.backFaceCulling = false;
  79. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  80. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  81. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  82. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  83. skyboxMaterial.disableLighting = true;
  84. skybox.material = skyboxMaterial;
  85. skybox.setEnabled(false);
  86. var currentMesh = sphere;
  87. // Rabbit
  88. var rabbit;
  89. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  90. rabbit = newMeshes[1];
  91. rabbit.setEnabled(false);
  92. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  93. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  94. // Shadow caster
  95. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  96. shadowCaster.setEnabled(false);
  97. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  98. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  99. shadowCaster2.setEnabled(false);
  100. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  101. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  102. shadowCaster3.setEnabled(false);
  103. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  104. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  105. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  106. shadowGenerator.usePoissonSampling = true;
  107. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  108. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  109. shadowGenerator2.usePoissonSampling = true;
  110. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  111. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  112. shadowGenerator3.usePoissonSampling = true;
  113. // Register a render loop to repeatedly render the scene
  114. engine.runRenderLoop(function () {
  115. scene.render();
  116. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  117. shadowCaster.rotation.x += 0.01;
  118. shadowCaster.rotation.y += 0.01;
  119. shadowCaster2.rotation.x += 0.01;
  120. shadowCaster2.rotation.y += 0.01;
  121. shadowCaster3.rotation.x += 0.01;
  122. shadowCaster3.rotation.y += 0.01;
  123. });
  124. // Resize
  125. window.addEventListener("resize", function () {
  126. engine.resize();
  127. });
  128. // Fog
  129. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  130. scene.fogDensity = 0.01;
  131. // Create shaders
  132. var std = new BABYLON.StandardMaterial("std", scene);
  133. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  134. std.diffuseTexture.uScale = 5;
  135. std.diffuseTexture.vScale = 5;
  136. // Lava
  137. var lava = prepareLava();
  138. var simple = new BABYLON.SimpleMaterial("simple", scene);
  139. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  140. simple.diffuseTexture.uScale = 5;
  141. simple.diffuseTexture.vScale = 5;
  142. var normal = prepareNormal();
  143. var fur = prepareFur();
  144. var water = prepareWater();
  145. water.addToRenderList(skybox);
  146. water.addToRenderList(shadowCaster);
  147. water.addToRenderList(shadowCaster2);
  148. water.addToRenderList(shadowCaster3);
  149. var fire = new BABYLON.FireMaterial("fire", scene);
  150. fire.diffuseTexture = new BABYLON.Texture("textures/fire/diffuse.png", scene);
  151. fire.distortionTexture = new BABYLON.Texture("textures/fire/distortion.png", scene);
  152. fire.opacityTexture = new BABYLON.Texture("textures/fire/opacity.png", scene);
  153. var terrain = new BABYLON.TerrainMaterial("terrain", scene);
  154. terrain.specularColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  155. terrain.specularPower = 64;
  156. terrain.mixTexture = new BABYLON.Texture("textures/mixMap.png", scene);
  157. terrain.diffuseTexture1 = new BABYLON.Texture("textures/grass.png", scene);
  158. terrain.diffuseTexture2 = new BABYLON.Texture("textures/rock.png", scene);
  159. terrain.diffuseTexture3 = new BABYLON.Texture("textures/floor.png", scene);
  160. terrain.bumpTexture1 = new BABYLON.Texture("textures/grassn.png", scene);
  161. terrain.bumpTexture2 = new BABYLON.Texture("textures/rockn.png", scene);
  162. terrain.bumpTexture3 = new BABYLON.Texture("textures/floor_bump.png", scene);
  163. terrain.diffuseTexture1.uScale = terrain.diffuseTexture1.vScale = 10;
  164. terrain.diffuseTexture2.uScale = terrain.diffuseTexture2.vScale = 10;
  165. terrain.diffuseTexture3.uScale = terrain.diffuseTexture3.vScale = 10;
  166. var pbr = preparePBR();
  167. // Default to std
  168. var currentMaterial = std;
  169. sphere.material = std;
  170. sphere.receiveShadows = true;
  171. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava', 'normal', 'terrain', 'pbr', 'fur']).onFinishChange(function () {
  172. water.enableRenderTargets(false);
  173. switch (options.material) {
  174. case "simple":
  175. currentMaterial = simple;
  176. break;
  177. case "water":
  178. currentMaterial = water;
  179. water.enableRenderTargets(true);
  180. skybox.setEnabled(true);
  181. break;
  182. case "fire":
  183. currentMaterial = fire;
  184. break;
  185. case "lava":
  186. currentMaterial = lava;
  187. break;
  188. case "normal":
  189. currentMaterial = normal;
  190. break;
  191. case "terrain":
  192. currentMaterial = terrain;
  193. break;
  194. case "pbr":
  195. currentMaterial = pbr;
  196. break;
  197. case "fur":
  198. currentMaterial = fur;
  199. break;
  200. default:
  201. currentMaterial = std;
  202. break;
  203. }
  204. currentMesh.material = currentMaterial;
  205. window.enableMaterial(options.material);
  206. });
  207. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  208. currentMesh.setEnabled(false);
  209. switch (options.mesh) {
  210. case "sphere":
  211. currentMesh = sphere;
  212. break;
  213. case "knot":
  214. currentMesh = knot;
  215. break;
  216. case "plane":
  217. currentMesh = plane;
  218. break;
  219. case "ground":
  220. currentMesh = ground;
  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>