c5154a16ee4b84523e39887b44e7fdf36c4bd86f.svn-base 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import * as THREE from 'three'
  2. import { Map$1, Coordinate, MultiPolygon, Polygon } from './featuresToPolygon'
  3. let procssMap = new Map$1(null, {
  4. center: [0, 0]
  5. })
  6. function sign(x) {
  7. if (Math.sign) {
  8. return Math.sign(x);
  9. }
  10. x = +x;
  11. if (x === 0 || isNaN(x)) {
  12. return Number(x);
  13. }
  14. return x > 0 ? 1 : -1;
  15. }
  16. function getAmount(amount, map) {
  17. let zoom = map.maxNativeZoom
  18. let center = procssMap.getCenter();
  19. let target = procssMap.locate(center, amount, amount);
  20. let p0 = procssMap.coordinateToPoint(center, zoom);
  21. let p1 = procssMap.coordinateToPoint(target, zoom);
  22. var x = Math.abs(p1.x - p0.x) * sign(amount);
  23. var y = Math.abs(p1.y - p0.y) * sign(amount);
  24. return new THREE.Vector3(x, y, 0).x;
  25. }
  26. function coordinateToVector3(coordinate, map) {
  27. var z = 0;
  28. var p = procssMap.coordinateToPoint(coordinate, map.maxNativeZoom)
  29. return new THREE.Vector3(p.x, p.y, -z);
  30. };
  31. function toShape(polygon, map) {
  32. var center = polygon.getCenter();
  33. var centerPt = coordinateToVector3(center, map);
  34. var shell = polygon.getShell();
  35. var outer = shell.map(function (c) {
  36. return coordinateToVector3(c, map).sub(centerPt);
  37. });
  38. var shape = new THREE.Shape(outer);
  39. var holes = polygon.getHoles();
  40. if (holes && holes.length > 0) {
  41. shape.holes = holes.map(function (item) {
  42. var pts = item.map(function (c) {
  43. return coordinateToVector3(c, map).sub(centerPt);
  44. });
  45. return new THREE.Shape(pts);
  46. });
  47. }
  48. return shape;
  49. };
  50. function toExtrudeGeometry(polygon, map, height) {
  51. if (!polygon || !(polygon instanceof MultiPolygon || polygon instanceof Polygon)) {
  52. return null;
  53. }
  54. if (polygon instanceof MultiPolygon) {
  55. return polygon.getGeometries().map(c =>
  56. toExtrudeGeometry(c, map, height)
  57. );
  58. }
  59. var shape = toShape(polygon, map);
  60. var center = coordinateToVector3(polygon.getCenter(), map);
  61. var amount = getAmount(height, map);
  62. var geom = new THREE.ExtrudeGeometry(shape, { 'depth': amount, 'bevelEnabled': true });
  63. var vertices = geom.vertices
  64. var vlength = vertices.length
  65. for (let i = 0; i < vlength; i += 1) {
  66. vertices[i].x += center.x
  67. vertices[i].y += center.y
  68. vertices[i].z += -amount
  69. }
  70. return geom
  71. }
  72. function reductive(map) {
  73. procssMap.setCenter(new Coordinate(map.center))
  74. // procssMap.width = map.size.width
  75. // procssMap.height = map.size.height
  76. // procssMap.setBearing(map.bearing)
  77. // procssMap.setFov(map.fov)
  78. // procssMap.setMaxZoom(map.maxZoom)
  79. // procssMap.setMinZoom(map.minZoom)
  80. // procssMap.setPitch(map.pitch)
  81. // procssMap.setSpatialReference(map.spatialReference)
  82. }
  83. export {
  84. reductive,
  85. toExtrudeGeometry
  86. }