SceneFramebuffer.js 5.3 KB

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