react-three-map 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # react-three-map
2
2
 
3
+ ## 0.8.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 58c42d9: - Improve accuracy at long distances for `coordsToVector3`.
8
+ - Improve translation accuracy for `NearCoordinates`, but scale is still ignored.
9
+ - Update `earthRadius` from `6378137` to `6371008.8` to match [Mapbox](https://github.com/maplibre/maplibre-gl-js/blob/8ea76118210dd18fa52fdb83f2cbdd1229807346/src/geo/lng_lat.ts#L8) and [Maplibre](https://github.com/maplibre/maplibre-gl-js/blob/main/src/geo/lng_lat.ts#L8).
10
+
3
11
  ## 0.8.1
4
12
 
5
13
  ### Patch Changes
package/README.md CHANGED
@@ -11,10 +11,10 @@
11
11
 
12
12
  Until now you had:
13
13
 
14
- | imperative | declarative (react) |
15
- | --------------- | ----------------- |
16
- | Maplibre/Mapbox | react-map-gl |
17
- | THREE.js | react-three-fiber |
14
+ | imperative | declarative (react) |
15
+ | --------------- | ------------------- |
16
+ | Maplibre/Mapbox | react-map-gl |
17
+ | THREE.js | react-three-fiber |
18
18
 
19
19
  Now with `react-three-map`, you can use them together :fist_right::star::fist_left:.
20
20
 
@@ -22,6 +22,22 @@ Now with `react-three-map`, you can use them together :fist_right::star::fist_le
22
22
  npm install react-three-map
23
23
  ```
24
24
 
25
+ - [React Three Map](#react-three-map)
26
+ - [:book: Examples](#book-examples)
27
+ - [:mag: What does it look like?](#mag-what-does-it-look-like)
28
+ - [:thinking: Why we build this?](#thinking-why-we-build-this)
29
+ - [:gear: API](#gear-api)
30
+ - [Canvas](#canvas)
31
+ - [Render Props](#render-props)
32
+ - [Render Props removed from `@react-three/fiber`](#render-props-removed-from-react-threefiber)
33
+ - [Coordinates](#coordinates)
34
+ - [NearCoordinates](#nearcoordinates)
35
+ - [useMap](#usemap)
36
+ - [coordsToVector3](#coordstovector3)
37
+ - [vector3ToCoords](#vector3tocoords)
38
+ - [:tada: Made with `react-three-map`](#tada-made-with-react-three-map)
39
+
40
+
25
41
  ## :book: Examples
26
42
 
27
43
  Check out our examples [here](https://rodrigohamuy.github.io/react-three-map) (powered by [Ladle](https://ladle.dev/)).
@@ -120,13 +136,13 @@ It shares most of the props from R3F `<Canvas>`, so you can check them directly
120
136
 
121
137
  #### Render Props
122
138
 
123
- | Prop | Description | Default |
124
- | --------- | ------------------------------------------------ | ---------- |
125
- | latitude | The latitude coordinate where to add the scene. | |
126
- | longitude | The longitude coordinate where to add the scene. | |
127
- | altitude | The altitude coordinate where to add the scene. | `0` |
128
- | frameloop | Render mode: `"always"`, `"demand"`. | `"always"` |
129
- | overlay | Render on a separated canvas. | `false` |
139
+ | Prop | Description | Default |
140
+ | --------- | ------------------------------------------------ | ---------- |
141
+ | latitude | The latitude coordinate where to add the scene. | |
142
+ | longitude | The longitude coordinate where to add the scene. | |
143
+ | altitude | The altitude coordinate where to add the scene. | `0` |
144
+ | frameloop | Render mode: `"always"`, `"demand"`. | `"always"` |
145
+ | overlay | Render on a separated canvas. | `false` |
130
146
 
131
147
  **About `overlay`**
132
148
 
@@ -172,21 +188,21 @@ import { Canvas, Coordinates } from 'react-three-map'
172
188
  </Canvas>
173
189
  ```
174
190
 
175
- | Props | Description | Default |
176
- | --------- | ------------------------------------------------ | ---------- |
177
- | latitude | The latitude coordinate where to add the scene. | |
178
- | longitude | The longitude coordinate where to add the scene. | |
179
- | altitude | The altitude coordinate where to add the scene. | `0` |
191
+ | Props | Description | Default |
192
+ | --------- | ------------------------------------------------ | ------- |
193
+ | latitude | The latitude coordinate where to add the scene. | |
194
+ | longitude | The longitude coordinate where to add the scene. | |
195
+ | altitude | The altitude coordinate where to add the scene. | `0` |
180
196
 
181
197
  ### NearCoordinates
182
198
 
183
199
  [![](https://img.shields.io/badge/-demo-%23ff69b4)](https://rodrigohamuy.github.io/react-three-map/?story=multi-coordinates--default)
184
200
 
185
- Same as `Coordinates`, but with an error margin that increases the further you are from the origin.
201
+ Same as `Coordinates`, but scale is ignored in exchange of being able to be rendered under the same scene.
186
202
 
187
- Recommended to use at city level distances, but margin errors will be noticeable at country level distances.
203
+ Works well at city level distances, but since scale remains unchanged, is not recommended at country level distances.
188
204
 
189
- Check the story to see the difference between the two.
205
+ Check the story to see the difference between the two or check #102 for more info.
190
206
 
191
207
  ### useMap
192
208
 
@@ -208,7 +224,7 @@ const Component = () => {
208
224
 
209
225
  This utility function converts geographic coordinates into a `Vector3Tuple`, which represents a 3D vector in meters.
210
226
 
211
- Similar to `NearCoordinates` it has a relatively good precision at city distances, but is not recommended if your distances are too big.
227
+ Similar to `NearCoordinates`, remember that this only updates positions (translation) but that scale is not taken into account, which has an important factor at very long distances (country level).
212
228
 
213
229
 
214
230
  | Parameter | Description |
@@ -224,7 +240,9 @@ Returns a `Vector3Tuple` representing the 3D position of the point relative to t
224
240
 
225
241
  This utility function converts a `Vector3Tuple`, which represents a 3D vector in meters, back into geographic coordinates.
226
242
 
227
- It is the inverse of `coordsToVector3` and maintains the same level of precision. It is not recommended for use with very large distances.
243
+ It is the inverse of `coordsToVector3` but it does not have a good level of precision at long distances since we haven't reverse engineered #102 fix yet.
244
+
245
+ Recommended to use at city level distances, but margin errors will be noticeable at country level distances.
228
246
 
229
247
  | Parameter | Description |
230
248
  | ------------------------ | --------------------------------------------------------------- |
@@ -232,3 +250,9 @@ It is the inverse of `coordsToVector3` and maintains the same level of precision
232
250
  | `origin: Coords` | The geographic coordinates used as the origin for calculations. |
233
251
 
234
252
  Returns a `Coords` object representing the geographic coordinates of the point relative to the origin.
253
+
254
+ ## :tada: Made with `react-three-map`
255
+
256
+ [![studio carto](./made-with/studio-carto-urban-project.webp)](https://x.com/lonjon_thomas/status/1767151007983685979?s=20)
257
+
258
+ [![post](https://img.shields.io/badge/post-black?style=for-the-badge&logo=x)](https://x.com/lonjon_thomas/status/1767151007983685979?s=20) [![page](https://img.shields.io/badge/page-black?style=for-the-badge)](https://studio-carto-urban-project.netlify.app/)
package/dist/cjs/main.js CHANGED
@@ -147,14 +147,33 @@ function RenderAtCoords({ r3m, origin }) {
147
147
  }, [set, r3m]);
148
148
  return /* @__PURE__ */ jsxRuntime.jsx("perspectiveCamera", { ref: cameraRef });
149
149
  }
150
- const earthRadius = 6378137;
150
+ const earthRadius = 63710088e-1;
151
+ const mercatorScaleLookup = {};
152
+ function getMercatorScale(lat) {
153
+ const index = Math.round(lat * 1e3);
154
+ if (mercatorScaleLookup[index] === void 0) {
155
+ mercatorScaleLookup[index] = 1 / Math.cos(lat * THREE.MathUtils.DEG2RAD);
156
+ }
157
+ return mercatorScaleLookup[index];
158
+ }
159
+ function averageMercatorScale(originLat, pointLat, steps = 10) {
160
+ let totalScale = 0;
161
+ const latStep = (pointLat - originLat) / steps;
162
+ for (let i = 0; i <= steps; i++) {
163
+ const lat = originLat + latStep * i;
164
+ totalScale += getMercatorScale(lat);
165
+ }
166
+ return totalScale / (steps + 1);
167
+ }
151
168
  function coordsToVector3(point, origin) {
152
169
  const latitudeDiff = (point.latitude - origin.latitude) * THREE.MathUtils.DEG2RAD;
153
170
  const longitudeDiff = (point.longitude - origin.longitude) * THREE.MathUtils.DEG2RAD;
154
171
  const altitudeDiff = (point.altitude || 0) - (origin.altitude || 0);
155
172
  const x = longitudeDiff * earthRadius * Math.cos(origin.latitude * THREE.MathUtils.DEG2RAD);
156
173
  const y = altitudeDiff;
157
- const z = -latitudeDiff * earthRadius;
174
+ const steps = Math.ceil(Math.abs(point.latitude - origin.latitude)) * 100 + 1;
175
+ const avgScale = averageMercatorScale(origin.latitude, point.latitude, steps);
176
+ const z = -latitudeDiff * earthRadius / getMercatorScale(origin.latitude) * avgScale;
158
177
  return [x, y, z];
159
178
  }
160
179
  function useCoords() {
@@ -557,6 +576,7 @@ const useMap = useMap$1;
557
576
  exports.Canvas = Canvas;
558
577
  exports.Coordinates = Coordinates;
559
578
  exports.NearCoordinates = NearCoordinates;
579
+ exports.averageMercatorScale = averageMercatorScale;
560
580
  exports.coordsToVector3 = coordsToVector3;
561
581
  exports.useMap = useMap;
562
582
  exports.vector3ToCoords = vector3ToCoords;
package/dist/es/main.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { useMap as useMap$1 } from "./main2.mjs";
2
2
  import { Canvas } from "./main3.mjs";
3
3
  import { Coordinates } from "./main4.mjs";
4
- import { coordsToVector3 } from "./main5.mjs";
4
+ import { averageMercatorScale, coordsToVector3 } from "./main5.mjs";
5
5
  import { NearCoordinates } from "./main6.mjs";
6
6
  import { vector3ToCoords } from "./main7.mjs";
7
7
  const useMap = useMap$1;
@@ -9,6 +9,7 @@ export {
9
9
  Canvas,
10
10
  Coordinates,
11
11
  NearCoordinates,
12
+ averageMercatorScale,
12
13
  coordsToVector3,
13
14
  useMap,
14
15
  vector3ToCoords
@@ -1,4 +1,4 @@
1
- const earthRadius = 6378137;
1
+ const earthRadius = 63710088e-1;
2
2
  export {
3
3
  earthRadius
4
4
  };
@@ -1 +1 @@
1
- {"version":3,"file":"main14.mjs","sources":["../../src/core/earth-radius.ts"],"sourcesContent":["export const earthRadius = 6378137;\n"],"names":[],"mappings":"AAAO,MAAM,cAAc;"}
1
+ {"version":3,"file":"main14.mjs","sources":["../../src/core/earth-radius.ts"],"sourcesContent":["export const earthRadius = 6371008.8;\n"],"names":[],"mappings":"AAAO,MAAM,cAAc;"}
package/dist/es/main5.mjs CHANGED
@@ -1,15 +1,35 @@
1
1
  import { MathUtils } from "three";
2
2
  import { earthRadius } from "./main14.mjs";
3
+ const mercatorScaleLookup = {};
4
+ function getMercatorScale(lat) {
5
+ const index = Math.round(lat * 1e3);
6
+ if (mercatorScaleLookup[index] === void 0) {
7
+ mercatorScaleLookup[index] = 1 / Math.cos(lat * MathUtils.DEG2RAD);
8
+ }
9
+ return mercatorScaleLookup[index];
10
+ }
11
+ function averageMercatorScale(originLat, pointLat, steps = 10) {
12
+ let totalScale = 0;
13
+ const latStep = (pointLat - originLat) / steps;
14
+ for (let i = 0; i <= steps; i++) {
15
+ const lat = originLat + latStep * i;
16
+ totalScale += getMercatorScale(lat);
17
+ }
18
+ return totalScale / (steps + 1);
19
+ }
3
20
  function coordsToVector3(point, origin) {
4
21
  const latitudeDiff = (point.latitude - origin.latitude) * MathUtils.DEG2RAD;
5
22
  const longitudeDiff = (point.longitude - origin.longitude) * MathUtils.DEG2RAD;
6
23
  const altitudeDiff = (point.altitude || 0) - (origin.altitude || 0);
7
24
  const x = longitudeDiff * earthRadius * Math.cos(origin.latitude * MathUtils.DEG2RAD);
8
25
  const y = altitudeDiff;
9
- const z = -latitudeDiff * earthRadius;
26
+ const steps = Math.ceil(Math.abs(point.latitude - origin.latitude)) * 100 + 1;
27
+ const avgScale = averageMercatorScale(origin.latitude, point.latitude, steps);
28
+ const z = -latitudeDiff * earthRadius / getMercatorScale(origin.latitude) * avgScale;
10
29
  return [x, y, z];
11
30
  }
12
31
  export {
32
+ averageMercatorScale,
13
33
  coordsToVector3
14
34
  };
15
35
  //# sourceMappingURL=main5.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"main5.mjs","sources":["../../src/api/coords-to-vector-3.ts"],"sourcesContent":["import { MathUtils, Vector3Tuple } from 'three';\nimport { Coords } from './coords';\nimport { earthRadius } from \"../core/earth-radius\";\n\nexport function coordsToVector3(point: Coords, origin: Coords): Vector3Tuple {\n const latitudeDiff = (point.latitude - origin.latitude) * MathUtils.DEG2RAD;\n const longitudeDiff = (point.longitude - origin.longitude) * MathUtils.DEG2RAD;\n const altitudeDiff = (point.altitude || 0) - (origin.altitude || 0);\n\n const x = longitudeDiff * earthRadius * Math.cos(origin.latitude * MathUtils.DEG2RAD);\n const y = altitudeDiff;\n const z = -latitudeDiff * earthRadius;\n\n return [x, y, z] as Vector3Tuple;\n}\n"],"names":[],"mappings":";;AAIgB,SAAA,gBAAgB,OAAe,QAA8B;AAC3E,QAAM,gBAAgB,MAAM,WAAW,OAAO,YAAY,UAAU;AACpE,QAAM,iBAAiB,MAAM,YAAY,OAAO,aAAa,UAAU;AACvE,QAAM,gBAAgB,MAAM,YAAY,MAAM,OAAO,YAAY;AAE3D,QAAA,IAAI,gBAAgB,cAAc,KAAK,IAAI,OAAO,WAAW,UAAU,OAAO;AACpF,QAAM,IAAI;AACJ,QAAA,IAAI,CAAC,eAAe;AAEnB,SAAA,CAAC,GAAG,GAAG,CAAC;AACjB;"}
1
+ {"version":3,"file":"main5.mjs","sources":["../../src/api/coords-to-vector-3.ts"],"sourcesContent":["import { MathUtils, Vector3Tuple } from 'three';\nimport { Coords } from './coords';\nimport { earthRadius } from \"../core/earth-radius\";\n\n\nconst mercatorScaleLookup: { [key: number]: number } = {};\n\nfunction getMercatorScale(lat: number): number {\n const index = Math.round(lat * 1000);\n if (mercatorScaleLookup[index] === undefined) {\n mercatorScaleLookup[index] = 1 / Math.cos(lat * MathUtils.DEG2RAD);\n }\n return mercatorScaleLookup[index];\n}\n\nexport function averageMercatorScale(originLat: number, pointLat: number, steps = 10): number {\n let totalScale = 0;\n const latStep = (pointLat - originLat) / steps;\n for (let i = 0; i <= steps; i++) {\n const lat = originLat + latStep * i;\n totalScale += getMercatorScale(lat);\n }\n return totalScale / (steps + 1);\n}\n\nexport function coordsToVector3(point: Coords, origin: Coords): Vector3Tuple {\n const latitudeDiff = (point.latitude - origin.latitude) * MathUtils.DEG2RAD;\n const longitudeDiff = (point.longitude - origin.longitude) * MathUtils.DEG2RAD;\n const altitudeDiff = (point.altitude || 0) - (origin.altitude || 0);\n\n const x = longitudeDiff * earthRadius * Math.cos(origin.latitude * MathUtils.DEG2RAD);\n const y = altitudeDiff;\n\n // dynamic step size based on latitude difference. calculate the mercator unit scale at origin\n // and the scale average along the line to the point for better accuracy far from origin\n const steps = Math.ceil(Math.abs(point.latitude - origin.latitude)) * 100 + 1;\n const avgScale = averageMercatorScale(origin.latitude, point.latitude, steps);\n\n const z = ((-latitudeDiff * earthRadius) / getMercatorScale(origin.latitude)) * avgScale;\n return [x, y, z] as Vector3Tuple;\n}"],"names":[],"mappings":";;AAKA,MAAM,sBAAiD,CAAA;AAEvD,SAAS,iBAAiB,KAAqB;AAC7C,QAAM,QAAQ,KAAK,MAAM,MAAM,GAAI;AAC/B,MAAA,oBAAoB,KAAK,MAAM,QAAW;AAC5C,wBAAoB,KAAK,IAAI,IAAI,KAAK,IAAI,MAAM,UAAU,OAAO;AAAA,EACnE;AACA,SAAO,oBAAoB,KAAK;AAClC;AAEO,SAAS,qBAAqB,WAAmB,UAAkB,QAAQ,IAAY;AAC5F,MAAI,aAAa;AACX,QAAA,WAAW,WAAW,aAAa;AACzC,WAAS,IAAI,GAAG,KAAK,OAAO,KAAK;AACzB,UAAA,MAAM,YAAY,UAAU;AAClC,kBAAc,iBAAiB,GAAG;AAAA,EACpC;AACA,SAAO,cAAc,QAAQ;AAC/B;AAEgB,SAAA,gBAAgB,OAAe,QAA8B;AAC3E,QAAM,gBAAgB,MAAM,WAAW,OAAO,YAAY,UAAU;AACpE,QAAM,iBAAiB,MAAM,YAAY,OAAO,aAAa,UAAU;AACvE,QAAM,gBAAgB,MAAM,YAAY,MAAM,OAAO,YAAY;AAE3D,QAAA,IAAI,gBAAgB,cAAc,KAAK,IAAI,OAAO,WAAW,UAAU,OAAO;AACpF,QAAM,IAAI;AAIJ,QAAA,QAAQ,KAAK,KAAK,KAAK,IAAI,MAAM,WAAW,OAAO,QAAQ,CAAC,IAAI,MAAM;AAC5E,QAAM,WAAW,qBAAqB,OAAO,UAAU,MAAM,UAAU,KAAK;AAE5E,QAAM,IAAM,CAAC,eAAe,cAAe,iBAAiB,OAAO,QAAQ,IAAK;AACzE,SAAA,CAAC,GAAG,GAAG,CAAC;AACjB;"}
@@ -147,14 +147,33 @@ function RenderAtCoords({ r3m, origin }) {
147
147
  }, [set, r3m]);
148
148
  return /* @__PURE__ */ jsxRuntime.jsx("perspectiveCamera", { ref: cameraRef });
149
149
  }
150
- const earthRadius = 6378137;
150
+ const earthRadius = 63710088e-1;
151
+ const mercatorScaleLookup = {};
152
+ function getMercatorScale(lat) {
153
+ const index = Math.round(lat * 1e3);
154
+ if (mercatorScaleLookup[index] === void 0) {
155
+ mercatorScaleLookup[index] = 1 / Math.cos(lat * THREE.MathUtils.DEG2RAD);
156
+ }
157
+ return mercatorScaleLookup[index];
158
+ }
159
+ function averageMercatorScale(originLat, pointLat, steps = 10) {
160
+ let totalScale = 0;
161
+ const latStep = (pointLat - originLat) / steps;
162
+ for (let i = 0; i <= steps; i++) {
163
+ const lat = originLat + latStep * i;
164
+ totalScale += getMercatorScale(lat);
165
+ }
166
+ return totalScale / (steps + 1);
167
+ }
151
168
  function coordsToVector3(point, origin) {
152
169
  const latitudeDiff = (point.latitude - origin.latitude) * THREE.MathUtils.DEG2RAD;
153
170
  const longitudeDiff = (point.longitude - origin.longitude) * THREE.MathUtils.DEG2RAD;
154
171
  const altitudeDiff = (point.altitude || 0) - (origin.altitude || 0);
155
172
  const x = longitudeDiff * earthRadius * Math.cos(origin.latitude * THREE.MathUtils.DEG2RAD);
156
173
  const y = altitudeDiff;
157
- const z = -latitudeDiff * earthRadius;
174
+ const steps = Math.ceil(Math.abs(point.latitude - origin.latitude)) * 100 + 1;
175
+ const avgScale = averageMercatorScale(origin.latitude, point.latitude, steps);
176
+ const z = -latitudeDiff * earthRadius / getMercatorScale(origin.latitude) * avgScale;
158
177
  return [x, y, z];
159
178
  }
160
179
  function useCoords() {
@@ -557,6 +576,7 @@ const useMap = useMap$1;
557
576
  exports.Canvas = Canvas;
558
577
  exports.Coordinates = Coordinates;
559
578
  exports.NearCoordinates = NearCoordinates;
579
+ exports.averageMercatorScale = averageMercatorScale;
560
580
  exports.coordsToVector3 = coordsToVector3;
561
581
  exports.useMap = useMap;
562
582
  exports.vector3ToCoords = vector3ToCoords;
@@ -1,7 +1,7 @@
1
1
  import { useMap as useMap$1 } from "./main2.mjs";
2
2
  import { Canvas } from "./main3.mjs";
3
3
  import { Coordinates } from "./main4.mjs";
4
- import { coordsToVector3 } from "./main5.mjs";
4
+ import { averageMercatorScale, coordsToVector3 } from "./main5.mjs";
5
5
  import { NearCoordinates } from "./main6.mjs";
6
6
  import { vector3ToCoords } from "./main7.mjs";
7
7
  const useMap = useMap$1;
@@ -9,6 +9,7 @@ export {
9
9
  Canvas,
10
10
  Coordinates,
11
11
  NearCoordinates,
12
+ averageMercatorScale,
12
13
  coordsToVector3,
13
14
  useMap,
14
15
  vector3ToCoords
@@ -1,4 +1,4 @@
1
- const earthRadius = 6378137;
1
+ const earthRadius = 63710088e-1;
2
2
  export {
3
3
  earthRadius
4
4
  };
@@ -1 +1 @@
1
- {"version":3,"file":"main14.mjs","sources":["../../../src/core/earth-radius.ts"],"sourcesContent":["export const earthRadius = 6378137;\n"],"names":[],"mappings":"AAAO,MAAM,cAAc;"}
1
+ {"version":3,"file":"main14.mjs","sources":["../../../src/core/earth-radius.ts"],"sourcesContent":["export const earthRadius = 6371008.8;\n"],"names":[],"mappings":"AAAO,MAAM,cAAc;"}
@@ -1,15 +1,35 @@
1
1
  import { MathUtils } from "three";
2
2
  import { earthRadius } from "./main14.mjs";
3
+ const mercatorScaleLookup = {};
4
+ function getMercatorScale(lat) {
5
+ const index = Math.round(lat * 1e3);
6
+ if (mercatorScaleLookup[index] === void 0) {
7
+ mercatorScaleLookup[index] = 1 / Math.cos(lat * MathUtils.DEG2RAD);
8
+ }
9
+ return mercatorScaleLookup[index];
10
+ }
11
+ function averageMercatorScale(originLat, pointLat, steps = 10) {
12
+ let totalScale = 0;
13
+ const latStep = (pointLat - originLat) / steps;
14
+ for (let i = 0; i <= steps; i++) {
15
+ const lat = originLat + latStep * i;
16
+ totalScale += getMercatorScale(lat);
17
+ }
18
+ return totalScale / (steps + 1);
19
+ }
3
20
  function coordsToVector3(point, origin) {
4
21
  const latitudeDiff = (point.latitude - origin.latitude) * MathUtils.DEG2RAD;
5
22
  const longitudeDiff = (point.longitude - origin.longitude) * MathUtils.DEG2RAD;
6
23
  const altitudeDiff = (point.altitude || 0) - (origin.altitude || 0);
7
24
  const x = longitudeDiff * earthRadius * Math.cos(origin.latitude * MathUtils.DEG2RAD);
8
25
  const y = altitudeDiff;
9
- const z = -latitudeDiff * earthRadius;
26
+ const steps = Math.ceil(Math.abs(point.latitude - origin.latitude)) * 100 + 1;
27
+ const avgScale = averageMercatorScale(origin.latitude, point.latitude, steps);
28
+ const z = -latitudeDiff * earthRadius / getMercatorScale(origin.latitude) * avgScale;
10
29
  return [x, y, z];
11
30
  }
12
31
  export {
32
+ averageMercatorScale,
13
33
  coordsToVector3
14
34
  };
15
35
  //# sourceMappingURL=main5.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"main5.mjs","sources":["../../../src/api/coords-to-vector-3.ts"],"sourcesContent":["import { MathUtils, Vector3Tuple } from 'three';\nimport { Coords } from './coords';\nimport { earthRadius } from \"../core/earth-radius\";\n\nexport function coordsToVector3(point: Coords, origin: Coords): Vector3Tuple {\n const latitudeDiff = (point.latitude - origin.latitude) * MathUtils.DEG2RAD;\n const longitudeDiff = (point.longitude - origin.longitude) * MathUtils.DEG2RAD;\n const altitudeDiff = (point.altitude || 0) - (origin.altitude || 0);\n\n const x = longitudeDiff * earthRadius * Math.cos(origin.latitude * MathUtils.DEG2RAD);\n const y = altitudeDiff;\n const z = -latitudeDiff * earthRadius;\n\n return [x, y, z] as Vector3Tuple;\n}\n"],"names":[],"mappings":";;AAIgB,SAAA,gBAAgB,OAAe,QAA8B;AAC3E,QAAM,gBAAgB,MAAM,WAAW,OAAO,YAAY,UAAU;AACpE,QAAM,iBAAiB,MAAM,YAAY,OAAO,aAAa,UAAU;AACvE,QAAM,gBAAgB,MAAM,YAAY,MAAM,OAAO,YAAY;AAE3D,QAAA,IAAI,gBAAgB,cAAc,KAAK,IAAI,OAAO,WAAW,UAAU,OAAO;AACpF,QAAM,IAAI;AACJ,QAAA,IAAI,CAAC,eAAe;AAEnB,SAAA,CAAC,GAAG,GAAG,CAAC;AACjB;"}
1
+ {"version":3,"file":"main5.mjs","sources":["../../../src/api/coords-to-vector-3.ts"],"sourcesContent":["import { MathUtils, Vector3Tuple } from 'three';\nimport { Coords } from './coords';\nimport { earthRadius } from \"../core/earth-radius\";\n\n\nconst mercatorScaleLookup: { [key: number]: number } = {};\n\nfunction getMercatorScale(lat: number): number {\n const index = Math.round(lat * 1000);\n if (mercatorScaleLookup[index] === undefined) {\n mercatorScaleLookup[index] = 1 / Math.cos(lat * MathUtils.DEG2RAD);\n }\n return mercatorScaleLookup[index];\n}\n\nexport function averageMercatorScale(originLat: number, pointLat: number, steps = 10): number {\n let totalScale = 0;\n const latStep = (pointLat - originLat) / steps;\n for (let i = 0; i <= steps; i++) {\n const lat = originLat + latStep * i;\n totalScale += getMercatorScale(lat);\n }\n return totalScale / (steps + 1);\n}\n\nexport function coordsToVector3(point: Coords, origin: Coords): Vector3Tuple {\n const latitudeDiff = (point.latitude - origin.latitude) * MathUtils.DEG2RAD;\n const longitudeDiff = (point.longitude - origin.longitude) * MathUtils.DEG2RAD;\n const altitudeDiff = (point.altitude || 0) - (origin.altitude || 0);\n\n const x = longitudeDiff * earthRadius * Math.cos(origin.latitude * MathUtils.DEG2RAD);\n const y = altitudeDiff;\n\n // dynamic step size based on latitude difference. calculate the mercator unit scale at origin\n // and the scale average along the line to the point for better accuracy far from origin\n const steps = Math.ceil(Math.abs(point.latitude - origin.latitude)) * 100 + 1;\n const avgScale = averageMercatorScale(origin.latitude, point.latitude, steps);\n\n const z = ((-latitudeDiff * earthRadius) / getMercatorScale(origin.latitude)) * avgScale;\n return [x, y, z] as Vector3Tuple;\n}"],"names":[],"mappings":";;AAKA,MAAM,sBAAiD,CAAA;AAEvD,SAAS,iBAAiB,KAAqB;AAC7C,QAAM,QAAQ,KAAK,MAAM,MAAM,GAAI;AAC/B,MAAA,oBAAoB,KAAK,MAAM,QAAW;AAC5C,wBAAoB,KAAK,IAAI,IAAI,KAAK,IAAI,MAAM,UAAU,OAAO;AAAA,EACnE;AACA,SAAO,oBAAoB,KAAK;AAClC;AAEO,SAAS,qBAAqB,WAAmB,UAAkB,QAAQ,IAAY;AAC5F,MAAI,aAAa;AACX,QAAA,WAAW,WAAW,aAAa;AACzC,WAAS,IAAI,GAAG,KAAK,OAAO,KAAK;AACzB,UAAA,MAAM,YAAY,UAAU;AAClC,kBAAc,iBAAiB,GAAG;AAAA,EACpC;AACA,SAAO,cAAc,QAAQ;AAC/B;AAEgB,SAAA,gBAAgB,OAAe,QAA8B;AAC3E,QAAM,gBAAgB,MAAM,WAAW,OAAO,YAAY,UAAU;AACpE,QAAM,iBAAiB,MAAM,YAAY,OAAO,aAAa,UAAU;AACvE,QAAM,gBAAgB,MAAM,YAAY,MAAM,OAAO,YAAY;AAE3D,QAAA,IAAI,gBAAgB,cAAc,KAAK,IAAI,OAAO,WAAW,UAAU,OAAO;AACpF,QAAM,IAAI;AAIJ,QAAA,QAAQ,KAAK,KAAK,KAAK,IAAI,MAAM,WAAW,OAAO,QAAQ,CAAC,IAAI,MAAM;AAC5E,QAAM,WAAW,qBAAqB,OAAO,UAAU,MAAM,UAAU,KAAK;AAE5E,QAAM,IAAM,CAAC,eAAe,cAAe,iBAAiB,OAAO,QAAQ,IAAK;AACzE,SAAA,CAAC,GAAG,GAAG,CAAC;AACjB;"}
@@ -1,3 +1,4 @@
1
1
  import { Vector3Tuple } from 'three';
2
2
  import { Coords } from './coords';
3
+ export declare function averageMercatorScale(originLat: number, pointLat: number, steps?: number): number;
3
4
  export declare function coordsToVector3(point: Coords, origin: Coords): Vector3Tuple;
@@ -1 +1 @@
1
- export declare const earthRadius = 6378137;
1
+ export declare const earthRadius = 6371008.8;
@@ -1,3 +1,4 @@
1
1
  import { Vector3Tuple } from 'three';
2
2
  import { Coords } from './coords';
3
+ export declare function averageMercatorScale(originLat: number, pointLat: number, steps?: number): number;
3
4
  export declare function coordsToVector3(point: Coords, origin: Coords): Vector3Tuple;
@@ -1 +1 @@
1
- export declare const earthRadius = 6378137;
1
+ export declare const earthRadius = 6371008.8;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-three-map",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "Use react-three-fiber inside MapLibre and Mapbox",
5
5
  "main": "dist/cjs/main.js",
6
6
  "module": "dist/es/main.mjs",
@@ -20,7 +20,7 @@
20
20
  "react": "^18.2.0",
21
21
  "react-dom": "^18.2.0",
22
22
  "react-map-gl": "^7.1.7",
23
- "react-three-map": "^0.6.1",
23
+ "react-three-map": "^0.8.1",
24
24
  "suncalc": "^1.9.0",
25
25
  "three": "^0.159.0",
26
26
  "three-stdlib": "^2.28.7",
package/stories/yarn.lock CHANGED
@@ -4199,10 +4199,10 @@ react-refresh@^0.14.0:
4199
4199
  resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.14.0.tgz#4e02825378a5f227079554d4284889354e5f553e"
4200
4200
  integrity sha512-wViHqhAd8OHeLS/IRMJjTSDHF3U9eWi62F/MledQGPdJGDhodXJ9PBLNGr6WWL7qlH12Mt3TyTpbS+hGXMjCzQ==
4201
4201
 
4202
- react-three-map@^0.6.1:
4203
- version "0.6.1"
4204
- resolved "https://registry.yarnpkg.com/react-three-map/-/react-three-map-0.6.1.tgz#566811527242bb8964586c405a5816a84f75cb40"
4205
- integrity sha512-DSL7fyeR+MqiB1f6fG8FHwMb0NoDhj6Yjyc5T9jU6Yo/dpxQdY9Mfjs++J0j8HofcEDTsgiQz2GXh2r4iSPmhA==
4202
+ react-three-map@^0.8.1:
4203
+ version "0.8.1"
4204
+ resolved "https://registry.yarnpkg.com/react-three-map/-/react-three-map-0.8.1.tgz#0312e9a2cb1a19f67127ad11526f1e4e1f091852"
4205
+ integrity sha512-QkOJzaUPs7ZjtsP9sA8Xdy/FGhh0Q+eR89yUJPM6zlNEdzr1pe1F+prQlgd3jmffzE3Drfg90DMzqh3yJZcLJQ==
4206
4206
 
4207
4207
  react-use-measure@^2.1.1:
4208
4208
  version "2.1.1"