WebGLRenderTarget.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { EventDispatcher } from '../core/EventDispatcher.js';
  2. import { Texture } from '../textures/Texture.js';
  3. import { LinearFilter } from '../constants.js';
  4. import { Vector4 } from '../math/Vector4.js';
  5. /*
  6. In options, we can specify:
  7. * Texture parameters for an auto-generated target texture
  8. * depthBuffer/stencilBuffer: Booleans to indicate if we should generate these buffers
  9. */
  10. class WebGLRenderTarget extends EventDispatcher {
  11. constructor( width, height, options ) {
  12. super();
  13. this.isWebGLRenderTarget = true;
  14. this.width = width;
  15. this.height = height;
  16. this.depth = 1;
  17. this.scissor = new Vector4( 0, 0, width, height );
  18. this.scissorTest = false;
  19. this.viewport = new Vector4( 0, 0, width, height );
  20. options = options || {};
  21. this.texture = new Texture( undefined, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.encoding );
  22. this.texture.image = {};
  23. this.texture.image.width = width;
  24. this.texture.image.height = height;
  25. this.texture.image.depth = 1;
  26. this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
  27. this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;
  28. this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true;
  29. this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : false;
  30. this.depthTexture = options.depthTexture !== undefined ? options.depthTexture : null;
  31. }
  32. setTexture( texture ) {
  33. texture.image = {
  34. width: this.width,
  35. height: this.height,
  36. depth: this.depth
  37. };
  38. this.texture = texture;
  39. }
  40. setSize( width, height, depth = 1 ) {
  41. if ( this.width !== width || this.height !== height || this.depth !== depth ) {
  42. this.width = width;
  43. this.height = height;
  44. this.depth = depth;
  45. this.texture.image.width = width;
  46. this.texture.image.height = height;
  47. this.texture.image.depth = depth;
  48. this.dispose();
  49. }
  50. this.viewport.set( 0, 0, width, height );
  51. this.scissor.set( 0, 0, width, height );
  52. }
  53. clone() {
  54. return new this.constructor().copy( this );
  55. }
  56. copy( source ) {
  57. this.width = source.width;
  58. this.height = source.height;
  59. this.depth = source.depth;
  60. this.viewport.copy( source.viewport );
  61. this.texture = source.texture.clone();
  62. this.depthBuffer = source.depthBuffer;
  63. this.stencilBuffer = source.stencilBuffer;
  64. this.depthTexture = source.depthTexture;
  65. return this;
  66. }
  67. dispose() {
  68. this.dispatchEvent( { type: 'dispose' } );
  69. }
  70. }
  71. export { WebGLRenderTarget };
粤ICP备19079148号