15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import Cartesian2 from "../Core/Cartesian2.js";
import Color from "../Core/Color.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import PixelFormat from "../Core/PixelFormat.js";
import PrimitiveType from "../Core/PrimitiveType.js";
import ClearCommand from "../Renderer/ClearCommand.js";
import DrawCommand from "../Renderer/DrawCommand.js";
import Framebuffer from "../Renderer/Framebuffer.js";
import Pass from "../Renderer/Pass.js";
import PixelDatatype from "../Renderer/PixelDatatype.js";
import RenderState from "../Renderer/RenderState.js";
import Sampler from "../Renderer/Sampler.js";
import ShaderSource from "../Renderer/ShaderSource.js";
import Texture from "../Renderer/Texture.js";
import BlendingState from "../Scene/BlendingState.js";
import StencilConstants from "../Scene/StencilConstants.js";
import PointCloudEyeDomeLightingShader from "../Shaders/PostProcessStages/PointCloudEyeDomeLighting.js";
 
/**
 * Eye dome lighting. Does not support points with per-point translucency, but does allow translucent styling against the globe.
 * Requires support for EXT_frag_depth and WEBGL_draw_buffers extensions in WebGL 1.0.
 *
 * @private
 */
function PointCloudEyeDomeLighting() {
  this._framebuffer = undefined;
  this._colorGBuffer = undefined; // color gbuffer
  this._depthGBuffer = undefined; // depth gbuffer
  this._depthTexture = undefined; // needed to write depth so camera based on depth works
  this._drawCommand = undefined;
  this._clearCommand = undefined;
 
  this._strength = 1.0;
  this._radius = 1.0;
}
 
function destroyFramebuffer(processor) {
  var framebuffer = processor._framebuffer;
  if (!defined(framebuffer)) {
    return;
  }
 
  processor._colorGBuffer.destroy();
  processor._depthGBuffer.destroy();
  processor._depthTexture.destroy();
  framebuffer.destroy();
 
  processor._framebuffer = undefined;
  processor._colorGBuffer = undefined;
  processor._depthGBuffer = undefined;
  processor._depthTexture = undefined;
  processor._drawCommand = undefined;
  processor._clearCommand = undefined;
}
 
function createFramebuffer(processor, context) {
  var screenWidth = context.drawingBufferWidth;
  var screenHeight = context.drawingBufferHeight;
 
  var colorGBuffer = new Texture({
    context: context,
    width: screenWidth,
    height: screenHeight,
    pixelFormat: PixelFormat.RGBA,
    pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
    sampler: Sampler.NEAREST,
  });
 
  var depthGBuffer = new Texture({
    context: context,
    width: screenWidth,
    height: screenHeight,
    pixelFormat: PixelFormat.RGBA,
    pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
    sampler: Sampler.NEAREST,
  });
 
  var depthTexture = new Texture({
    context: context,
    width: screenWidth,
    height: screenHeight,
    pixelFormat: PixelFormat.DEPTH_COMPONENT,
    pixelDatatype: PixelDatatype.UNSIGNED_INT,
    sampler: Sampler.NEAREST,
  });
 
  processor._framebuffer = new Framebuffer({
    context: context,
    colorTextures: [colorGBuffer, depthGBuffer],
    depthTexture: depthTexture,
    destroyAttachments: false,
  });
  processor._colorGBuffer = colorGBuffer;
  processor._depthGBuffer = depthGBuffer;
  processor._depthTexture = depthTexture;
}
 
var distanceAndEdlStrengthScratch = new Cartesian2();
 
function createCommands(processor, context) {
  var blendFS = new ShaderSource({
    defines: ["LOG_DEPTH_WRITE"],
    sources: [PointCloudEyeDomeLightingShader],
  });
 
  var blendUniformMap = {
    u_pointCloud_colorGBuffer: function () {
      return processor._colorGBuffer;
    },
    u_pointCloud_depthGBuffer: function () {
      return processor._depthGBuffer;
    },
    u_distanceAndEdlStrength: function () {
      distanceAndEdlStrengthScratch.x = processor._radius;
      distanceAndEdlStrengthScratch.y = processor._strength;
      return distanceAndEdlStrengthScratch;
    },
  };
 
  var blendRenderState = RenderState.fromCache({
    blending: BlendingState.ALPHA_BLEND,
    depthMask: true,
    depthTest: {
      enabled: true,
    },
    stencilTest: StencilConstants.setCesium3DTileBit(),
    stencilMask: StencilConstants.CESIUM_3D_TILE_MASK,
  });
 
  processor._drawCommand = context.createViewportQuadCommand(blendFS, {
    uniformMap: blendUniformMap,
    renderState: blendRenderState,
    pass: Pass.CESIUM_3D_TILE,
    owner: processor,
  });
 
  processor._clearCommand = new ClearCommand({
    framebuffer: processor._framebuffer,
    color: new Color(0.0, 0.0, 0.0, 0.0),
    depth: 1.0,
    renderState: RenderState.fromCache(),
    pass: Pass.CESIUM_3D_TILE,
    owner: processor,
  });
}
 
function createResources(processor, context) {
  var screenWidth = context.drawingBufferWidth;
  var screenHeight = context.drawingBufferHeight;
  var colorGBuffer = processor._colorGBuffer;
  var nowDirty = false;
  var resized =
    defined(colorGBuffer) &&
    (colorGBuffer.width !== screenWidth ||
      colorGBuffer.height !== screenHeight);
 
  if (!defined(colorGBuffer) || resized) {
    destroyFramebuffer(processor);
    createFramebuffer(processor, context);
    createCommands(processor, context);
    nowDirty = true;
  }
  return nowDirty;
}
 
