VoxelBoxShape.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import CesiumMath from "../Core/Math.js";
  4. import Check from "../Core/Check.js";
  5. import Matrix3 from "../Core/Matrix3.js";
  6. import Matrix4 from "../Core/Matrix4.js";
  7. import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
  8. import defaultValue from "../Core/defaultValue.js";
  9. /**
  10. * A box {@link VoxelShape}.
  11. *
  12. * @alias VoxelBoxShape
  13. * @constructor
  14. *
  15. * @see VoxelShape
  16. * @see VoxelEllipsoidShape
  17. * @see VoxelCylinderShape
  18. * @see VoxelShapeType
  19. *
  20. * @private
  21. */
  22. function VoxelBoxShape() {
  23. /**
  24. * An oriented bounding box containing the bounded shape.
  25. * The update function must be called before accessing this value.
  26. * @type {OrientedBoundingBox}
  27. * @readonly
  28. */
  29. this.orientedBoundingBox = new OrientedBoundingBox();
  30. /**
  31. * A bounding sphere containing the bounded shape.
  32. * The update function must be called before accessing this value.
  33. * @type {BoundingSphere}
  34. * @readonly
  35. */
  36. this.boundingSphere = new BoundingSphere();
  37. /**
  38. * A transformation matrix containing the bounded shape.
  39. * The update function must be called before accessing this value.
  40. * @type {Matrix4}
  41. * @readonly
  42. */
  43. this.boundTransform = new Matrix4();
  44. /**
  45. * A transformation matrix containing the shape, ignoring the bounds.
  46. * The update function must be called before accessing this value.
  47. * @type {Matrix4}
  48. * @readonly
  49. */
  50. this.shapeTransform = new Matrix4();
  51. /**
  52. * @type {Cartesian3}
  53. * @private
  54. */
  55. this._minBounds = Cartesian3.clone(
  56. VoxelBoxShape.DefaultMinBounds,
  57. new Cartesian3()
  58. );
  59. /**
  60. * @type {Cartesian3}
  61. * @private
  62. */
  63. this._maxBounds = Cartesian3.clone(
  64. VoxelBoxShape.DefaultMaxBounds,
  65. new Cartesian3()
  66. );
  67. /**
  68. * @type {Object<string, any>}
  69. * @readonly
  70. */
  71. this.shaderUniforms = {
  72. renderMinBounds: new Cartesian3(),
  73. renderMaxBounds: new Cartesian3(),
  74. boxUvToShapeUvScale: new Cartesian3(),
  75. boxUvToShapeUvTranslate: new Cartesian3(),
  76. };
  77. /**
  78. * @type {Object<string, any>}
  79. * @readonly
  80. */
  81. this.shaderDefines = {
  82. BOX_INTERSECTION_INDEX: undefined,
  83. BOX_HAS_SHAPE_BOUNDS: undefined,
  84. };
  85. /**
  86. * The maximum number of intersections against the shape for any ray direction.
  87. * @type {number}
  88. * @readonly
  89. */
  90. this.shaderMaximumIntersectionsLength = 0; // not known until update
  91. }
  92. const scratchCenter = new Cartesian3();
  93. const scratchScale = new Cartesian3();
  94. const scratchRotation = new Matrix3();
  95. const scratchClipMinBounds = new Cartesian3();
  96. const scratchClipMaxBounds = new Cartesian3();
  97. const scratchRenderMinBounds = new Cartesian3();
  98. const scratchRenderMaxBounds = new Cartesian3();
  99. const transformLocalToUv = Matrix4.fromRotationTranslation(
  100. Matrix3.fromUniformScale(0.5, new Matrix3()),
  101. new Cartesian3(0.5, 0.5, 0.5),
  102. new Matrix4()
  103. );
  104. /**
  105. * Update the shape's state.
  106. *
  107. * @param {Matrix4} modelMatrix The model matrix.
  108. * @param {Cartesian3} minBounds The minimum bounds.
  109. * @param {Cartesian3} maxBounds The maximum bounds.
  110. * @param {Cartesian3} [clipMinBounds=VoxelBoxShape.DefaultMinBounds] The minimum clip bounds.
  111. * @param {Cartesian3} [clipMaxBounds=VoxelBoxShape.DefaultMaxBounds] The maximum clip bounds.
  112. * @returns {boolean} Whether the shape is visible.
  113. */
  114. VoxelBoxShape.prototype.update = function (
  115. modelMatrix,
  116. minBounds,
  117. maxBounds,
  118. clipMinBounds,
  119. clipMaxBounds
  120. ) {
  121. clipMinBounds = defaultValue(clipMinBounds, VoxelBoxShape.DefaultMinBounds);
  122. clipMaxBounds = defaultValue(clipMaxBounds, VoxelBoxShape.DefaultMaxBounds);
  123. //>>includeStart('debug', pragmas.debug);
  124. Check.typeOf.object("modelMatrix", modelMatrix);
  125. Check.typeOf.object("minBounds", minBounds);
  126. Check.typeOf.object("maxBounds", maxBounds);
  127. //>>includeEnd('debug');
  128. const defaultMinBounds = VoxelBoxShape.DefaultMinBounds;
  129. const defaultMaxBounds = VoxelBoxShape.DefaultMaxBounds;
  130. minBounds = this._minBounds = Cartesian3.clamp(
  131. minBounds,
  132. defaultMinBounds,
  133. defaultMaxBounds,
  134. this._minBounds
  135. );
  136. maxBounds = this._maxBounds = Cartesian3.clamp(
  137. maxBounds,
  138. defaultMinBounds,
  139. defaultMaxBounds,
  140. this._maxBounds
  141. );
  142. clipMinBounds = Cartesian3.clamp(
  143. clipMinBounds,
  144. defaultMinBounds,
  145. defaultMaxBounds,
  146. scratchClipMinBounds
  147. );
  148. clipMaxBounds = Cartesian3.clamp(
  149. clipMaxBounds,
  150. defaultMinBounds,
  151. defaultMaxBounds,
  152. scratchClipMaxBounds
  153. );
  154. const renderMinBounds = Cartesian3.clamp(
  155. minBounds,
  156. clipMinBounds,
  157. clipMaxBounds,
  158. scratchRenderMinBounds
  159. );
  160. const renderMaxBounds = Cartesian3.clamp(
  161. maxBounds,
  162. clipMinBounds,
  163. clipMaxBounds,
  164. scratchRenderMaxBounds
  165. );
  166. const scale = Matrix4.getScale(modelMatrix, scratchScale);
  167. // Box is not visible if:
  168. // - any of the min render bounds exceed the max render bounds
  169. // - two or more of the min bounds equal the max bounds (line / point)
  170. // - any of the min clip bounds exceed the max clip bounds
  171. // - scale is 0 for any component (too annoying to reconstruct rotation matrix)
  172. if (
  173. renderMinBounds.x > renderMaxBounds.x ||
  174. renderMinBounds.y > renderMaxBounds.y ||
  175. renderMinBounds.z > renderMaxBounds.z ||
  176. (renderMinBounds.x === renderMaxBounds.x) +
  177. (renderMinBounds.y === renderMaxBounds.y) +
  178. (renderMinBounds.z === renderMaxBounds.z) >=
  179. 2 ||
  180. clipMinBounds.x > clipMaxBounds.x ||
  181. clipMinBounds.y > clipMaxBounds.y ||
  182. clipMinBounds.z > clipMaxBounds.z ||
  183. scale.x === 0.0 ||
  184. scale.y === 0.0 ||
  185. scale.z === 0.0
  186. ) {
  187. return false;
  188. }
  189. this.shapeTransform = Matrix4.clone(modelMatrix, this.shapeTransform);
  190. this.orientedBoundingBox = getBoxChunkObb(
  191. renderMinBounds,
  192. renderMaxBounds,
  193. this.shapeTransform,
  194. this.orientedBoundingBox
  195. );
  196. // All of the box bounds go from -1 to +1, so the model matrix scale can be
  197. // used as the oriented bounding box half axes.
  198. this.boundTransform = Matrix4.fromRotationTranslation(
  199. this.orientedBoundingBox.halfAxes,
  200. this.orientedBoundingBox.center,
  201. this.boundTransform
  202. );
  203. this.boundingSphere = BoundingSphere.fromOrientedBoundingBox(
  204. this.orientedBoundingBox,
  205. this.boundingSphere
  206. );
  207. const { shaderUniforms, shaderDefines } = this;
  208. // To keep things simple, clear the defines every time
  209. for (const key in shaderDefines) {
  210. if (shaderDefines.hasOwnProperty(key)) {
  211. shaderDefines[key] = undefined;
  212. }
  213. }
  214. const hasShapeBounds =
  215. !Cartesian3.equals(minBounds, defaultMinBounds) ||
  216. !Cartesian3.equals(maxBounds, defaultMaxBounds);
  217. // Keep track of how many intersections there are going to be.
  218. let intersectionCount = 0;
  219. shaderDefines["BOX_INTERSECTION_INDEX"] = intersectionCount;
  220. intersectionCount += 1;
  221. shaderUniforms.renderMinBounds = Matrix4.multiplyByPoint(
  222. transformLocalToUv,
  223. renderMinBounds,
  224. shaderUniforms.renderMinBounds
  225. );
  226. shaderUniforms.renderMaxBounds = Matrix4.multiplyByPoint(
  227. transformLocalToUv,
  228. renderMaxBounds,
  229. shaderUniforms.renderMaxBounds
  230. );
  231. if (hasShapeBounds) {
  232. shaderDefines["BOX_HAS_SHAPE_BOUNDS"] = true;
  233. const min = minBounds;
  234. const max = maxBounds;
  235. // Go from UV space to bounded UV space:
  236. // delerp(posUv, minBoundsUv, maxBoundsUv)
  237. // (posUv - minBoundsUv) / (maxBoundsUv - minBoundsUv)
  238. // posUv / (maxBoundsUv - minBoundsUv) - minBoundsUv / (maxBoundsUv - minBoundsUv)
  239. // scale = 1.0 / (maxBoundsUv - minBoundsUv)
  240. // scale = 1.0 / ((maxBounds * 0.5 + 0.5) - (minBounds * 0.5 + 0.5))
  241. // scale = 2.0 / (maxBounds - minBounds)
  242. // offset = -minBoundsUv / ((maxBounds * 0.5 + 0.5) - (minBounds * 0.5 + 0.5))
  243. // offset = -2.0 * (minBounds * 0.5 + 0.5) / (maxBounds - minBounds)
  244. // offset = -scale * (minBounds * 0.5 + 0.5)
  245. shaderUniforms.boxUvToShapeUvScale = Cartesian3.fromElements(
  246. 2.0 / (min.x === max.x ? 1.0 : max.x - min.x),
  247. 2.0 / (min.y === max.y ? 1.0 : max.y - min.y),
  248. 2.0 / (min.z === max.z ? 1.0 : max.z - min.z),
  249. shaderUniforms.boxUvToShapeUvScale
  250. );
  251. shaderUniforms.boxUvToShapeUvTranslate = Cartesian3.fromElements(
  252. -shaderUniforms.boxUvToShapeUvScale.x * (min.x * 0.5 + 0.5),
  253. -shaderUniforms.boxUvToShapeUvScale.y * (min.y * 0.5 + 0.5),
  254. -shaderUniforms.boxUvToShapeUvScale.z * (min.z * 0.5 + 0.5),
  255. shaderUniforms.boxUvToShapeUvTranslate
  256. );
  257. }
  258. this.shaderMaximumIntersectionsLength = intersectionCount;
  259. return true;
  260. };
  261. const scratchTileMinBounds = new Cartesian3();
  262. const scratchTileMaxBounds = new Cartesian3();
  263. /**
  264. * Computes an oriented bounding box for a specified tile.
  265. * The update function must be called before calling this function.
  266. *
  267. * @param {number} tileLevel The tile's level.
  268. * @param {number} tileX The tile's x coordinate.
  269. * @param {number} tileY The tile's y coordinate.
  270. * @param {number} tileZ The tile's z coordinate.
  271. * @param {OrientedBoundingBox} result The oriented bounding box that will be set to enclose the specified tile
  272. * @returns {OrientedBoundingBox} The oriented bounding box.
  273. */
  274. VoxelBoxShape.prototype.computeOrientedBoundingBoxForTile = function (
  275. tileLevel,
  276. tileX,
  277. tileY,
  278. tileZ,
  279. result
  280. ) {
  281. //>>includeStart('debug', pragmas.debug);
  282. Check.typeOf.number("tileLevel", tileLevel);
  283. Check.typeOf.number("tileX", tileX);
  284. Check.typeOf.number("tileY", tileY);
  285. Check.typeOf.number("tileZ", tileZ);
  286. Check.typeOf.object("result", result);
  287. //>>includeEnd('debug');
  288. const minBounds = this._minBounds;
  289. const maxBounds = this._maxBounds;
  290. const sizeAtLevel = 1.0 / Math.pow(2, tileLevel);
  291. const tileMinBounds = Cartesian3.fromElements(
  292. CesiumMath.lerp(minBounds.x, maxBounds.x, sizeAtLevel * tileX),
  293. CesiumMath.lerp(minBounds.y, maxBounds.y, sizeAtLevel * tileY),
  294. CesiumMath.lerp(minBounds.z, maxBounds.z, sizeAtLevel * tileZ),
  295. scratchTileMinBounds
  296. );
  297. const tileMaxBounds = Cartesian3.fromElements(
  298. CesiumMath.lerp(minBounds.x, maxBounds.x, sizeAtLevel * (tileX + 1)),
  299. CesiumMath.lerp(minBounds.y, maxBounds.y, sizeAtLevel * (tileY + 1)),
  300. CesiumMath.lerp(minBounds.z, maxBounds.z, sizeAtLevel * (tileZ + 1)),
  301. scratchTileMaxBounds
  302. );
  303. return getBoxChunkObb(
  304. tileMinBounds,
  305. tileMaxBounds,
  306. this.shapeTransform,
  307. result
  308. );
  309. };
  310. /**
  311. * Computes an approximate step size for raymarching the root tile of a voxel grid.
  312. * The update function must be called before calling this function.
  313. *
  314. * @param {Cartesian3} dimensions The voxel grid dimensions for a tile.
  315. * @returns {number} The step size.
  316. */
  317. VoxelBoxShape.prototype.computeApproximateStepSize = function (dimensions) {
  318. //>>includeStart('debug', pragmas.debug);
  319. Check.typeOf.object("dimensions", dimensions);
  320. //>>includeEnd('debug');
  321. return 1.0 / Cartesian3.maximumComponent(dimensions);
  322. };
  323. /**
  324. * Defines the minimum bounds of the shape. Corresponds to minimum X, Y, Z.
  325. *
  326. * @type {Cartesian3}
  327. * @constant
  328. * @readonly
  329. */
  330. VoxelBoxShape.DefaultMinBounds = Object.freeze(
  331. new Cartesian3(-1.0, -1.0, -1.0)
  332. );
  333. /**
  334. * Defines the maximum bounds of the shape. Corresponds to maximum X, Y, Z.
  335. *
  336. * @type {Cartesian3}
  337. * @constant
  338. * @readonly
  339. */
  340. VoxelBoxShape.DefaultMaxBounds = Object.freeze(
  341. new Cartesian3(+1.0, +1.0, +1.0)
  342. );
  343. /**
  344. * Computes an {@link OrientedBoundingBox} for a subregion of the shape.
  345. *
  346. * @function
  347. *
  348. * @param {Cartesian3} minimumBounds The minimum bounds, in the local coordinates of the shape.
  349. * @param {Cartesian3} maximumBounds The maximum bounds, in the local coordinates of the shape.
  350. * @param {Matrix4} matrix The matrix to transform the points.
  351. * @param {OrientedBoundingBox} result The object onto which to store the result.
  352. * @returns {OrientedBoundingBox} The oriented bounding box that contains this subregion.
  353. *
  354. * @private
  355. */
  356. function getBoxChunkObb(minimumBounds, maximumBounds, matrix, result) {
  357. const defaultMinBounds = VoxelBoxShape.DefaultMinBounds;
  358. const defaultMaxBounds = VoxelBoxShape.DefaultMaxBounds;
  359. const isDefaultBounds =
  360. Cartesian3.equals(minimumBounds, defaultMinBounds) &&
  361. Cartesian3.equals(maximumBounds, defaultMaxBounds);
  362. if (isDefaultBounds) {
  363. result.center = Matrix4.getTranslation(matrix, result.center);
  364. result.halfAxes = Matrix4.getMatrix3(matrix, result.halfAxes);
  365. } else {
  366. let scale = Matrix4.getScale(matrix, scratchScale);
  367. const localCenter = Cartesian3.midpoint(
  368. minimumBounds,
  369. maximumBounds,
  370. scratchCenter
  371. );
  372. result.center = Matrix4.multiplyByPoint(matrix, localCenter, result.center);
  373. scale = Cartesian3.fromElements(
  374. scale.x * 0.5 * (maximumBounds.x - minimumBounds.x),
  375. scale.y * 0.5 * (maximumBounds.y - minimumBounds.y),
  376. scale.z * 0.5 * (maximumBounds.z - minimumBounds.z),
  377. scratchScale
  378. );
  379. const rotation = Matrix4.getRotation(matrix, scratchRotation);
  380. result.halfAxes = Matrix3.setScale(rotation, scale, result.halfAxes);
  381. }
  382. return result;
  383. }
  384. export default VoxelBoxShape;