WebGLBackground.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { BackSide } from '../../constants';
  5. import { OrthographicCamera } from '../../cameras/OrthographicCamera';
  6. import { PerspectiveCamera } from '../../cameras/PerspectiveCamera';
  7. import { BoxBufferGeometry } from '../../geometries/BoxGeometry';
  8. import { PlaneBufferGeometry } from '../../geometries/PlaneGeometry';
  9. import { MeshBasicMaterial } from '../../materials/MeshBasicMaterial';
  10. import { ShaderMaterial } from '../../materials/ShaderMaterial';
  11. import { Color } from '../../math/Color';
  12. import { Mesh } from '../../objects/Mesh';
  13. import { ShaderLib } from '../shaders/ShaderLib';
  14. function WebGLBackground( renderer, state, geometries, premultipliedAlpha ) {
  15. var clearColor = new Color( 0x000000 );
  16. var clearAlpha = 0;
  17. var planeCamera, planeMesh;
  18. var boxMesh;
  19. function render( renderList, scene, camera, forceClear ) {
  20. var background = scene.background;
  21. if ( background === null ) {
  22. setClear( clearColor, clearAlpha );
  23. } else if ( background && background.isColor ) {
  24. setClear( background, 1 );
  25. forceClear = true;
  26. }
  27. if ( renderer.autoClear || forceClear ) {
  28. renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  29. }
  30. if ( background && background.isCubeTexture ) {
  31. if ( boxMesh === undefined ) {
  32. // TODO Adjust skybox to camera somehow
  33. boxMesh = new Mesh(
  34. new BoxBufferGeometry( 2, 2, 2 ),
  35. new ShaderMaterial( {
  36. uniforms: ShaderLib.cube.uniforms,
  37. vertexShader: ShaderLib.cube.vertexShader,
  38. fragmentShader: ShaderLib.cube.fragmentShader,
  39. side: BackSide,
  40. depthTest: true,
  41. depthWrite: false,
  42. fog: false
  43. } )
  44. );
  45. geometries.update( boxMesh.geometry );
  46. }
  47. boxMesh.material.uniforms.tCube.value = background;
  48. var scale = camera.far / 1.732; // distance from 0,0,0 to 1,1,1
  49. boxMesh.matrixWorld.makeScale( scale, scale, scale );
  50. boxMesh.matrixWorld.copyPosition( camera.matrixWorld );
  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 };
粤ICP备19079148号