PolylineMaterialAppearance.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import defaultValue from '../Core/defaultValue.js';
  2. import defined from '../Core/defined.js';
  3. import defineProperties from '../Core/defineProperties.js';
  4. import FeatureDetection from '../Core/FeatureDetection.js';
  5. import VertexFormat from '../Core/VertexFormat.js';
  6. import PolylineMaterialAppearanceVS from '../Shaders/Appearances/PolylineMaterialAppearanceVS.js';
  7. import PolylineCommon from '../Shaders/PolylineCommon.js';
  8. import PolylineFS from '../Shaders/PolylineFS.js';
  9. import Appearance from './Appearance.js';
  10. import Material from './Material.js';
  11. var defaultVertexShaderSource = PolylineCommon + '\n' + PolylineMaterialAppearanceVS;
  12. var defaultFragmentShaderSource = PolylineFS;
  13. if (!FeatureDetection.isInternetExplorer()) {
  14. defaultVertexShaderSource = '#define CLIP_POLYLINE \n' + defaultVertexShaderSource;
  15. }
  16. /**
  17. * An appearance for {@link PolylineGeometry} that supports shading with materials.
  18. *
  19. * @alias PolylineMaterialAppearance
  20. * @constructor
  21. *
  22. * @param {Object} [options] Object with the following properties:
  23. * @param {Boolean} [options.translucent=true] When <code>true</code>, the geometry is expected to appear translucent so {@link PolylineMaterialAppearance#renderState} has alpha blending enabled.
  24. * @param {Material} [options.material=Material.ColorType] The material used to determine the fragment color.
  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. * @see {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  30. *
  31. * @example
  32. * var primitive = new Cesium.Primitive({
  33. * geometryInstances : new Cesium.GeometryInstance({
  34. * geometry : new Cesium.PolylineGeometry({
  35. * positions : Cesium.Cartesian3.fromDegreesArray([
  36. * 0.0, 0.0,
  37. * 5.0, 0.0
  38. * ]),
  39. * width : 10.0,
  40. * vertexFormat : Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
  41. * })
  42. * }),
  43. * appearance : new Cesium.PolylineMaterialAppearance({
  44. * material : Cesium.Material.fromType('Color')
  45. * })
  46. * });
  47. */
  48. function PolylineMaterialAppearance(options) {
  49. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  50. var translucent = defaultValue(options.translucent, true);
  51. var closed = false;
  52. var vertexFormat = PolylineMaterialAppearance.VERTEX_FORMAT;
  53. /**
  54. * The material used to determine the fragment color. Unlike other {@link PolylineMaterialAppearance}
  55. * properties, this is not read-only, so an appearance's material can change on the fly.
  56. *
  57. * @type Material
  58. *
  59. * @default {@link Material.ColorType}
  60. *
  61. * @see {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  62. */
  63. this.material = defined(options.material) ? options.material : Material.fromType(Material.ColorType);
  64. /**
  65. * When <code>true</code>, the geometry is expected to appear translucent so
  66. * {@link PolylineMaterialAppearance#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(PolylineMaterialAppearance.prototype, {
  81. /**
  82. * The GLSL source code for the vertex shader.
  83. *
  84. * @memberof PolylineMaterialAppearance.prototype
  85. *
  86. * @type {String}
  87. * @readonly
  88. */
  89. vertexShaderSource : {
  90. get : function() {
  91. var vs = this._vertexShaderSource;
  92. if (this.material.shaderSource.search(/varying\s+float\s+v_polylineAngle;/g) !== -1) {
  93. vs = '#define POLYLINE_DASH\n' + vs;
  94. }
  95. return vs;
  96. }
  97. },
  98. /**
  99. * The GLSL source code for the fragment shader.
  100. *
  101. * @memberof PolylineMaterialAppearance.prototype
  102. *
  103. * @type {String}
  104. * @readonly
  105. */
  106. fragmentShaderSource : {
  107. get : function() {
  108. return this._fragmentShaderSource;
  109. }
  110. },
  111. /**
  112. * The WebGL fixed-function state to use when rendering the geometry.
  113. * <p>
  114. * The render state can be explicitly defined when constructing a {@link PolylineMaterialAppearance}
  115. * instance, or it is set implicitly via {@link PolylineMaterialAppearance#translucent}
  116. * and {@link PolylineMaterialAppearance#closed}.
  117. * </p>
  118. *
  119. * @memberof PolylineMaterialAppearance.prototype
  120. *
  121. * @type {Object}
  122. * @readonly
  123. */
  124. renderState : {
  125. get : function() {
  126. return this._renderState;
  127. }
  128. },
  129. /**
  130. * When <code>true</code>, the geometry is expected to be closed so
  131. * {@link PolylineMaterialAppearance#renderState} has backface culling enabled.
  132. * This is always <code>false</code> for <code>PolylineMaterialAppearance</code>.
  133. *
  134. * @memberof PolylineMaterialAppearance.prototype
  135. *
  136. * @type {Boolean}
  137. * @readonly
  138. *
  139. * @default false
  140. */
  141. closed : {
  142. get : function() {
  143. return this._closed;
  144. }
  145. },
  146. /**
  147. * The {@link VertexFormat} that this appearance instance is compatible with.
  148. * A geometry can have more vertex attributes and still be compatible - at a
  149. * potential performance cost - but it can't have less.
  150. *
  151. * @memberof PolylineMaterialAppearance.prototype
  152. *
  153. * @type VertexFormat
  154. * @readonly
  155. *
  156. * @default {@link PolylineMaterialAppearance.VERTEX_FORMAT}
  157. */
  158. vertexFormat : {
  159. get : function() {
  160. return this._vertexFormat;
  161. }
  162. }
  163. });
  164. /**
  165. * The {@link VertexFormat} that all {@link PolylineMaterialAppearance} instances
  166. * are compatible with. This requires <code>position</code> and <code>st</code> attributes.
  167. *
  168. * @type VertexFormat
  169. *
  170. * @constant
  171. */
  172. PolylineMaterialAppearance.VERTEX_FORMAT = VertexFormat.POSITION_AND_ST;
  173. /**
  174. * Procedurally creates the full GLSL fragment shader source. For {@link PolylineMaterialAppearance},
  175. * this is derived from {@link PolylineMaterialAppearance#fragmentShaderSource} and {@link PolylineMaterialAppearance#material}.
  176. *
  177. * @function
  178. *
  179. * @returns {String} The full GLSL fragment shader source.
  180. */
  181. PolylineMaterialAppearance.prototype.getFragmentShaderSource = Appearance.prototype.getFragmentShaderSource;
  182. /**
  183. * Determines if the geometry is translucent based on {@link PolylineMaterialAppearance#translucent} and {@link Material#isTranslucent}.
  184. *
  185. * @function
  186. *
  187. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  188. */
  189. PolylineMaterialAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  190. /**
  191. * Creates a render state. This is not the final render state instance; instead,
  192. * it can contain a subset of render state properties identical to the render state
  193. * created in the context.
  194. *
  195. * @function
  196. *
  197. * @returns {Object} The render state.
  198. */
  199. PolylineMaterialAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  200. export default PolylineMaterialAppearance;