index.html 15 KB

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