EllipsoidSurfaceAppearance.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import defaultValue from '../Core/defaultValue.js';
  2. import defined from '../Core/defined.js';
  3. import defineProperties from '../Core/defineProperties.js';
  4. import VertexFormat from '../Core/VertexFormat.js';
  5. import EllipsoidSurfaceAppearanceFS from '../Shaders/Appearances/EllipsoidSurfaceAppearanceFS.js';
  6. import EllipsoidSurfaceAppearanceVS from '../Shaders/Appearances/EllipsoidSurfaceAppearanceVS.js';
  7. import Appearance from './Appearance.js';
  8. import Material from './Material.js';
  9. /**
  10. * An appearance for geometry on the surface of the ellipsoid like {@link PolygonGeometry}
  11. * and {@link RectangleGeometry}, which supports all materials like {@link MaterialAppearance}
  12. * with {@link MaterialAppearance.MaterialSupport.ALL}. However, this appearance requires
  13. * fewer vertex attributes since the fragment shader can procedurally compute <code>normal</code>,
  14. * <code>tangent</code>, and <code>bitangent</code>.
  15. *
  16. * @alias EllipsoidSurfaceAppearance
  17. * @constructor
  18. *
  19. * @param {Object} [options] Object with the following properties:
  20. * @param {Boolean} [options.flat=false] When <code>true</code>, flat shading is used in the fragment shader, which means lighting is not taking into account.
  21. * @param {Boolean} [options.faceForward=options.aboveGround] When <code>true</code>, the fragment shader flips the surface normal as needed to ensure that the normal faces the viewer to avoid dark spots. This is useful when both sides of a geometry should be shaded like {@link WallGeometry}.
  22. * @param {Boolean} [options.translucent=true] When <code>true</code>, the geometry is expected to appear translucent so {@link EllipsoidSurfaceAppearance#renderState} has alpha blending enabled.
  23. * @param {Boolean} [options.aboveGround=false] When <code>true</code>, the geometry is expected to be on the ellipsoid's surface - not at a constant height above it - so {@link EllipsoidSurfaceAppearance#renderState} has backface culling 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.PolygonGeometry({
  35. * vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT,
  36. * // ...
  37. * })
  38. * }),
  39. * appearance : new Cesium.EllipsoidSurfaceAppearance({
  40. * material : Cesium.Material.fromType('Stripe')
  41. * })
  42. * });
  43. */
  44. function EllipsoidSurfaceAppearance(options) {
  45. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  46. var translucent = defaultValue(options.translucent, true);
  47. var aboveGround = defaultValue(options.aboveGround, false);
  48. /**
  49. * The material used to determine the fragment color. Unlike other {@link EllipsoidSurfaceAppearance}
  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/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  57. */
  58. this.material = (defined(options.material)) ? options.material : Material.fromType(Material.ColorType);
  59. /**
  60. * When <code>true</code>, the geometry is expected to appear translucent.
  61. *
  62. * @type {Boolean}
  63. *
  64. * @default true
  65. */
  66. this.translucent = defaultValue(options.translucent, true);
  67. this._vertexShaderSource = defaultValue(options.vertexShaderSource, EllipsoidSurfaceAppearanceVS);
  68. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, EllipsoidSurfaceAppearanceFS);
  69. this._renderState = Appearance.getDefaultRenderState(translucent, !aboveGround, options.renderState);
  70. this._closed = false;
  71. // Non-derived members
  72. this._flat = defaultValue(options.flat, false);
  73. this._faceForward = defaultValue(options.faceForward, aboveGround);
  74. this._aboveGround = aboveGround;
  75. }
  76. defineProperties(EllipsoidSurfaceAppearance.prototype, {
  77. /**
  78. * The GLSL source code for the vertex shader.
  79. *
  80. * @memberof EllipsoidSurfaceAppearance.prototype
  81. *
  82. * @type {String}
  83. * @readonly
  84. */
  85. vertexShaderSource : {
  86. get : function() {
  87. return this._vertexShaderSource;
  88. }
  89. },
  90. /**
  91. * The GLSL source code for the fragment shader. The full fragment shader
  92. * source is built procedurally taking into account {@link EllipsoidSurfaceAppearance#material},
  93. * {@link EllipsoidSurfaceAppearance#flat}, and {@link EllipsoidSurfaceAppearance#faceForward}.
  94. * Use {@link EllipsoidSurfaceAppearance#getFragmentShaderSource} to get the full source.
  95. *
  96. * @memberof EllipsoidSurfaceAppearance.prototype
  97. *
  98. * @type {String}
  99. * @readonly
  100. */
  101. fragmentShaderSource : {
  102. get : function() {
  103. return this._fragmentShaderSource;
  104. }
  105. },
  106. /**
  107. * The WebGL fixed-function state to use when rendering the geometry.
  108. * <p>
  109. * The render state can be explicitly defined when constructing a {@link EllipsoidSurfaceAppearance}
  110. * instance, or it is set implicitly via {@link EllipsoidSurfaceAppearance#translucent}
  111. * and {@link EllipsoidSurfaceAppearance#aboveGround}.
  112. * </p>
  113. *
  114. * @memberof EllipsoidSurfaceAppearance.prototype
  115. *
  116. * @type {Object}
  117. * @readonly
  118. */
  119. renderState : {
  120. get : function() {
  121. return this._renderState;
  122. }
  123. },
  124. /**
  125. * When <code>true</code>, the geometry is expected to be closed so
  126. * {@link EllipsoidSurfaceAppearance#renderState} has backface culling enabled.
  127. * If the viewer enters the geometry, it will not be visible.
  128. *
  129. * @memberof EllipsoidSurfaceAppearance.prototype
  130. *
  131. * @type {Boolean}
  132. * @readonly
  133. *
  134. * @default false
  135. */
  136. closed : {
  137. get : function() {
  138. return this._closed;
  139. }
  140. },
  141. /**
  142. * The {@link VertexFormat} that this appearance instance is compatible with.
  143. * A geometry can have more vertex attributes and still be compatible - at a
  144. * potential performance cost - but it can't have less.
  145. *
  146. * @memberof EllipsoidSurfaceAppearance.prototype
  147. *
  148. * @type VertexFormat
  149. * @readonly
  150. *
  151. * @default {@link EllipsoidSurfaceAppearance.VERTEX_FORMAT}
  152. */
  153. vertexFormat : {
  154. get : function() {
  155. return EllipsoidSurfaceAppearance.VERTEX_FORMAT;
  156. }
  157. },
  158. /**
  159. * When <code>true</code>, flat shading is used in the fragment shader,
  160. * which means lighting is not taking into account.
  161. *
  162. * @memberof EllipsoidSurfaceAppearance.prototype
  163. *
  164. * @type {Boolean}
  165. * @readonly
  166. *
  167. * @default false
  168. */
  169. flat : {
  170. get : function() {
  171. return this._flat;
  172. }
  173. },
  174. /**
  175. * When <code>true</code>, the fragment shader flips the surface normal
  176. * as needed to ensure that the normal faces the viewer to avoid
  177. * dark spots. This is useful when both sides of a geometry should be
  178. * shaded like {@link WallGeometry}.
  179. *
  180. * @memberof EllipsoidSurfaceAppearance.prototype
  181. *
  182. * @type {Boolean}
  183. * @readonly
  184. *
  185. * @default true
  186. */
  187. faceForward : {
  188. get : function() {
  189. return this._faceForward;
  190. }
  191. },
  192. /**
  193. * When <code>true</code>, the geometry is expected to be on the ellipsoid's
  194. * surface - not at a constant height above it - so {@link EllipsoidSurfaceAppearance#renderState}
  195. * has backface culling enabled.
  196. *
  197. *
  198. * @memberof EllipsoidSurfaceAppearance.prototype
  199. *
  200. * @type {Boolean}
  201. * @readonly
  202. *
  203. * @default false
  204. */
  205. aboveGround : {
  206. get : function() {
  207. return this._aboveGround;
  208. }
  209. }
  210. });
  211. /**
  212. * The {@link VertexFormat} that all {@link EllipsoidSurfaceAppearance} instances
  213. * are compatible with, which requires only <code>position</code> and <code>st</code>
  214. * attributes. Other attributes are procedurally computed in the fragment shader.
  215. *
  216. * @type VertexFormat
  217. *
  218. * @constant
  219. */
  220. EllipsoidSurfaceAppearance.VERTEX_FORMAT = VertexFormat.POSITION_AND_ST;
  221. /**
  222. * Procedurally creates the full GLSL fragment shader source. For {@link EllipsoidSurfaceAppearance},
  223. * this is derived from {@link EllipsoidSurfaceAppearance#fragmentShaderSource}, {@link EllipsoidSurfaceAppearance#flat},
  224. * and {@link EllipsoidSurfaceAppearance#faceForward}.
  225. *
  226. * @function
  227. *
  228. * @returns {String} The full GLSL fragment shader source.
  229. */
  230. EllipsoidSurfaceAppearance.prototype.getFragmentShaderSource = Appearance.prototype.getFragmentShaderSource;
  231. /**
  232. * Determines if the geometry is translucent based on {@link EllipsoidSurfaceAppearance#translucent} and {@link Material#isTranslucent}.
  233. *
  234. * @function
  235. *
  236. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  237. */
  238. EllipsoidSurfaceAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  239. /**
  240. * Creates a render state. This is not the final render state instance; instead,
  241. * it can contain a subset of render state properties identical to the render state
  242. * created in the context.
  243. *
  244. * @function
  245. *
  246. * @returns {Object} The render state.
  247. */
  248. EllipsoidSurfaceAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  249. export default EllipsoidSurfaceAppearance;