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 Feb 20, 2024
1 parent 7d05a0f commit 4a389d5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 54 deletions.
4 changes: 2 additions & 2 deletions examples/source_file_gpx_3d.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
this.updateMatrixWorld();
}

var waypointGeometry = new itowns.THREE.BoxGeometry(1, 1, 80);
var waypointMaterial = new itowns.THREE.MeshBasicMaterial({ color: 0xffffff });
const style = {
stroke: {
color: 'red',
Expand All @@ -70,6 +68,8 @@
color: 'white',
}
};
var waypointGeometry = new itowns.THREE.BoxGeometry(1, 1, 80);
var waypointMaterial = new itowns.THREE.MeshBasicMaterial({ color: 0xffffff });
// Listen for globe full initialisation event
view.addEventListener(itowns.GLOBE_VIEW_EVENTS.GLOBE_INITIALIZED, function () {
console.info('Globe initialized');
Expand Down
104 changes: 67 additions & 37 deletions src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let style;
const dim_ref = new THREE.Vector2();
const dim = new THREE.Vector2();
const normal = new THREE.Vector3();
const base = new THREE.Vector3();
const extrusion = new THREE.Vector3();
const baseCoord = new THREE.Vector3();
const topCoord = new THREE.Vector3();
const inverseScale = new THREE.Vector3();
const extent = new Extent('EPSG:4326', 0, 0, 0, 0);

Expand All @@ -42,15 +42,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 @@ -209,18 +200,28 @@ 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);
style.setContext(context);
const { base_altitude, color, radius } = style.point;
coord.z = 0;

if (!pointMaterialSize.includes(radius)) {
pointMaterialSize.push(radius);
coord.copy(localCoord)
.applyMatrix4(context.collection.matrixWorld);
if (coord.crs == 'EPSG:4978') {
// altitude convertion from geocentered to elevation (from ground)
coord.as('EPSG:4326', coord);
}

// populate vertices
base.copy(normal).multiplyScalar(base_altitude).add(coord).toArray(vertices, v);
// Calculate the new coordinates using the elevation shift (baseCoord)
baseCoord.copy(normal)
.multiplyScalar(base_altitude - coord.z).add(localCoord)
// and update the geometry buffer (vertices).
.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 @@ -251,7 +252,6 @@ function featureToLine(feature, options) {

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

const lineMaterialWidth = [];
context.setFeature(feature);
Expand Down Expand Up @@ -289,18 +289,28 @@ 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);
style.setContext(context);
const { base_altitude, color, width } = style.stroke;
coord.z = 0;

if (!lineMaterialWidth.includes(width)) {
lineMaterialWidth.push(width);
coord.copy(localCoord)
.applyMatrix4(context.collection.matrixWorld);
if (coord.crs == 'EPSG:4978') {
// altitude convertion from geocentered to elevation (from ground)
coord.as('EPSG:4326', coord);
}

// populate geometry buffers
base.copy(normal).multiplyScalar(base_altitude).add(coord).toArray(vertices, v);
// Calculate the new coordinates using the elevation shift (baseCoord)
baseCoord.copy(normal)
.multiplyScalar(base_altitude - coord.z).add(localCoord)
// and update the geometry buffer (vertices).
.toArray(vertices, v);

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

if (!lineMaterialWidth.includes(width)) {
lineMaterialWidth.push(width);
}
batchIds[j] = id;
}
featureId++;
Expand All @@ -310,9 +320,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 @@ -350,17 +362,26 @@ 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);
style.setContext(context);
const { base_altitude, color } = style.fill;
coord.z = 0;

// populate geometry buffers
base.copy(normal).multiplyScalar(base_altitude).add(coord).toArray(vertices, i);
batchIds[b] = id;
coord.copy(localCoord)
.applyMatrix4(context.collection.matrixWorld);
if (coord.crs == 'EPSG:4978') {
// altitude convertion from geocentered to elevation (from ground)
coord.as('EPSG:4326', coord);
}

// Calculate the new coordinates using the elevation shift (baseCoord)
baseCoord.copy(normal)
.multiplyScalar(base_altitude - coord.z).add(localCoord)
// and update the geometry buffer (vertices).
.toArray(vertices, i);

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

featureId++;

const geomVertices = vertices.slice(start * 3, end * 3);
Expand All @@ -379,7 +400,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 @@ -435,18 +455,28 @@ function featureToExtrudedPolygon(feature, options) {
normal.fromArray(feature.normals, i).multiply(inverseScale);
}

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

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

// populate base geometry buffers
base.copy(normal).multiplyScalar(base_altitude).add(coord).toArray(vertices, i);
coord.copy(localCoord)
.applyMatrix4(context.collection.matrixWorld);
if (coord.crs == 'EPSG:4978') {
// altitude convertion from geocentered to elevation (from ground)
coord.as('EPSG:4326', coord);
}

// Calculate the new base coordinates using the elevation shift (baseCoord)
baseCoord.copy(normal)
.multiplyScalar(base_altitude - coord.z).add(localCoord)
// and update the geometry buffer (vertices).
.toArray(vertices, i);
batchIds[b] = id;

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

// coloring base and top mesh
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 4a389d5

Please sign in to comment.