function isSupported(context) {
  return context.drawBuffers && context.fragmentDepth;
}
 
PointCloudEyeDomeLighting.isSupported = isSupported;
 
function getECShaderProgram(context, shaderProgram) {
  var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, "EC");
  if (!defined(shader)) {
    var attributeLocations = shaderProgram._attributeLocations;
 
    var fs = shaderProgram.fragmentShaderSource.clone();
 
    fs.sources = fs.sources.map(function (source) {
      source = ShaderSource.replaceMain(
        source,
        "czm_point_cloud_post_process_main"
      );
      source = source.replace(/gl_FragColor/g, "gl_FragData[0]");
      return source;
    });
 
    fs.sources.unshift("#extension GL_EXT_draw_buffers : enable \n");
    fs.sources.push(
      "void main() \n" +
        "{ \n" +
        "    czm_point_cloud_post_process_main(); \n" +
        "#ifdef LOG_DEPTH\n" +
        "    czm_writeLogDepth();\n" +
        "    gl_FragData[1] = czm_packDepth(gl_FragDepthEXT); \n" +
        "#else\n" +
        "    gl_FragData[1] = czm_packDepth(gl_FragCoord.z);\n" +
        "#endif\n" +
        "}"
    );
 
    shader = context.shaderCache.createDerivedShaderProgram(
      shaderProgram,
      "EC",
      {
        vertexShaderSource: shaderProgram.vertexShaderSource,
        fragmentShaderSource: fs,
        attributeLocations: attributeLocations,
      }
    );
  }
 
  return shader;
}
 
PointCloudEyeDomeLighting.prototype.update = function (
  frameState,
  commandStart,
  pointCloudShading,
  boundingVolume
) {
  if (!isSupported(frameState.context)) {
    return;
  }
 
  this._strength = pointCloudShading.eyeDomeLightingStrength;
  this._radius =
    pointCloudShading.eyeDomeLightingRadius * frameState.pixelRatio;
 
  var dirty = createResources(this, frameState.context);
 
  // Hijack existing point commands to render into an offscreen FBO.
  var i;
  var commandList = frameState.commandList;
  var commandEnd = commandList.length;
 
  var derivedCommand;
  var originalShaderProgram;
 
  for (i = commandStart; i < commandEnd; ++i) {
    var command = commandList[i];
    if (
      command.primitiveType !== PrimitiveType.POINTS ||
      command.pass === Pass.TRANSLUCENT
    ) {
      continue;
    }
 
    // These variables need to get reset for each iteration. It has to be
    // done manually since var is function scope not block scope.
    derivedCommand = undefined;
    originalShaderProgram = undefined;
 
    var derivedCommandObject = command.derivedCommands.pointCloudProcessor;
    if (defined(derivedCommandObject)) {
      derivedCommand = derivedCommandObject.command;
      originalShaderProgram = derivedCommandObject.originalShaderProgram;
    }
 
    if (
      !defined(derivedCommand) ||
      command.dirty ||
      dirty ||
      originalShaderProgram !== command.shaderProgram ||
      derivedCommand.framebuffer !== this._framebuffer
    ) {
      // Prevent crash when tiles out-of-view come in-view during context size change or
      // when the underlying shader changes while EDL is disabled
      derivedCommand = DrawCommand.shallowClone(command, derivedCommand);
      derivedCommand.framebuffer = this._framebuffer;
      derivedCommand.shaderProgram = getECShaderProgram(
        frameState.context,
        command.shaderProgram
      );
      derivedCommand.castShadows = false;
      derivedCommand.receiveShadows = false;
 
      if (!defined(derivedCommandObject)) {
        derivedCommandObject = {
          command: derivedCommand,
          originalShaderProgram: command.shaderProgram,
        };
        command.derivedCommands.pointCloudProcessor = derivedCommandObject;
      }
 
      derivedCommandObject.originalShaderProgram = command.shaderProgram;
    }
 
    commandList[i] = derivedCommand;
  }
 
  var clearCommand = this._clearCommand;
  var blendCommand = this._drawCommand;
 
  blendCommand.boundingVolume = boundingVolume;
 
  // Blend EDL into the main FBO
  commandList.push(blendCommand);
  commandList.push(clearCommand);
};
 
/**
 * Returns true if this object was destroyed; otherwise, false.
 * <br /><br />
 * If this object was destroyed, it should not be used; calling any function other than
 * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
 *
 * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
 *
 * @see PointCloudEyeDomeLighting#destroy
 */
PointCloudEyeDomeLighting.prototype.isDestroyed = function () {
  return false;
};
 
/**
 * Destroys the WebGL resources held by this object.  Destroying an object allows for deterministic
 * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
 * <br /><br />
 * Once an object is destroyed, it should not be used; calling any function other than
 * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.  Therefore,
 * assign the return value (<code>undefined</code>) to the object as done in the example.
 *
 * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
 *
 * @example
 * processor = processor && processor.destroy();
 *
 * @see PointCloudEyeDomeLighting#isDestroyed
 */
PointCloudEyeDomeLighting.prototype.destroy = function () {
  destroyFramebuffer(this);
  return destroyObject(this);
};
export default PointCloudEyeDomeLighting;