EllipsoidSurfaceAppearance.js 9.3 KB

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