ssao 2.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var createScene = function () {
  2. // Create scene
  3. var scene = new BABYLON.Scene(engine);
  4. scene.clearColor = BABYLON.Color3.Black();
  5. // Create camera
  6. var camera = new BABYLON.FreeCamera("camera", new BABYLON.Vector3(29, 13, 23), scene);
  7. camera.setTarget(new BABYLON.Vector3(0, 0, 0));
  8. camera.attachControl(canvas);
  9. // Create some boxes and deactivate lighting (specular color and back faces)
  10. var boxMaterial = new BABYLON.StandardMaterial("boxMaterail", scene);
  11. boxMaterial.diffuseTexture = new BABYLON.Texture("textures/ground.jpg", scene);
  12. boxMaterial.specularColor = BABYLON.Color3.Black();
  13. boxMaterial.emissiveColor = BABYLON.Color3.White();
  14. for (var i = 0; i < 10; i++) {
  15. for (var j = 0; j < 10; j++) {
  16. var box = BABYLON.Mesh.CreateBox("box" + i + " - " + j, 5, scene);
  17. box.position = new BABYLON.Vector3(i * 5, 2.5, j * 5);
  18. box.rotation = new BABYLON.Vector3(i, i * j, j);
  19. box.material = boxMaterial;
  20. }
  21. }
  22. // Create SSAO and configure all properties (for the example)
  23. var ssaoRatio = {
  24. ssaoRatio: 0.5, // Ratio of the SSAO post-process, in a lower resolution
  25. blurRatio: 0.5// Ratio of the combine post-process (combines the SSAO and the scene)
  26. };
  27. if (BABYLON.SSAO2RenderingPipeline.IsSupported) {
  28. var ssao = new BABYLON.SSAO2RenderingPipeline("ssao", scene, ssaoRatio);
  29. ssao.radius = 3.5;
  30. ssao.totalStrength = 1.3;
  31. ssao.expensiveBlur = true;
  32. ssao.samples = 16;
  33. ssao.maxZ = 250;
  34. // Attach camera to the SSAO render pipeline
  35. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline("ssao", camera);
  36. // Manage SSAO
  37. var isAttached = true;
  38. window.addEventListener("keydown", function (evt) {
  39. // draw SSAO with scene when pressed "1"
  40. if (evt.keyCode === 49) {
  41. if (!isAttached) {
  42. isAttached = true;
  43. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline("ssao", camera);
  44. }
  45. scene.postProcessRenderPipelineManager.enableEffectInPipeline("ssao", ssao.SSAOCombineRenderEffect, camera);
  46. }
  47. // draw without SSAO when pressed "2"
  48. else if (evt.keyCode === 50) {
  49. isAttached = false;
  50. scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline("ssao", camera);
  51. }
  52. // draw only SSAO when pressed "2"
  53. else if (evt.keyCode === 51) {
  54. if (!isAttached) {
  55. isAttached = true;
  56. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline("ssao", camera);
  57. }
  58. scene.postProcessRenderPipelineManager.disableEffectInPipeline("ssao", ssao.SSAOCombineRenderEffect, camera);
  59. }
  60. });
  61. } else {
  62. alert("WebGL2 is required to use SSAO2 effect");
  63. }
  64. return scene;
  65. }