SceneFramebuffer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import Color from '../Core/Color.js';
  2. import defined from '../Core/defined.js';
  3. import destroyObject from '../Core/destroyObject.js';
  4. import PixelFormat from '../Core/PixelFormat.js';
  5. import ClearCommand from '../Renderer/ClearCommand.js';
  6. import Framebuffer from '../Renderer/Framebuffer.js';
  7. import PixelDatatype from '../Renderer/PixelDatatype.js';
  8. import Renderbuffer from '../Renderer/Renderbuffer.js';
  9. import RenderbufferFormat from '../Renderer/RenderbufferFormat.js';
  10. import Sampler from '../Renderer/Sampler.js';
  11. import Texture from '../Renderer/Texture.js';
  12. import TextureMagnificationFilter from '../Renderer/TextureMagnificationFilter.js';
  13. import TextureMinificationFilter from '../Renderer/TextureMinificationFilter.js';
  14. import TextureWrap from '../Renderer/TextureWrap.js';
  15. /**
  16. * @private
  17. */
  18. function SceneFramebuffer() {
  19. this._colorTexture = undefined;
  20. this._idTexture = undefined;
  21. this._depthStencilTexture = undefined;
  22. this._depthStencilRenderbuffer = undefined;
  23. this._framebuffer = undefined;
  24. this._idFramebuffer = undefined;
  25. this._idClearColor = new Color(0.0, 0.0, 0.0, 0.0);
  26. this._useHdr = undefined;
  27. this._clearCommand = new ClearCommand({
  28. color : new Color(0.0, 0.0, 0.0, 0.0),
  29. depth : 1.0,
  30. owner : this
  31. });
  32. }
  33. function destroyResources(post) {
  34. post._framebuffer = post._framebuffer && post._framebuffer.destroy();
  35. post._idFramebuffer = post._idFramebuffer && post._idFramebuffer.destroy();
  36. post._colorTexture = post._colorTexture && post._colorTexture.destroy();
  37. post._idTexture = post._idTexture && post._idTexture.destroy();
  38. post._depthStencilTexture = post._depthStencilTexture && post._depthStencilTexture.destroy();
  39. post._depthStencilRenderbuffer = post._depthStencilRenderbuffer && post._depthStencilRenderbuffer.destroy();
  40. post._depthStencilIdTexture = post._depthStencilIdTexture && post._depthStencilIdTexture.destroy();
  41. post._depthStencilIdRenderbuffer = post._depthStencilIdRenderbuffer && post._depthStencilIdRenderbuffer.destroy();
  42. post._framebuffer = undefined;
  43. post._idFramebuffer = undefined;
  44. post._colorTexture = undefined;
  45. post._idTexture = undefined;
  46. post._depthStencilTexture = undefined;
  47. post._depthStencilRenderbuffer = undefined;
  48. post._depthStencilIdTexture = undefined;
  49. post._depthStencilIdRenderbuffer = undefined;
  50. }
  51. SceneFramebuffer.prototype.update = function(context, viewport, hdr) {
  52. var width = viewport.width;
  53. var height = viewport.height;
  54. var colorTexture = this._colorTexture;
  55. if (defined(colorTexture) && colorTexture.width === width && colorTexture.height === height && hdr === this._useHdr) {
  56. return;
  57. }
  58. destroyResources(this);
  59. this._useHdr = hdr;
  60. var pixelDatatype = hdr ? (context.halfFloatingPointTexture ? PixelDatatype.HALF_FLOAT : PixelDatatype.FLOAT) : PixelDatatype.UNSIGNED_BYTE;
  61. this._colorTexture = new Texture({
  62. context : context,
  63. width : width,
  64. height : height,
  65. pixelFormat : PixelFormat.RGBA,
  66. pixelDatatype : pixelDatatype,
  67. sampler : new Sampler({
  68. wrapS : TextureWrap.CLAMP_TO_EDGE,
  69. wrapT : TextureWrap.CLAMP_TO_EDGE,
  70. minificationFilter : TextureMinificationFilter.NEAREST,
  71. magnificationFilter : TextureMagnificationFilter.NEAREST
  72. })
  73. });
  74. this._idTexture = new Texture({
  75. context : context,
  76. width : width,
  77. height : height,
  78. pixelFormat : PixelFormat.RGBA,
  79. pixelDatatype : PixelDatatype.UNSIGNED_BYTE,
  80. sampler : new Sampler({
  81. wrapS : TextureWrap.CLAMP_TO_EDGE,
  82. wrapT : TextureWrap.CLAMP_TO_EDGE,
  83. minificationFilter : TextureMinificationFilter.NEAREST,
  84. magnificationFilter : TextureMagnificationFilter.NEAREST
  85. })
  86. });
  87. if (context.depthTexture) {
  88. this._depthStencilTexture = new Texture({
  89. context : context,
  90. width : width,
  91. height : height,
  92. pixelFormat : PixelFormat.DEPTH_STENCIL,
  93. pixelDatatype : PixelDatatype.UNSIGNED_INT_24_8,
  94. sampler : new Sampler({
  95. wrapS : TextureWrap.CLAMP_TO_EDGE,
  96. wrapT : TextureWrap.CLAMP_TO_EDGE,
  97. minificationFilter : TextureMinificationFilter.NEAREST,
  98. magnificationFilter : TextureMagnificationFilter.NEAREST
  99. })
  100. });
  101. this._depthStencilIdTexture = new Texture({
  102. context : context,
  103. width : width,
  104. height : height,
  105. pixelFormat : PixelFormat.DEPTH_STENCIL,
  106. pixelDatatype : PixelDatatype.UNSIGNED_INT_24_8,
  107. sampler : new Sampler({
  108. wrapS : TextureWrap.CLAMP_TO_EDGE,
  109. wrapT : TextureWrap.CLAMP_TO_EDGE,
  110. minificationFilter : TextureMinificationFilter.NEAREST,
  111. magnificationFilter : TextureMagnificationFilter.NEAREST
  112. })
  113. });
  114. } else {
  115. this._depthStencilRenderbuffer = new Renderbuffer({
  116. context : context,
  117. width : width,
  118. height : height,
  119. format : RenderbufferFormat.DEPTH_STENCIL
  120. });
  121. this._depthStencilIdRenderbuffer = new Renderbuffer({
  122. context : context,
  123. width : width,
  124. height : height,
  125. format : RenderbufferFormat.DEPTH_STENCIL
  126. });
  127. }
  128. this._framebuffer = new Framebuffer({
  129. context : context,
  130. colorTextures : [this._colorTexture],
  131. depthStencilTexture : this._depthStencilTexture,
  132. depthStencilRenderbuffer : this._depthStencilRenderbuffer,
  133. destroyAttachments : false
  134. });
  135. this._idFramebuffer = new Framebuffer({
  136. context : context,
  137. colorTextures : [this._idTexture],
  138. depthStencilTexture : this._depthStencilIdTexture,
  139. depthStencilRenderbuffer : this._depthStencilIdRenderbuffer,
  140. destroyAttachments : false
  141. });
  142. };
  143. SceneFramebuffer.prototype.clear = function(context, passState, clearColor) {
  144. var framebuffer = passState.framebuffer;
  145. passState.framebuffer = this._framebuffer;
  146. Color.clone(clearColor, this._clearCommand.color);
  147. this._clearCommand.execute(context, passState);
  148. passState.framebuffer = this._idFramebuffer;
  149. Color.clone(this._idClearColor, this._clearCommand.color);
  150. this._clearCommand.execute(context, passState);
  151. passState.framebuffer = framebuffer;
  152. };
  153. SceneFramebuffer.prototype.getFramebuffer = function() {
  154. return this._framebuffer;
  155. };
  156. SceneFramebuffer.prototype.getIdFramebuffer = function() {
  157. return this._idFramebuffer;
  158. };
  159. SceneFramebuffer.prototype.isDestroyed = function() {
  160. return false;
  161. };
  162. SceneFramebuffer.prototype.destroy = function() {
  163. destroyResources(this);
  164. return destroyObject(this);
  165. };
  166. export default SceneFramebuffer;