index.html 17 KB

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