ViewportQuad.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*global define*/
  2. define([
  3. '../Core/BoundingRectangle',
  4. '../Core/Color',
  5. '../Core/defined',
  6. '../Core/destroyObject',
  7. '../Core/DeveloperError',
  8. '../Renderer/ShaderSource',
  9. '../Shaders/ViewportQuadFS',
  10. './BlendingState',
  11. './Material',
  12. './Pass'
  13. ], function(
  14. BoundingRectangle,
  15. Color,
  16. defined,
  17. destroyObject,
  18. DeveloperError,
  19. ShaderSource,
  20. ViewportQuadFS,
  21. BlendingState,
  22. Material,
  23. Pass) {
  24. "use strict";
  25. /**
  26. * A viewport aligned quad.
  27. *
  28. * @alias ViewportQuad
  29. * @constructor
  30. *
  31. * @param {BoundingRectangle} [rectangle] The {@link BoundingRectangle} defining the quad's position within the viewport.
  32. * @param {Material} [material] The {@link Material} defining the surface appearance of the viewport quad.
  33. *
  34. * @example
  35. * var viewportQuad = new Cesium.ViewportQuad(new Cesium.BoundingRectangle(0, 0, 80, 40));
  36. * viewportQuad.material.uniforms.color = new Cesium.Color(1.0, 0.0, 0.0, 1.0);
  37. */
  38. var ViewportQuad = function(rectangle, material) {
  39. /**
  40. * Determines if the viewport quad primitive will be shown.
  41. *
  42. * @type {Boolean}
  43. * @default true
  44. */
  45. this.show = true;
  46. if (!defined(rectangle)) {
  47. rectangle = new BoundingRectangle();
  48. }
  49. /**
  50. * The BoundingRectangle defining the quad's position within the viewport.
  51. *
  52. * @type {BoundingRectangle}
  53. *
  54. * @example
  55. * viewportQuad.rectangle = new Cesium.BoundingRectangle(0, 0, 80, 40);
  56. */
  57. this.rectangle = BoundingRectangle.clone(rectangle);
  58. if (!defined(material)) {
  59. material = Material.fromType(Material.ColorType, {
  60. color : new Color(1.0, 1.0, 1.0, 1.0)
  61. });
  62. }
  63. /**
  64. * The surface appearance of the viewport quad. This can be one of several built-in {@link Material} objects or a custom material, scripted with
  65. * {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}.
  66. * <p>
  67. * The default material is <code>Material.ColorType</code>.
  68. * </p>
  69. *
  70. * @type Material
  71. *
  72. * @example
  73. * // 1. Change the color of the default material to yellow
  74. * viewportQuad.material.uniforms.color = new Cesium.Color(1.0, 1.0, 0.0, 1.0);
  75. *
  76. * // 2. Change material to horizontal stripes
  77. * viewportQuad.material = Cesium.Material.fromType(Material.StripeType);
  78. *
  79. * @see {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  80. */
  81. this.material = material;
  82. this._material = undefined;
  83. this._overlayCommand = undefined;
  84. this._rs = undefined;
  85. };
  86. /**
  87. * Called when {@link Viewer} or {@link CesiumWidget} render the scene to
  88. * get the draw commands needed to render this primitive.
  89. * <p>
  90. * Do not call this function directly. This is documented just to
  91. * list the exceptions that may be propagated when the scene is rendered:
  92. * </p>
  93. *
  94. * @exception {DeveloperError} this.material must be defined.
  95. * @exception {DeveloperError} this.rectangle must be defined.
  96. */
  97. ViewportQuad.prototype.update = function(context, frameState, commandList) {
  98. if (!this.show) {
  99. return;
  100. }
  101. //>>includeStart('debug', pragmas.debug);
  102. if (!defined(this.material)) {
  103. throw new DeveloperError('this.material must be defined.');
  104. }
  105. if (!defined(this.rectangle)) {
  106. throw new DeveloperError('this.rectangle must be defined.');
  107. }
  108. //>>includeEnd('debug');
  109. var rs = this._rs;
  110. if ((!defined(rs)) || !BoundingRectangle.equals(rs.viewport, this.rectangle)) {
  111. this._rs = context.createRenderState({
  112. blending : BlendingState.ALPHA_BLEND,
  113. viewport : this.rectangle
  114. });
  115. }
  116. var pass = frameState.passes;
  117. if (pass.render) {
  118. if (this._material !== this.material || !defined(this._overlayCommand)) {
  119. // Recompile shader when material changes
  120. this._material = this.material;
  121. if (defined(this._overlayCommand)) {
  122. this._overlayCommand.shaderProgram.destroy();
  123. }
  124. var fs = new ShaderSource({
  125. sources : [this._material.shaderSource, ViewportQuadFS]
  126. });
  127. this._overlayCommand = context.createViewportQuadCommand(fs, {
  128. renderState : this._rs,
  129. uniformMap : this._material._uniforms,
  130. owner : this
  131. });
  132. this._overlayCommand.pass = Pass.OVERLAY;
  133. }
  134. this._material.update(context);
  135. this._overlayCommand.uniformMap = this._material._uniforms;
  136. commandList.push(this._overlayCommand);
  137. }
  138. };
  139. /**
  140. * Returns true if this object was destroyed; otherwise, false.
  141. * <br /><br />
  142. * If this object was destroyed, it should not be used; calling any function other than
  143. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  144. *
  145. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  146. *
  147. * @see ViewportQuad#destroy
  148. */
  149. ViewportQuad.prototype.isDestroyed = function() {
  150. return false;
  151. };
  152. /**
  153. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  154. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  155. * <br /><br />
  156. * Once an object is destroyed, it should not be used; calling any function other than
  157. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  158. * assign the return value (<code>undefined</code>) to the object as done in the example.
  159. *
  160. * @returns {undefined}
  161. *
  162. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  163. *
  164. * @see ViewportQuad#isDestroyed
  165. *
  166. * @example
  167. * quad = quad && quad.destroy();
  168. */
  169. ViewportQuad.prototype.destroy = function() {
  170. if (defined(this._overlayCommand)) {
  171. this._overlayCommand.shaderProgram = this._overlayCommand.shaderProgram && this._overlayCommand.shaderProgram.destroy();
  172. }
  173. return destroyObject(this);
  174. };
  175. return ViewportQuad;
  176. });