threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TonemapPlugin.ts 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // noinspection ES6PreferShortImport
  2. import {uiDropdown, uiFolderContainer, uiSlider, uiToggle} from 'uiconfig.js'
  3. import {
  4. ACESFilmicToneMapping,
  5. CineonToneMapping,
  6. CustomToneMapping,
  7. LinearToneMapping,
  8. Object3D,
  9. ReinhardToneMapping,
  10. ShaderChunk,
  11. ToneMapping,
  12. Vector4,
  13. WebGLRenderer,
  14. } from 'three'
  15. import {glsl, onChange, serialize} from 'ts-browser-helpers'
  16. import {IMaterial} from '../../core'
  17. import {updateBit} from '../../utils'
  18. import {matDefine, uniform} from '../../three'
  19. import Uncharted2ToneMappingShader from './shaders/Uncharted2ToneMapping.glsl'
  20. import TonemapShader from './shaders/TonemapPlugin.pars.glsl'
  21. import TonemapShaderPatch from './shaders/TonemapPlugin.patch.glsl'
  22. import {AScreenPassExtensionPlugin} from './AScreenPassExtensionPlugin'
  23. import {GBufferUpdaterContext} from '../pipeline/GBufferPlugin'
  24. // eslint-disable-next-line @typescript-eslint/naming-convention
  25. export const Uncharted2Tonemapping: ToneMapping = CustomToneMapping
  26. /**
  27. * Tonemap Plugin
  28. *
  29. * Adds an extension to {@link ScreenPass} material
  30. * for applying tonemapping on the final buffer before rendering to screen.
  31. *
  32. * Also adds support for Uncharted2 tone-mapping.
  33. * @category Plugins
  34. */
  35. @uiFolderContainer('Tonemapping')
  36. export class TonemapPlugin extends AScreenPassExtensionPlugin<''> {
  37. static readonly PluginType = 'Tonemap'
  38. readonly extraUniforms = {
  39. toneMappingContrast: {value: 1},
  40. toneMappingSaturation: {value: 1},
  41. } as const
  42. readonly extraDefines = {
  43. ['TONEMAP_BACKGROUND']: '1',
  44. } as const
  45. @serialize() @uiToggle('Enabled') enabled = true
  46. @uiDropdown('Mode', ([
  47. ['Linear', LinearToneMapping],
  48. ['Reinhard', ReinhardToneMapping],
  49. ['Cineon', CineonToneMapping],
  50. ['ACESFilmic', ACESFilmicToneMapping],
  51. ['Uncharted2', Uncharted2Tonemapping],
  52. ] as [string, ToneMapping][]).map(value => ({
  53. label: value[0],
  54. value: value[1],
  55. })))
  56. @onChange(TonemapPlugin.prototype.setDirty)
  57. @serialize() toneMapping: ToneMapping = ACESFilmicToneMapping
  58. @uiToggle('Tonemap Background', (t: TonemapPlugin)=>({hidden: ()=>!t._viewer?.renderManager.gbufferTarget}))
  59. @matDefine('TONEMAP_BACKGROUND', undefined, true, TonemapPlugin.prototype.setDirty, (v)=>v ? '1' : '0', (v) => v !== '0')
  60. @serialize() tonemapBackground = true
  61. // todo handle legacy deserialize
  62. // @onChange(TonemapPlugin.prototype.setDirty)
  63. // @uiToggle('Clip Background')
  64. // @serialize() clipBackground = false
  65. @onChange(TonemapPlugin.prototype.setDirty)
  66. @uiSlider('Exposure', [0, 2 * Math.PI], 0.01)
  67. @serialize() exposure = 1
  68. @uiSlider('Saturation', [0, 2], 0.01)
  69. @uniform({propKey: 'toneMappingSaturation'})
  70. @serialize() saturation: number
  71. @uiSlider('Contrast', [0, 2], 0.01)
  72. @uniform({propKey: 'toneMappingContrast'})
  73. @serialize() contrast: number
  74. /**
  75. * The priority of the material extension when applied to the material in ScreenPass
  76. * set to very low priority, so applied at the end
  77. */
  78. priority = -100
  79. parsFragmentSnippet = () => {
  80. if (this.isDisabled()) return ''
  81. return glsl`
  82. uniform float toneMappingContrast;
  83. uniform float toneMappingSaturation;
  84. ${TonemapShader}
  85. `
  86. }
  87. protected _shaderPatch = TonemapShaderPatch
  88. private _rendererState: any = {}
  89. onObjectRender(_: Object3D, material: IMaterial, renderer: WebGLRenderer): void {
  90. if (this.isDisabled()) return
  91. const {toneMapping, toneMappingExposure} = renderer
  92. this._rendererState.toneMapping = toneMapping
  93. this._rendererState.toneMappingExposure = toneMappingExposure
  94. renderer.toneMapping = this.toneMapping
  95. renderer.toneMappingExposure = this.exposure
  96. material.toneMapped = true
  97. material.needsUpdate = true
  98. }
  99. onAfterRender(_: Object3D, _1: IMaterial, renderer: WebGLRenderer): void {
  100. renderer.toneMapping = this._rendererState.toneMapping
  101. renderer.toneMappingExposure = this._rendererState.toneMappingExposure
  102. }
  103. fromJSON(data: any, meta?: any): this|null|Promise<this|null> {
  104. // legacy
  105. if (data.extension) {
  106. if (data.clipBackground !== undefined) {
  107. if (this._viewer) this._viewer.renderManager.screenPass.clipBackground = data.clipBackground
  108. else console.warn('TonemapPlugin: no viewer attached, clipBackground ignored')
  109. delete data.clipBackground
  110. }
  111. }
  112. return super.fromJSON(data, meta)
  113. }
  114. // TODO: add gBufferData or just tonemapEnabled to the scene material UI with an extension
  115. updateGBufferFlags(data: Vector4, c: GBufferUpdaterContext): void {
  116. const x = (c.material.userData.gBufferData?.tonemapEnabled ?? c.material?.userData.postTonemap) === false ? 0 : 1
  117. data.w = updateBit(data.w, 1, x) // 2nd Bit
  118. super.updateGBufferFlags(data, c)
  119. }
  120. static {
  121. // Add support for Uncharted2 tone mapping
  122. ShaderChunk.tonemapping_pars_fragment = ShaderChunk.tonemapping_pars_fragment.replace('vec3 CustomToneMapping( vec3 color ) { return color; }', Uncharted2ToneMappingShader)
  123. }
  124. }