智慧水务管理系统 - 精河县供水工程综合管理平台

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