index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Shaders Library</title>
  5. <script src="dat.gui.min.js"></script>
  6. <script src="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>
  44. if (BABYLON.Engine.isSupported()) {
  45. var canvas = document.getElementById("renderCanvas");
  46. var engine = new BABYLON.Engine(canvas, true);
  47. var divFps = document.getElementById("fps");
  48. var scene = new BABYLON.Scene(engine);
  49. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  50. camera.attachControl(canvas, true);
  51. // Lights
  52. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  53. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  54. pointLight.setEnabled(false);
  55. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  56. directionalLight.setEnabled(false);
  57. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  58. spotLight.setEnabled(false);
  59. // Create meshes
  60. var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
  61. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  62. plane.setEnabled(false);
  63. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  64. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  65. ground.setEnabled(false);
  66. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  67. knot.setEnabled(false);
  68. // Skybox
  69. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  70. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  71. skyboxMaterial.backFaceCulling = false;
  72. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  73. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  74. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  75. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  76. skyboxMaterial.disableLighting = true;
  77. skybox.material = skyboxMaterial;
  78. skybox.setEnabled(false);
  79. var currentMesh = sphere;
  80. // Rabbit
  81. var rabbit;
  82. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  83. rabbit = newMeshes[1];
  84. rabbit.setEnabled(false);
  85. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  86. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  87. // Shadow caster
  88. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  89. shadowCaster.setEnabled(false);
  90. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  91. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  92. shadowCaster2.setEnabled(false);
  93. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  94. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  95. shadowCaster3.setEnabled(false);
  96. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  97. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  98. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  99. shadowGenerator.usePoissonSampling = true;
  100. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  101. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  102. shadowGenerator2.usePoissonSampling = true;
  103. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  104. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  105. shadowGenerator3.usePoissonSampling = true;
  106. // Register a render loop to repeatedly render the scene
  107. engine.runRenderLoop(function () {
  108. scene.render();
  109. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  110. shadowCaster.rotation.x += 0.01;
  111. shadowCaster.rotation.y += 0.01;
  112. shadowCaster2.rotation.x += 0.01;
  113. shadowCaster2.rotation.y += 0.01;
  114. shadowCaster3.rotation.x += 0.01;
  115. shadowCaster3.rotation.y += 0.01;
  116. });
  117. // Resize
  118. window.addEventListener("resize", function () {
  119. engine.resize();
  120. });
  121. // Fog
  122. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  123. scene.fogDensity = 0.01;
  124. // Create shaders
  125. var std = new BABYLON.StandardMaterial("std", scene);
  126. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  127. std.diffuseTexture.uScale = 5;
  128. std.diffuseTexture.vScale = 5;
  129. // Lava
  130. var lava = new BABYLON.LavaMaterial("lava", scene);
  131. lava.diffuseTexture = new BABYLON.Texture("textures/lava/lavatile.jpg", scene);
  132. lava.diffuseTexture.uScale = 0.5;
  133. lava.diffuseTexture.vScale = 0.5;
  134. lava.noiseTexture = new BABYLON.Texture("textures/lava/cloud.png", scene);
  135. lava.fogColor = BABYLON.Color3.Black();
  136. lava.speed = 2.5;
  137. var simple = new BABYLON.SimpleMaterial("simple", scene);
  138. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  139. simple.diffuseTexture.uScale = 5;
  140. simple.diffuseTexture.vScale = 5;
  141. var normal = new BABYLON.NormalMaterial("normal", scene);
  142. var water = new BABYLON.WaterMaterial("water", scene);
  143. water.backFaceCulling = false;
  144. water.enableRenderTargets(false);
  145. water.bumpTexture = new BABYLON.Texture("textures/waterbump.png", scene);
  146. water.windForce = -45;
  147. water.waveHeight = 1.3;
  148. water.windDirection = new BABYLON.Vector2(1, 1);
  149. water.addToRenderList(skybox);
  150. water.addToRenderList(shadowCaster);
  151. water.addToRenderList(shadowCaster2);
  152. water.addToRenderList(shadowCaster3);
  153. var fire = new BABYLON.FireMaterial("fire", scene);
  154. fire.diffuseTexture = new BABYLON.Texture("textures/fire/diffuse.png", scene);
  155. fire.distortionTexture = new BABYLON.Texture("textures/fire/distortion.png", scene);
  156. fire.opacityTexture = new BABYLON.Texture("textures/fire/opacity.png", scene);
  157. var terrain = new BABYLON.TerrainMaterial("terrain", scene);
  158. terrain.specularColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  159. terrain.specularPower = 64;
  160. terrain.mixTexture = new BABYLON.Texture("textures/mixMap.png", scene);
  161. terrain.diffuseTexture1 = new BABYLON.Texture("textures/grass.png", scene);
  162. terrain.diffuseTexture2 = new BABYLON.Texture("textures/rock.png", scene);
  163. terrain.diffuseTexture3 = new BABYLON.Texture("textures/floor.png", scene);
  164. terrain.bumpTexture1 = new BABYLON.Texture("textures/grassn.png", scene);
  165. terrain.bumpTexture2 = new BABYLON.Texture("textures/rockn.png", scene);
  166. terrain.bumpTexture3 = new BABYLON.Texture("textures/floor_bump.png", scene);
  167. terrain.diffuseTexture1.uScale = terrain.diffuseTexture1.vScale = 10;
  168. terrain.diffuseTexture2.uScale = terrain.diffuseTexture2.vScale = 10;
  169. terrain.diffuseTexture3.uScale = terrain.diffuseTexture3.vScale = 10;
  170. var pbr = new BABYLON.PBRMaterial("pbr", scene);
  171. // Default to std
  172. var currentMaterial = std;
  173. sphere.material = std;
  174. sphere.receiveShadows = true;
  175. //UI
  176. var gui = new dat.GUI();
  177. var options = {
  178. material: "standard",
  179. mesh: "sphere",
  180. hemisphericLight: true,
  181. pointLight: false,
  182. directionalLight: false,
  183. castShadows: false,
  184. spotLight: false,
  185. fog: false,
  186. skybox: false
  187. }
  188. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava', 'normal', 'terrain', 'pbr']).onFinishChange(function () {
  189. water.enableRenderTargets(false);
  190. switch (options.material) {
  191. case "simple":
  192. currentMaterial = simple;
  193. break;
  194. case "water":
  195. currentMaterial = water;
  196. water.enableRenderTargets(true);
  197. skybox.setEnabled(true);
  198. break;
  199. case "fire":
  200. currentMaterial = fire;
  201. break;
  202. case "lava":
  203. currentMaterial = lava;
  204. break;
  205. case "normal":
  206. currentMaterial = normal;
  207. break;
  208. case "terrain":
  209. currentMaterial = terrain;
  210. break;
  211. case "pbr":
  212. currentMaterial = pbr;
  213. break;
  214. default:
  215. currentMaterial = std;
  216. break;
  217. }
  218. currentMesh.material = currentMaterial;
  219. });
  220. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  221. currentMesh.setEnabled(false);
  222. switch (options.mesh) {
  223. case "sphere":
  224. currentMesh = sphere;
  225. break;
  226. case "knot":
  227. currentMesh = knot;
  228. break;
  229. case "plane":
  230. currentMesh = plane;
  231. break;
  232. case "ground":
  233. currentMesh = ground;
  234. break;
  235. case "rabbit":
  236. currentMesh = rabbit;
  237. break;
  238. }
  239. currentMesh.setEnabled(true);
  240. currentMesh.receiveShadows = true;
  241. currentMesh.material = currentMaterial;
  242. water.mesh = currentMesh;
  243. });
  244. var f1 = gui.addFolder('lights');
  245. f1.add(options, 'hemisphericLight').onChange(function () {
  246. hemisphericLight.setEnabled(options.hemisphericLight);
  247. });
  248. f1.add(options, 'pointLight').onChange(function () {
  249. pointLight.setEnabled(options.pointLight);
  250. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  251. });
  252. f1.add(options, 'spotLight').onChange(function () {
  253. spotLight.setEnabled(options.spotLight);
  254. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  255. });
  256. f1.add(options, 'directionalLight').onChange(function () {
  257. directionalLight.setEnabled(options.directionalLight);
  258. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  259. });
  260. f1.add(options, 'castShadows').onChange(function () {
  261. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  262. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  263. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  264. });
  265. gui.add(options, 'fog').onChange(function () {
  266. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  267. });
  268. gui.add(options, 'skybox').onChange(function() {
  269. skybox.setEnabled(options.skybox);
  270. });
  271. });
  272. }
  273. </script>
  274. </body>
  275. </html>