threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DepthBufferPlugin.ts 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {
  2. BasicDepthPacking,
  3. BufferGeometry,
  4. Camera,
  5. Color,
  6. DepthPackingStrategies,
  7. DoubleSide,
  8. FrontSide,
  9. MeshDepthMaterial,
  10. NoBlending,
  11. Object3D,
  12. Scene,
  13. Texture,
  14. TextureDataType,
  15. UnsignedByteType,
  16. WebGLRenderer,
  17. WebGLRenderTarget,
  18. } from 'three'
  19. import {GBufferRenderPass} from '../../postprocessing'
  20. import {ThreeViewer} from '../../viewer'
  21. import {MaterialExtension} from '../../materials'
  22. import {PipelinePassPlugin} from '../base/PipelinePassPlugin'
  23. import {uiDropdown, uiFolderContainer, uiImage} from 'uiconfig.js'
  24. import {shaderReplaceString} from '../../utils'
  25. import {onChange} from 'ts-browser-helpers'
  26. import DepthBufferUnpack from './shaders/DepthBufferPlugin.unpack.glsl'
  27. import {threeConstMappings} from '../../three'
  28. import {IMaterial, PhysicalMaterial} from '../../core'
  29. export type DepthBufferPluginEventTypes = ''
  30. // type DepthBufferPluginTarget = WebGLMultipleRenderTargets | WebGLRenderTarget
  31. export type DepthBufferPluginTarget = WebGLRenderTarget
  32. export type DepthBufferPluginPass = GBufferRenderPass<'depth', DepthBufferPluginTarget>
  33. /**
  34. * Depth Buffer Plugin
  35. *
  36. * Adds a pre-render pass to render the depth buffer to a render target that can be used as gbuffer or for postprocessing.
  37. * @category Plugins
  38. */
  39. @uiFolderContainer('Depth Buffer Plugin')
  40. export class DepthBufferPlugin
  41. extends PipelinePassPlugin<DepthBufferPluginPass, 'depth', DepthBufferPluginEventTypes> {
  42. readonly passId = 'depth'
  43. public static readonly PluginType = 'DepthBufferPlugin'
  44. target?: DepthBufferPluginTarget
  45. @uiImage('Depth Buffer' /* {readOnly: true}*/) texture?: Texture
  46. readonly material: MeshDepthMaterial = new MeshDepthMaterialOverride({
  47. depthPacking: BasicDepthPacking,
  48. blending: NoBlending,
  49. transparent: true,
  50. })
  51. @onChange(DepthBufferPlugin.prototype._depthPackingChanged)
  52. @uiDropdown('Depth Packing', threeConstMappings.DepthPackingStrategies.uiConfig) depthPacking: DepthPackingStrategies
  53. // @onChange2(DepthBufferPlugin.prototype._createTarget)
  54. // @uiDropdown('Buffer Type', threeConstMappings.TextureDataType.uiConfig)
  55. readonly bufferType: TextureDataType // cannot be changed after creation (for now)
  56. // @uiToggle()
  57. // @onChange2(DepthBufferPlugin.prototype._createTarget)
  58. readonly isPrimaryGBuffer: boolean // cannot be changed after creation (for now)
  59. protected _depthPackingChanged() {
  60. this.material.depthPacking = this.depthPacking
  61. this.material.needsUpdate = true
  62. if (this.unpackExtension && this.unpackExtension.extraDefines) {
  63. this.unpackExtension.extraDefines.DEPTH_PACKING = this.depthPacking
  64. this.unpackExtension.setDirty?.()
  65. }
  66. this.setDirty()
  67. }
  68. unpackExtension: MaterialExtension = {
  69. shaderExtender: (shader)=>{
  70. shader.fragmentShader = shaderReplaceString(shader.fragmentShader,
  71. '#include <packing>',
  72. '\n' + DepthBufferUnpack + '\n', {append: true})
  73. },
  74. extraUniforms: {
  75. tDepthBuffer: ()=>({value: this.target?.texture}),
  76. },
  77. extraDefines: {
  78. ['DEPTH_PACKING']: BasicDepthPacking,
  79. ['HAS_DEPTH_BUFFER']: ()=>this.target?.texture ? 1 : undefined,
  80. ['HAS_GBUFFER']: ()=>this.isPrimaryGBuffer && this.target?.texture ? 1 : undefined,
  81. },
  82. priority: 100,
  83. isCompatible: () => true,
  84. }
  85. private _isPrimaryGBufferSet = false
  86. protected _createTarget(recreate = true) {
  87. if (!this._viewer) return
  88. if (recreate) this._disposeTarget()
  89. if (!this.target)
  90. this.target = this._viewer.renderManager.createTarget<DepthBufferPluginTarget>(
  91. {
  92. depthBuffer: true,
  93. samples: this._viewer.renderManager.composerTarget.samples || 0,
  94. type: this.bufferType,
  95. // magFilter: NearestFilter,
  96. // minFilter: NearestFilter,
  97. // generateMipmaps: false,
  98. // encoding: LinearEncoding,
  99. })
  100. this.texture = this.target.texture
  101. this.texture.name = 'depthBuffer'
  102. if (this.isPrimaryGBuffer) {
  103. this._viewer.renderManager.gbufferTarget = this.target
  104. this._viewer.renderManager.screenPass.material.registerMaterialExtensions([this.unpackExtension])
  105. this._isPrimaryGBufferSet = true
  106. }
  107. }
  108. protected _disposeTarget() {
  109. if (!this._viewer) return
  110. if (this.target) {
  111. this._viewer.renderManager.disposeTarget(this.target)
  112. this.target = undefined
  113. }
  114. this.texture = undefined
  115. if (this._isPrimaryGBufferSet) { // using a separate flag as when isPrimaryGBuffer is changed, we cannot check it.
  116. this._viewer.renderManager.gbufferTarget = undefined
  117. // this._viewer.renderManager.screenPass.material.unregisterMaterialExtensions([this.unpackExtension]) // todo
  118. this._isPrimaryGBufferSet = false
  119. }
  120. }
  121. protected _createPass() {
  122. this._createTarget(true)
  123. if (!this.target) throw new Error('DepthBufferPlugin: target not created')
  124. this.material.userData.isGBufferMaterial = true
  125. const pass = new GBufferRenderPass('depth', this.target, this.material, new Color(0, 0, 0), 1)
  126. const preprocessMaterial = pass.preprocessMaterial
  127. pass.preprocessMaterial = (m) => preprocessMaterial(m, m.userData.renderToDepth)
  128. pass.before = ['render']
  129. pass.after = []
  130. pass.required = ['render']
  131. return pass
  132. }
  133. constructor(
  134. bufferType: TextureDataType = UnsignedByteType,
  135. isPrimaryGBuffer = false,
  136. enabled = true,
  137. depthPacking: DepthPackingStrategies = BasicDepthPacking,
  138. ) {
  139. super()
  140. this.enabled = enabled
  141. this.depthPacking = depthPacking
  142. this.bufferType = bufferType
  143. this.isPrimaryGBuffer = isPrimaryGBuffer
  144. }
  145. onRemove(viewer: ThreeViewer): void {
  146. this._disposeTarget()
  147. return super.onRemove(viewer)
  148. }
  149. }
  150. class MeshDepthMaterialOverride extends MeshDepthMaterial {
  151. onBeforeRender(renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, object: Object3D) {
  152. super.onBeforeRender(renderer, scene, camera, geometry, object)
  153. let material = (object as any).material as IMaterial & Partial<PhysicalMaterial>
  154. if (Array.isArray(material)) { // todo: add support for multi materials.
  155. material = material[0]
  156. }
  157. if (!material) return
  158. if (material.map !== undefined) this.map = material.map // in case there is alpha in the map.
  159. if (material.side !== undefined) this.side = DoubleSide
  160. if (material.alphaMap !== undefined) this.alphaMap = material.alphaMap
  161. if (material.alphaTest !== undefined) this.alphaTest = material.alphaTest < 1e-4 ? 1e-4 : material.alphaTest
  162. if (material.displacementMap !== undefined) this.displacementMap = material.displacementMap
  163. if (material.displacementScale !== undefined) this.displacementScale = material.displacementScale
  164. if (material.displacementBias !== undefined) this.displacementBias = material.displacementBias
  165. if (material.wireframe !== undefined) this.wireframe = material.wireframe
  166. if (material.wireframeLinewidth !== undefined) this.wireframeLinewidth = material.wireframeLinewidth
  167. this.needsUpdate = true
  168. }
  169. onAfterRender(renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, object: Object3D) {
  170. super.onAfterRender(renderer, scene, camera, geometry, object)
  171. this.map = null
  172. this.side = FrontSide
  173. this.alphaMap = null
  174. this.alphaTest = 0.001
  175. this.displacementMap = null
  176. this.displacementScale = 1
  177. this.displacementBias = 0
  178. this.wireframe = false
  179. this.wireframeLinewidth = 1
  180. }
  181. }