智慧水务管理系统 - 精河县供水工程综合管理平台

PolylineMaterialAppearance.js 6.9KB

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