I3SGeometry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import defined from "../Core/defined.js";
  3. import Matrix3 from "../Core/Matrix3.js";
  4. /**
  5. * This class implements an I3S Geometry. Each I3SGeometry
  6. * generates an in memory glTF to be used as content for a Cesium3DTile
  7. * <p>
  8. * Do not construct this directly, instead access tiles through {@link I3SNode}.
  9. * </p>
  10. * @alias I3SGeometry
  11. * @internalConstructor
  12. * @privateParam {I3SNode} parent The parent of that geometry
  13. * @privateParam {string} uri The uri to load the data from
  14. */
  15. function I3SGeometry(parent, uri) {
  16. const dataProvider = parent._dataProvider;
  17. const layer = parent._layer;
  18. let resource;
  19. if (defined(parent._nodeIndex)) {
  20. resource = layer.resource.getDerivedResource({
  21. url: `nodes/${parent._data.mesh.geometry.resource}/${uri}`,
  22. });
  23. } else {
  24. resource = parent.resource.getDerivedResource({ url: uri });
  25. }
  26. this._parent = parent;
  27. this._dataProvider = dataProvider;
  28. this._layer = layer;
  29. this._resource = resource;
  30. this._customAttributes = undefined;
  31. }
  32. Object.defineProperties(I3SGeometry.prototype, {
  33. /**
  34. * Gets the resource for the geometry
  35. * @memberof I3SGeometry.prototype
  36. * @type {Resource}
  37. * @readonly
  38. */
  39. resource: {
  40. get: function () {
  41. return this._resource;
  42. },
  43. },
  44. /**
  45. * Gets the I3S data for this object.
  46. * @memberof I3SGeometry.prototype
  47. * @type {object}
  48. * @readonly
  49. */
  50. data: {
  51. get: function () {
  52. return this._data;
  53. },
  54. },
  55. /**
  56. * Gets the custom attributes of the geometry.
  57. * @memberof I3SGeometry.prototype
  58. * @type {object}
  59. * @readonly
  60. */
  61. customAttributes: {
  62. get: function () {
  63. return this._customAttributes;
  64. },
  65. },
  66. });
  67. /**
  68. * Loads the content.
  69. * @returns {Promise<object>} A promise that is resolved when the geometry data is loaded
  70. * @private
  71. */
  72. I3SGeometry.prototype.load = function () {
  73. const that = this;
  74. return this._dataProvider._loadBinary(this._resource).then(function (data) {
  75. that._data = data;
  76. return data;
  77. });
  78. };
  79. const scratchAb = new Cartesian3();
  80. const scratchAp1 = new Cartesian3();
  81. const scratchAp2 = new Cartesian3();
  82. const scratchCp1 = new Cartesian3();
  83. const scratchCp2 = new Cartesian3();
  84. function sameSide(p1, p2, a, b) {
  85. const ab = Cartesian3.subtract(b, a, scratchAb);
  86. const cp1 = Cartesian3.cross(
  87. ab,
  88. Cartesian3.subtract(p1, a, scratchAp1),
  89. scratchCp1
  90. );
  91. const cp2 = Cartesian3.cross(
  92. ab,
  93. Cartesian3.subtract(p2, a, scratchAp2),
  94. scratchCp2
  95. );
  96. return Cartesian3.dot(cp1, cp2) >= 0;
  97. }
  98. const scratchV0 = new Cartesian3();
  99. const scratchV1 = new Cartesian3();
  100. const scratchV2 = new Cartesian3();
  101. const scratchV0V1 = new Cartesian3();
  102. const scratchV0V2 = new Cartesian3();
  103. const scratchCrossProd = new Cartesian3();
  104. const scratchNormal = new Cartesian3();
  105. const scratchV0p = new Cartesian3();
  106. const scratchV1p = new Cartesian3();
  107. const scratchV2p = new Cartesian3();
  108. /**
  109. * Find a triangle touching the point [px, py, pz], then return the vertex closest to the search point
  110. * @param {number} px The x component of the point to query
  111. * @param {number} py The y component of the point to query
  112. * @param {number} pz The z component of the point to query
  113. * @returns {object} A structure containing the index of the closest point,
  114. * the squared distance from the queried point to the point that is found,
  115. * the distance from the queried point to the point that is found,
  116. * the queried position in local space,
  117. * the closest position in local space
  118. */
  119. I3SGeometry.prototype.getClosestPointIndexOnTriangle = function (px, py, pz) {
  120. if (
  121. defined(this._customAttributes) &&
  122. defined(this._customAttributes.positions)
  123. ) {
  124. // Convert queried position to local
  125. const position = new Cartesian3(px, py, pz);
  126. position.x -= this._customAttributes.cartesianCenter.x;
  127. position.y -= this._customAttributes.cartesianCenter.y;
  128. position.z -= this._customAttributes.cartesianCenter.z;
  129. Matrix3.multiplyByVector(
  130. this._customAttributes.parentRotation,
  131. position,
  132. position
  133. );
  134. let bestTriDist = Number.MAX_VALUE;
  135. let bestTri;
  136. let bestDistSq;
  137. let bestIndex;
  138. let bestPt;
  139. // Brute force lookup, @TODO: this can be improved with a spatial partitioning search system
  140. const positions = this._customAttributes.positions;
  141. const indices = this._customAttributes.indices;
  142. // We may have indexed or non-indexed triangles here
  143. let triCount;
  144. if (defined(indices)) {
  145. triCount = indices.length;
  146. } else {
  147. triCount = positions.length / 3;
  148. }
  149. for (let triIndex = 0; triIndex < triCount; triIndex++) {
  150. let i0, i1, i2;
  151. if (defined(indices)) {
  152. i0 = indices[triIndex];
  153. i1 = indices[triIndex + 1];
  154. i2 = indices[triIndex + 2];
  155. } else {
  156. i0 = triIndex * 3;
  157. i1 = triIndex * 3 + 1;
  158. i2 = triIndex * 3 + 2;
  159. }
  160. const v0 = Cartesian3.fromElements(
  161. positions[i0 * 3],
  162. positions[i0 * 3 + 1],
  163. positions[i0 * 3 + 2],
  164. scratchV0
  165. );
  166. const v1 = Cartesian3.fromElements(
  167. positions[i1 * 3],
  168. positions[i1 * 3 + 1],
  169. positions[i1 * 3 + 2],
  170. scratchV1
  171. );
  172. const v2 = new Cartesian3(
  173. positions[i2 * 3],
  174. positions[i2 * 3 + 1],
  175. positions[i2 * 3 + 2],
  176. scratchV2
  177. );
  178. // Check how the point is positioned relative to the triangle.
  179. // This will tell us whether the projection of the point in the triangle's plane lands in the triangle
  180. if (
  181. !sameSide(position, v0, v1, v2) ||
  182. !sameSide(position, v1, v0, v2) ||
  183. !sameSide(position, v2, v0, v1)
  184. ) {
  185. continue;
  186. }
  187. // Because of precision issues, we can't always reliably tell if the point lands directly on the face, so the most robust way is just to find the closest one
  188. const v0v1 = Cartesian3.subtract(v1, v0, scratchV0V1);
  189. const v0v2 = Cartesian3.subtract(v2, v0, scratchV0V2);
  190. const crossProd = Cartesian3.cross(v0v1, v0v2, scratchCrossProd);
  191. // Skip "triangles" with 3 colinear points
  192. if (Cartesian3.magnitude(crossProd) === 0) {
  193. continue;
  194. }
  195. const normal = Cartesian3.normalize(crossProd, scratchNormal);
  196. const v0p = Cartesian3.subtract(position, v0, scratchV0p);
  197. const normalDist = Math.abs(Cartesian3.dot(v0p, normal));
  198. if (normalDist < bestTriDist) {
  199. bestTriDist = normalDist;
  200. bestTri = triIndex;
  201. // Found a triangle, return the index of the closest point
  202. const d0 = Cartesian3.magnitudeSquared(
  203. Cartesian3.subtract(position, v0, v0p)
  204. );
  205. const d1 = Cartesian3.magnitudeSquared(
  206. Cartesian3.subtract(position, v1, scratchV1p)
  207. );
  208. const d2 = Cartesian3.magnitudeSquared(
  209. Cartesian3.subtract(position, v2, scratchV2p)
  210. );
  211. if (d0 < d1 && d0 < d2) {
  212. bestIndex = i0;
  213. bestPt = v0;
  214. bestDistSq = d0;
  215. } else if (d1 < d2) {
  216. bestIndex = i1;
  217. bestPt = v1;
  218. bestDistSq = d1;
  219. } else {
  220. bestIndex = i2;
  221. bestPt = v2;
  222. bestDistSq = d2;
  223. }
  224. }
  225. }
  226. if (defined(bestTri)) {
  227. return {
  228. index: bestIndex,
  229. distanceSquared: bestDistSq,
  230. distance: Math.sqrt(bestDistSq),
  231. queriedPosition: position,
  232. closestPosition: Cartesian3.clone(bestPt),
  233. };
  234. }
  235. }
  236. // No hits found
  237. return {
  238. index: -1,
  239. distanceSquared: Number.Infinity,
  240. distance: Number.Infinity,
  241. };
  242. };
  243. /**
  244. * @private
  245. */
  246. I3SGeometry.prototype._generateGltf = function (
  247. nodesInScene,
  248. nodes,
  249. meshes,
  250. buffers,
  251. bufferViews,
  252. accessors
  253. ) {
  254. // Get the material definition
  255. let gltfMaterial = {
  256. pbrMetallicRoughness: {
  257. metallicFactor: 0.0,
  258. },
  259. doubleSided: true,
  260. name: "Material",
  261. };
  262. let isTextured = false;
  263. let materialDefinition;
  264. let texturePath = "";
  265. if (
  266. defined(this._parent._data.mesh) &&
  267. defined(this._layer._data.materialDefinitions)
  268. ) {
  269. const materialInfo = this._parent._data.mesh.material;
  270. const materialIndex = materialInfo.definition;
  271. if (
  272. materialIndex >= 0 &&
  273. materialIndex < this._layer._data.materialDefinitions.length
  274. ) {
  275. materialDefinition = this._layer._data.materialDefinitions[materialIndex];
  276. gltfMaterial = materialDefinition;
  277. if (
  278. defined(gltfMaterial.pbrMetallicRoughness) &&
  279. defined(gltfMaterial.pbrMetallicRoughness.baseColorTexture)
  280. ) {
  281. isTextured = true;
  282. gltfMaterial.pbrMetallicRoughness.baseColorTexture.index = 0;
  283. // Choose the JPG for the texture
  284. let textureName = "0";
  285. if (defined(this._layer._data.textureSetDefinitions)) {
  286. for (
  287. let defIndex = 0;
  288. defIndex < this._layer._data.textureSetDefinitions.length;
  289. defIndex++
  290. ) {
  291. const textureSetDefinition = this._layer._data
  292. .textureSetDefinitions[defIndex];
  293. for (
  294. let formatIndex = 0;
  295. formatIndex < textureSetDefinition.formats.length;
  296. formatIndex++
  297. ) {
  298. const textureFormat = textureSetDefinition.formats[formatIndex];
  299. if (textureFormat.format === "jpg") {
  300. textureName = textureFormat.name;
  301. break;
  302. }
  303. }
  304. }
  305. }
  306. if (
  307. defined(this._parent._data.mesh) &&
  308. this._parent._data.mesh.material.resource >= 0
  309. ) {
  310. texturePath = this._layer.resource.getDerivedResource({
  311. url: `nodes/${this._parent._data.mesh.material.resource}/textures/${textureName}`,
  312. }).url;
  313. }
  314. }
  315. }
  316. } else if (defined(this._parent._data.textureData)) {
  317. // No material definition, but if there's a texture reference, we can create a simple material using it (version 1.6 support)
  318. isTextured = true;
  319. texturePath = this._parent.resource.getDerivedResource({
  320. url: `${this._parent._data.textureData[0].href}`,
  321. }).url;
  322. gltfMaterial.pbrMetallicRoughness.baseColorTexture = { index: 0 };
  323. }
  324. let gltfTextures = [];
  325. let gltfImages = [];
  326. let gltfSamplers = [];
  327. if (isTextured) {
  328. gltfTextures = [
  329. {
  330. sampler: 0,
  331. source: 0,
  332. },
  333. ];
  334. gltfImages = [
  335. {
  336. uri: texturePath,
  337. },
  338. ];
  339. gltfSamplers = [
  340. {
  341. magFilter: 9729,
  342. minFilter: 9986,
  343. wrapS: 10497,
  344. wrapT: 10497,
  345. },
  346. ];
  347. }
  348. const gltfData = {
  349. scene: 0,
  350. scenes: [
  351. {
  352. nodes: nodesInScene,
  353. },
  354. ],
  355. nodes: nodes,
  356. meshes: meshes,
  357. buffers: buffers,
  358. bufferViews: bufferViews,
  359. accessors: accessors,
  360. materials: [gltfMaterial],
  361. textures: gltfTextures,
  362. images: gltfImages,
  363. samplers: gltfSamplers,
  364. asset: {
  365. version: "2.0",
  366. },
  367. };
  368. return gltfData;
  369. };
  370. export default I3SGeometry;