1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| import * as THREE from 'three'; import * as d3 from 'd3'; import { Line2 } from 'three/examples/jsm/lines/Line2'; import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry'; import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial';
export class LineRender { constructor(private readonly scene: THREE.Scene) {}
private readonly circleYs: THREE.Mesh<THREE.RingGeometry, THREE.MeshBasicMaterial>[] = []; private readonly projection = d3.geoMercator().center([104.0, 37.5]).translate([0, 0]); private readonly lines: Line2[] = [];
render(poses: [[number, number], [number, number]][]) { this.circleYs.splice(0); this.lines.splice(0);
this.addLines(poses); }
animation() { this.pointAnimate(); this.arclineAnimate(); }
private pointAnimate() { this.circleYs.forEach(function (mesh) { mesh['_s'] += 0.01; mesh.scale.set(1.1 * mesh['_s'], 1.1 * mesh['_s'], 1.1 * mesh['_s']); if (mesh['_s'] <= 2) { mesh.material.opacity = 2 - mesh['_s']; } else { mesh['_s'] = 1; } }); }
private arclineAnimate() { for (const line of this.lines) { if (!line['_tick']) line['_tick'] = 0; line['_tick'] = (line['_tick'] + 1) % 4; if (line['_tick'] > 1) continue;
const colors: any[] = line['_colors']; colors.splice(0, 0, ...colors.splice(colors.length - 3, 3));
line.geometry.setColors(colors); } }
private addLines(poses: [[number, number], [number, number]][]) { poses.forEach((item) => { const start = this.projection(item[0]); const end = this.projection(item[1]); if (start && end) { this.spotCircle(start); this.spotCircle(end); this.lineConnect(...start, ...end); } }); }
private spotCircle(spot: [number, number]) { const geometry1 = new THREE.CircleGeometry(2, 200); const material1 = new THREE.MeshBasicMaterial({ color: '#F44336', side: THREE.DoubleSide }); const circle = new THREE.Mesh(geometry1, material1); circle.position.set(spot[0], -spot[1], 0.4); this.scene.add(circle);
const geometry2 = new THREE.RingGeometry(2, 1, 50); const material2 = new THREE.MeshBasicMaterial({ color: 0xf44336, side: THREE.DoubleSide, transparent: true, }); const circleY = new THREE.Mesh(geometry2, material2); circleY.position.set(spot[0], -spot[1], 0.4); this.scene.add(circleY);
this.circleYs.push(circleY); }
private lineConnect(posStartX: number, posStartY: number, posEndX: number, posEndY: number) { const [x0, y0, z0] = [posStartX, posStartY, 0]; const [x1, y1, z1] = [posEndX, posEndY, 0];
const curve = new THREE.QuadraticBezierCurve3( new THREE.Vector3(x0, -y0, z0), new THREE.Vector3((x0 + x1) / 2, -(y0 + y1) / 2, 20), new THREE.Vector3(x1, -y1, z1), );
const points = curve.getPoints(50).reduce((arr, cur) => { return arr.concat(cur.x, cur.y, cur.z); }, [] as number[]); const lineGeometry = new LineGeometry();
lineGeometry.setPositions(points); const colors = [ ...this.gradientColors('#00ffff', '#f44336', points.length / 3 / 2), ...this.gradientColors('#f44336', '#f44336', points.length / 3 / 2), ].reverse(); const colorArr = colors.reduce((arr: number[], item) => { const Tcolor = new THREE.Color(item); return arr.concat(Tcolor.r, Tcolor.g, Tcolor.b); }, []); lineGeometry.setColors(colorArr);
const material = new LineMaterial({ vertexColors: true, linewidth: 2, transparent: true, side: THREE.DoubleSide, });
material.resolution.set(window.innerWidth, window.innerHeight); const line = new Line2(lineGeometry, material); line['_colors'] = colorArr; this.lines.push(line); this.scene.add(line); }
private gradientColors(start: string, end: string, steps: number, gamma = 1) { const parseColor = (hexStr: string) => { return hexStr.length === 4 ? hexStr .substr(1) .split('') .map(function (s) { return 0x11 * parseInt(s, 16); }) : [hexStr.substr(1, 2), hexStr.substr(3, 2), hexStr.substr(5, 2)].map(function (s) { return parseInt(s, 16); }); }; const pad = function (s) { return s.length === 1 ? `0${s}` : s; }; let j; let ms; let me; const output: string[] = []; const so: string[] = []; const normalize = (channel: number) => { return Math.pow(channel / 255, gamma); }; const startNum = parseColor(start).map(normalize); const endNum = parseColor(end).map(normalize); for (let i = 0; i < steps; i++) { ms = i / (steps - 1); me = 1 - ms; for (j = 0; j < 3; j++) { so[j] = pad( Math.round(Math.pow(startNum[j] * me + endNum[j] * ms, 1 / gamma) * 255).toString(16), ); } output.push(`#${so.join('')}`); } return output; } }
|