WebGPUBackground.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { GPULoadOp, GPUStoreOp } from './constants.js';
  2. import { Color, Mesh, BoxGeometry, BackSide } from 'three';
  3. import { context, positionWorldDirection, MeshBasicNodeMaterial } from '../../nodes/Nodes.js';
  4. let _clearAlpha;
  5. const _clearColor = new Color();
  6. class WebGPUBackground {
  7. constructor( renderer, properties ) {
  8. this.renderer = renderer;
  9. this.properties = properties;
  10. this.boxMesh = null;
  11. this.boxMeshNode = null;
  12. }
  13. update( scene, renderList, renderState ) {
  14. const renderer = this.renderer;
  15. const background = ( scene.isScene === true ) ? scene.backgroundNode || this.properties.get( scene ).backgroundNode || scene.background : null;
  16. let forceClear = false;
  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 if ( background.isNode === true ) {
  27. const sceneProperties = this.properties.get( scene );
  28. const backgroundNode = background;
  29. _clearColor.copy( renderer._clearColor );
  30. _clearAlpha = renderer._clearAlpha;
  31. let boxMesh = this.boxMesh;
  32. if ( boxMesh === null ) {
  33. this.boxMeshNode = context( backgroundNode, {
  34. // @TODO: Add Texture2D support using node context
  35. getUVNode: () => positionWorldDirection
  36. } );
  37. const nodeMaterial = new MeshBasicNodeMaterial();
  38. nodeMaterial.colorNode = this.boxMeshNode;
  39. nodeMaterial.side = BackSide;
  40. nodeMaterial.depthTest = false;
  41. nodeMaterial.depthWrite = false;
  42. nodeMaterial.fog = false;
  43. this.boxMesh = boxMesh = new Mesh( new BoxGeometry( 1, 1, 1 ), nodeMaterial );
  44. boxMesh.frustumCulled = false;
  45. boxMesh.onBeforeRender = function ( renderer, scene, camera ) {
  46. const scale = camera.far;
  47. this.matrixWorld.makeScale( scale, scale, scale ).copyPosition( camera.matrixWorld );
  48. };
  49. }
  50. const backgroundCacheKey = backgroundNode.getCacheKey();
  51. if ( sceneProperties.backgroundCacheKey !== backgroundCacheKey ) {
  52. this.boxMeshNode.node = backgroundNode;
  53. boxMesh.material.needsUpdate = true;
  54. sceneProperties.backgroundCacheKey = backgroundCacheKey;
  55. }
  56. renderList.unshift( boxMesh, boxMesh.geometry, boxMesh.material, 0, 0, null );
  57. } else {
  58. console.error( 'THREE.WebGPURenderer: Unsupported background configuration.', background );
  59. }
  60. // configure render pass descriptor
  61. const colorAttachment = renderState.descriptorGPU.colorAttachments[ 0 ];
  62. const depthStencilAttachment = renderState.descriptorGPU.depthStencilAttachment;
  63. if ( renderer.autoClear === true || forceClear === true ) {
  64. if ( renderer.autoClearColor === true ) {
  65. _clearColor.multiplyScalar( _clearAlpha );
  66. colorAttachment.clearValue = { r: _clearColor.r, g: _clearColor.g, b: _clearColor.b, a: _clearAlpha };
  67. colorAttachment.loadOp = GPULoadOp.Clear;
  68. colorAttachment.storeOp = GPUStoreOp.Store;
  69. } else {
  70. colorAttachment.loadOp = GPULoadOp.Load;
  71. colorAttachment.storeOp = GPUStoreOp.Store;
  72. }
  73. if ( renderState.depth ) {
  74. if ( renderer.autoClearDepth === true ) {
  75. depthStencilAttachment.depthClearValue = renderer._clearDepth;
  76. depthStencilAttachment.depthLoadOp = GPULoadOp.Clear;
  77. depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
  78. } else {
  79. depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
  80. depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
  81. }
  82. }
  83. if ( renderState.stencil ) {
  84. if ( renderer.autoClearStencil === true ) {
  85. depthStencilAttachment.stencilClearValue = renderer._clearStencil;
  86. depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear;
  87. depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
  88. } else {
  89. depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
  90. depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
  91. }
  92. }
  93. } else {
  94. colorAttachment.loadOp = GPULoadOp.Load;
  95. colorAttachment.storeOp = GPUStoreOp.Store;
  96. if ( renderState.depth ) {
  97. depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
  98. depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
  99. }
  100. if ( renderState.stencil ) {
  101. depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
  102. depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
  103. }
  104. }
  105. this.forceClear = false;
  106. }
  107. }
  108. export default WebGPUBackground;