<commit_before>import combine from "../Core/combine.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import NearFarScalar from "../Core/NearFarScalar.js";
import Rectangle from "../Core/Rectangle.js";
import DrawCommand from "../Renderer/DrawCommand.js";
import Pass from "../Renderer/Pass.js";
import RenderState from "../Renderer/RenderState.js";
import ShaderSource from "../Renderer/ShaderSource.js";
import BlendingState from "./BlendingState.js";
import CullFace from "./CullFace.js";
import SceneMode from "./SceneMode.js";

var DerivedCommandType = {
  OPAQUE_FRONT_FACE: 0,
  OPAQUE_BACK_FACE: 1,
  DEPTH_ONLY_FRONT_FACE: 2,
  DEPTH_ONLY_BACK_FACE: 3,
  DEPTH_ONLY_FRONT_AND_BACK_FACE: 4,
  TRANSLUCENT_FRONT_FACE: 5,
  TRANSLUCENT_BACK_FACE: 6,
  TRANSLUCENT_FRONT_FACE_MANUAL_DEPTH_TEST: 7,
  TRANSLUCENT_BACK_FACE_MANUAL_DEPTH_TEST: 8,
  PICK_FRONT_FACE: 9,
  PICK_BACK_FACE: 10,
  DERIVED_COMMANDS_MAXIMUM_LENGTH: 11,
};

var derivedCommandsMaximumLength =
  DerivedCommandType.DERIVED_COMMANDS_MAXIMUM_LENGTH;

var DerivedCommandNames = [
  "opaqueFrontFaceCommand",
  "opaqueBackFaceCommand",
  "depthOnlyFrontFaceCommand",
  "depthOnlyBackFaceCommand",
  "depthOnlyFrontAndBackFaceCommand",
  "translucentFrontFaceCommand",
  "translucentBackFaceCommand",
  "translucentFrontFaceManualDepthTestCommand",
  "translucentBackFaceManualDepthTestCommand",
  "pickFrontFaceCommand",
  "pickBackFaceCommand",
];

/**
 * @private
 */
function GlobeTranslucencyState() {
  this._frontFaceAlphaByDistance = new NearFarScalar(0.0, 1.0, 0.0, 1.0);
  this._backFaceAlphaByDistance = new NearFarScalar(0.0, 1.0, 0.0, 1.0);

  this._frontFaceTranslucent = false;
  this._backFaceTranslucent = false;
  this._requiresManualDepthTest = false;
  this._sunVisibleThroughGlobe = false;
  this._environmentVisible = false;
  this._useDepthPlane = false;
  this._numberOfTextureUniforms = 0;
  this._globeTranslucencyFramebuffer = undefined;
  this._rectangle = Rectangle.clone(Rectangle.MAX_VALUE);

  this._derivedCommandKey = 0;
  this._derivedCommandsDirty = false;
  this._derivedCommandPacks = undefined;

  this._derivedCommandTypes = new Array(derivedCommandsMaximumLength);
  this._derivedBlendCommandTypes = new Array(derivedCommandsMaximumLength);
  this._derivedPickCommandTypes = new Array(derivedCommandsMaximumLength);
  this._derivedCommandTypesToUpdate = new Array(derivedCommandsMaximumLength);

  this._derivedCommandsLength = 0;
  this._derivedBlendCommandsLength = 0;
  this._derivedPickCommandsLength = 0;
  this._derivedCommandsToUpdateLength = 0;
}

Object.defineProperties(GlobeTranslucencyState.prototype, {
  frontFaceAlphaByDistance: {
    get: function () {
      return this._frontFaceAlphaByDistance;
    },
  },
  backFaceAlphaByDistance: {
    get: function () {
      return this._backFaceAlphaByDistance;
    },
  },
  translucent: {
    get: function () {
      return this._frontFaceTranslucent;
    },
  },
  sunVisibleThroughGlobe: {
    get: function () {
      return this._sunVisibleThroughGlobe;
    },
  },
  environmentVisible: {
    get: function () {
      return this._environmentVisible;
    },
  },
  useDepthPlane: {
    get: function () {
      return this._useDepthPlane;
    },
  },
  numberOfTextureUniforms: {
    get: function () {
      return this._numberOfTextureUniforms;
    },
  },
  rectangle: {
    get: function () {
      return this._rectangle;
    },
  },
});

