index.html 15 KB

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