screenSpaceCurvaturePostProcess.ts 4.7 KB

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