ImageMaterialProperty.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import Cartesian2 from '../Core/Cartesian2.js';
  2. import Color from '../Core/Color.js';
  3. import defaultValue from '../Core/defaultValue.js';
  4. import defined from '../Core/defined.js';
  5. import defineProperties from '../Core/defineProperties.js';
  6. import Event from '../Core/Event.js';
  7. import createPropertyDescriptor from './createPropertyDescriptor.js';
  8. import Property from './Property.js';
  9. var defaultRepeat = new Cartesian2(1, 1);
  10. var defaultTransparent = false;
  11. var defaultColor = Color.WHITE;
  12. /**
  13. * A {@link MaterialProperty} that maps to image {@link Material} uniforms.
  14. * @alias ImageMaterialProperty
  15. * @constructor
  16. *
  17. * @param {Object} [options] Object with the following properties:
  18. * @param {Property} [options.image] A Property specifying the Image, URL, Canvas, or Video.
  19. * @param {Property} [options.repeat=new Cartesian2(1.0, 1.0)] A {@link Cartesian2} Property specifying the number of times the image repeats in each direction.
  20. * @param {Property} [options.color=Color.WHITE] The color applied to the image
  21. * @param {Property} [options.transparent=false] Set to true when the image has transparency (for example, when a png has transparent sections)
  22. */
  23. function ImageMaterialProperty(options) {
  24. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  25. this._definitionChanged = new Event();
  26. this._image = undefined;
  27. this._imageSubscription = undefined;
  28. this._repeat = undefined;
  29. this._repeatSubscription = undefined;
  30. this._color = undefined;
  31. this._colorSubscription = undefined;
  32. this._transparent = undefined;
  33. this._transparentSubscription = undefined;
  34. this.image = options.image;
  35. this.repeat = options.repeat;
  36. this.color = options.color;
  37. this.transparent = options.transparent;
  38. }
  39. defineProperties(ImageMaterialProperty.prototype, {
  40. /**
  41. * Gets a value indicating if this property is constant. A property is considered
  42. * constant if getValue always returns the same result for the current definition.
  43. * @memberof ImageMaterialProperty.prototype
  44. *
  45. * @type {Boolean}
  46. * @readonly
  47. */
  48. isConstant : {
  49. get : function() {
  50. return Property.isConstant(this._image) && Property.isConstant(this._repeat);
  51. }
  52. },
  53. /**
  54. * Gets the event that is raised whenever the definition of this property changes.
  55. * The definition is considered to have changed if a call to getValue would return
  56. * a different result for the same time.
  57. * @memberof ImageMaterialProperty.prototype
  58. *
  59. * @type {Event}
  60. * @readonly
  61. */
  62. definitionChanged : {
  63. get : function() {
  64. return this._definitionChanged;
  65. }
  66. },
  67. /**
  68. * Gets or sets the Property specifying Image, URL, Canvas, or Video to use.
  69. * @memberof ImageMaterialProperty.prototype
  70. * @type {Property}
  71. */
  72. image : createPropertyDescriptor('image'),
  73. /**
  74. * Gets or sets the {@link Cartesian2} Property specifying the number of times the image repeats in each direction.
  75. * @memberof ImageMaterialProperty.prototype
  76. * @type {Property}
  77. * @default new Cartesian2(1, 1)
  78. */
  79. repeat : createPropertyDescriptor('repeat'),
  80. /**
  81. * Gets or sets the Color Property specifying the desired color applied to the image.
  82. * @memberof ImageMaterialProperty.prototype
  83. * @type {Property}
  84. * @default 1.0
  85. */
  86. color : createPropertyDescriptor('color'),
  87. /**
  88. * Gets or sets the Boolean Property specifying whether the image has transparency
  89. * @memberof ImageMaterialProperty.prototype
  90. * @type {Property}
  91. * @default 1.0
  92. */
  93. transparent : createPropertyDescriptor('transparent')
  94. });
  95. /**
  96. * Gets the {@link Material} type at the provided time.
  97. *
  98. * @param {JulianDate} time The time for which to retrieve the type.
  99. * @returns {String} The type of material.
  100. */
  101. ImageMaterialProperty.prototype.getType = function(time) {
  102. return 'Image';
  103. };
  104. /**
  105. * Gets the value of the property at the provided time.
  106. *
  107. * @param {JulianDate} time The time for which to retrieve the value.
  108. * @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  109. * @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied.
  110. */
  111. ImageMaterialProperty.prototype.getValue = function(time, result) {
  112. if (!defined(result)) {
  113. result = {};
  114. }
  115. result.image = Property.getValueOrUndefined(this._image, time);
  116. result.repeat = Property.getValueOrClonedDefault(this._repeat, time, defaultRepeat, result.repeat);
  117. result.color = Property.getValueOrClonedDefault(this._color, time, defaultColor, result.color);
  118. if (Property.getValueOrDefault(this._transparent, time, defaultTransparent)) {
  119. result.color.alpha = Math.min(0.99, result.color.alpha);
  120. }
  121. return result;
  122. };
  123. /**
  124. * Compares this property to the provided property and returns
  125. * <code>true</code> if they are equal, <code>false</code> otherwise.
  126. *
  127. * @param {Property} [other] The other property.
  128. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  129. */
  130. ImageMaterialProperty.prototype.equals = function(other) {
  131. return this === other ||
  132. (other instanceof ImageMaterialProperty &&
  133. Property.equals(this._image, other._image) &&
  134. Property.equals(this._repeat, other._repeat) &&
  135. Property.equals(this._color, other._color) &&
  136. Property.equals(this._transparent, other._transparent));
  137. };
  138. export default ImageMaterialProperty;