TerrainOffsetProperty.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import Cartesian3 from '../Core/Cartesian3.js';
  2. import Cartographic from '../Core/Cartographic.js';
  3. import Check from '../Core/Check.js';
  4. import defined from '../Core/defined.js';
  5. import defineProperties from '../Core/defineProperties.js';
  6. import destroyObject from '../Core/destroyObject.js';
  7. import Event from '../Core/Event.js';
  8. import Iso8601 from '../Core/Iso8601.js';
  9. import CesiumMath from '../Core/Math.js';
  10. import HeightReference from '../Scene/HeightReference.js';
  11. import SceneMode from '../Scene/SceneMode.js';
  12. import Property from './Property.js';
  13. var scratchPosition = new Cartesian3();
  14. var scratchCarto = new Cartographic();
  15. /**
  16. * @private
  17. */
  18. function TerrainOffsetProperty(scene, positionProperty, heightReferenceProperty, extrudedHeightReferenceProperty) {
  19. //>>includeStart('debug', pragmas.debug);
  20. Check.defined('scene', scene);
  21. Check.defined('positionProperty', positionProperty);
  22. //>>includeEnd('debug');
  23. this._scene = scene;
  24. this._heightReference = heightReferenceProperty;
  25. this._extrudedHeightReference = extrudedHeightReferenceProperty;
  26. this._positionProperty = positionProperty;
  27. this._position = new Cartesian3();
  28. this._cartographicPosition = new Cartographic();
  29. this._normal = new Cartesian3();
  30. this._definitionChanged = new Event();
  31. this._terrainHeight = 0;
  32. this._removeCallbackFunc = undefined;
  33. this._removeEventListener = undefined;
  34. this._removeModeListener = undefined;
  35. var that = this;
  36. if (defined(scene.globe)) {
  37. this._removeEventListener = scene.terrainProviderChanged.addEventListener(function() {
  38. that._updateClamping();
  39. });
  40. this._removeModeListener = scene.morphComplete.addEventListener(function() {
  41. that._updateClamping();
  42. });
  43. }
  44. if (positionProperty.isConstant) {
  45. var position = positionProperty.getValue(Iso8601.MINIMUM_VALUE, scratchPosition);
  46. if (!defined(position) || Cartesian3.equals(position, Cartesian3.ZERO) || !defined(scene.globe)) {
  47. return;
  48. }
  49. this._position = Cartesian3.clone(position, this._position);
  50. this._updateClamping();
  51. this._normal = scene.globe.ellipsoid.geodeticSurfaceNormal(position, this._normal);
  52. }
  53. }
  54. defineProperties(TerrainOffsetProperty.prototype, {
  55. /**
  56. * Gets a value indicating if this property is constant.
  57. * @memberof TerrainOffsetProperty.prototype
  58. *
  59. * @type {Boolean}
  60. * @readonly
  61. */
  62. isConstant : {
  63. get : function() {
  64. return false;
  65. }
  66. },
  67. /**
  68. * Gets the event that is raised whenever the definition of this property changes.
  69. * @memberof TerrainOffsetProperty.prototype
  70. *
  71. * @type {Event}
  72. * @readonly
  73. */
  74. definitionChanged : {
  75. get : function() {
  76. return this._definitionChanged;
  77. }
  78. }
  79. });
  80. /**
  81. * @private
  82. */
  83. TerrainOffsetProperty.prototype._updateClamping = function() {
  84. if (defined(this._removeCallbackFunc)) {
  85. this._removeCallbackFunc();
  86. }
  87. var scene = this._scene;
  88. var globe = scene.globe;
  89. var position = this._position;
  90. if (!defined(globe) || Cartesian3.equals(position, Cartesian3.ZERO)) {
  91. this._terrainHeight = 0;
  92. return;
  93. }
  94. var ellipsoid = globe.ellipsoid;
  95. var surface = globe._surface;
  96. var that = this;
  97. var cartographicPosition = ellipsoid.cartesianToCartographic(position, this._cartographicPosition);
  98. var height = globe.getHeight(cartographicPosition);
  99. if (defined(height)) {
  100. this._terrainHeight = height;
  101. } else {
  102. this._terrainHeight = 0;
  103. }
  104. function updateFunction(clampedPosition) {
  105. if (scene.mode === SceneMode.SCENE3D) {
  106. var carto = ellipsoid.cartesianToCartographic(clampedPosition, scratchCarto);
  107. that._terrainHeight = carto.height;
  108. } else {
  109. that._terrainHeight = clampedPosition.x;
  110. }
  111. that.definitionChanged.raiseEvent();
  112. }
  113. this._removeCallbackFunc = surface.updateHeight(cartographicPosition, updateFunction);
  114. };
  115. /**
  116. * Gets the height relative to the terrain based on the positions.
  117. *
  118. * @returns {Cartesian3} The offset
  119. */
  120. TerrainOffsetProperty.prototype.getValue = function(time, result) {
  121. var heightReference = Property.getValueOrDefault(this._heightReference, time, HeightReference.NONE);
  122. var extrudedHeightReference = Property.getValueOrDefault(this._extrudedHeightReference, time, HeightReference.NONE);
  123. if (heightReference === HeightReference.NONE && extrudedHeightReference !== HeightReference.RELATIVE_TO_GROUND) {
  124. this._position = Cartesian3.clone(Cartesian3.ZERO, this._position);
  125. return Cartesian3.clone(Cartesian3.ZERO, result);
  126. }
  127. if (this._positionProperty.isConstant) {
  128. return Cartesian3.multiplyByScalar(this._normal, this._terrainHeight, result);
  129. }
  130. var scene = this._scene;
  131. var position = this._positionProperty.getValue(time, scratchPosition);
  132. if (!defined(position) || Cartesian3.equals(position, Cartesian3.ZERO) || !defined(scene.globe)) {
  133. return Cartesian3.clone(Cartesian3.ZERO, result);
  134. }
  135. if (Cartesian3.equalsEpsilon(this._position, position, CesiumMath.EPSILON10)) {
  136. return Cartesian3.multiplyByScalar(this._normal, this._terrainHeight, result);
  137. }
  138. this._position = Cartesian3.clone(position, this._position);
  139. this._updateClamping();
  140. var normal = scene.globe.ellipsoid.geodeticSurfaceNormal(position, this._normal);
  141. return Cartesian3.multiplyByScalar(normal, this._terrainHeight, result);
  142. };
  143. TerrainOffsetProperty.prototype.isDestroyed = function() {
  144. return false;
  145. };
  146. TerrainOffsetProperty.prototype.destroy = function() {
  147. if (defined(this._removeEventListener)) {
  148. this._removeEventListener();
  149. }
  150. if (defined(this._removeModeListener)) {
  151. this._removeModeListener();
  152. }
  153. if (defined(this._removeCallbackFunc)) {
  154. this._removeCallbackFunc();
  155. }
  156. return destroyObject(this);
  157. };
  158. /**
  159. * A function which creates one or more providers.
  160. * @callback TerrainOffsetProperty~PositionFunction
  161. * @param {JulianDate} time The clock time at which to retrieve the position
  162. * @param {Cartesian3} result The result position
  163. * @returns {Cartesian3} The position at which to do the terrain height check
  164. */
  165. export default TerrainOffsetProperty;