WebGLBackground.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { BackSide } from '../../constants.js';
  5. import { OrthographicCamera } from '../../cameras/OrthographicCamera.js';
  6. import { BoxBufferGeometry } from '../../geometries/BoxGeometry.js';
  7. import { PlaneBufferGeometry } from '../../geometries/PlaneGeometry.js';
  8. import { MeshBasicMaterial } from '../../materials/MeshBasicMaterial.js';
  9. import { ShaderMaterial } from '../../materials/ShaderMaterial.js';
  10. import { Color } from '../../math/Color.js';
  11. import { Mesh } from '../../objects/Mesh.js';
  12. import { ShaderLib } from '../shaders/ShaderLib.js';
  13. function WebGLBackground( renderer, state, geometries, premultipliedAlpha ) {
  14. var clearColor = new Color( 0x000000 );
  15. var clearAlpha = 0;
  16. var planeCamera, planeMesh;
  17. var boxMesh;
  18. function render( renderList, scene, camera, forceClear ) {
  19. var background = scene.background;
  20. if ( background === null ) {
  21. setClear( clearColor, clearAlpha );
  22. } else if ( background && background.isColor ) {
  23. setClear( background, 1 );
  24. forceClear = true;
  25. }
  26. if ( renderer.autoClear || forceClear ) {
  27. renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  28. }
  29. if ( background && background.isCubeTexture ) {
  30. if ( boxMesh === undefined ) {
  31. boxMesh = new Mesh(
  32. new BoxBufferGeometry( 1, 1, 1 ),
  33. new ShaderMaterial( {
  34. uniforms: ShaderLib.cube.uniforms,
  35. vertexShader: ShaderLib.cube.vertexShader,
  36. fragmentShader: ShaderLib.cube.fragmentShader,
  37. side: BackSide,
  38. depthTest: true,
  39. depthWrite: false,
  40. fog: false
  41. } )
  42. );
  43. boxMesh.geometry.removeAttribute( 'normal' );
  44. boxMesh.geometry.removeAttribute( 'uv' );
  45. boxMesh.onBeforeRender = function ( renderer, scene, camera ) {
  46. this.matrixWorld.copyPosition( camera.matrixWorld );
  47. };
  48. geometries.update( boxMesh.geometry );
  49. }
  50. boxMesh.material.uniforms.tCube.value = background;
  51. renderList.push( boxMesh, boxMesh.geometry, boxMesh.material, 0, null );
  52. } else if ( background && background.isTexture ) {
  53. if ( planeCamera === undefined ) {
  54. planeCamera = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  55. planeMesh = new Mesh(
  56. new PlaneBufferGeometry( 2, 2 ),
  57. new MeshBasicMaterial( { depthTest: false, depthWrite: false, fog: false } )
  58. );
  59. geometries.update( planeMesh.geometry );
  60. }
  61. planeMesh.material.map = background;
  62. // TODO Push this to renderList
  63. renderer.renderBufferDirect( planeCamera, null, planeMesh.geometry, planeMesh.material, planeMesh, null );
  64. }
  65. }
  66. function setClear( color, alpha ) {
  67. state.buffers.color.setClear( color.r, color.g, color.b, alpha, premultipliedAlpha );
  68. }
  69. return {
  70. getClearColor: function () {
  71. return clearColor;
  72. },
  73. setClearColor: function ( color, alpha ) {
  74. clearColor.set( color );
  75. clearAlpha = alpha !== undefined ? alpha : 1;
  76. setClear( clearColor, clearAlpha );
  77. },
  78. getClearAlpha: function () {
  79. return clearAlpha;
  80. },
  81. setClearAlpha: function ( alpha ) {
  82. clearAlpha = alpha;
  83. setClear( clearColor, clearAlpha );
  84. },
  85. render: render
  86. };
  87. }
  88. export { WebGLBackground };