Skip to content

Commit

Permalink
add ignoreBlackPixels option to czm_applyHSBShift
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrgags committed Jan 18, 2024
1 parent dca2dcf commit 7adf61d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@
*
* @param {vec3} rgb The color in RGB space.
* @param {vec3} hsbShift The amount to shift each component. The xyz components correspond to hue, saturation, and brightness. Shifting the hue by +/- 1.0 corresponds to shifting the hue by a full cycle. Saturation and brightness are clamped between 0 and 1 after the adjustment
* @param {bool} ignoreBlackPixels If true, black pixels will be unchanged. This is necessary in some shaders such as atmosphere-related effects.
*
* @return {vec3} The RGB color after shifting in HSB space and clamping saturation and brightness to a valid range.
*/
vec3 czm_applyHSBShift(vec3 rgb, vec3 hsbShift) {
vec3 czm_applyHSBShift(vec3 rgb, vec3 hsbShift, bool ignoreBlackPixels) {
// Convert rgb color to hsb
vec3 hsb = czm_RGBToHSB(rgb);

// Perform hsb shift
// Hue cycles around so no clamp is needed.
hsb.x += hsbShift.x; // hue
hsb.y = clamp(hsb.y + hsbShift.y, 0.0, 1.0); // saturation
hsb.z = clamp(hsb.z + hsbShift.z, 0.0, 1.0); // brightness

// brightness
//
// Some shaders such as atmosphere-related effects need to leave black
// pixels unchanged
if (ignoreBlackPixels) {
hsb.z = hsb.z > czm_epsilon7 ? hsb.z + hsbShift.z : 0.0;
} else {
hsb.z = hsb.z + hsbShift.z;
}
hsb.z = clamp(hsb.z, 0.0, 1.0);

// Convert shifted hsb back to rgb
return czm_HSBToRGB(hsb);
Expand Down
19 changes: 3 additions & 16 deletions packages/engine/Source/Shaders/GlobeFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,6 @@ vec4 sampleAndBlend(
return vec4(outColor, max(outAlpha, 0.0));
}

vec3 colorCorrect(vec3 rgb) {
#ifdef COLOR_CORRECT
// Convert rgb color to hsb
vec3 hsb = czm_RGBToHSB(rgb);
// Perform hsb shift
hsb.x += u_hsbShift.x; // hue
hsb.y = clamp(hsb.y + u_hsbShift.y, 0.0, 1.0); // saturation
hsb.z = hsb.z > czm_epsilon7 ? hsb.z + u_hsbShift.z : 0.0; // brightness
// Convert shifted hsb back to rgb
rgb = czm_HSBToRGB(hsb);
#endif
return rgb;
}

vec4 computeDayColor(vec4 initialColor, vec3 textureCoordinates, float nightBlend);
vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat3 enuToEye, vec4 imageryColor, float specularMapValue, float fade);

Expand Down Expand Up @@ -473,8 +459,9 @@ void main()
#endif

#ifdef COLOR_CORRECT
rayleighColor = czm_applyHSBShift(rayleighColor, u_hsbShift);
mieColor = czm_applyHSBShift(mieColor, u_hsbShift);
const bool ignoreBlackPixels = true;
rayleighColor = czm_applyHSBShift(rayleighColor, u_hsbShift, ignoreBlackPixels);
mieColor = czm_applyHSBShift(mieColor, u_hsbShift, ignoreBlackPixels);
#endif

vec4 groundAtmosphereColor = computeAtmosphereColor(positionWC, lightDirection, rayleighColor, mieColor, opacity);
Expand Down
5 changes: 3 additions & 2 deletions packages/engine/Source/Shaders/Model/AtmosphereStageFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ void atmosphereStage(inout vec4 color, in ProcessedAttributes attributes) {
}

//color correct rayleigh and mie colors
rayleighColor = czm_applyHSBShift(rayleighColor, czm_atmosphereHsbShift);
mieColor = czm_applyHSBShift(mieColor, czm_atmosphereHsbShift);
const bool ignoreBlackPixels = true;
rayleighColor = czm_applyHSBShift(rayleighColor, czm_atmosphereHsbShift, ignoreBlackPixels);
mieColor = czm_applyHSBShift(mieColor, czm_atmosphereHsbShift, ignoreBlackPixels);

vec4 groundAtmosphereColor = czm_computeAtmosphereColor(positionWC, lightDirection, rayleighColor, mieColor, opacity);

Expand Down

0 comments on commit 7adf61d

Please sign in to comment.