PolylineMaterialAppearance.js 7.2 KB

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