index.html 12 KB

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