screenSpaceCurvaturePostProcess.js 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { __decorate } from "../tslib.es6.js";
  2. import { Logger } from "../Misc/logger.js";
  3. import { PostProcess } from "./postProcess.js";
  4. import "../Rendering/geometryBufferRendererSceneComponent.js";
  5. import "../Shaders/screenSpaceCurvature.fragment.js";
  6. import { EngineStore } from "../Engines/engineStore.js";
  7. import { RegisterClass } from "../Misc/typeStore.js";
  8. import { serialize } from "../Misc/decorators.js";
  9. import { SerializationHelper } from "../Misc/decorators.serialization.js";
  10. /**
  11. * The Screen Space curvature effect can help highlighting ridge and valley of a model.
  12. */
  13. export class ScreenSpaceCurvaturePostProcess extends PostProcess {
  14. /**
  15. * Gets a string identifying the name of the class
  16. * @returns "ScreenSpaceCurvaturePostProcess" string
  17. */
  18. getClassName() {
  19. return "ScreenSpaceCurvaturePostProcess";
  20. }
  21. /**
  22. * Creates a new instance ScreenSpaceCurvaturePostProcess
  23. * @param name The name of the effect.
  24. * @param scene The scene containing the objects to blur according to their velocity.
  25. * @param options The required width/height ratio to downsize to before computing the render pass.
  26. * @param camera The camera to apply the render pass to.
  27. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  28. * @param engine The engine which the post process will be applied. (default: current engine)
  29. * @param reusable If the post process can be reused on the same frame. (default: false)
  30. * @param textureType Type of textures used when performing the post process. (default: 0)
  31. * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
  32. */
  33. constructor(name, scene, options, camera, samplingMode, engine, reusable, textureType = 0, blockCompilation = false) {
  34. super(name, "screenSpaceCurvature", ["curvature_ridge", "curvature_valley"], ["textureSampler", "normalSampler"], options, camera, samplingMode, engine, reusable, undefined, textureType, undefined, null, blockCompilation);
  35. /**
  36. * Defines how much ridge the curvature effect displays.
  37. */
  38. this.ridge = 1;
  39. /**
  40. * Defines how much valley the curvature effect displays.
  41. */
  42. this.valley = 1;
  43. this._geometryBufferRenderer = scene.enableGeometryBufferRenderer();
  44. if (!this._geometryBufferRenderer) {
  45. // Geometry buffer renderer is not supported. So, work as a passthrough.
  46. Logger.Error("Multiple Render Target support needed for screen space curvature post process. Please use IsSupported test first.");
  47. }
  48. else {
  49. if (this._geometryBufferRenderer.generateNormalsInWorldSpace) {
  50. Logger.Error("ScreenSpaceCurvaturePostProcess does not support generateNormalsInWorldSpace=true for the geometry buffer renderer!");
  51. }
  52. // Geometry buffer renderer is supported.
  53. this.onApply = (effect) => {
  54. effect.setFloat("curvature_ridge", 0.5 / Math.max(this.ridge * this.ridge, 1e-4));
  55. effect.setFloat("curvature_valley", 0.7 / Math.max(this.valley * this.valley, 1e-4));
  56. const normalTexture = this._geometryBufferRenderer.getGBuffer().textures[1];
  57. effect.setTexture("normalSampler", normalTexture);
  58. };
  59. }
  60. }
  61. /**
  62. * Support test.
  63. */
  64. static get IsSupported() {
  65. const engine = EngineStore.LastCreatedEngine;
  66. if (!engine) {
  67. return false;
  68. }
  69. return engine.getCaps().drawBuffersExtension;
  70. }
  71. /**
  72. * @internal
  73. */
  74. static _Parse(parsedPostProcess, targetCamera, scene, rootUrl) {
  75. return SerializationHelper.Parse(() => {
  76. return new ScreenSpaceCurvaturePostProcess(parsedPostProcess.name, scene, parsedPostProcess.options, targetCamera, parsedPostProcess.renderTargetSamplingMode, scene.getEngine(), parsedPostProcess.textureType, parsedPostProcess.reusable);
  77. }, parsedPostProcess, scene, rootUrl);
  78. }
  79. }
  80. __decorate([
  81. serialize()
  82. ], ScreenSpaceCurvaturePostProcess.prototype, "ridge", void 0);
  83. __decorate([
  84. serialize()
  85. ], ScreenSpaceCurvaturePostProcess.prototype, "valley", void 0);
  86. RegisterClass("BABYLON.ScreenSpaceCurvaturePostProcess", ScreenSpaceCurvaturePostProcess);
  87. //# sourceMappingURL=screenSpaceCurvaturePostProcess.js.map