builtin-dof-pass.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. Copyright (c) 2021-2024 Xiamen Yaji Software Co., Ltd.
  3. https://www.cocos.com/
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. import {
  21. _decorator, assert, CCBoolean, CCFloat, CCInteger,
  22. gfx, Material, renderer, rendering, Vec3, Vec4,
  23. } from 'cc';
  24. import { EDITOR } from 'cc/env';
  25. import {
  26. BuiltinPipelineSettings,
  27. } from './builtin-pipeline-settings';
  28. import {
  29. BuiltinPipelinePassBuilder,
  30. } from './builtin-pipeline-pass';
  31. import {
  32. CameraConfigs,
  33. getPingPongRenderTarget,
  34. PipelineConfigs,
  35. PipelineContext,
  36. } from './builtin-pipeline';
  37. const { ccclass, disallowMultiple, executeInEditMode, menu, property, requireComponent, type } = _decorator;
  38. const { Color, LoadOp, StoreOp } = gfx;
  39. export interface DofPassConfigs {
  40. enableDof: boolean;
  41. }
  42. @ccclass('BuiltinDepthOfFieldPass')
  43. @menu('Rendering/BuiltinDepthOfFieldPass')
  44. @requireComponent(BuiltinPipelineSettings)
  45. @disallowMultiple
  46. @executeInEditMode
  47. export class BuiltinDepthOfFieldPass extends BuiltinPipelinePassBuilder
  48. implements rendering.PipelinePassBuilder {
  49. @property({
  50. group: { id: 'BuiltinPass', name: 'Pass Settings', style: 'section' },
  51. type: CCInteger,
  52. })
  53. configOrder = 0;
  54. @property({
  55. group: { id: 'BuiltinPass', name: 'Pass Settings', style: 'section' },
  56. type: CCInteger,
  57. })
  58. renderOrder = 150;
  59. @property
  60. private _enableDof = false;
  61. @property
  62. private _material: Material | null = null;
  63. @property
  64. private _minRange = 0;
  65. @property
  66. private _maxRange = 2;
  67. @property
  68. private _blurRadius = 1;
  69. @property
  70. private _intensity = 1;
  71. @property
  72. private _focusPos = new Vec3(0, 0, 0);
  73. // DepthOfField
  74. @property({
  75. group: { id: 'DepthOfField', name: 'DepthOfField (PostProcessing)', style: 'section' },
  76. type: CCBoolean,
  77. visible: true,
  78. })
  79. set dofEnable(value: boolean) {
  80. this._enableDof = value;
  81. if (EDITOR) {
  82. this._parent._tryEnableEditorPreview();
  83. }
  84. }
  85. get dofEnable(): boolean {
  86. return this._enableDof;
  87. }
  88. @property({
  89. group: { id: 'DepthOfField', name: 'DepthOfField (PostProcessing)', style: 'section' },
  90. type: Material,
  91. visible: true,
  92. })
  93. set dofMaterial(value: Material) {
  94. if (this._material === value) {
  95. return;
  96. }
  97. this._material = value;
  98. if (EDITOR) {
  99. this._parent._tryEnableEditorPreview();
  100. }
  101. }
  102. get dofMaterial(): Material {
  103. return this._material!;
  104. }
  105. @property({
  106. group: { id: 'DepthOfField', name: 'DepthOfField (PostProcessing)', style: 'section' },
  107. type: CCFloat,
  108. min: 0,
  109. visible: true,
  110. })
  111. set dofMinRange(value: number) {
  112. this._minRange = value;
  113. }
  114. get dofMinRange(): number {
  115. return this._minRange;
  116. }
  117. @property({
  118. group: { id: 'DepthOfField', name: 'DepthOfField (PostProcessing)', style: 'section' },
  119. type: CCFloat,
  120. min: 0,
  121. visible: true,
  122. })
  123. set dofMaxRange(value: number) {
  124. this._maxRange = value;
  125. }
  126. get dofMaxRange(): number {
  127. return this._maxRange;
  128. }
  129. @property({
  130. group: { id: 'DepthOfField', name: 'DepthOfField (PostProcessing)', style: 'section' },
  131. type: CCFloat,
  132. range: [0.0, 2, 0.01],
  133. slide: true,
  134. visible: true,
  135. })
  136. set dofIntensity(value: number) {
  137. this._intensity = value;
  138. }
  139. get dofIntensity(): number {
  140. return this._intensity;
  141. }
  142. @type(CCFloat)
  143. @property({
  144. group: { id: 'DepthOfField', name: 'DepthOfField (PostProcessing)', style: 'section' },
  145. type: CCFloat,
  146. range: [0.01, 10, 0.01],
  147. slide: true,
  148. visible: true,
  149. })
  150. set dofBlurRadius(value: number) {
  151. this._blurRadius = value;
  152. }
  153. get dofBlurRadius(): number {
  154. return this._blurRadius;
  155. }
  156. @type(Vec3)
  157. @property({
  158. group: { id: 'DepthOfField', name: 'DepthOfField (PostProcessing)', style: 'section' },
  159. type: Vec3,
  160. visible: true,
  161. })
  162. set dofFocusPos(value: Vec3) {
  163. this._focusPos = value;
  164. }
  165. get dofFocusPos(): Vec3 {
  166. return this._focusPos;
  167. }
  168. // PipelinePassBuilder
  169. getConfigOrder(): number {
  170. return this.configOrder;
  171. }
  172. getRenderOrder(): number {
  173. return this.renderOrder;
  174. }
  175. configCamera(
  176. camera: Readonly<renderer.scene.Camera>,
  177. pplConfigs: Readonly<PipelineConfigs>,
  178. cameraConfigs: CameraConfigs & DofPassConfigs): void {
  179. cameraConfigs.enableDof = pplConfigs.supportDepthSample
  180. && this._enableDof
  181. && !!this._material;
  182. if (cameraConfigs.enableDof) {
  183. // Output scene depth, this is allowed but has performance impact
  184. cameraConfigs.enableStoreSceneDepth = true;
  185. ++cameraConfigs.remainingPasses;
  186. }
  187. }
  188. windowResize(
  189. ppl: rendering.BasicPipeline,
  190. pplConfigs: Readonly<PipelineConfigs>,
  191. cameraConfigs: Readonly<CameraConfigs & DofPassConfigs>,
  192. window: renderer.RenderWindow): void {
  193. const id = window.renderWindowId;
  194. if (cameraConfigs.enableDof) {
  195. ppl.addRenderTarget(`DofRadiance${id}`,
  196. cameraConfigs.radianceFormat,
  197. cameraConfigs.width,
  198. cameraConfigs.height);
  199. }
  200. }
  201. setup(
  202. ppl: rendering.BasicPipeline,
  203. pplConfigs: Readonly<PipelineConfigs>,
  204. cameraConfigs: CameraConfigs & Readonly<DofPassConfigs>,
  205. camera: renderer.scene.Camera,
  206. context: PipelineContext,
  207. prevRenderPass?: rendering.BasicRenderPassBuilder): rendering.BasicRenderPassBuilder | undefined {
  208. if (!cameraConfigs.enableDof) {
  209. return prevRenderPass;
  210. }
  211. --cameraConfigs.remainingPasses;
  212. assert(!!this._material);
  213. if (cameraConfigs.remainingPasses === 0) {
  214. return this._addDepthOfFieldPasses(ppl, pplConfigs,
  215. cameraConfigs, this._material,
  216. camera, cameraConfigs.width, cameraConfigs.height,
  217. context.colorName,
  218. context.depthStencilName,
  219. cameraConfigs.colorName);
  220. } else {
  221. const prefix = cameraConfigs.enableShadingScale
  222. ? `ScaledRadiance`
  223. : `Radiance`;
  224. const outputRadianceName = getPingPongRenderTarget(
  225. context.colorName, prefix, cameraConfigs.renderWindowId);
  226. const inputRadianceName = context.colorName;
  227. context.colorName = outputRadianceName;
  228. return this._addDepthOfFieldPasses(ppl, pplConfigs,
  229. cameraConfigs, this._material,
  230. camera, cameraConfigs.width, cameraConfigs.height,
  231. inputRadianceName,
  232. context.depthStencilName,
  233. outputRadianceName);
  234. }
  235. }
  236. private _addDepthOfFieldPasses(
  237. ppl: rendering.BasicPipeline,
  238. pplConfigs: Readonly<PipelineConfigs>,
  239. cameraConfigs: CameraConfigs & Readonly<DofPassConfigs>,
  240. dofMaterial: Material,
  241. camera: renderer.scene.Camera,
  242. width: number,
  243. height: number,
  244. inputRadiance: string,
  245. inputDepthStencil: string,
  246. outputRadianceName: string,
  247. ): rendering.BasicRenderPassBuilder {
  248. this._cocParams.x = this._minRange;
  249. this._cocParams.y = this._maxRange;
  250. this._cocParams.z = this._blurRadius;
  251. this._cocParams.w = this._intensity;
  252. this._focusPosVec4.x = this._focusPos.x;
  253. this._focusPosVec4.y = this._focusPos.y;
  254. this._focusPosVec4.z = this._focusPos.z;
  255. this._cocTexSize.x = 1.0 / width;
  256. this._cocTexSize.y = 1.0 / height;
  257. this._cocTexSize.z = width;
  258. this._cocTexSize.w = height;
  259. const id = cameraConfigs.renderWindowId;
  260. const tempRadiance = `DofRadiance${id}`;
  261. // Blur Pass
  262. const blurPass = ppl.addRenderPass(width, height, 'cc-dof-blur');
  263. blurPass.addRenderTarget(tempRadiance, LoadOp.CLEAR, StoreOp.STORE, this._clearColorTransparentBlack);
  264. blurPass.addTexture(inputRadiance, 'screenTex');
  265. blurPass.setVec4('blurParams', this._cocParams);
  266. blurPass.setVec4('mainTexTexelSize', this._cocTexSize);
  267. blurPass
  268. .addQueue(rendering.QueueHint.OPAQUE)
  269. .addCameraQuad(camera, dofMaterial, 0); // addCameraQuad will set camera related UBOs
  270. // coc pass
  271. const cocPass = ppl.addRenderPass(width, height, 'cc-dof-coc');
  272. cocPass.addRenderTarget(outputRadianceName, LoadOp.CLEAR, StoreOp.STORE, this._clearColorTransparentBlack);
  273. cocPass.addTexture(tempRadiance, 'colorTex');
  274. cocPass.addTexture(inputDepthStencil, "DepthTex");
  275. cocPass.addTexture(inputRadiance, "screenTex");
  276. cocPass.setMat4('proj', camera.matProj);
  277. cocPass.setMat4('invProj', camera.matProjInv);
  278. cocPass.setMat4('viewMatInv', camera.node.worldMatrix);
  279. cocPass.setVec4('cocParams', this._cocParams);
  280. cocPass.setVec4('focus', this._focusPosVec4);
  281. cocPass
  282. .addQueue(rendering.QueueHint.OPAQUE)
  283. .addCameraQuad(camera, dofMaterial, 1);
  284. return cocPass;
  285. }
  286. // Runtime members
  287. private readonly _clearColorTransparentBlack = new Color(0, 0, 0, 0);
  288. private readonly _cocParams = new Vec4(0, 0, 0, 0);
  289. private readonly _focusPosVec4 = new Vec4(0, 0, 0, 1);
  290. private readonly _cocTexSize = new Vec4(0, 0, 0, 0);
  291. }