PolylineMaterialAppearance.js 7.2 KB

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