Background.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import DataMap from './DataMap.js';
  2. import { Color, Mesh, SphereGeometry, BackSide } from 'three';
  3. import { context, normalWorld, backgroundBlurriness, backgroundIntensity, NodeMaterial, modelViewProjection, tslFn } from '../../nodes/Nodes.js';
  4. let _clearAlpha;
  5. const _clearColor = new Color();
  6. class Background extends DataMap {
  7. constructor( renderer, nodes ) {
  8. super();
  9. this.renderer = renderer;
  10. this.nodes = nodes;
  11. this.backgroundMesh = null;
  12. this.backgroundMeshNode = null;
  13. }
  14. update( scene, renderList, renderContext ) {
  15. const renderer = this.renderer;
  16. const background = this.nodes.getBackgroundNode( scene ) || scene.background;
  17. let forceClear = false;
  18. if ( background === null ) {
  19. // no background settings, use clear color configuration from the renderer
  20. _clearColor.copyLinearToSRGB( renderer._clearColor );
  21. _clearAlpha = renderer._clearAlpha;
  22. } else if ( background.isColor === true ) {
  23. // background is an opaque color
  24. _clearColor.copyLinearToSRGB( background );
  25. _clearAlpha = 1;
  26. forceClear = true;
  27. } else if ( background.isNode === true ) {
  28. const sceneData = this.get( scene );
  29. const backgroundNode = background;
  30. _clearColor.copy( renderer._clearColor );
  31. _clearAlpha = renderer._clearAlpha;
  32. let backgroundMesh = this.backgroundMesh;
  33. if ( backgroundMesh === null ) {
  34. this.backgroundMeshNode = context( backgroundNode, {
  35. // @TODO: Add Texture2D support using node context
  36. getUVNode: () => normalWorld,
  37. getSamplerLevelNode: () => backgroundBlurriness
  38. } ).mul( backgroundIntensity );
  39. const viewProj = tslFn( () => {
  40. const matrix = modelViewProjection();
  41. matrix.z = matrix.w;
  42. return matrix;
  43. } )();
  44. const nodeMaterial = new NodeMaterial();
  45. nodeMaterial.outputNode = this.backgroundMeshNode;
  46. nodeMaterial.side = BackSide;
  47. nodeMaterial.depthTest = false;
  48. nodeMaterial.depthWrite = false;
  49. nodeMaterial.fog = false;
  50. nodeMaterial.vertexNode = viewProj;
  51. this.backgroundMesh = backgroundMesh = new Mesh( new SphereGeometry( 1, 32, 32 ), nodeMaterial );
  52. backgroundMesh.frustumCulled = false;
  53. backgroundMesh.onBeforeRender = function ( renderer, scene, camera ) {
  54. this.matrixWorld.copyPosition( camera.matrixWorld );
  55. };
  56. }
  57. const backgroundCacheKey = backgroundNode.getCacheKey();
  58. if ( sceneData.backgroundCacheKey !== backgroundCacheKey ) {
  59. this.backgroundMeshNode.node = backgroundNode;
  60. backgroundMesh.material.needsUpdate = true;
  61. sceneData.backgroundCacheKey = backgroundCacheKey;
  62. }
  63. renderList.unshift( backgroundMesh, backgroundMesh.geometry, backgroundMesh.material, 0, 0, null );
  64. } else {
  65. console.error( 'THREE.Renderer: Unsupported background configuration.', background );
  66. }
  67. //
  68. if ( renderer.autoClear === true || forceClear === true ) {
  69. _clearColor.multiplyScalar( _clearAlpha );
  70. const clearColorValue = renderContext.clearColorValue;
  71. clearColorValue.r = _clearColor.r;
  72. clearColorValue.g = _clearColor.g;
  73. clearColorValue.b = _clearColor.b;
  74. clearColorValue.a = _clearAlpha;
  75. renderContext.depthClearValue = renderer._clearDepth;
  76. renderContext.stencilClearValue = renderer._clearStencil;
  77. renderContext.clearColor = renderer.autoClearColor === true;
  78. renderContext.clearDepth = renderer.autoClearDepth === true;
  79. renderContext.clearStencil = renderer.autoClearStencil === true;
  80. } else {
  81. renderContext.clearColor = false;
  82. renderContext.clearDepth = false;
  83. renderContext.clearStencil = false;
  84. }
  85. }
  86. }
  87. export default Background;