index.html 15 KB

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