Skip to content

Commit

Permalink
fix(Feature2Mesh): fix proj on base alti
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Nov 24, 2023
1 parent fb2895d commit a5b639b
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 59 deletions.
23 changes: 13 additions & 10 deletions examples/source_file_gpx_3d.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
var layer = new itowns.ElevationLayer(config.id, config);
view.addLayer(layer);
}
itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json').then(addElevationLayerFromConfig);
// itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json').then(addElevationLayerFromConfig);
itowns.Fetcher.json('./layers/JSONLayers/WORLD_DTM.json').then(addElevationLayerFromConfig);

// update the waypoint
Expand All @@ -61,16 +61,19 @@

var waypointGeometry = new itowns.THREE.BoxGeometry(1, 1, 80);
var waypointMaterial = new itowns.THREE.MeshBasicMaterial({ color: 0xffffff });
// Listen for globe full initialisation event

const style = {
stroke: {
color: 'red',
width: 2,
},
point: {
color: 'white',
}
};
stroke: {
color: 'red',
width: 2,
base_alti: 500,
},
point: {
color: 'white',
}
};

// Listen for globe full initialisation event
view.addEventListener(itowns.GLOBE_VIEW_EVENTS.GLOBE_INITIALIZED, function () {
console.info('Globe initialized');
itowns.Fetcher.xml('https://raw.githubusercontent.com/iTowns/iTowns2-sample-data/master/ULTRA2009.gpx')
Expand Down
1 change: 1 addition & 0 deletions examples/source_stream_wfs_25d.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
style: {
fill: {
color: colorBuildings,
// base_altitude: altitudeLine,
base_altitude: altitudeBuildings,
extrusion_height: extrudeBuildings,
}
Expand Down
81 changes: 48 additions & 33 deletions src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ class FeatureMesh extends THREE.Group {
this.#collection.quaternion.copy(collection.quaternion);
this.#collection.position.copy(collection.position);

if (collection.crs == 'EPSG:4978') {
normal.copy(collection.center.geodesicNormal);
} else {
normal.set(0, 0, 1);
}

normal.multiplyScalar(collection.center.z);
this.#collection.position.sub(normal);

this.#collection.scale.copy(collection.scale);
this.#collection.updateMatrix();

Expand Down Expand Up @@ -208,18 +199,24 @@ function featureToPoint(feature, options) {
normal.fromArray(feature.normals, v).multiply(inverseScale);
}

coord.copy(context.setLocalCoordinatesFromArray(feature.vertices, v));
const localCoord = context.setLocalCoordinatesFromArray(feature.vertices, v);
userStyle.setContext(context);
const { base_altitude, color, radius } = userStyle.point;
coord.z = 0;

if (!pointMaterialSize.includes(radius)) {
pointMaterialSize.push(radius);
}
coord.copy(localCoord)
.applyMatrix4(context.collection.matrixWorld)
.as('EPSG:4326', coord);

// populate vertices
base.copy(normal).multiplyScalar(base_altitude).add(coord).toArray(vertices, v);
base.copy(normal)
.multiplyScalar(base_altitude - coord.z).add(localCoord)
.toArray(vertices, v);

toColor(color).multiplyScalar(255).toArray(colors, v);

if (!pointMaterialSize.includes(radius)) {
pointMaterialSize.push(radius);
}
batchIds[j] = id;
}
featureId++;
Expand Down Expand Up @@ -250,7 +247,7 @@ function featureToLine(feature, options) {

const vertices = new Float32Array(ptsIn.length);
const geom = new THREE.BufferGeometry();
geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
// geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));

const lineMaterialWidth = [];
context.setFeature(feature);
Expand Down Expand Up @@ -288,18 +285,24 @@ function featureToLine(feature, options) {
normal.fromArray(feature.normals, v).multiply(inverseScale);
}

coord.copy(context.setLocalCoordinatesFromArray(feature.vertices, v));
const localCoord = context.setLocalCoordinatesFromArray(feature.vertices, v);
userStyle.setContext(context);
const { base_altitude, color, width } = userStyle.stroke;
coord.z = 0;

coord.copy(localCoord)
.applyMatrix4(context.collection.matrixWorld)
.as('EPSG:4326', coord);

// populate vertices
base.copy(normal)
.multiplyScalar(base_altitude - coord.z).add(localCoord)
.toArray(vertices, v);

toColor(color).multiplyScalar(255).toArray(colors, v);

if (!lineMaterialWidth.includes(width)) {
lineMaterialWidth.push(width);
}

// populate geometry buffers
base.copy(normal).multiplyScalar(base_altitude).add(coord).toArray(vertices, v);
toColor(color).multiplyScalar(255).toArray(colors, v);
batchIds[j] = id;
}
featureId++;
Expand All @@ -309,9 +312,11 @@ function featureToLine(feature, options) {
// TODO CREATE material for each feature
console.warn('Too many differents stroke.width, only the first one will be used');
}
geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
geom.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
geom.setAttribute('batchId', new THREE.BufferAttribute(batchIds, 1));
geom.setIndex(new THREE.BufferAttribute(indices, 1));

return new THREE.LineSegments(geom, options.lineMaterial);
}

Expand Down Expand Up @@ -349,17 +354,22 @@ function featureToPolygon(feature, options) {
normal.fromArray(feature.normals, i).multiply(inverseScale);
}

coord.copy(context.setLocalCoordinatesFromArray(feature.vertices, i));
const localCoord = context.setLocalCoordinatesFromArray(feature.vertices, i);
userStyle.setContext(context);
const { base_altitude, color } = userStyle.fill;
coord.z = 0;

coord.copy(localCoord)
.applyMatrix4(context.collection.matrixWorld)
.as('EPSG:4326', coord);

// populate geometry buffers
base.copy(normal).multiplyScalar(base_altitude).add(coord).toArray(vertices, i);
batchIds[b] = id;
base.copy(normal)
.multiplyScalar(base_altitude - coord.z).add(localCoord)
.toArray(vertices, i);

toColor(color).multiplyScalar(255).toArray(colors, i);
batchIds[b] = id;
}

featureId++;

const geomVertices = vertices.slice(start * 3, end * 3);
Expand All @@ -378,7 +388,6 @@ function featureToPolygon(feature, options) {
geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
geom.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
geom.setAttribute('batchId', new THREE.BufferAttribute(batchIds, 1));

geom.setIndex(new THREE.BufferAttribute(getIntArrayFromSize(indices, vertices.length / 3), 1));

return new THREE.Mesh(geom, options.polygonMaterial);
Expand Down Expand Up @@ -434,18 +443,24 @@ function featureToExtrudedPolygon(feature, options) {
normal.fromArray(feature.normals, i).multiply(inverseScale);
}

coord.copy(context.setLocalCoordinatesFromArray(ptsIn, i));

const localCoord = context.setLocalCoordinatesFromArray(ptsIn, i);
userStyle.setContext(context);
const { base_altitude, extrusion_height, color } = userStyle.fill;
coord.z = 0;

coord.copy(localCoord)
.applyMatrix4(context.collection.matrixWorld)
.as('EPSG:4326', coord);

// populate base geometry buffers
base.copy(normal).multiplyScalar(base_altitude).add(coord).toArray(vertices, i);
base.copy(normal)
.multiplyScalar(base_altitude - coord.z).add(localCoord)
.toArray(vertices, i);
batchIds[b] = id;

// populate top geometry buffers
extrusion.copy(normal).multiplyScalar(extrusion_height).add(base).toArray(vertices, t);
extrusion.copy(normal)
.multiplyScalar(extrusion_height).add(base)
.toArray(vertices, t);
batchIds[b + totalVertices] = id;

// coloring base and top mesh
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,11 @@ export class FeatureCollection extends THREE.Object3D {
};
} else {
this.extent = options.buildExtent ? defaultExtent(options.forcedExtentCrs || this.crs) : undefined;
this.#setLocalSystem = (center) => {
this.#setLocalSystem = /* center.crs == FC crs */ (center) => {
// set local system center
// console.log(center);
center.as('EPSG:4326', this.center);
// console.log(this.center);

if (this.crs == 'EPSG:4978') {
// align Z axe to geodesic normal.
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const inv255 = 1 / 255;
const canvas = (typeof document !== 'undefined') ? document.createElement('canvas') : {};

function baseAltitudeDefault(properties, ctx) {
return ctx?.coordinates?.z || ctx?.collection?.center?.z || 0;
return ctx?.coordinates?.z || 0;
}

export function readExpression(property, ctx) {
Expand Down
1 change: 1 addition & 0 deletions src/Layer/FeatureGeometryLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class FeatureGeometryLayer extends GeometryLayer {
batchId: options.batchId,
});
super(id, options.object3d || new Group(), options);

this.isFeatureGeometryLayer = true;
this.accurate = options.accurate ?? true;
this.buildExtent = !this.accurate;
Expand Down
1 change: 0 additions & 1 deletion src/Layer/GeometryLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class GeometryLayer extends Layer {
// Remove this part when Object.assign(this, config) will be removed from Layer Constructor
const visible = config.visible;
delete config.visible;

super(id, config);

this.isGeometryLayer = true;
Expand Down
23 changes: 11 additions & 12 deletions test/unit/featuregeometrylayererror.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,23 @@ files.forEach((geojson, i) => {
layerProj4.whenReady
.then(() => {
tile.visible = true;
return layerProj4.update(context, layerProj4, tile)
.then(() => {
assert.equal(layerProj4.object3d.children.length, 1);
done();
});
return layerProj4.update(context, layerProj4, tile);
})
.then(() => {
assert.equal(layerProj4.object3d.children.length, 1);
done();
}).catch(done);
});

it('update without proj4', function (done) {
layerNoProj4.whenReady
.then(() => {
tile.visible = true;
context.layer = layerNoProj4;
return layerNoProj4.update(context, layerNoProj4, tile)
.then(() => {
assert.equal(layerNoProj4.object3d.children.length, 1);
done();
});
return layerNoProj4.update(context, layerNoProj4, tile);
})
.then(() => {
assert.equal(layerNoProj4.object3d.children.length, 1);
done();
}).catch(done);
});

Expand Down Expand Up @@ -116,7 +115,7 @@ files.forEach((geojson, i) => {

error /= (array.length / 3);

assert.ok(error < max_error);
assert.ok(error < max_error, `error (${error}) sup. to ${max_error}`);
done();
}).catch(done);
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/style.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Style from 'Core/Style';
import { FEATURE_TYPES } from 'Core/Feature';
import Style from 'Core/Style';
import assert from 'assert';
import Fetcher from 'Provider/Fetcher';
import { TextureLoader } from 'three';
Expand Down

0 comments on commit a5b639b

Please sign in to comment.