WebGPUBackground.js 4.5 KB

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