WebGPUBackground.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { GPULoadOp } from './constants.js';
  2. import { Color } from 'three';
  3. let _clearAlpha;
  4. const _clearColor = new Color();
  5. class WebGPUBackground {
  6. constructor( renderer ) {
  7. this.renderer = renderer;
  8. this.forceClear = false;
  9. }
  10. clear() {
  11. this.forceClear = true;
  12. }
  13. update( scene ) {
  14. const renderer = this.renderer;
  15. const background = ( scene.isScene === true ) ? scene.background : null;
  16. let forceClear = this.forceClear;
  17. if ( background === null ) {
  18. // no background settings, use clear color configuration from the renderer
  19. _clearColor.copy( renderer._clearColor );
  20. _clearAlpha = renderer._clearAlpha;
  21. } else if ( background.isColor === true ) {
  22. // background is an opaque color
  23. _clearColor.copy( background );
  24. _clearAlpha = 1;
  25. forceClear = true;
  26. } else {
  27. console.error( 'THREE.WebGPURenderer: Unsupported background configuration.', background );
  28. }
  29. // configure render pass descriptor
  30. const renderPassDescriptor = renderer._renderPassDescriptor;
  31. const colorAttachment = renderPassDescriptor.colorAttachments[ 0 ];
  32. const depthStencilAttachment = renderPassDescriptor.depthStencilAttachment;
  33. if ( renderer.autoClear === true || forceClear === true ) {
  34. if ( renderer.autoClearColor === true ) {
  35. colorAttachment.loadValue = { r: _clearColor.r, g: _clearColor.g, b: _clearColor.b, a: _clearAlpha };
  36. } else {
  37. colorAttachment.loadValue = GPULoadOp.Load;
  38. }
  39. if ( renderer.autoClearDepth === true ) {
  40. depthStencilAttachment.depthLoadValue = renderer._clearDepth;
  41. } else {
  42. depthStencilAttachment.depthLoadValue = GPULoadOp.Load;
  43. }
  44. if ( renderer.autoClearStencil === true ) {
  45. depthStencilAttachment.stencilLoadValue = renderer._clearStencil;
  46. } else {
  47. depthStencilAttachment.stencilLoadValue = GPULoadOp.Load;
  48. }
  49. } else {
  50. colorAttachment.loadValue = GPULoadOp.Load;
  51. depthStencilAttachment.depthLoadValue = GPULoadOp.Load;
  52. depthStencilAttachment.stencilLoadValue = GPULoadOp.Load;
  53. }
  54. this.forceClear = false;
  55. }
  56. }
  57. export default WebGPUBackground;