ssao rendering pipeline.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. combineRatio: 1.0 // Ratio of the combine post-process (combines the SSAO and the scene)
  26. };
  27. var ssao = new BABYLON.SSAORenderingPipeline("ssao", scene, ssaoRatio);
  28. ssao.fallOff = 0.000001;
  29. ssao.area = 1;
  30. ssao.radius = 0.0001;
  31. ssao.totalStrength = 1.0;
  32. ssao.base = 0.5;
  33. // Attach camera to the SSAO render pipeline
  34. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline("ssao", camera);
  35. // Manage SSAO
  36. var isAttached = true;
  37. window.addEventListener("keydown", function (evt) {
  38. // draw SSAO with scene when pressed "1"
  39. if (evt.keyCode === 49) {
  40. if (!isAttached) {
  41. isAttached = true;
  42. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline("ssao", camera);
  43. }
  44. scene.postProcessRenderPipelineManager.enableEffectInPipeline("ssao", ssao.SSAOCombineRenderEffect, camera);
  45. }
  46. // draw without SSAO when pressed "2"
  47. else if (evt.keyCode === 50) {
  48. isAttached = false;
  49. scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline("ssao", camera);
  50. }
  51. // draw only SSAO when pressed "2"
  52. else if (evt.keyCode === 51) {
  53. if (!isAttached) {
  54. isAttached = true;
  55. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline("ssao", camera);
  56. }
  57. scene.postProcessRenderPipelineManager.disableEffectInPipeline("ssao", ssao.SSAOCombineRenderEffect, camera);
  58. }
  59. });
  60. return scene;
  61. }