All files / engine/Source/Scene SunPostProcess.js

97.47% Statements 116/119
100% Branches 4/4
90% Functions 18/20
97.39% Lines 112/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                      55x   55x 55x   55x             55x                     55x 55x 55x 55x   55x       4x   4x     4x     4x               55x       4x   4x     4x     4x               55x         55x 55x   55x       4x     4x     4x         55x       55x 55x 55x 330x     55x 55x     1x 44x     1x 44x 44x 144x 144x 44x           1x 1x 1x 1x     7x 7x 7x 7x 7x     7x           7x         7x             7x 7x             7x       7x 7x 7x   7x 7x   7x 7x   7x 7x   7x 7x   7x 7x 7x     7x           7x             7x 7x   7x 7x 7x 7x 7x   7x 21x       1x 7x 7x     1x 7x 7x   7x 7x 7x   7x 7x   7x   7x     1x 7x 7x 7x 7x 7x 35x       1x 7x 4x 4x                   7x 7x     1x       1x 54x 54x 54x      
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();
 
  const scale = 0.125;
  const stages = new Array(6);
 
  stages[0] = new PostProcessStage({
    fragmentShader: PassThrough,
    textureScale: scale,
    forcePowerOfTwo: true,
    sampleMode: PostProcessStageSampleMode.LINEAR,
  });
 
  const 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,
  }));
 
  const 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.framebuffer.getColorTexture(0);
      },
    },
  });
 
  this._stages = new PostProcessStageComposite({
    stages: stages,
  });
 
  const textureCache = new PostProcessStageTextureCache(this);
  const length = stages.length;
  for (let 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) {
  const length = this._stages.length;
  for (let i = 0; i < length; ++i) {
    const stage = this._stages.get(i);
    if (stage.name === name) {
      return stage;
    }
  }
  return undefined;
};
 
const sunPositionECScratch = new Cartesian4();
const sunPositionWCScratch = new Cartesian2();
const sizeScratch = new Cartesian2();
const postProcessMatrix4Scratch = new Matrix4();
 
function updateSunPosition(postProcess, context, viewport) {
  const us = context.uniformState;
  const sunPosition = us.sunPositionWC;
  const viewMatrix = us.view;
  const viewProjectionMatrix = us.viewProjection;
  const projectionMatrix = us.projection;
 
  // create up sampled render state
  let viewportTransformation = Matrix4.computeViewportTransformation(
    viewport,
    0.0,
    1.0,
    postProcessMatrix4Scratch,
  );
  const sunPositionEC = Matrix4.multiplyByPoint(
    viewMatrix,
    sunPosition,
    sunPositionECScratch,
  );
  let sunPositionWC = Transforms.pointToGLWindowCoordinates(
    viewProjectionMatrix,
    viewportTransformation,
    sunPosition,
    sunPositionWCScratch,
  );
 
  sunPositionEC.x += CesiumMath.SOLAR_RADIUS;
  const limbWC = Transforms.pointToGLWindowCoordinates(
    projectionMatrix,
    viewportTransformation,
    sunPositionEC,
    sunPositionEC,
  );
  const sunSize =
    Cartesian2.magnitude(Cartesian2.subtract(limbWC, sunPositionWC, limbWC)) *
    30.0 *
    2.0;
 
  const 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;
 
  const width = context.drawingBufferWidth;
  const height = context.drawingBufferHeight;
 
  const stages = postProcess._stages;
  const firstStage = stages.get(0);
 
  const downSampleWidth = firstStage.outputTexture.width;
  const downSampleHeight = firstStage.outputTexture.height;
 
  const 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;
 
  const 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 (let 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) {
  const context = passState.context;
  const viewport = passState.viewport;
 
  const sceneFramebuffer = this._sceneFramebuffer;
  sceneFramebuffer.update(context, viewport);
  const framebuffer = sceneFramebuffer.framebuffer;
 
  this._textureCache.update(context);
  this._stages.update(context, false);
 
  updateSunPosition(this, context, viewport);
 
  return framebuffer;
};
 
SunPostProcess.prototype.execute = function (context) {
  const colorTexture = this._sceneFramebuffer.framebuffer.getColorTexture(0);
  const stages = this._stages;
  const length = stages.length;
  stages.get(0).execute(context, colorTexture);
  for (let 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)) {
    const 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;