GlobeTranslucencyState.prototype.update = function (scene) {
  var globe = scene.globe;
  if (!defined(globe) || !globe.show) {
    this._frontFaceTranslucent = false;
    this._backFaceTranslucent = false;
    this._sunVisibleThroughGlobe = true;
    this._environmentVisible = true;
    this._useDepthPlane = false;
    return;
  }

  this._frontFaceAlphaByDistance = updateAlphaByDistance(
    globe.translucency.enabled,
    globe.translucency.frontFaceAlpha,
    globe.translucency.frontFaceAlphaByDistance,
    this._frontFaceAlphaByDistance
  );
  this._backFaceAlphaByDistance = updateAlphaByDistance(
    globe.translucency.enabled,
    globe.translucency.backFaceAlpha,
    globe.translucency.backFaceAlphaByDistance,
    this._backFaceAlphaByDistance
  );

  this._frontFaceTranslucent = isFaceTranslucent(
    this._frontFaceAlphaByDistance,
    globe
  );
  this._backFaceTranslucent = isFaceTranslucent(
    this._backFaceAlphaByDistance,
    globe
  );

  this._requiresManualDepthTest = requiresManualDepthTest(this, scene, globe);

  this._sunVisibleThroughGlobe = isSunVisibleThroughGlobe(this, scene);
  this._environmentVisible = isEnvironmentVisible(this, scene);
  this._useDepthPlane = useDepthPlane(this, scene);
  this._numberOfTextureUniforms = getNumberOfTextureUniforms(this);

  this._rectangle = Rectangle.clone(
    globe.translucency.rectangle,
    this._rectangle
  );

  gatherDerivedCommandRequirements(this, scene);
};

function updateAlphaByDistance(enabled, alpha, alphaByDistance, result) {
  if (!enabled) {
    result.nearValue = 1.0;
    result.farValue = 1.0;
    return result;
  }

  if (!defined(alphaByDistance)) {
    result.nearValue = alpha;
    result.farValue = alpha;
    return result;
  }

  NearFarScalar.clone(alphaByDistance, result);
  result.nearValue *= alpha;
  result.farValue *= alpha;
  return result;
}

function isFaceTranslucent(alphaByDistance, globe) {
  return (
    globe.baseColor.alpha < 1.0 ||
    alphaByDistance.nearValue < 1.0 ||
    alphaByDistance.farValue < 1.0
  );
}

