PointCloudEyeDomeLighting.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Color from "../Core/Color.js";
  3. import defined from "../Core/defined.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import PrimitiveType from "../Core/PrimitiveType.js";
  6. import ClearCommand from "../Renderer/ClearCommand.js";
  7. import DrawCommand from "../Renderer/DrawCommand.js";
  8. import FramebufferManager from "../Renderer/FramebufferManager.js";
  9. import Pass from "../Renderer/Pass.js";
  10. import RenderState from "../Renderer/RenderState.js";
  11. import ShaderSource from "../Renderer/ShaderSource.js";
  12. import BlendingState from "../Scene/BlendingState.js";
  13. import StencilConstants from "../Scene/StencilConstants.js";
  14. import PointCloudEyeDomeLightingShader from "../Shaders/PostProcessStages/PointCloudEyeDomeLighting.js";
  15. /**
  16. * Eye dome lighting. Does not support points with per-point translucency, but does allow translucent styling against the globe.
  17. * Requires support for EXT_frag_depth and WEBGL_draw_buffers extensions in WebGL 1.0.
  18. *
  19. * @private
  20. */
  21. function PointCloudEyeDomeLighting() {
  22. this._framebuffer = new FramebufferManager({
  23. colorAttachmentsLength: 2,
  24. depth: true,
  25. supportsDepthTexture: true,
  26. });
  27. this._drawCommand = undefined;
  28. this._clearCommand = undefined;
  29. this._strength = 1.0;
  30. this._radius = 1.0;
  31. }
  32. Object.defineProperties(PointCloudEyeDomeLighting.prototype, {
  33. framebuffer: {
  34. get: function () {
  35. return this._framebuffer.framebuffer;
  36. },
  37. },
  38. colorGBuffer: {
  39. get: function () {
  40. return this._framebuffer.getColorTexture(0);
  41. },
  42. },
  43. depthGBuffer: {
  44. get: function () {
  45. return this._framebuffer.getColorTexture(1);
  46. },
  47. },
  48. });
  49. function destroyFramebuffer(processor) {
  50. processor._framebuffer.destroy();
  51. processor._drawCommand = undefined;
  52. processor._clearCommand = undefined;
  53. }
  54. const distanceAndEdlStrengthScratch = new Cartesian2();
  55. function createCommands(processor, context) {
  56. const blendFS = new ShaderSource({
  57. defines: ["LOG_DEPTH_WRITE"],
  58. sources: [PointCloudEyeDomeLightingShader],
  59. });
  60. const blendUniformMap = {
  61. u_pointCloud_colorGBuffer: function () {
  62. return processor.colorGBuffer;
  63. },
  64. u_pointCloud_depthGBuffer: function () {
  65. return processor.depthGBuffer;
  66. },
  67. u_distanceAndEdlStrength: function () {
  68. distanceAndEdlStrengthScratch.x = processor._radius;
  69. distanceAndEdlStrengthScratch.y = processor._strength;
  70. return distanceAndEdlStrengthScratch;
  71. },
  72. };
  73. const blendRenderState = RenderState.fromCache({
  74. blending: BlendingState.ALPHA_BLEND,
  75. depthMask: true,
  76. depthTest: {
  77. enabled: true,
  78. },
  79. stencilTest: StencilConstants.setCesium3DTileBit(),
  80. stencilMask: StencilConstants.CESIUM_3D_TILE_MASK,
  81. });
  82. processor._drawCommand = context.createViewportQuadCommand(blendFS, {
  83. uniformMap: blendUniformMap,
  84. renderState: blendRenderState,
  85. pass: Pass.CESIUM_3D_TILE,
  86. owner: processor,
  87. });
  88. processor._clearCommand = new ClearCommand({
  89. framebuffer: processor.framebuffer,
  90. color: new Color(0.0, 0.0, 0.0, 0.0),
  91. depth: 1.0,
  92. renderState: RenderState.fromCache(),
  93. pass: Pass.CESIUM_3D_TILE,
  94. owner: processor,
  95. });
  96. }
  97. function createResources(processor, context) {
  98. const width = context.drawingBufferWidth;
  99. const height = context.drawingBufferHeight;
  100. processor._framebuffer.update(context, width, height);
  101. createCommands(processor, context);
  102. }
  103. function isSupported(context) {
  104. return context.drawBuffers && context.fragmentDepth;
  105. }
  106. PointCloudEyeDomeLighting.isSupported = isSupported;
  107. function getECShaderProgram(context, shaderProgram) {
  108. let shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, "EC");
  109. if (!defined(shader)) {
  110. const attributeLocations = shaderProgram._attributeLocations;
  111. const fs = shaderProgram.fragmentShaderSource.clone();
  112. fs.sources.splice(
  113. 0,
  114. 0,
  115. `layout (location = 0) out vec4 out_FragData_0;\nlayout (location = 1) out vec4 out_FragData_1;`
  116. );
  117. fs.sources = fs.sources.map(function (source) {
  118. source = ShaderSource.replaceMain(
  119. source,
  120. "czm_point_cloud_post_process_main"
  121. );
  122. source = source.replaceAll(/out_FragColor/g, "out_FragData_0");
  123. return source;
  124. });
  125. fs.sources.push(
  126. "void main() \n" +
  127. "{ \n" +
  128. " czm_point_cloud_post_process_main(); \n" +
  129. "#ifdef LOG_DEPTH\n" +
  130. " czm_writeLogDepth();\n" +
  131. " out_FragData_1 = czm_packDepth(gl_FragDepth); \n" +
  132. "#else\n" +
  133. " out_FragData_1 = czm_packDepth(gl_FragCoord.z);\n" +
  134. "#endif\n" +
  135. "}"
  136. );
  137. shader = context.shaderCache.createDerivedShaderProgram(
  138. shaderProgram,
  139. "EC",
  140. {
  141. vertexShaderSource: shaderProgram.vertexShaderSource,
  142. fragmentShaderSource: fs,
  143. attributeLocations: attributeLocations,
  144. }
  145. );
  146. }
  147. return shader;
  148. }
  149. PointCloudEyeDomeLighting.prototype.update = function (
  150. frameState,
  151. commandStart,
  152. pointCloudShading,
  153. boundingVolume
  154. ) {
  155. if (!isSupported(frameState.context)) {
  156. return;
  157. }
  158. this._strength = pointCloudShading.eyeDomeLightingStrength;
  159. this._radius =
  160. pointCloudShading.eyeDomeLightingRadius * frameState.pixelRatio;
  161. createResources(this, frameState.context);
  162. // Hijack existing point commands to render into an offscreen FBO.
  163. let i;
  164. const commandList = frameState.commandList;
  165. const commandEnd = commandList.length;
  166. for (i = commandStart; i < commandEnd; ++i) {
  167. const command = commandList[i];
  168. if (
  169. command.primitiveType !== PrimitiveType.POINTS ||
  170. command.pass === Pass.TRANSLUCENT
  171. ) {
  172. continue;
  173. }
  174. let derivedCommand;
  175. let originalShaderProgram;
  176. let derivedCommandObject = command.derivedCommands.pointCloudProcessor;
  177. if (defined(derivedCommandObject)) {
  178. derivedCommand = derivedCommandObject.command;
  179. originalShaderProgram = derivedCommandObject.originalShaderProgram;
  180. }
  181. if (
  182. !defined(derivedCommand) ||
  183. command.dirty ||
  184. originalShaderProgram !== command.shaderProgram ||
  185. derivedCommand.framebuffer !== this.framebuffer
  186. ) {
  187. // Prevent crash when tiles out-of-view come in-view during context size change or
  188. // when the underlying shader changes while EDL is disabled
  189. derivedCommand = DrawCommand.shallowClone(command, derivedCommand);
  190. derivedCommand.framebuffer = this.framebuffer;
  191. derivedCommand.shaderProgram = getECShaderProgram(
  192. frameState.context,
  193. command.shaderProgram
  194. );
  195. derivedCommand.castShadows = false;
  196. derivedCommand.receiveShadows = false;
  197. if (!defined(derivedCommandObject)) {
  198. derivedCommandObject = {
  199. command: derivedCommand,
  200. originalShaderProgram: command.shaderProgram,
  201. };
  202. command.derivedCommands.pointCloudProcessor = derivedCommandObject;
  203. }
  204. derivedCommandObject.originalShaderProgram = command.shaderProgram;
  205. }
  206. commandList[i] = derivedCommand;
  207. }
  208. const clearCommand = this._clearCommand;
  209. const blendCommand = this._drawCommand;
  210. blendCommand.boundingVolume = boundingVolume;
  211. // Blend EDL into the main FBO
  212. commandList.push(blendCommand);
  213. commandList.push(clearCommand);
  214. };
  215. /**
  216. * Returns true if this object was destroyed; otherwise, false.
  217. * <br /><br />
  218. * If this object was destroyed, it should not be used; calling any function other than
  219. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  220. *
  221. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  222. *
  223. * @see PointCloudEyeDomeLighting#destroy
  224. */
  225. PointCloudEyeDomeLighting.prototype.isDestroyed = function () {
  226. return false;
  227. };
  228. /**
  229. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  230. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  231. * <br /><br />
  232. * Once an object is destroyed, it should not be used; calling any function other than
  233. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  234. * assign the return value (<code>undefined</code>) to the object as done in the example.
  235. *
  236. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  237. *
  238. * @example
  239. * processor = processor && processor.destroy();
  240. *
  241. * @see PointCloudEyeDomeLighting#isDestroyed
  242. */
  243. PointCloudEyeDomeLighting.prototype.destroy = function () {
  244. destroyFramebuffer(this);
  245. return destroyObject(this);
  246. };
  247. export default PointCloudEyeDomeLighting;