From 2c353955a3c9916d6eb01e93ae78f4ba4543abac Mon Sep 17 00:00:00 2001 From: gchoqueux Date: Wed, 27 Sep 2023 11:39:09 +0200 Subject: [PATCH] refactor(feature2Mesh): context using REVIEW: this commit will be squatched !! --- src/Converter/Feature2Mesh.js | 33 +++++++++++++-------------------- src/Core/Style.js | 2 +- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/Converter/Feature2Mesh.js b/src/Converter/Feature2Mesh.js index a6e789dd5e..ec0c4c9ef5 100644 --- a/src/Converter/Feature2Mesh.js +++ b/src/Converter/Feature2Mesh.js @@ -14,7 +14,6 @@ class FeatureContext { #worldCoord = new Coordinates('EPSG:4326', 0, 0, 0); #localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0); #geometry = {}; - #collection = {}; constructor() { this.globals = {}; @@ -25,7 +24,7 @@ class FeatureContext { } setCollection(c) { - this.#collection = c; + this.collection = c; this.#localCoordinates.setCrs(c.crs); } @@ -38,14 +37,10 @@ class FeatureContext { return this.#geometry.properties; } - get localCoordinates() { - return this.#localCoordinates; - } - get coordinates() { if (this.#worldCoord.isLocal) { this.#worldCoord.isLocal = false; - this.#worldCoord.copy(this.#localCoordinates).applyMatrix4(this.#collection.matrixWorld); + this.#worldCoord.copy(this.#localCoordinates).applyMatrix4(this.collection.matrixWorld); if (this.#localCoordinates.crs == 'EPSG:4978') { return this.#worldCoord.as('EPSG:4326', this.#worldCoord); } @@ -77,10 +72,15 @@ class FeatureMesh extends THREE.Group { #place = new THREE.Group(); constructor(meshes, collection) { super(); + this.meshes = new THREE.Group().add(...meshes); + this.#collection = new THREE.Group().add(this.meshes); this.#collection.quaternion.copy(collection.quaternion); this.#collection.position.copy(collection.position); + normal.copy(collection.center.geodesicNormal).multiplyScalar(collection.center.z); + this.#collection.position.sub(normal); + this.#collection.scale.copy(collection.scale); this.#collection.updateMatrix(); @@ -218,11 +218,10 @@ function featureToPoint(feature, options) { const colors = new Uint8Array(ptsIn.length); const batchIds = new Uint32Array(ptsIn.length); const batchId = options.batchId || ((p, id) => id); - context.setCollection(options.collection); let featureId = 0; const vertices = new Float32Array(ptsIn); - inverseScale.setFromMatrixScale(options.collection.matrixWorldInverse); + inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse); normal.set(0, 0, 1).multiply(inverseScale); context.globals = { point: true }; @@ -265,7 +264,6 @@ function featureToLine(feature, options) { const ptsIn = feature.vertices; const colors = new Uint8Array(ptsIn.length); const count = ptsIn.length / 3; - context.setCollection(options.collection); const batchIds = new Uint32Array(count); const batchId = options.batchId || ((p, id) => id); @@ -283,7 +281,7 @@ function featureToLine(feature, options) { const indices = getIntArrayFromSize(countIndices, count); let i = 0; - inverseScale.setFromMatrixScale(options.collection.matrixWorldInverse); + inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse); normal.set(0, 0, 1).multiply(inverseScale); // Multi line case for (const geometry of feature.geometries) { @@ -339,10 +337,9 @@ function featureToPolygon(feature, options) { const batchIds = new Uint32Array(vertices.length / 3); const batchId = options.batchId || ((p, id) => id); - context.setCollection(options.collection); context.globals = { fill: true }; - inverseScale.setFromMatrixScale(options.collection.matrixWorldInverse); + inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse); normal.set(0, 0, 1).multiply(inverseScale); let featureId = 0; @@ -425,14 +422,13 @@ function featureToExtrudedPolygon(feature, options) { const batchIds = new Uint32Array(vertices.length / 3); const batchId = options.batchId || ((p, id) => id); - context.setCollection(options.collection); let featureId = 0; context.globals = { fill: true }; - inverseScale.setFromMatrixScale(options.collection.matrixWorldInverse); + inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse); normal.set(0, 0, 1).multiply(inverseScale); - coord.setCrs(options.collection.crs); + coord.setCrs(context.collection.crs); for (const geometry of feature.geometries) { context.setGeometry(geometry); @@ -609,7 +605,6 @@ function featureToMesh(feature, options) { mesh.material.color = new THREE.Color(0xffffff); } mesh.feature = feature; - mesh.position.z = -options.GlobalZTrans; if (options.layer) { mesh.layer = options.layer; @@ -669,14 +664,12 @@ export default { options.layer = this; } - options.collection = collection; + context.setCollection(collection); const features = collection.features; if (!features || features.length == 0) { return; } - options.GlobalZTrans = collection.center.z; - const meshes = features.map(feature => featureToMesh(feature, options)); const featureNode = new FeatureMesh(meshes, collection); diff --git a/src/Core/Style.js b/src/Core/Style.js index 30df597c9d..3fe3ec9c68 100644 --- a/src/Core/Style.js +++ b/src/Core/Style.js @@ -14,7 +14,7 @@ const canvas = (typeof document !== 'undefined') ? document.createElement('canva const style_properties = {}; function base_altitudeDefault(properties, ctx) { - return ctx?.coordinates?.z || 0; + return ctx?.coordinates?.z || ctx?.collection?.center?.z || 0; } function mapPropertiesFromContext(mainKey, from, to, context) {