ConditionsExpression.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import clone from "../Core/clone.js";
  2. import defined from "../Core/defined.js";
  3. import Expression from "./Expression.js";
  4. /**
  5. * An expression for a style applied to a {@link Cesium3DTileset}.
  6. * <p>
  7. * Evaluates a conditions expression defined using the
  8. * {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Styling|3D Tiles Styling language}.
  9. * </p>
  10. * <p>
  11. * Implements the {@link StyleExpression} interface.
  12. * </p>
  13. *
  14. * @alias ConditionsExpression
  15. * @constructor
  16. *
  17. * @param {Object} [conditionsExpression] The conditions expression defined using the 3D Tiles Styling language.
  18. * @param {Object} [defines] Defines in the style.
  19. *
  20. * @example
  21. * const expression = new Cesium.ConditionsExpression({
  22. * conditions : [
  23. * ['${Area} > 10, 'color("#FF0000")'],
  24. * ['${id} !== "1"', 'color("#00FF00")'],
  25. * ['true', 'color("#FFFFFF")']
  26. * ]
  27. * });
  28. * expression.evaluateColor(feature, result); // returns a Cesium.Color object
  29. */
  30. function ConditionsExpression(conditionsExpression, defines) {
  31. this._conditionsExpression = clone(conditionsExpression, true);
  32. this._conditions = conditionsExpression.conditions;
  33. this._runtimeConditions = undefined;
  34. setRuntime(this, defines);
  35. }
  36. Object.defineProperties(ConditionsExpression.prototype, {
  37. /**
  38. * Gets the conditions expression defined in the 3D Tiles Styling language.
  39. *
  40. * @memberof ConditionsExpression.prototype
  41. *
  42. * @type {Object}
  43. * @readonly
  44. *
  45. * @default undefined
  46. */
  47. conditionsExpression: {
  48. get: function () {
  49. return this._conditionsExpression;
  50. },
  51. },
  52. });
  53. function Statement(condition, expression) {
  54. this.condition = condition;
  55. this.expression = expression;
  56. }
  57. function setRuntime(expression, defines) {
  58. const runtimeConditions = [];
  59. const conditions = expression._conditions;
  60. if (!defined(conditions)) {
  61. return;
  62. }
  63. const length = conditions.length;
  64. for (let i = 0; i < length; ++i) {
  65. const statement = conditions[i];
  66. const cond = String(statement[0]);
  67. const condExpression = String(statement[1]);
  68. runtimeConditions.push(
  69. new Statement(
  70. new Expression(cond, defines),
  71. new Expression(condExpression, defines)
  72. )
  73. );
  74. }
  75. expression._runtimeConditions = runtimeConditions;
  76. }
  77. /**
  78. * Evaluates the result of an expression, optionally using the provided feature's properties. If the result of
  79. * the expression in the
  80. * {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Styling|3D Tiles Styling language}
  81. * is of type <code>Boolean</code>, <code>Number</code>, or <code>String</code>, the corresponding JavaScript
  82. * primitive type will be returned. If the result is a <code>RegExp</code>, a Javascript <code>RegExp</code>
  83. * object will be returned. If the result is a <code>Cartesian2</code>, <code>Cartesian3</code>, or <code>Cartesian4</code>,
  84. * a {@link Cartesian2}, {@link Cartesian3}, or {@link Cartesian4} object will be returned. If the <code>result</code> argument is
  85. * a {@link Color}, the {@link Cartesian4} value is converted to a {@link Color} and then returned.
  86. *
  87. * @param {Cesium3DTileFeature} feature The feature whose properties may be used as variables in the expression.
  88. * @param {Object} [result] The object onto which to store the result.
  89. * @returns {Boolean|Number|String|RegExp|Cartesian2|Cartesian3|Cartesian4|Color} The result of evaluating the expression.
  90. */
  91. ConditionsExpression.prototype.evaluate = function (feature, result) {
  92. const conditions = this._runtimeConditions;
  93. if (!defined(conditions)) {
  94. return undefined;
  95. }
  96. const length = conditions.length;
  97. for (let i = 0; i < length; ++i) {
  98. const statement = conditions[i];
  99. if (statement.condition.evaluate(feature)) {
  100. return statement.expression.evaluate(feature, result);
  101. }
  102. }
  103. };
  104. /**
  105. * Evaluates the result of a Color expression, using the values defined by a feature.
  106. * <p>
  107. * This is equivalent to {@link ConditionsExpression#evaluate} but always returns a {@link Color} object.
  108. * </p>
  109. * @param {Cesium3DTileFeature} feature The feature whose properties may be used as variables in the expression.
  110. * @param {Color} [result] The object in which to store the result
  111. * @returns {Color} The modified result parameter or a new Color instance if one was not provided.
  112. */
  113. ConditionsExpression.prototype.evaluateColor = function (feature, result) {
  114. const conditions = this._runtimeConditions;
  115. if (!defined(conditions)) {
  116. return undefined;
  117. }
  118. const length = conditions.length;
  119. for (let i = 0; i < length; ++i) {
  120. const statement = conditions[i];
  121. if (statement.condition.evaluate(feature)) {
  122. return statement.expression.evaluateColor(feature, result);
  123. }
  124. }
  125. };
  126. /**
  127. * Gets the shader function for this expression.
  128. * Returns undefined if the shader function can't be generated from this expression.
  129. *
  130. * @param {String} functionSignature Signature of the generated function.
  131. * @param {Object} variableSubstitutionMap Maps variable names to shader variable names.
  132. * @param {Object} shaderState Stores information about the generated shader function, including whether it is translucent.
  133. * @param {String} returnType The return type of the generated function.
  134. *
  135. * @returns {String} The shader function.
  136. *
  137. * @private
  138. */
  139. ConditionsExpression.prototype.getShaderFunction = function (
  140. functionSignature,
  141. variableSubstitutionMap,
  142. shaderState,
  143. returnType
  144. ) {
  145. const conditions = this._runtimeConditions;
  146. if (!defined(conditions) || conditions.length === 0) {
  147. return undefined;
  148. }
  149. let shaderFunction = "";
  150. const length = conditions.length;
  151. for (let i = 0; i < length; ++i) {
  152. const statement = conditions[i];
  153. const condition = statement.condition.getShaderExpression(
  154. variableSubstitutionMap,
  155. shaderState
  156. );
  157. const expression = statement.expression.getShaderExpression(
  158. variableSubstitutionMap,
  159. shaderState
  160. );
  161. // Build the if/else chain from the list of conditions
  162. shaderFunction +=
  163. ` ${i === 0 ? "if" : "else if"} (${condition})\n` +
  164. ` {\n` +
  165. ` return ${expression};\n` +
  166. ` }\n`;
  167. }
  168. shaderFunction =
  169. `${returnType} ${functionSignature}\n` +
  170. `{\n${shaderFunction} return ${returnType}(1.0);\n` + // Return a default value if no conditions are met
  171. `}\n`;
  172. return shaderFunction;
  173. };
  174. /**
  175. * Gets the variables used by the expression.
  176. *
  177. * @returns {String[]} The variables used by the expression.
  178. *
  179. * @private
  180. */
  181. ConditionsExpression.prototype.getVariables = function () {
  182. let variables = [];
  183. const conditions = this._runtimeConditions;
  184. if (!defined(conditions) || conditions.length === 0) {
  185. return variables;
  186. }
  187. const length = conditions.length;
  188. for (let i = 0; i < length; ++i) {
  189. const statement = conditions[i];
  190. variables.push.apply(variables, statement.condition.getVariables());
  191. variables.push.apply(variables, statement.expression.getVariables());
  192. }
  193. // Remove duplicates
  194. variables = variables.filter(function (variable, index, variables) {
  195. return variables.indexOf(variable) === index;
  196. });
  197. return variables;
  198. };
  199. export default ConditionsExpression;