index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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>
  49. BABYLONDEVTOOLS.Loader.load(function() {
  50. if (BABYLON.Engine.isSupported()) {
  51. var canvas = document.getElementById("renderCanvas");
  52. var engine = new BABYLON.Engine(canvas, true);
  53. BABYLONDEVTOOLS.Loader.debugShortcut(engine);
  54. var divFps = document.getElementById("fps");
  55. 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. camera.minZ = 0.1;
  59. // Lights
  60. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  61. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  62. pointLight.setEnabled(false);
  63. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  64. directionalLight.setEnabled(false);
  65. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  66. spotLight.setEnabled(false);
  67. // Create meshes
  68. var sphere = BABYLON.Mesh.CreateSphere("sphere", 48, 30.0, scene);
  69. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  70. plane.setEnabled(false);
  71. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  72. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  73. ground.setEnabled(false);
  74. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  75. knot.setEnabled(false);
  76. var heightMap = BABYLON.Mesh.CreateGroundFromHeightMap("heightMap", "../assets/textures/heightMap.png", 100, 100, 100, 0, 10, scene, false);
  77. heightMap.setEnabled(false);
  78. // Skybox
  79. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  80. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  81. skyboxMaterial.backFaceCulling = false;
  82. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("../assets/textures/skybox/TropicalSunnyDay", scene);
  83. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  84. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  85. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  86. skyboxMaterial.disableLighting = true;
  87. skybox.material = skyboxMaterial;
  88. skybox.setEnabled(false);
  89. var currentMesh = sphere;
  90. // Rabbit
  91. var rabbit;
  92. BABYLON.SceneLoader.ImportMesh("Rabbit", "../assets/meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  93. rabbit = newMeshes[1];
  94. rabbit.setEnabled(false);
  95. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  96. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  97. // Shadow caster
  98. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  99. shadowCaster.setEnabled(false);
  100. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  101. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  102. shadowCaster2.setEnabled(false);
  103. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  104. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  105. shadowCaster3.setEnabled(false);
  106. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  107. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  108. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  109. shadowGenerator.usePoissonSampling = true;
  110. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  111. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  112. shadowGenerator2.usePoissonSampling = true;
  113. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  114. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  115. shadowGenerator3.usePoissonSampling = false;
  116. shadowGenerator3.bias = 0;
  117. // Register a render loop to repeatedly render the scene
  118. engine.runRenderLoop(function () {
  119. scene.render();
  120. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  121. shadowCaster.rotation.x += 0.01;
  122. shadowCaster.rotation.y += 0.01;
  123. shadowCaster2.rotation.x += 0.01;
  124. shadowCaster2.rotation.y += 0.01;
  125. shadowCaster3.rotation.x += 0.01;
  126. shadowCaster3.rotation.y += 0.01;
  127. });
  128. // Resize
  129. window.addEventListener("resize", function () {
  130. engine.resize();
  131. });
  132. // Fog
  133. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  134. scene.fogDensity = 0.01;
  135. // Create shaders
  136. var std = new BABYLON.StandardMaterial("std", scene);
  137. std.diffuseTexture = new BABYLON.Texture("../assets/textures/amiga.jpg", scene);
  138. std.diffuseTexture.uScale = 5;
  139. std.diffuseTexture.vScale = 5;
  140. var lava = prepareLava();
  141. var simple = new BABYLON.SimpleMaterial("simple", scene);
  142. simple.diffuseTexture = new BABYLON.Texture("../assets/textures/amiga.jpg", scene);
  143. simple.diffuseTexture.uScale = 5;
  144. simple.diffuseTexture.vScale = 5;
  145. var normal = prepareNormal();
  146. var gradient = prepareGradient();
  147. var fur = prepareFur();
  148. var water = prepareWater();
  149. water.addToRenderList(skybox);
  150. water.addToRenderList(shadowCaster);
  151. water.addToRenderList(shadowCaster2);
  152. water.addToRenderList(shadowCaster3);
  153. var fire = prepareFire();
  154. var terrain = prepareTerrain();
  155. var pbr = preparePBR();
  156. var triPlanar = prepareTriPlanar();
  157. var sky = prepareSky();
  158. var grid = prepareGrid();
  159. var shadowOnly = new BABYLON.ShadowOnlyMaterial();
  160. // Default to std
  161. var currentMaterial = std;
  162. sphere.material = std;
  163. sphere.receiveShadows = true;
  164. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava', 'normal', 'terrain', 'pbr', 'fur', 'triPlanar', 'gradient', 'sky', 'grid', 'shadowOnly']).onFinishChange(function () {
  165. water.enableRenderTargets(false);
  166. skybox.material = skyboxMaterial;
  167. currentMesh.isVisible = true;
  168. fur.resetFur();
  169. switch (options.material) {
  170. case "shadowOnly":
  171. currentMaterial = shadowOnly;
  172. break;
  173. case "simple":
  174. currentMaterial = simple;
  175. break;
  176. case "water":
  177. currentMaterial = water;
  178. water.enableRenderTargets(true);
  179. skybox.setEnabled(true);
  180. break;
  181. case "fire":
  182. currentMaterial = fire;
  183. break;
  184. case "lava":
  185. currentMaterial = lava;
  186. break;
  187. case "normal":
  188. currentMaterial = normal;
  189. break;
  190. case "terrain":
  191. currentMaterial = terrain;
  192. break;
  193. case "pbr":
  194. currentMaterial = pbr;
  195. break;
  196. case "fur":
  197. currentMaterial = fur.material;
  198. fur.configureFur(currentMesh);
  199. break;
  200. case "triPlanar":
  201. currentMaterial = triPlanar;
  202. break;
  203. case "gradient":
  204. currentMaterial = gradient;
  205. break;
  206. case "sky":
  207. skybox.setEnabled(true);
  208. skybox.material = sky;
  209. break;
  210. case "grid":
  211. currentMaterial = grid;
  212. break;
  213. default:
  214. currentMaterial = std;
  215. break;
  216. }
  217. currentMesh.material = currentMaterial;
  218. window.enableMaterial(options.material);
  219. });
  220. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'heightMap', '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 "heightMap":
  236. currentMesh = heightMap;
  237. break;
  238. case "rabbit":
  239. currentMesh = rabbit;
  240. break;
  241. }
  242. currentMesh.setEnabled(true);
  243. currentMesh.receiveShadows = true;
  244. currentMesh.material = currentMaterial;
  245. water.mesh = currentMesh;
  246. if (currentMaterial === fur.material) {
  247. // Furify the mesh
  248. fur.resetFur();
  249. fur.configureFur(currentMesh);
  250. }
  251. });
  252. var f1 = gui.addFolder('lights');
  253. f1.add(options, 'lightIntensity').onChange(function() {
  254. hemisphericLight.intensity = options.lightIntensity;
  255. directionalLight.intensity = options.lightIntensity;
  256. pointLight.intensity = options.lightIntensity;
  257. spotLight.intensity = options.lightIntensity;
  258. });
  259. f1.add(options, 'lightRange').onChange(function() {
  260. hemisphericLight.range = options.lightRange;
  261. directionalLight.range = options.lightRange;
  262. pointLight.range = options.lightRange;
  263. spotLight.range = options.lightRange;
  264. });
  265. f1.add(options, 'lightRadius').onChange(function() {
  266. hemisphericLight.radius = options.lightRadius;
  267. directionalLight.radius = options.lightRadius;
  268. pointLight.radius = options.lightRadius;
  269. spotLight.radius = options.lightRadius;
  270. });
  271. f1.add(options, 'hemisphericLight').onChange(function () {
  272. hemisphericLight.setEnabled(options.hemisphericLight);
  273. });
  274. f1.add(options, 'pointLight').onChange(function () {
  275. pointLight.setEnabled(options.pointLight);
  276. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  277. });
  278. f1.add(options, 'spotLight').onChange(function () {
  279. spotLight.setEnabled(options.spotLight);
  280. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  281. });
  282. f1.add(options, 'directionalLight').onChange(function () {
  283. directionalLight.setEnabled(options.directionalLight);
  284. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  285. });
  286. f1.add(options, 'castShadows').onChange(function () {
  287. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  288. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  289. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  290. });
  291. gui.add(options, 'fog').onChange(function () {
  292. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  293. });
  294. gui.add(options, 'skybox').onChange(function() {
  295. skybox.setEnabled(options.skybox);
  296. });
  297. });
  298. }
  299. });
  300. </script>
  301. </body>
  302. </html>