PointCloudEyeDomeLighting.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 = fs.sources.map(function (source) {
  113. source = ShaderSource.replaceMain(
  114. source,
  115. "czm_point_cloud_post_process_main"
  116. );
  117. source = source.replace(/gl_FragColor/g, "gl_FragData[0]");
  118. return source;
  119. });
  120. fs.sources.unshift("#extension GL_EXT_draw_buffers : enable \n");
  121. fs.sources.push(
  122. "void main() \n" +
  123. "{ \n" +
  124. " czm_point_cloud_post_process_main(); \n" +
  125. "#ifdef LOG_DEPTH\n" +
  126. " czm_writeLogDepth();\n" +
  127. " gl_FragData[1] = czm_packDepth(gl_FragDepthEXT); \n" +
  128. "#else\n" +
  129. " gl_FragData[1] = czm_packDepth(gl_FragCoord.z);\n" +
  130. "#endif\n" +
  131. "}"
  132. );
  133. shader = context.shaderCache.createDerivedShaderProgram(
  134. shaderProgram,
  135. "EC",
  136. {
  137. vertexShaderSource: shaderProgram.vertexShaderSource,
  138. fragmentShaderSource: fs,
  139. attributeLocations: attributeLocations,
  140. }
  141. );
  142. }
  143. return shader;
  144. }
  145. PointCloudEyeDomeLighting.prototype.update = function (
  146. frameState,
  147. commandStart,
  148. pointCloudShading,
  149. boundingVolume
  150. ) {
  151. if (!isSupported(frameState.context)) {
  152. return;
  153. }
  154. this._strength = pointCloudShading.eyeDomeLightingStrength;
  155. this._radius =
  156. pointCloudShading.eyeDomeLightingRadius * frameState.pixelRatio;
  157. createResources(this, frameState.context);
  158. // Hijack existing point commands to render into an offscreen FBO.
  159. let i;
  160. const commandList = frameState.commandList;
  161. const commandEnd = commandList.length;
  162. for (i = commandStart; i < commandEnd; ++i) {
  163. const command = commandList[i];
  164. if (
  165. command.primitiveType !== PrimitiveType.POINTS ||
  166. command.pass === Pass.TRANSLUCENT
  167. ) {
  168. continue;
  169. }
  170. let derivedCommand;
  171. let originalShaderProgram;
  172. let derivedCommandObject = command.derivedCommands.pointCloudProcessor;
  173. if (defined(derivedCommandObject)) {
  174. derivedCommand = derivedCommandObject.command;
  175. originalShaderProgram = derivedCommandObject.originalShaderProgram;
  176. }
  177. if (
  178. !defined(derivedCommand) ||
  179. command.dirty ||
  180. originalShaderProgram !== command.shaderProgram ||
  181. derivedCommand.framebuffer !== this.framebuffer
  182. ) {
  183. // Prevent crash when tiles out-of-view come in-view during context size change or
  184. // when the underlying shader changes while EDL is disabled
  185. derivedCommand = DrawCommand.shallowClone(command, derivedCommand);
  186. derivedCommand.framebuffer = this.framebuffer;
  187. derivedCommand.shaderProgram = getECShaderProgram(
  188. frameState.context,
  189. command.shaderProgram
  190. );
  191. derivedCommand.castShadows = false;
  192. derivedCommand.receiveShadows = false;
  193. if (!defined(derivedCommandObject)) {
  194. derivedCommandObject = {
  195. command: derivedCommand,
  196. originalShaderProgram: command.shaderProgram,
  197. };
  198. command.derivedCommands.pointCloudProcessor = derivedCommandObject;
  199. }
  200. derivedCommandObject.originalShaderProgram = command.shaderProgram;
  201. }
  202. commandList[i] = derivedCommand;
  203. }
  204. const clearCommand = this._clearCommand;
  205. const blendCommand = this._drawCommand;
  206. blendCommand.boundingVolume = boundingVolume;
  207. // Blend EDL into the main FBO
  208. commandList.push(blendCommand);
  209. commandList.push(clearCommand);
  210. };
  211. /**
  212. * Returns true if this object was destroyed; otherwise, false.
  213. * <br /><br />
  214. * If this object was destroyed, it should not be used; calling any function other than
  215. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  216. *
  217. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  218. *
  219. * @see PointCloudEyeDomeLighting#destroy
  220. */
  221. PointCloudEyeDomeLighting.prototype.isDestroyed = function () {
  222. return false;
  223. };
  224. /**
  225. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  226. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  227. * <br /><br />
  228. * Once an object is destroyed, it should not be used; calling any function other than
  229. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  230. * assign the return value (<code>undefined</code>) to the object as done in the example.
  231. *
  232. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  233. *
  234. * @example
  235. * processor = processor && processor.destroy();
  236. *
  237. * @see PointCloudEyeDomeLighting#isDestroyed
  238. */
  239. PointCloudEyeDomeLighting.prototype.destroy = function () {
  240. destroyFramebuffer(this);
  241. return destroyObject(this);
  242. };
  243. export default PointCloudEyeDomeLighting;