PerInstanceColorAppearance.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import defaultValue from '../Core/defaultValue.js';
  2. import defineProperties from '../Core/defineProperties.js';
  3. import VertexFormat from '../Core/VertexFormat.js';
  4. import PerInstanceColorAppearanceFS from '../Shaders/Appearances/PerInstanceColorAppearanceFS.js';
  5. import PerInstanceColorAppearanceVS from '../Shaders/Appearances/PerInstanceColorAppearanceVS.js';
  6. import PerInstanceFlatColorAppearanceFS from '../Shaders/Appearances/PerInstanceFlatColorAppearanceFS.js';
  7. import PerInstanceFlatColorAppearanceVS from '../Shaders/Appearances/PerInstanceFlatColorAppearanceVS.js';
  8. import Appearance from './Appearance.js';
  9. /**
  10. * An appearance for {@link GeometryInstance} instances with color attributes.
  11. * This allows several geometry instances, each with a different color, to
  12. * be drawn with the same {@link Primitive} as shown in the second example below.
  13. *
  14. * @alias PerInstanceColorAppearance
  15. * @constructor
  16. *
  17. * @param {Object} [options] Object with the following properties:
  18. * @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.
  19. * @param {Boolean} [options.faceForward=!options.closed] 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}.
  20. * @param {Boolean} [options.translucent=true] When <code>true</code>, the geometry is expected to appear translucent so {@link PerInstanceColorAppearance#renderState} has alpha blending enabled.
  21. * @param {Boolean} [options.closed=false] When <code>true</code>, the geometry is expected to be closed so {@link PerInstanceColorAppearance#renderState} has backface culling enabled.
  22. * @param {String} [options.vertexShaderSource] Optional GLSL vertex shader source to override the default vertex shader.
  23. * @param {String} [options.fragmentShaderSource] Optional GLSL fragment shader source to override the default fragment shader.
  24. * @param {RenderState} [options.renderState] Optional render state to override the default render state.
  25. *
  26. * @example
  27. * // A solid white line segment
  28. * var primitive = new Cesium.Primitive({
  29. * geometryInstances : new Cesium.GeometryInstance({
  30. * geometry : new Cesium.SimplePolylineGeometry({
  31. * positions : Cesium.Cartesian3.fromDegreesArray([
  32. * 0.0, 0.0,
  33. * 5.0, 0.0
  34. * ])
  35. * }),
  36. * attributes : {
  37. * color : Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 1.0, 1.0, 1.0))
  38. * }
  39. * }),
  40. * appearance : new Cesium.PerInstanceColorAppearance({
  41. * flat : true,
  42. * translucent : false
  43. * })
  44. * });
  45. *
  46. * // Two rectangles in a primitive, each with a different color
  47. * var instance = new Cesium.GeometryInstance({
  48. * geometry : new Cesium.RectangleGeometry({
  49. * rectangle : Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0)
  50. * }),
  51. * attributes : {
  52. * color : new Cesium.ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 0.5)
  53. * }
  54. * });
  55. *
  56. * var anotherInstance = new Cesium.GeometryInstance({
  57. * geometry : new Cesium.RectangleGeometry({
  58. * rectangle : Cesium.Rectangle.fromDegrees(0.0, 40.0, 10.0, 50.0)
  59. * }),
  60. * attributes : {
  61. * color : new Cesium.ColorGeometryInstanceAttribute(0.0, 0.0, 1.0, 0.5)
  62. * }
  63. * });
  64. *
  65. * var rectanglePrimitive = new Cesium.Primitive({
  66. * geometryInstances : [instance, anotherInstance],
  67. * appearance : new Cesium.PerInstanceColorAppearance()
  68. * });
  69. */
  70. function PerInstanceColorAppearance(options) {
  71. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  72. var translucent = defaultValue(options.translucent, true);
  73. var closed = defaultValue(options.closed, false);
  74. var flat = defaultValue(options.flat, false);
  75. var vs = flat ? PerInstanceFlatColorAppearanceVS : PerInstanceColorAppearanceVS;
  76. var fs = flat ? PerInstanceFlatColorAppearanceFS : PerInstanceColorAppearanceFS;
  77. var vertexFormat = flat ? PerInstanceColorAppearance.FLAT_VERTEX_FORMAT : PerInstanceColorAppearance.VERTEX_FORMAT;
  78. /**
  79. * This property is part of the {@link Appearance} interface, but is not
  80. * used by {@link PerInstanceColorAppearance} since a fully custom fragment shader is used.
  81. *
  82. * @type Material
  83. *
  84. * @default undefined
  85. */
  86. this.material = undefined;
  87. /**
  88. * When <code>true</code>, the geometry is expected to appear translucent so
  89. * {@link PerInstanceColorAppearance#renderState} has alpha blending enabled.
  90. *
  91. * @type {Boolean}
  92. *
  93. * @default true
  94. */
  95. this.translucent = translucent;
  96. this._vertexShaderSource = defaultValue(options.vertexShaderSource, vs);
  97. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, fs);
  98. this._renderState = Appearance.getDefaultRenderState(translucent, closed, options.renderState);
  99. this._closed = closed;
  100. // Non-derived members
  101. this._vertexFormat = vertexFormat;
  102. this._flat = flat;
  103. this._faceForward = defaultValue(options.faceForward, !closed);
  104. }
  105. defineProperties(PerInstanceColorAppearance.prototype, {
  106. /**
  107. * The GLSL source code for the vertex shader.
  108. *
  109. * @memberof PerInstanceColorAppearance.prototype
  110. *
  111. * @type {String}
  112. * @readonly
  113. */
  114. vertexShaderSource : {
  115. get : function() {
  116. return this._vertexShaderSource;
  117. }
  118. },
  119. /**
  120. * The GLSL source code for the fragment shader.
  121. *
  122. * @memberof PerInstanceColorAppearance.prototype
  123. *
  124. * @type {String}
  125. * @readonly
  126. */
  127. fragmentShaderSource : {
  128. get : function() {
  129. return this._fragmentShaderSource;
  130. }
  131. },
  132. /**
  133. * The WebGL fixed-function state to use when rendering the geometry.
  134. * <p>
  135. * The render state can be explicitly defined when constructing a {@link PerInstanceColorAppearance}
  136. * instance, or it is set implicitly via {@link PerInstanceColorAppearance#translucent}
  137. * and {@link PerInstanceColorAppearance#closed}.
  138. * </p>
  139. *
  140. * @memberof PerInstanceColorAppearance.prototype
  141. *
  142. * @type {Object}
  143. * @readonly
  144. */
  145. renderState : {
  146. get : function() {
  147. return this._renderState;
  148. }
  149. },
  150. /**
  151. * When <code>true</code>, the geometry is expected to be closed so
  152. * {@link PerInstanceColorAppearance#renderState} has backface culling enabled.
  153. * If the viewer enters the geometry, it will not be visible.
  154. *
  155. * @memberof PerInstanceColorAppearance.prototype
  156. *
  157. * @type {Boolean}
  158. * @readonly
  159. *
  160. * @default false
  161. */
  162. closed : {
  163. get : function() {
  164. return this._closed;
  165. }
  166. },
  167. /**
  168. * The {@link VertexFormat} that this appearance instance is compatible with.
  169. * A geometry can have more vertex attributes and still be compatible - at a
  170. * potential performance cost - but it can't have less.
  171. *
  172. * @memberof PerInstanceColorAppearance.prototype
  173. *
  174. * @type VertexFormat
  175. * @readonly
  176. */
  177. vertexFormat : {
  178. get : function() {
  179. return this._vertexFormat;
  180. }
  181. },
  182. /**
  183. * When <code>true</code>, flat shading is used in the fragment shader,
  184. * which means lighting is not taking into account.
  185. *
  186. * @memberof PerInstanceColorAppearance.prototype
  187. *
  188. * @type {Boolean}
  189. * @readonly
  190. *
  191. * @default false
  192. */
  193. flat : {
  194. get : function() {
  195. return this._flat;
  196. }
  197. },
  198. /**
  199. * When <code>true</code>, the fragment shader flips the surface normal
  200. * as needed to ensure that the normal faces the viewer to avoid
  201. * dark spots. This is useful when both sides of a geometry should be
  202. * shaded like {@link WallGeometry}.
  203. *
  204. * @memberof PerInstanceColorAppearance.prototype
  205. *
  206. * @type {Boolean}
  207. * @readonly
  208. *
  209. * @default true
  210. */
  211. faceForward : {
  212. get : function() {
  213. return this._faceForward;
  214. }
  215. }
  216. });
  217. /**
  218. * The {@link VertexFormat} that all {@link PerInstanceColorAppearance} instances
  219. * are compatible with. This requires only <code>position</code> and <code>normal</code>
  220. * attributes.
  221. *
  222. * @type VertexFormat
  223. *
  224. * @constant
  225. */
  226. PerInstanceColorAppearance.VERTEX_FORMAT = VertexFormat.POSITION_AND_NORMAL;
  227. /**
  228. * The {@link VertexFormat} that all {@link PerInstanceColorAppearance} instances
  229. * are compatible with when {@link PerInstanceColorAppearance#flat} is <code>true</code>.
  230. * This requires only a <code>position</code> attribute.
  231. *
  232. * @type VertexFormat
  233. *
  234. * @constant
  235. */
  236. PerInstanceColorAppearance.FLAT_VERTEX_FORMAT = VertexFormat.POSITION_ONLY;
  237. /**
  238. * Procedurally creates the full GLSL fragment shader source. For {@link PerInstanceColorAppearance},
  239. * this is derived from {@link PerInstanceColorAppearance#fragmentShaderSource}, {@link PerInstanceColorAppearance#flat},
  240. * and {@link PerInstanceColorAppearance#faceForward}.
  241. *
  242. * @function
  243. *
  244. * @returns {String} The full GLSL fragment shader source.
  245. */
  246. PerInstanceColorAppearance.prototype.getFragmentShaderSource = Appearance.prototype.getFragmentShaderSource;
  247. /**
  248. * Determines if the geometry is translucent based on {@link PerInstanceColorAppearance#translucent}.
  249. *
  250. * @function
  251. *
  252. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  253. */
  254. PerInstanceColorAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  255. /**
  256. * Creates a render state. This is not the final render state instance; instead,
  257. * it can contain a subset of render state properties identical to the render state
  258. * created in the context.
  259. *
  260. * @function
  261. *
  262. * @returns {Object} The render state.
  263. */
  264. PerInstanceColorAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  265. export default PerInstanceColorAppearance;