index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Shaders Library</title>
  5. <script src="../assets/refs/dat.gui.min.js"></script>
  6. <script src="../Tools/DevLoader/BabylonLoader.js"></script>
  7. <style>
  8. html, body {
  9. width: 100%;
  10. height: 100%;
  11. padding: 0;
  12. margin: 0;
  13. overflow: hidden;
  14. }
  15. #renderCanvas {
  16. width: 100%;
  17. height: 100%;
  18. }
  19. #fps {
  20. position: absolute;
  21. background-color: black;
  22. border: 2px solid red;
  23. text-align: center;
  24. font-size: 16px;
  25. color: white;
  26. top: 15px;
  27. left: 10px;
  28. width: 60px;
  29. height: 20px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="fps">0</div>
  35. <canvas id="renderCanvas"></canvas>
  36. <script src="test/index.js"></script>
  37. <script src="test/addlava.js"></script>
  38. <script src="test/addnormal.js"></script>
  39. <script src="test/addwater.js"></script>
  40. <script src="test/addfur.js"></script>
  41. <script src="test/addterrain.js"></script>
  42. <script src="test/addfire.js"></script>
  43. <script src="test/addtriplanar.js"></script>
  44. <script src="test/addgradient.js"></script>
  45. <script src="test/addsky.js"></script>
  46. <script src="test/addgrid.js"></script>
  47. <script src="test/addpbr.js"></script>
  48. <script src="test/addCell.js"></script>
  49. <script>
  50. BABYLONDEVTOOLS.Loader.load(function() {
  51. if (BABYLON.Engine.isSupported()) {
  52. var canvas = document.getElementById("renderCanvas");
  53. var engine = new BABYLON.Engine(canvas, true);
  54. BABYLONDEVTOOLS.Loader.debugShortcut(engine);
  55. var divFps = document.getElementById("fps");
  56. scene = new BABYLON.Scene(engine);
  57. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  58. camera.attachControl(canvas, true);
  59. camera.minZ = 0.1;
  60. // Lights
  61. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  62. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  63. pointLight.setEnabled(false);
  64. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  65. directionalLight.setEnabled(false);
  66. directionalLight.position = new BABYLON.Vector3(0, 50, 0);
  67. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  68. spotLight.setEnabled(false);
  69. // Create meshes
  70. var sphere = BABYLON.Mesh.CreateSphere("sphere", 48, 30.0, scene);
  71. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  72. plane.setEnabled(false);
  73. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  74. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  75. ground.setEnabled(false);
  76. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  77. knot.setEnabled(false);
  78. var heightMap = BABYLON.Mesh.CreateGroundFromHeightMap("heightMap", "../assets/textures/heightMap.png", 100, 100, 100, 0, 10, scene, false);
  79. heightMap.setEnabled(false);
  80. // Skybox
  81. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  82. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  83. skyboxMaterial.backFaceCulling = false;
  84. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("../assets/textures/skybox/TropicalSunnyDay", scene);
  85. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  86. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  87. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  88. skyboxMaterial.disableLighting = true;
  89. skybox.material = skyboxMaterial;
  90. skybox.setEnabled(false);
  91. var currentMesh = sphere;
  92. // Rabbit
  93. var rabbit;
  94. BABYLON.SceneLoader.ImportMesh("Rabbit", "../assets/meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  95. rabbit = newMeshes[1];
  96. rabbit.setEnabled(false);
  97. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  98. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  99. // Shadow caster
  100. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  101. shadowCaster.setEnabled(false);
  102. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  103. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  104. shadowCaster2.setEnabled(false);
  105. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  106. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  107. shadowCaster3.setEnabled(false);
  108. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  109. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  110. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  111. shadowGenerator.usePoissonSampling = true;
  112. shadowGenerator.bias = 0;
  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 = false;
  119. shadowGenerator3.bias = 0;
  120. // Register a render loop to repeatedly render the scene
  121. engine.runRenderLoop(function () {
  122. scene.render();
  123. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  124. shadowCaster.rotation.x += 0.01;
  125. shadowCaster.rotation.y += 0.01;
  126. shadowCaster2.rotation.x += 0.01;
  127. shadowCaster2.rotation.y += 0.01;
  128. shadowCaster3.rotation.x += 0.01;
  129. shadowCaster3.rotation.y += 0.01;
  130. });
  131. // Resize
  132. window.addEventListener("resize", function () {
  133. engine.resize();
  134. });
  135. // Fog
  136. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  137. scene.fogDensity = 0.01;
  138. // Create shaders
  139. var std = new BABYLON.StandardMaterial("std", scene);
  140. std.diffuseTexture = new BABYLON.Texture("../assets/textures/amiga.jpg", scene);
  141. std.diffuseTexture.uScale = 5;
  142. std.diffuseTexture.vScale = 5;
  143. var lava = prepareLava();
  144. var simple = new BABYLON.SimpleMaterial("simple", scene);
  145. simple.diffuseTexture = new BABYLON.Texture("../assets/textures/amiga.jpg", scene);
  146. simple.diffuseTexture.uScale = 5;
  147. simple.diffuseTexture.vScale = 5;
  148. var normal = prepareNormal();
  149. var gradient = prepareGradient();
  150. var fur = prepareFur();
  151. var water = prepareWater();
  152. water.addToRenderList(skybox);
  153. water.addToRenderList(shadowCaster);
  154. water.addToRenderList(shadowCaster2);
  155. water.addToRenderList(shadowCaster3);
  156. var fire = prepareFire();
  157. var terrain = prepareTerrain();
  158. var pbr = preparePBR();
  159. var triPlanar = prepareTriPlanar();
  160. var sky = prepareSky();
  161. var grid = prepareGrid();
  162. var shadowOnly = new BABYLON.ShadowOnlyMaterial();
  163. var cell = prepareCell();
  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', 'fur', 'triPlanar', 'gradient', 'sky', 'grid', 'shadowOnly', 'cell']).onFinishChange(function () {
  169. water.enableRenderTargets(false);
  170. skybox.material = skyboxMaterial;
  171. currentMesh.isVisible = true;
  172. fur.resetFur();
  173. switch (options.material) {
  174. case "shadowOnly":
  175. currentMaterial = shadowOnly;
  176. break;
  177. case "simple":
  178. currentMaterial = simple;
  179. break;
  180. case "water":
  181. currentMaterial = water;
  182. water.enableRenderTargets(true);
  183. skybox.setEnabled(true);
  184. break;
  185. case "fire":
  186. currentMaterial = fire;
  187. break;
  188. case "lava":
  189. currentMaterial = lava;
  190. break;
  191. case "normal":
  192. currentMaterial = normal;
  193. break;
  194. case "terrain":
  195. currentMaterial = terrain;
  196. break;
  197. case "pbr":
  198. currentMaterial = pbr;
  199. break;
  200. case "fur":
  201. currentMaterial = fur.material;
  202. fur.configureFur(currentMesh);
  203. break;
  204. case "triPlanar":
  205. currentMaterial = triPlanar;
  206. break;
  207. case "gradient":
  208. currentMaterial = gradient;
  209. break;
  210. case "sky":
  211. skybox.setEnabled(true);
  212. skybox.material = sky;
  213. break;
  214. case "grid":
  215. currentMaterial = grid;
  216. break;
  217. case "cell":
  218. currentMaterial = cell;
  219. break;
  220. default:
  221. currentMaterial = std;
  222. break;
  223. }
  224. currentMesh.material = currentMaterial;
  225. window.enableMaterial(options.material);
  226. });
  227. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'heightMap', 'rabbit']).onFinishChange(function () {
  228. currentMesh.setEnabled(false);
  229. switch (options.mesh) {
  230. case "sphere":
  231. currentMesh = sphere;
  232. break;
  233. case "knot":
  234. currentMesh = knot;
  235. break;
  236. case "plane":
  237. currentMesh = plane;
  238. break;
  239. case "ground":
  240. currentMesh = ground;
  241. break;
  242. case "heightMap":
  243. currentMesh = heightMap;
  244. break;
  245. case "rabbit":
  246. currentMesh = rabbit;
  247. break;
  248. }
  249. currentMesh.setEnabled(true);
  250. currentMesh.receiveShadows = true;
  251. currentMesh.material = currentMaterial;
  252. water.mesh = currentMesh;
  253. if (currentMaterial === fur.material) {
  254. // Furify the mesh
  255. fur.resetFur();
  256. fur.configureFur(currentMesh);
  257. }
  258. });
  259. var f1 = gui.addFolder('lights');
  260. f1.add(options, 'lightIntensity').onChange(function() {
  261. hemisphericLight.intensity = options.lightIntensity;
  262. directionalLight.intensity = options.lightIntensity;
  263. pointLight.intensity = options.lightIntensity;
  264. spotLight.intensity = options.lightIntensity;
  265. });
  266. f1.add(options, 'lightRange').onChange(function() {
  267. hemisphericLight.range = options.lightRange;
  268. directionalLight.range = options.lightRange;
  269. pointLight.range = options.lightRange;
  270. spotLight.range = options.lightRange;
  271. });
  272. f1.add(options, 'lightRadius').onChange(function() {
  273. hemisphericLight.radius = options.lightRadius;
  274. directionalLight.radius = options.lightRadius;
  275. pointLight.radius = options.lightRadius;
  276. spotLight.radius = options.lightRadius;
  277. });
  278. f1.add(options, 'hemisphericLight').onChange(function () {
  279. hemisphericLight.setEnabled(options.hemisphericLight);
  280. });
  281. f1.add(options, 'pointLight').onChange(function () {
  282. pointLight.setEnabled(options.pointLight);
  283. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  284. });
  285. f1.add(options, 'spotLight').onChange(function () {
  286. spotLight.setEnabled(options.spotLight);
  287. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  288. });
  289. f1.add(options, 'directionalLight').onChange(function () {
  290. directionalLight.setEnabled(options.directionalLight);
  291. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  292. });
  293. f1.add(options, 'castShadows').onChange(function () {
  294. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  295. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  296. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  297. });
  298. gui.add(options, 'fog').onChange(function () {
  299. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  300. });
  301. gui.add(options, 'skybox').onChange(function() {
  302. skybox.setEnabled(options.skybox);
  303. });
  304. });
  305. }
  306. });
  307. </script>
  308. </body>
  309. </html>