index.html 11 KB

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