drawFunc_20240513112607.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import * as THREE from "three";
  2. import * as d3 from "d3";
  3. import { CSS2DObject } from "three/examples/jsm/renderers/CSS2DRenderer";
  4. import { Line2 } from "three/examples/jsm/lines/Line2";
  5. import { LineGeometry } from "three/examples/jsm/lines/LineGeometry";
  6. import { LineMaterial } from "three/examples/jsm/lines/LineMaterial";
  7. // import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js';
  8. // import { ProjectionFnParamType } from "./index.vue";
  9. import { mapConfig } from "./mapConfig";
  10. // 绘制挤出的材质
  11. export function drawExtrudeMesh(
  12. point,
  13. projectionFn
  14. ) {
  15. const shape = new THREE.Shape();
  16. const pointsArray = [];
  17. for (let i = 0; i < point.length; i++) {
  18. const [x, y] = projectionFn(point[i]); // 将每一个经纬度转化为坐标点
  19. if (i === 0) {
  20. shape.moveTo(x, -y);
  21. }
  22. shape.lineTo(x, -y);
  23. pointsArray.push(x, -y, mapConfig.topLineZIndex);
  24. }
  25. const geometry = new THREE.ExtrudeGeometry(shape, {
  26. depth: mapConfig.mapDepth, // 挤出的形状深度
  27. bevelEnabled: false, // 对挤出的形状应用是否斜角
  28. });
  29. const material = new THREE.MeshPhongMaterial({
  30. // color: mapConfig.mapColor,
  31. color: mapConfig.mapColorGradient[Math.floor(Math.random() * 4)], // 随机颜色
  32. // transparent: mapConfig.mapTransparent,
  33. // opacity: mapConfig.mapOpacity,
  34. });
  35. const materialSide = new THREE.ShaderMaterial({
  36. uniforms: {
  37. color1: {
  38. value: new THREE.Color(mapConfig.mapSideColor1),
  39. },
  40. color2: {
  41. value: new THREE.Color(mapConfig.mapSideColor2),
  42. },
  43. },
  44. vertexShader: `
  45. varying vec3 vPosition;
  46. void main() {
  47. vPosition = position;
  48. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  49. }
  50. `,
  51. fragmentShader: `
  52. uniform vec3 color1;
  53. uniform vec3 color2;
  54. varying vec3 vPosition;
  55. void main() {
  56. vec3 mixColor = mix(color1, color2, 0.5 - vPosition.z * 0.2); // 使用顶点坐标 z 分量来控制混合
  57. gl_FragColor = vec4(mixColor, 1.0);
  58. }
  59. `,
  60. // wireframe: true,
  61. });
  62. const mesh = new THREE.Mesh(geometry, [material, materialSide]);
  63. // userData 存储自定义属性
  64. mesh.userData = {
  65. isChangeColor: true,
  66. };
  67. // 边框线,赋值空间点坐标,3个一组
  68. const lineGeometry = new LineGeometry();
  69. lineGeometry.setPositions(pointsArray);
  70. const lineMaterial = new LineMaterial({
  71. color: mapConfig.topLineColor,
  72. linewidth: mapConfig.topLineWidth,
  73. });
  74. lineMaterial.resolution.set(window.innerWidth, window.innerHeight);
  75. const line = new Line2(lineGeometry, lineMaterial);
  76. return { mesh, line };
  77. }
  78. // 生成地图3D模型
  79. export function generateMapObject3D(
  80. mapdata,
  81. projectionFnParam
  82. ) {
  83. // 地图对象
  84. const mapObject3D = new THREE.Object3D();
  85. // 地图数据
  86. const { features: basicFeatures } = mapdata;
  87. const { center, scale } = projectionFnParam;
  88. const projectionFn = d3
  89. .geoMercator()
  90. .center(center)
  91. .scale(scale)
  92. .translate([0, 0]);
  93. const label2dData = []; // 存储自定义 2d 标签数据
  94. // 每个省的数据
  95. basicFeatures.forEach((basicFeatureItem) => {
  96. // 每个省份的地图对象
  97. const provinceMapObject3D = new THREE.Object3D();
  98. // 将地图数据挂在到模型数据上
  99. provinceMapObject3D.customProperties = basicFeatureItem.properties;
  100. // 每个坐标类型
  101. const featureType = basicFeatureItem.geometry.type;
  102. // 每个坐标数组
  103. const featureCoords =
  104. basicFeatureItem.geometry.coordinates;
  105. // 每个中心点位置
  106. const featureCenterCoord =
  107. basicFeatureItem.properties.centroid &&
  108. projectionFn(basicFeatureItem.properties.centroid);
  109. // 名字
  110. const featureName = basicFeatureItem.properties.name;
  111. if (featureCenterCoord) {
  112. label2dData.push({
  113. featureCenterCoord,
  114. featureName,
  115. });
  116. }
  117. // MultiPolygon 类型
  118. if (featureType === "MultiPolygon") {
  119. featureCoords.forEach((multiPolygon) => {
  120. multiPolygon.forEach((polygon) => {
  121. const { mesh, line } = drawExtrudeMesh(polygon, projectionFn);
  122. provinceMapObject3D.add(mesh);
  123. provinceMapObject3D.add(line);
  124. });
  125. });
  126. }
  127. // Polygon 类型
  128. if (featureType === "Polygon") {
  129. featureCoords.forEach((polygon) => {
  130. const { mesh, line } = drawExtrudeMesh(polygon, projectionFn);
  131. provinceMapObject3D.add(mesh);
  132. provinceMapObject3D.add(line);
  133. });
  134. }
  135. mapObject3D.add(provinceMapObject3D);
  136. });
  137. return { mapObject3D, label2dData };
  138. }
  139. // 生成地图2D标签
  140. export function generateMapLabel2D(label2dData) {
  141. const labelObject2D = new THREE.Object3D();
  142. label2dData.forEach((item) => {
  143. const { featureCenterCoord, featureName } = item;
  144. const labelObjectItem = draw2dLabel(featureCenterCoord, featureName);
  145. if (labelObjectItem) {
  146. labelObject2D.add(labelObjectItem);
  147. }
  148. });
  149. return labelObject2D;
  150. }
  151. // 生成地图spot点位
  152. export function generateMapSpot(label2dData) {
  153. const spotObject3D = new THREE.Object3D();
  154. const spotList = [];
  155. label2dData.forEach((item) => {
  156. const { featureCenterCoord } = item;
  157. const spotObjectItem = drawSpot(featureCenterCoord);
  158. if (spotObjectItem && spotObjectItem.circle && spotObjectItem.ring) {
  159. spotObject3D.add(spotObjectItem.circle);
  160. spotObject3D.add(spotObjectItem.ring);
  161. spotList.push(spotObjectItem.ring);
  162. }
  163. //TODO:渲染铁塔
  164. });
  165. return { spotObject3D, spotList };
  166. }
  167. // 绘制二维标签
  168. export const draw2dLabel = (coord, proviceName) => {
  169. if (coord && coord.length) {
  170. // 模版字符串
  171. const innerHTML = `<div class="your-classname" style="color: #fff">${proviceName}</div>`;
  172. const labelDiv = document.createElement("div");
  173. labelDiv.innerHTML = innerHTML;
  174. labelDiv.style.pointerEvents = "none"; // 禁用事件
  175. const labelObject = new CSS2DObject(labelDiv);
  176. labelObject.position.set(coord[0], -coord[1], mapConfig.label2dZIndex);
  177. return labelObject;
  178. }
  179. };
  180. // 绘制圆点
  181. export const drawSpot = (coord) => {
  182. if (coord && coord.length) {
  183. /**
  184. * 绘制圆点
  185. */
  186. const spotGeometry = new THREE.CircleGeometry(0.2, 200);
  187. const spotMaterial = new THREE.MeshBasicMaterial({
  188. color: "#3EC5FB",
  189. side: THREE.DoubleSide,
  190. });
  191. const circle = new THREE.Mesh(spotGeometry, spotMaterial);
  192. circle.position.set(coord[0], -coord[1], mapConfig.spotZIndex);
  193. // 圆环
  194. const ringGeometry = new THREE.RingGeometry(0.2, 0.3, 50);
  195. const ringMaterial = new THREE.MeshBasicMaterial({
  196. color: "#3FC5FB",
  197. side: THREE.DoubleSide,
  198. transparent: true,
  199. });
  200. const ring = new THREE.Mesh(ringGeometry, ringMaterial);
  201. ring.position.set(coord[0], -coord[1], mapConfig.spotZIndex);
  202. return { circle, ring };
  203. }
  204. };
  205. /**
  206. * 线上移动物体
  207. */
  208. export const drawflySpot = (curve) => {
  209. const aGeo = new THREE.SphereGeometry(0.2);
  210. const aMater = new THREE.MeshBasicMaterial({
  211. color: "#77f077",
  212. side: THREE.DoubleSide,
  213. });
  214. const aMesh = new THREE.Mesh(aGeo, aMater);
  215. // 保存曲线实例
  216. aMesh.curve = curve;
  217. aMesh._s = 0;
  218. return aMesh;
  219. };
  220. // 绘制两点链接飞线
  221. export const drawLineBetween2Spot = (
  222. coordStart,
  223. coordEnd
  224. ) => {
  225. const [x0, y0, z0] = [...coordStart, mapConfig.spotZIndex];
  226. const [x1, y1, z1] = [...coordEnd, mapConfig.spotZIndex];
  227. // 使用 QuadraticBezierCurve3 创建 三维二次贝塞尔曲线
  228. const curve = new THREE.QuadraticBezierCurve3(
  229. new THREE.Vector3(x0, -y0, z0),
  230. new THREE.Vector3((x0 + x1) / 2, -(y0 + y1) / 2, 20),
  231. new THREE.Vector3(x1, -y1, z1)
  232. );
  233. const flySpot = drawflySpot(curve);
  234. const lineGeometry = new THREE.BufferGeometry();
  235. // 获取曲线上50个点
  236. const points = curve.getPoints(50);
  237. const positions = [];
  238. const colors = [];
  239. const color = new THREE.Color();
  240. // 给每个顶点设置演示 实现渐变
  241. for (let j = 0; j < points.length; j++) {
  242. color.setHSL(0.21 + j, 0.77, 0.55 + j * 0.0025); // 色
  243. colors.push(color.r, color.g, color.b);
  244. positions.push(points[j].x, points[j].y, points[j].z);
  245. }
  246. // 放入顶点 和 设置顶点颜色
  247. lineGeometry.setAttribute(
  248. "position",
  249. new THREE.BufferAttribute(new Float32Array(positions), 3, true)
  250. );
  251. lineGeometry.setAttribute(
  252. "color",
  253. new THREE.BufferAttribute(new Float32Array(colors), 3, true)
  254. );
  255. const material = new THREE.LineBasicMaterial({
  256. vertexColors: true,
  257. // color: "red",
  258. side: THREE.DoubleSide,
  259. });
  260. const flyLine = new THREE.Line(lineGeometry, material);
  261. return { flyLine, flySpot };
  262. };