ssao 2.js 2.8 KB

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