MaterialAppearance.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defined',
  5. '../Core/defineProperties',
  6. '../Core/freezeObject',
  7. '../Core/VertexFormat',
  8. '../Shaders/Appearances/AllMaterialAppearanceFS',
  9. '../Shaders/Appearances/AllMaterialAppearanceVS',
  10. '../Shaders/Appearances/BasicMaterialAppearanceFS',
  11. '../Shaders/Appearances/BasicMaterialAppearanceVS',
  12. '../Shaders/Appearances/TexturedMaterialAppearanceFS',
  13. '../Shaders/Appearances/TexturedMaterialAppearanceVS',
  14. './Appearance',
  15. './Material'
  16. ], function(
  17. defaultValue,
  18. defined,
  19. defineProperties,
  20. freezeObject,
  21. VertexFormat,
  22. AllMaterialAppearanceFS,
  23. AllMaterialAppearanceVS,
  24. BasicMaterialAppearanceFS,
  25. BasicMaterialAppearanceVS,
  26. TexturedMaterialAppearanceFS,
  27. TexturedMaterialAppearanceVS,
  28. Appearance,
  29. Material) {
  30. "use strict";
  31. /**
  32. * An appearance for arbitrary geometry (as opposed to {@link EllipsoidSurfaceAppearance}, for example)
  33. * that supports shading with materials.
  34. *
  35. * @alias MaterialAppearance
  36. * @constructor
  37. *
  38. * @param {Object} [options] Object with the following properties:
  39. * @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.
  40. * @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}.
  41. * @param {Boolean} [options.translucent=true] When <code>true</code>, the geometry is expected to appear translucent so {@link MaterialAppearance#renderState} has alpha blending enabled.
  42. * @param {Boolean} [options.closed=false] When <code>true</code>, the geometry is expected to be closed so {@link MaterialAppearance#renderState} has backface culling enabled.
  43. * @param {MaterialAppearance.MaterialSupport} [options.materialSupport=MaterialAppearance.MaterialSupport.TEXTURED] The type of materials that will be supported.
  44. * @param {Material} [options.material=Material.ColorType] The material used to determine the fragment color.
  45. * @param {String} [options.vertexShaderSource] Optional GLSL vertex shader source to override the default vertex shader.
  46. * @param {String} [options.fragmentShaderSource] Optional GLSL fragment shader source to override the default fragment shader.
  47. * @param {RenderState} [options.renderState] Optional render state to override the default render state.
  48. *
  49. * @see {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  50. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Material.html|Cesium Sandcastle Material Appearance Demo}
  51. *
  52. * @example
  53. * var primitive = new Cesium.Primitive({
  54. * geometryInstances : new Cesium.GeometryInstance({
  55. * geometry : new Cesium.WallGeometry({
  56. materialSupport : Cesium.MaterialAppearance.MaterialSupport.BASIC.vertexFormat,
  57. * // ...
  58. * })
  59. * }),
  60. * appearance : new Cesium.MaterialAppearance({
  61. * material : Cesium.Material.fromType('Color'),
  62. * faceForward : true
  63. * })
  64. *
  65. * });
  66. */
  67. var MaterialAppearance = function(options) {
  68. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  69. var translucent = defaultValue(options.translucent, true);
  70. var closed = defaultValue(options.closed, false);
  71. var materialSupport = defaultValue(options.materialSupport, MaterialAppearance.MaterialSupport.TEXTURED);
  72. /**
  73. * The material used to determine the fragment color. Unlike other {@link MaterialAppearance}
  74. * properties, this is not read-only, so an appearance's material can change on the fly.
  75. *
  76. * @type Material
  77. *
  78. * @default {@link Material.ColorType}
  79. *
  80. * @see {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  81. */
  82. this.material = (defined(options.material)) ? options.material : Material.fromType(Material.ColorType);
  83. /**
  84. * When <code>true</code>, the geometry is expected to appear translucent.
  85. *
  86. * @type {Boolean}
  87. *
  88. * @default true
  89. */
  90. this.translucent = translucent;
  91. this._vertexShaderSource = defaultValue(options.vertexShaderSource, materialSupport.vertexShaderSource);
  92. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, materialSupport.fragmentShaderSource);
  93. this._renderState = Appearance.getDefaultRenderState(translucent, closed, options.renderState);
  94. this._closed = closed;
  95. // Non-derived members
  96. this._materialSupport = materialSupport;
  97. this._vertexFormat = materialSupport.vertexFormat;
  98. this._flat = defaultValue(options.flat, false);
  99. this._faceForward = defaultValue(options.faceForward, !closed);
  100. };
  101. defineProperties(MaterialAppearance.prototype, {
  102. /**
  103. * The GLSL source code for the vertex shader.
  104. *
  105. * @memberof MaterialAppearance.prototype
  106. *
  107. * @type {String}
  108. * @readonly
  109. */
  110. vertexShaderSource : {
  111. get : function() {
  112. return this._vertexShaderSource;
  113. }
  114. },
  115. /**
  116. * The GLSL source code for the fragment shader. The full fragment shader
  117. * source is built procedurally taking into account {@link MaterialAppearance#material},
  118. * {@link MaterialAppearance#flat}, and {@link MaterialAppearance#faceForward}.
  119. * Use {@link MaterialAppearance#getFragmentShaderSource} to get the full source.
  120. *
  121. * @memberof MaterialAppearance.prototype
  122. *
  123. * @type {String}
  124. * @readonly
  125. */
  126. fragmentShaderSource : {
  127. get : function() {
  128. return this._fragmentShaderSource;
  129. }
  130. },
  131. /**
  132. * The WebGL fixed-function state to use when rendering the geometry.
  133. * <p>
  134. * The render state can be explicitly defined when constructing a {@link MaterialAppearance}
  135. * instance, or it is set implicitly via {@link MaterialAppearance#translucent}
  136. * and {@link MaterialAppearance#closed}.
  137. * </p>
  138. *
  139. * @memberof MaterialAppearance.prototype
  140. *
  141. * @type {Object}
  142. * @readonly
  143. */
  144. renderState : {
  145. get : function() {
  146. return this._renderState;
  147. }
  148. },
  149. /**
  150. * When <code>true</code>, the geometry is expected to be closed so
  151. * {@link MaterialAppearance#renderState} has backface culling enabled.
  152. * If the viewer enters the geometry, it will not be visible.
  153. *
  154. * @memberof MaterialAppearance.prototype
  155. *
  156. * @type {Boolean}
  157. * @readonly
  158. *
  159. * @default false
  160. */
  161. closed : {
  162. get : function() {
  163. return this._closed;
  164. }
  165. },
  166. /**
  167. * The type of materials supported by this instance. This impacts the required
  168. * {@link VertexFormat} and the complexity of the vertex and fragment shaders.
  169. *
  170. * @memberof MaterialAppearance.prototype
  171. *
  172. * @type {MaterialAppearance.MaterialSupport}
  173. * @readonly
  174. *
  175. * @default {@link MaterialAppearance.MaterialSupport.TEXTURED}
  176. */
  177. materialSupport : {
  178. get : function() {
  179. return this._materialSupport;
  180. }
  181. },
  182. /**
  183. * The {@link VertexFormat} that this appearance instance is compatible with.
  184. * A geometry can have more vertex attributes and still be compatible - at a
  185. * potential performance cost - but it can't have less.
  186. *
  187. * @memberof MaterialAppearance.prototype
  188. *
  189. * @type VertexFormat
  190. * @readonly
  191. *
  192. * @default {@link MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat}
  193. */
  194. vertexFormat : {
  195. get : function() {
  196. return this._vertexFormat;
  197. }
  198. },
  199. /**
  200. * When <code>true</code>, flat shading is used in the fragment shader,
  201. * which means lighting is not taking into account.
  202. *
  203. * @memberof MaterialAppearance.prototype
  204. *
  205. * @type {Boolean}
  206. * @readonly
  207. *
  208. * @default false
  209. */
  210. flat : {
  211. get : function() {
  212. return this._flat;
  213. }
  214. },
  215. /**
  216. * When <code>true</code>, the fragment shader flips the surface normal
  217. * as needed to ensure that the normal faces the viewer to avoid
  218. * dark spots. This is useful when both sides of a geometry should be
  219. * shaded like {@link WallGeometry}.
  220. *
  221. * @memberof MaterialAppearance.prototype
  222. *
  223. * @type {Boolean}
  224. * @readonly
  225. *
  226. * @default true
  227. */
  228. faceForward : {
  229. get : function() {
  230. return this._faceForward;
  231. }
  232. }
  233. });
  234. /**
  235. * Procedurally creates the full GLSL fragment shader source. For {@link MaterialAppearance},
  236. * this is derived from {@link MaterialAppearance#fragmentShaderSource}, {@link MaterialAppearance#material},
  237. * {@link MaterialAppearance#flat}, and {@link MaterialAppearance#faceForward}.
  238. *
  239. * @function
  240. *
  241. * @returns String The full GLSL fragment shader source.
  242. */
  243. MaterialAppearance.prototype.getFragmentShaderSource = Appearance.prototype.getFragmentShaderSource;
  244. /**
  245. * Determines if the geometry is translucent based on {@link MaterialAppearance#translucent} and {@link Material#isTranslucent}.
  246. *
  247. * @function
  248. *
  249. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  250. */
  251. MaterialAppearance.prototype.isTranslucent = 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. MaterialAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  262. /**
  263. * Determines the type of {@link Material} that is supported by a
  264. * {@link MaterialAppearance} instance. This is a trade-off between
  265. * flexibility (a wide array of materials) and memory/performance
  266. * (required vertex format and GLSL shader complexity.
  267. */
  268. MaterialAppearance.MaterialSupport = {
  269. /**
  270. * Only basic materials, which require just <code>position</code> and
  271. * <code>normal</code> vertex attributes, are supported.
  272. *
  273. * @constant
  274. */
  275. BASIC : freezeObject({
  276. vertexFormat : VertexFormat.POSITION_AND_NORMAL,
  277. vertexShaderSource : BasicMaterialAppearanceVS,
  278. fragmentShaderSource : BasicMaterialAppearanceFS
  279. }),
  280. /**
  281. * Materials with textures, which require <code>position</code>,
  282. * <code>normal</code>, and <code>st</code> vertex attributes,
  283. * are supported. The vast majority of materials fall into this category.
  284. *
  285. * @constant
  286. */
  287. TEXTURED : freezeObject({
  288. vertexFormat : VertexFormat.POSITION_NORMAL_AND_ST,
  289. vertexShaderSource : TexturedMaterialAppearanceVS,
  290. fragmentShaderSource : TexturedMaterialAppearanceFS
  291. }),
  292. /**
  293. * All materials, including those that work in tangent space, are supported.
  294. * This requires <code>position</code>, <code>normal</code>, <code>st</code>,
  295. * <code>binormal</code>, and <code>tangent</code> vertex attributes.
  296. *
  297. * @constant
  298. */
  299. ALL : freezeObject({
  300. vertexFormat : VertexFormat.ALL,
  301. vertexShaderSource : AllMaterialAppearanceVS,
  302. fragmentShaderSource : AllMaterialAppearanceFS
  303. })
  304. };
  305. return MaterialAppearance;
  306. });