PolylineColorAppearance.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import defaultValue from '../Core/defaultValue.js';
  2. import defineProperties from '../Core/defineProperties.js';
  3. import FeatureDetection from '../Core/FeatureDetection.js';
  4. import VertexFormat from '../Core/VertexFormat.js';
  5. import PerInstanceFlatColorAppearanceFS from '../Shaders/Appearances/PerInstanceFlatColorAppearanceFS.js';
  6. import PolylineColorAppearanceVS from '../Shaders/Appearances/PolylineColorAppearanceVS.js';
  7. import PolylineCommon from '../Shaders/PolylineCommon.js';
  8. import Appearance from './Appearance.js';
  9. var defaultVertexShaderSource = PolylineCommon + '\n' + PolylineColorAppearanceVS;
  10. var defaultFragmentShaderSource = PerInstanceFlatColorAppearanceFS;
  11. if (!FeatureDetection.isInternetExplorer()) {
  12. defaultVertexShaderSource = '#define CLIP_POLYLINE \n' + defaultVertexShaderSource;
  13. }
  14. /**
  15. * An appearance for {@link GeometryInstance} instances with color attributes and
  16. * {@link PolylineGeometry} or {@link GroundPolylineGeometry}.
  17. * This allows several geometry instances, each with a different color, to
  18. * be drawn with the same {@link Primitive}.
  19. *
  20. * @alias PolylineColorAppearance
  21. * @constructor
  22. *
  23. * @param {Object} [options] Object with the following properties:
  24. * @param {Boolean} [options.translucent=true] When <code>true</code>, the geometry is expected to appear translucent so {@link PolylineColorAppearance#renderState} has alpha blending enabled.
  25. * @param {String} [options.vertexShaderSource] Optional GLSL vertex shader source to override the default vertex shader.
  26. * @param {String} [options.fragmentShaderSource] Optional GLSL fragment shader source to override the default fragment shader.
  27. * @param {RenderState} [options.renderState] Optional render state to override the default render state.
  28. *
  29. * @example
  30. * // A solid white line segment
  31. * var primitive = new Cesium.Primitive({
  32. * geometryInstances : new Cesium.GeometryInstance({
  33. * geometry : new Cesium.PolylineGeometry({
  34. * positions : Cesium.Cartesian3.fromDegreesArray([
  35. * 0.0, 0.0,
  36. * 5.0, 0.0
  37. * ]),
  38. * width : 10.0,
  39. * vertexFormat : Cesium.PolylineColorAppearance.VERTEX_FORMAT
  40. * }),
  41. * attributes : {
  42. * color : Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 1.0, 1.0, 1.0))
  43. * }
  44. * }),
  45. * appearance : new Cesium.PolylineColorAppearance({
  46. * translucent : false
  47. * })
  48. * });
  49. */
  50. function PolylineColorAppearance(options) {
  51. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  52. var translucent = defaultValue(options.translucent, true);
  53. var closed = false;
  54. var vertexFormat = PolylineColorAppearance.VERTEX_FORMAT;
  55. /**
  56. * This property is part of the {@link Appearance} interface, but is not
  57. * used by {@link PolylineColorAppearance} since a fully custom fragment shader is used.
  58. *
  59. * @type Material
  60. *
  61. * @default undefined
  62. */
  63. this.material = undefined;
  64. /**
  65. * When <code>true</code>, the geometry is expected to appear translucent so
  66. * {@link PolylineColorAppearance#renderState} has alpha blending enabled.
  67. *
  68. * @type {Boolean}
  69. *
  70. * @default true
  71. */
  72. this.translucent = translucent;
  73. this._vertexShaderSource = defaultValue(options.vertexShaderSource, defaultVertexShaderSource);
  74. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, defaultFragmentShaderSource);
  75. this._renderState = Appearance.getDefaultRenderState(translucent, closed, options.renderState);
  76. this._closed = closed;
  77. // Non-derived members
  78. this._vertexFormat = vertexFormat;
  79. }
  80. defineProperties(PolylineColorAppearance.prototype, {
  81. /**
  82. * The GLSL source code for the vertex shader.
  83. *
  84. * @memberof PolylineColorAppearance.prototype
  85. *
  86. * @type {String}
  87. * @readonly
  88. */
  89. vertexShaderSource : {
  90. get : function() {
  91. return this._vertexShaderSource;
  92. }
  93. },
  94. /**
  95. * The GLSL source code for the fragment shader.
  96. *
  97. * @memberof PolylineColorAppearance.prototype
  98. *
  99. * @type {String}
  100. * @readonly
  101. */
  102. fragmentShaderSource : {
  103. get : function() {
  104. return this._fragmentShaderSource;
  105. }
  106. },
  107. /**
  108. * The WebGL fixed-function state to use when rendering the geometry.
  109. * <p>
  110. * The render state can be explicitly defined when constructing a {@link PolylineColorAppearance}
  111. * instance, or it is set implicitly via {@link PolylineColorAppearance#translucent}.
  112. * </p>
  113. *
  114. * @memberof PolylineColorAppearance.prototype
  115. *
  116. * @type {Object}
  117. * @readonly
  118. */
  119. renderState : {
  120. get : function() {
  121. return this._renderState;
  122. }
  123. },
  124. /**
  125. * When <code>true</code>, the geometry is expected to be closed so
  126. * {@link PolylineColorAppearance#renderState} has backface culling enabled.
  127. * This is always <code>false</code> for <code>PolylineColorAppearance</code>.
  128. *
  129. * @memberof PolylineColorAppearance.prototype
  130. *
  131. * @type {Boolean}
  132. * @readonly
  133. *
  134. * @default false
  135. */
  136. closed : {
  137. get : function() {
  138. return this._closed;
  139. }
  140. },
  141. /**
  142. * The {@link VertexFormat} that this appearance instance is compatible with.
  143. * A geometry can have more vertex attributes and still be compatible - at a
  144. * potential performance cost - but it can't have less.
  145. *
  146. * @memberof PolylineColorAppearance.prototype
  147. *
  148. * @type VertexFormat
  149. * @readonly
  150. *
  151. * @default {@link PolylineColorAppearance.VERTEX_FORMAT}
  152. */
  153. vertexFormat : {
  154. get : function() {
  155. return this._vertexFormat;
  156. }
  157. }
  158. });
  159. /**
  160. * The {@link VertexFormat} that all {@link PolylineColorAppearance} instances
  161. * are compatible with. This requires only a <code>position</code> attribute.
  162. *
  163. * @type VertexFormat
  164. *
  165. * @constant
  166. */
  167. PolylineColorAppearance.VERTEX_FORMAT = VertexFormat.POSITION_ONLY;
  168. /**
  169. * Procedurally creates the full GLSL fragment shader source.
  170. *
  171. * @function
  172. *
  173. * @returns {String} The full GLSL fragment shader source.
  174. */
  175. PolylineColorAppearance.prototype.getFragmentShaderSource = Appearance.prototype.getFragmentShaderSource;
  176. /**
  177. * Determines if the geometry is translucent based on {@link PolylineColorAppearance#translucent}.
  178. *
  179. * @function
  180. *
  181. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  182. */
  183. PolylineColorAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  184. /**
  185. * Creates a render state. This is not the final render state instance; instead,
  186. * it can contain a subset of render state properties identical to the render state
  187. * created in the context.
  188. *
  189. * @function
  190. *
  191. * @returns {Object} The render state.
  192. */
  193. PolylineColorAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  194. export default PolylineColorAppearance;