function isSunVisibleThroughGlobe(state, scene) {
  // The sun is visible through the globe if the front and back faces are translucent when above ground<commit_msg>Fix translucencyEnabled not applying when base color alpha is < 1.0<commit_after>import combine from "../Core/combine.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import NearFarScalar from "../Core/NearFarScalar.js";
import Rectangle from "../Core/Rectangle.js";
import DrawCommand from "../Renderer/DrawCommand.js";
import Pass from "../Renderer/Pass.js";
import RenderState from "../Renderer/RenderState.js";
import ShaderSource from "../Renderer/ShaderSource.js";
import BlendingState from "./BlendingState.js";
import CullFace from "./CullFace.js";
import SceneMode from "./SceneMode.js";

var DerivedCommandType = {
  OPAQUE_FRONT_FACE: 0,
  OPAQUE_BACK_FACE: 1,
  DEPTH_ONLY_FRONT_FACE: 2,
  DEPTH_ONLY_BACK_FACE: 3,
  DEPTH_ONLY_FRONT_AND_BACK_FACE: 4,
  TRANSLUCENT_FRONT_FACE: 5,
  TRANSLUCENT_BACK_FACE: 6,
  TRANSLUCENT_FRONT_FACE_MANUAL_DEPTH_TEST: 7,
  TRANSLUCENT_BACK_FACE_MANUAL_DEPTH_TEST: 8,
  PICK_FRONT_FACE: 9,
  PICK_BACK_FACE: 10,
  DERIVED_COMMANDS_MAXIMUM_LENGTH: 11,
};

var derivedCommandsMaximumLength =
  DerivedCommandType.DERIVED_COMMANDS_MAXIMUM_LENGTH;

var DerivedCommandNames = [
  "opaqueFrontFaceCommand",
  "opaqueBackFaceCommand",
  "depthOnlyFrontFaceCommand",
  "depthOnlyBackFaceCommand",
  "depthOnlyFrontAndBackFaceCommand",
  "translucentFrontFaceCommand",
  "translucentBackFaceCommand",
  "translucentFrontFaceManualDepthTestCommand",
  "translucentBackFaceManualDepthTestCommand",
  "pickFrontFaceCommand",
  "pickBackFaceCommand",
];

/**
 * @private
 */
function GlobeTranslucencyState() {
  this._frontFaceAlphaByDistance = new NearFarScalar(0.0, 1.0, 0.0, 1.0);
  this._backFaceAlphaByDistance = new NearFarScalar(0.0, 1.0, 0.0, 1.0);

  this._frontFaceTranslucent = false;
  this._backFaceTranslucent = false;
  this._requiresManualDepthTest = false;
  this._sunVisibleThroughGlobe = false;
  this._environmentVisible = false;
  this._useDepthPlane = false;
  this._numberOfTextureUniforms = 0;
  this._globeTranslucencyFramebuffer = undefined;
  this._rectangle = Rectangle.clone(Rectangle.MAX_VALUE);

  this._derivedCommandKey = 0;
  this._derivedCommandsDirty = false;
  this._derivedCommandPacks = undefined;

  this._derivedCommandTypes = new Array(derivedCommandsMaximumLength);
  this._derivedBlendCommandTypes = new Array(derivedCommandsMaximumLength);
  this._derivedPickCommandTypes = new Array(derivedCommandsMaximumLength);
  this._derivedCommandTypesToUpdate = new Array(derivedCommandsMaximumLength);

  this._derivedCommandsLength = 0;
  this._derivedBlendCommandsLength = 0;
  this._derivedPickCommandsLength = 0;
  this._derivedCommandsToUpdateLength = 0;
}

Object.defineProperties(GlobeTranslucencyState.prototype, {
  frontFaceAlphaByDistance: {
    get: function () {
      return this._frontFaceAlphaByDistance;
    },
  },
  backFaceAlphaByDistance: {
    get: function () {
      return this._backFaceAlphaByDistance;
    },
  },
  translucent: {
    get: function () {
      return this._frontFaceTranslucent;
    },
  },
  sunVisibleThroughGlobe: {
    get: function () {
      return this._sunVisibleThroughGlobe;
    },
  },
  environmentVisible: {
    get: function () {
      return this._environmentVisible;
    },
  },
  useDepthPlane: {
    get: function () {
      return this._useDepthPlane;
    },
  },
  numberOfTextureUniforms: {
    get: function () {
      return this._numberOfTextureUniforms;
    },
  },
  rectangle: {
    get: function () {
      return this._rectangle;
    },
  },
});

GlobeTranslucencyState.prototype.update = function (scene) {
  var globe = scene.globe;
  if (!defined(globe) || !globe.show) {
    this._frontFaceTranslucent = false;
    this._backFaceTranslucent = false;
    this._sunVisibleThroughGlobe = true;
    this._environmentVisible = true;
    this._useDepthPlane = false;
    return;
  }

  this._frontFaceAlphaByDistance = updateAlphaByDistance(
    globe.translucency.enabled,
    globe.translucency.frontFaceAlpha,
    globe.translucency.frontFaceAlphaByDistance,
    this._frontFaceAlphaByDistance
  );
  this._backFaceAlphaByDistance = updateAlphaByDistance(
    globe.translucency.enabled,
    globe.translucency.backFaceAlpha,
    globe.translucency.backFaceAlphaByDistance,
    this._backFaceAlphaByDistance
  );

  this._frontFaceTranslucent = isFaceTranslucent(
    globe.translucency.enabled,
    this._frontFaceAlphaByDistance,
    globe
  );
  this._backFaceTranslucent = isFaceTranslucent(
    globe.translucency.enabled,
    this._backFaceAlphaByDistance,
    globe
  );

  this._requiresManualDepthTest = requiresManualDepthTest(this, scene, globe);

  this._sunVisibleThroughGlobe = isSunVisibleThroughGlobe(this, scene);
  this._environmentVisible = isEnvironmentVisible(this, scene);
  this._useDepthPlane = useDepthPlane(this, scene);
  this._numberOfTextureUniforms = getNumberOfTextureUniforms(this);

  this._rectangle = Rectangle.clone(
    globe.translucency.rectangle,
    this._rectangle
  );

  gatherDerivedCommandRequirements(this, scene);
};

function updateAlphaByDistance(enabled, alpha, alphaByDistance, result) {
  if (!enabled) {
    result.nearValue = 1.0;
    result.farValue = 1.0;
    return result;
  }

  if (!defined(alphaByDistance)) {
    result.nearValue = alpha;
    result.farValue = alpha;
    return result;
  }

  NearFarScalar.clone(alphaByDistance, result);
  result.nearValue *= alpha;
  result.farValue *= alpha;
  return result;
}

function isFaceTranslucent(translucencyEnabled, alphaByDistance, globe) {
  return (
    translucencyEnabled &&
    (globe.baseColor.alpha < 1.0 ||
      alphaByDistance.nearValue < 1.0 ||
      alphaByDistance.farValue < 1.0)
  );
}

function isSunVisibleThroughGlobe(state, scene) {
  // The sun is visible through the globe if the front and back faces are translucent when above ground
{'fraction_non_alphanumeric': 0.10067520775623269, 'fraction_numerical': 0.008137119113573408, 'mean_word_length': 4.525107604017217, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3937752', 'n_tokens_mistral': 3812, 'n_tokens_neox': 3655, 'n_words': 634}