ConditionsExpression.js 7.2 KB

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