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
import BoundingRectangle from "../Core/BoundingRectangle.js";
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian4 from "../Core/Cartesian4.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import CesiumMath from "../Core/Math.js";
import Matrix4 from "../Core/Matrix4.js";
import Transforms from "../Core/Transforms.js";
import AdditiveBlend from "../Shaders/PostProcessStages/AdditiveBlend.js";
import BrightPass from "../Shaders/PostProcessStages/BrightPass.js";
import GaussianBlur1D from "../Shaders/PostProcessStages/GaussianBlur1D.js";
import PassThrough from "../Shaders/PostProcessStages/PassThrough.js";
import PostProcessStage from "./PostProcessStage.js";
import PostProcessStageComposite from "./PostProcessStageComposite.js";
import PostProcessStageSampleMode from "./PostProcessStageSampleMode.js";
import PostProcessStageTextureCache from "./PostProcessStageTextureCache.js";
import SceneFramebuffer from "./SceneFramebuffer.js";
 
function SunPostProcess() {
  this._sceneFramebuffer = new SceneFramebuffer();
 
  var scale = 0.125;
  var stages = new Array(6);
 
  stages[0] = new PostProcessStage({
    fragmentShader: PassThrough,
    textureScale: scale,
    forcePowerOfTwo: true,
    sampleMode: PostProcessStageSampleMode.LINEAR,
  });
 
  var brightPass = (stages[1] = new PostProcessStage({
    fragmentShader: BrightPass,
    uniforms: {
      avgLuminance: 0.5, // A guess at the average luminance across the entire scene
      threshold: 0.25,
      offset: 0.1,
    },
    textureScale: scale,
    forcePowerOfTwo: true,
  }));
 
  var that = this;
  this._delta = 1.0;
  this._sigma = 2.0;
  this._blurStep = new Cartesian2();
 
  stages[2] = new PostProcessStage({
    fragmentShader: GaussianBlur1D,
    uniforms: {
      step: function () {
        that._blurStep.x = that._blurStep.y =
          1.0 / brightPass.outputTexture.width;
        return that._blurStep;
      },
      delta: function () {
        return that._delta;
      },
      sigma: function () {
        return that._sigma;
      },
      direction: 0.0,
    },
    textureScale: scale,
    forcePowerOfTwo: true,
  });
 
  stages[3] = new PostProcessStage({
    fragmentShader: GaussianBlur1D,
    uniforms: {
      step: function () {
        that._blurStep.x = that._blurStep.y =
          1.0 / brightPass.outputTexture.width;
        return that._blurStep;
      },
      delta: function () {
        return that._delta;
      },
      sigma: function () {
        return that._sigma;
      },
      direction: 1.0,
    },
    textureScale: scale,
    forcePowerOfTwo: true,
  });
 
  stages[4] = new PostProcessStage({
    fragmentShader: PassThrough,
    sampleMode: PostProcessStageSampleMode.LINEAR,
  });
 
  this._uCenter = new Cartesian2();
  this._uRadius = undefined;
 
  stages[5] = new PostProcessStage({
    fragmentShader: AdditiveBlend,
    uniforms: {
      center: function () {
        return that._uCenter;
      },
      radius: function () {
        return that._uRadius;
      },
      colorTexture2: function () {
        return that._sceneFramebuffer.getFramebuffer().getColorTexture(0);
      },
    },
  });
 
  this._stages = new PostProcessStageComposite({
    stages: stages,
  });
 
  var textureCache = new PostProcessStageTextureCache(this);
  var length = stages.length;
  for (var i = 0; i < length; ++i) {
    stages[i]._textureCache = textureCache;
  }
 
  this._textureCache = textureCache;
  this.length = stages.length;
}
 
SunPostProcess.prototype.get = function (index) {
  return this._stages.get(index);
};
 
SunPostProcess.prototype.getStageByName = function (name) {
  var length = this._stages.length;
  for (var i = 0; i < length; ++i) {
    var stage = this._stages.get(i);
    if (stage.name === name) {
      return stage;
    }
  }
  return undefined;
};
 
var sunPositionECScratch = new Cartesian4();
var sunPositionWCScratch = new Cartesian2();
var sizeScratch = new Cartesian2();
var postProcessMatrix4Scratch = new Matrix4();
 
