postprocessBloom.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var CreatePostProcessBloomTestScene = function (engine) {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
  4. var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0, -1, -0.2), scene);
  5. var light2 = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(-1, -2, -1), scene);
  6. light.position = new BABYLON.Vector3(0, 30, 0);
  7. light2.position = new BABYLON.Vector3(10, 20, 10);
  8. light.intensity = 0.6;
  9. light2.intensity = 0.6;
  10. camera.setPosition(new BABYLON.Vector3(-40, 40, 0));
  11. camera.lowerBetaLimit = (Math.PI / 2) * 0.9;
  12. // Skybox
  13. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  14. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  15. skyboxMaterial.backFaceCulling = false;
  16. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("Scenes/Customs/skybox/snow", scene);
  17. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  18. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  19. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  20. skybox.material = skyboxMaterial;
  21. // Spheres
  22. var sphere0 = BABYLON.Mesh.CreateSphere("Sphere0", 16, 10, scene);
  23. var sphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 16, 10, scene);
  24. var sphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 16, 10, scene);
  25. var cube = BABYLON.Mesh.CreateBox("Cube", 10.0, scene);
  26. sphere0.material = new BABYLON.StandardMaterial("white", scene);
  27. sphere0.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
  28. sphere0.material.specularColor = new BABYLON.Color3(0, 0, 0);
  29. sphere0.material.emissiveColor = new BABYLON.Color3(1.0, 1.0, 1.0);
  30. sphere1.material = sphere0.material;
  31. sphere2.material = sphere0.material;
  32. cube.material = new BABYLON.StandardMaterial("red", scene);
  33. cube.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
  34. cube.material.specularColor = new BABYLON.Color3(0, 0, 0);
  35. cube.material.emissiveColor = new BABYLON.Color3(1.0, 0, 0);
  36. // Post-process
  37. var blurWidth = 1.0;
  38. var postProcess0 = new BABYLON.PassPostProcess("Scene copy", 1.0, camera);
  39. var postProcess1 = new BABYLON.PostProcess("Down sample", "./Scenes/Customs/postprocesses/downsample", ["screenSize", "highlightThreshold"], null, 0.25, camera, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  40. postProcess1.onApply = function (effect) {
  41. effect.setFloat2("screenSize", postProcess1.width, postProcess1.height);
  42. effect.setFloat("highlightThreshold", 0.90);
  43. };
  44. var postProcess2 = new BABYLON.BlurPostProcess("Horizontal blur", new BABYLON.Vector2(1.0, 0), blurWidth, 0.25, camera);
  45. var postProcess3 = new BABYLON.BlurPostProcess("Vertical blur", new BABYLON.Vector2(0, 1.0), blurWidth, 0.25, camera);
  46. var postProcess4 = new BABYLON.PostProcess("Final compose", "./Scenes/Customs/postprocesses/compose", ["sceneIntensity", "glowIntensity", "highlightIntensity"], ["sceneSampler"], 1, camera);
  47. postProcess4.onApply = function (effect) {
  48. effect.setTextureFromPostProcess("sceneSampler", postProcess0);
  49. effect.setFloat("sceneIntensity", 0.5);
  50. effect.setFloat("glowIntensity", 0.4);
  51. effect.setFloat("highlightIntensity", 1.0);
  52. };
  53. // Animations
  54. var alpha = 0;
  55. scene.registerBeforeRender(function() {
  56. sphere0.position = new BABYLON.Vector3(20 * Math.sin(alpha), 0, 20 * Math.cos(alpha));
  57. sphere1.position = new BABYLON.Vector3(20 * Math.sin(alpha), 0, -20 * Math.cos(alpha));
  58. sphere2.position = new BABYLON.Vector3(20 * Math.cos(alpha), 0, 20 * Math.sin(alpha));
  59. cube.rotation.y += 0.01;
  60. cube.rotation.z += 0.01;
  61. alpha += 0.01;
  62. });
  63. return scene;
  64. };