Skip to content

Commit

Permalink
fix(C3DTilesLayer): handle tileContent with several child containing …
Browse files Browse the repository at this point in the history
…C3DTFeature.

BREAKING CHANGE: C3DTFeature constructor changed from (tileId, batchId, groups, info, userData) to (tileId, batchId, groups, info, userData, object3d)
  • Loading branch information
valentinMachado authored and jailln committed Nov 13, 2023
1 parent de88737 commit 219e015
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 156 deletions.
4 changes: 2 additions & 2 deletions examples/widgets_3dtiles_style.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@
const material = new itowns.THREE.MeshBasicMaterial( {color: 0x00ff00, opacity: 0.5, transparent: true} );
const cube = new itowns.THREE.Mesh( geometry, material );
view.domElement.onclick = (event) => {
const intersects = view.pickObjectsAt(event, 0, [$3dTilesLayer]);
const intersects = view.pickObjectsAt(event, 5, [$3dTilesLayer]);
const c3DTileFeatureClicked = $3dTilesLayer.getC3DTileFeatureFromIntersectsArray(intersects);

if (c3DTileFeatureClicked) {
const worldBox3 = $3dTilesLayer.computeWorldBox3(c3DTileFeatureClicked);
const worldBox3 = c3DTileFeatureClicked.computeWorldBox3();
cube.scale.copy(worldBox3.max.clone().sub(worldBox3.min));
worldBox3.getCenter(cube.position);
cube.updateMatrixWorld();
Expand Down
63 changes: 57 additions & 6 deletions src/Core/3DTiles/C3DTFeature.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
// eslint-disable-next-line no-unused-vars
import { Object3D, Box3 } from 'three';

/**
* C3DTFeature is a feature of a 3DTiles
*
* @class C3DTFeature
* @param {number} tileId - tile id
* @param {number} tileId - tileId
* @param {number} batchId - batch id
* @param {Array<{start:number,count:number}>} groups - groups in geometry.attributes matching batchId
* @param {object} info - info in the batchTable
* @param {object} [userData={}] - some userData
* @param {object} [userData] - some userData
* @param {Object3D} object3d - object3d in which feature is present
* @property {number} tileId - tile id
* @property {Object3D} object3d - object3d in which feature is present
* @property {number} batchId - batch id
* @property {Array<{start:number,count:number}>} groups - groups in geometry.attributes matching batchId
* @property {object} info - info in the batchTable
* @property {object} [userData={}] - some userData
* @property {object} [userData] - some userData
*/
class C3DTFeature {
#info;
constructor(tileId, batchId, groups, info, userData = {}) {
/** @type {number} */
this.tileId = tileId;
constructor(tileId, batchId, groups, info, userData, object3d) {
if (!object3d) {
console.error('BREAKING CHANGE: C3DTFeature constructor changed from (tileId, batchId, groups, info, userData) to (tileId, batchId, groups, info, userData, object3d)');
}

/** @type {Object3D} */
this.object3d = object3d;

/** @type {number} */
this.batchId = batchId;
Expand All @@ -30,6 +39,48 @@ class C3DTFeature {

/** @type {object} */
this.#info = info;

/** @type {number} */
this.tileId = tileId;
}

/**
* Compute world box3 of this
*
* @param {Box3} target - target of the result
* @returns {Box3}
*/
computeWorldBox3(target = new Box3()) {
// reset
target.max.x = -Infinity;
target.max.y = -Infinity;
target.max.z = -Infinity;
target.min.x = Infinity;
target.min.y = Infinity;
target.min.z = Infinity;

this.groups.forEach((group) => {
const positionIndexStart = group.start * 3;
const positionIndexCount = (group.start + group.count) * 3;

for (let index = positionIndexStart; index < positionIndexCount; index += 3) {
const x = this.object3d.geometry.attributes.position.array[index];
const y = this.object3d.geometry.attributes.position.array[index + 1];
const z = this.object3d.geometry.attributes.position.array[index + 2];

target.max.x = Math.max(x, target.max.x);
target.max.y = Math.max(y, target.max.y);
target.max.z = Math.max(z, target.max.z);

target.min.x = Math.min(x, target.min.x);
target.min.y = Math.min(y, target.min.y);
target.min.z = Math.min(z, target.min.z);
}
});

target.applyMatrix4(this.object3d.matrixWorld);

return target;
}

/**
Expand Down
Loading

0 comments on commit 219e015

Please sign in to comment.