PolylineMaterialAppearance.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defined',
  5. '../Core/defineProperties',
  6. '../Core/VertexFormat',
  7. '../Shaders/Appearances/PolylineMaterialAppearanceVS',
  8. '../Shaders/PolylineCommon',
  9. '../Shaders/PolylineFS',
  10. './Appearance',
  11. './Material'
  12. ], function(
  13. defaultValue,
  14. defined,
  15. defineProperties,
  16. VertexFormat,
  17. PolylineMaterialAppearanceVS,
  18. PolylineCommon,
  19. PolylineFS,
  20. Appearance,
  21. Material) {
  22. "use strict";
  23. var defaultVertexShaderSource = PolylineCommon + '\n' + PolylineMaterialAppearanceVS;
  24. var defaultFragmentShaderSource = PolylineFS;
  25. /**
  26. * An appearance for {@link PolylineGeometry} that supports shading with materials.
  27. *
  28. * @alias PolylineMaterialAppearance
  29. * @constructor
  30. *
  31. * @param {Object} [options] Object with the following properties:
  32. * @param {Boolean} [options.translucent=true] When <code>true</code>, the geometry is expected to appear translucent so {@link PolylineMaterialAppearance#renderState} has alpha blending enabled.
  33. * @param {Material} [options.material=Material.ColorType] The material used to determine the fragment color.
  34. * @param {String} [options.vertexShaderSource] Optional GLSL vertex shader source to override the default vertex shader.
  35. * @param {String} [options.fragmentShaderSource] Optional GLSL fragment shader source to override the default fragment shader.
  36. * @param {RenderState} [options.renderState] Optional render state to override the default render state.
  37. *
  38. * @see {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  39. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Polyline%20Material.html|Cesium Sandcastle Polyline Material Appearance Demo}
  40. *
  41. * @example
  42. * var primitive = new Cesium.Primitive({
  43. * geometryInstances : new Cesium.GeometryInstance({
  44. * geometry : new Cesium.PolylineGeometry({
  45. * positions : Cesium.Cartesian3.fromDegreesArray([
  46. * 0.0, 0.0,
  47. * 5.0, 0.0
  48. * ]),
  49. * width : 10.0,
  50. * vertexFormat : Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
  51. * })
  52. * }),
  53. * appearance : new Cesium.PolylineMaterialAppearance({
  54. * material : Cesium.Material.fromType('Color')
  55. * })
  56. * });
  57. */
  58. var PolylineMaterialAppearance = function(options) {
  59. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  60. var translucent = defaultValue(options.translucent, true);
  61. var closed = false;
  62. var vertexFormat = PolylineMaterialAppearance.VERTEX_FORMAT;
  63. /**
  64. * The material used to determine the fragment color. Unlike other {@link PolylineMaterialAppearance}
  65. * properties, this is not read-only, so an appearance's material can change on the fly.
  66. *
  67. * @type Material
  68. *
  69. * @default {@link Material.ColorType}
  70. *
  71. * @see {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  72. */
  73. this.material = defined(options.material) ? options.material : Material.fromType(Material.ColorType);
  74. /**
  75. * When <code>true</code>, the geometry is expected to appear translucent so
  76. * {@link PolylineMaterialAppearance#renderState} has alpha blending enabled.
  77. *
  78. * @type {Boolean}
  79. *
  80. * @default true
  81. */
  82. this.translucent = translucent;
  83. this._vertexShaderSource = defaultValue(options.vertexShaderSource, defaultVertexShaderSource);
  84. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, defaultFragmentShaderSource);
  85. this._renderState = Appearance.getDefaultRenderState(translucent, closed, options.renderState);
  86. this._closed = closed;
  87. // Non-derived members
  88. this._vertexFormat = vertexFormat;
  89. };
  90. defineProperties(PolylineMaterialAppearance.prototype, {
  91. /**
  92. * The GLSL source code for the vertex shader.
  93. *
  94. * @memberof PolylineMaterialAppearance.prototype
  95. *
  96. * @type {String}
  97. * @readonly
  98. */
  99. vertexShaderSource : {
  100. get : function() {
  101. return this._vertexShaderSource;
  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 = Appearance.prototype.getFragmentShaderSource;
  188. /**
  189. * Determines if the geometry is translucent based on {@link PolylineMaterialAppearance#translucent} and {@link Material#isTranslucent}.
  190. *
  191. * @function
  192. *
  193. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  194. */
  195. PolylineMaterialAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  196. /**
  197. * Creates a render state. This is not the final render state instance; instead,
  198. * it can contain a subset of render state properties identical to the render state
  199. * created in the context.
  200. *
  201. * @function
  202. *
  203. * @returns {Object} The render state.
  204. */
  205. PolylineMaterialAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  206. return PolylineMaterialAppearance;
  207. });