ssao rendering pipeline.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. window.addEventListener("keydown", function (evt) {
  37. // draw SSAO with scene when pressed "1"
  38. if (evt.keyCode === 49) {
  39. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline("ssao", camera);
  40. scene.postProcessRenderPipelineManager.enableEffectInPipeline("ssao", ssao.SSAOCombineRenderEffect, camera);
  41. }
  42. // draw without SSAO when pressed "2"
  43. else if (evt.keyCode === 50) {
  44. scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline("ssao", camera);
  45. }
  46. // draw only SSAO when pressed "2"
  47. else if (evt.keyCode === 51) {
  48. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline("ssao", camera);
  49. scene.postProcessRenderPipelineManager.disableEffectInPipeline("ssao", ssao.SSAOCombineRenderEffect, camera);
  50. }
  51. });
  52. return scene;
  53. }