function updateSunPosition(postProcess, context, viewport) {
  var us = context.uniformState;
  var sunPosition = us.sunPositionWC;
  var viewMatrix = us.view;
  var viewProjectionMatrix = us.viewProjection;
  var projectionMatrix = us.projection;
 
  // create up sampled render state
  var viewportTransformation = Matrix4.computeViewportTransformation(
    viewport,
    0.0,
    1.0,
    postProcessMatrix4Scratch
  );
  var sunPositionEC = Matrix4.multiplyByPoint(
    viewMatrix,
    sunPosition,
    sunPositionECScratch
  );
  var sunPositionWC = Transforms.pointToGLWindowCoordinates(
    viewProjectionMatrix,
    viewportTransformation,
    sunPosition,
    sunPositionWCScratch
  );
 
  sunPositionEC.x += CesiumMath.SOLAR_RADIUS;
  var limbWC = Transforms.pointToGLWindowCoordinates(
    projectionMatrix,
    viewportTransformation,
    sunPositionEC,
    sunPositionEC
  );
  var sunSize =
    Cartesian2.magnitude(Cartesian2.subtract(limbWC, sunPositionWC, limbWC)) *
    30.0 *
    2.0;
 
  var size = sizeScratch;
  size.x = sunSize;
  size.y = sunSize;
 
  postProcess._uCenter = Cartesian2.clone(sunPositionWC, postProcess._uCenter);
  postProcess._uRadius = Math.max(size.x, size.y) * 0.15;
 
  var width = context.drawingBufferWidth;
  var height = context.drawingBufferHeight;
 
  var stages = postProcess._stages;
  var firstStage = stages.get(0);
 
  var downSampleWidth = firstStage.outputTexture.width;
  var downSampleHeight = firstStage.outputTexture.height;
 
  var downSampleViewport = new BoundingRectangle();
  downSampleViewport.width = downSampleWidth;
  downSampleViewport.height = downSampleHeight;
 
  // create down sampled render state
  viewportTransformation = Matrix4.computeViewportTransformation(
    downSampleViewport,
    0.0,
    1.0,
    postProcessMatrix4Scratch
  );
  sunPositionWC = Transforms.pointToGLWindowCoordinates(
    viewProjectionMatrix,
    viewportTransformation,
    sunPosition,
    sunPositionWCScratch
  );
 
  size.x *= downSampleWidth / width;
  size.y *= downSampleHeight / height;
 
  var scissorRectangle = firstStage.scissorRectangle;
  scissorRectangle.x = Math.max(sunPositionWC.x - size.x * 0.5, 0.0);
  scissorRectangle.y = Math.max(sunPositionWC.y - size.y * 0.5, 0.0);
  scissorRectangle.width = Math.min(size.x, width);
  scissorRectangle.height = Math.min(size.y, height);
 
  for (var i = 1; i < 4; ++i) {
    BoundingRectangle.clone(scissorRectangle, stages.get(i).scissorRectangle);
  }
}
 
SunPostProcess.prototype.clear = function (context, passState, clearColor) {
  this._sceneFramebuffer.clear(context, passState, clearColor);
  this._textureCache.clear(context);
};
 
SunPostProcess.prototype.update = function (passState) {
  var context = passState.context;
  var viewport = passState.viewport;
 
  var sceneFramebuffer = this._sceneFramebuffer;
  sceneFramebuffer.update(context, viewport);
  var framebuffer = sceneFramebuffer.getFramebuffer();
 
  this._textureCache.update(context);
  this._stages.update(context, false);
 
  updateSunPosition(this, context, viewport);
 
  return framebuffer;
};
 
SunPostProcess.prototype.execute = function (context) {
  var colorTexture = this._sceneFramebuffer.getFramebuffer().getColorTexture(0);
  var stages = this._stages;
  var length = stages.length;
  stages.get(0).execute(context, colorTexture);
  for (var i = 1; i < length; ++i) {
    stages.get(i).execute(context, stages.get(i - 1).outputTexture);
  }
};
 
SunPostProcess.prototype.copy = function (context, framebuffer) {
  if (!defined(this._copyColorCommand)) {
    var that = this;
    this._copyColorCommand = context.createViewportQuadCommand(PassThrough, {
      uniformMap: {
        colorTexture: function () {
          return that._stages.get(that._stages.length - 1).outputTexture;
        },
      },
      owner: this,
    });
  }
 
  this._copyColorCommand.framebuffer = framebuffer;
  this._copyColorCommand.execute(context);
};
 
SunPostProcess.prototype.isDestroyed = function () {
  return false;
};
 
SunPostProcess.prototype.destroy = function () {
  this._textureCache.destroy();
  this._stages.destroy();
  return destroyObject(this);
};
export default SunPostProcess;