智慧水务管理系统 - 精河县供水工程综合管理平台

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