获取路线中点(长度中点)通过点位数据生成

方法一:

curve.getPointAt(0.5)

曲线方法获取长度为中间时的点位

方法二:

通过点位数据,计算出点位

 //计算模型线中点位置===================================
        const getModelLineCenterToLabel = () => {
          let _count = 0;
          // // 总长度
          const _len = option.points.length;
          for (let i = 0; i < _len - 1; i++) {
            const p1 = option.points[i];
            const p2 = option.points[i + 1];
            const v1 = new THREE.Vector3(p1.x, p1.y, p1.z);
            const v2 = new THREE.Vector3(p2.x, p2.y, p2.z);

            _count += v1.distanceTo(v2);
          }

          // // 中段位置
          let _center = new THREE.Vector3();

          if (_count !== 0) {
            // 中段长度
            const _half = _count / 2;

            // 对比中心点间距
            _count = 0;
            for (let i = 0; i < _len - 1; i++) {
              const p1 = option.points[i];
              const p2 = option.points[i + 1];
              const v1 = new THREE.Vector3(p1.x, p1.y, p1.z);
              const v2 = new THREE.Vector3(p2.x, p2.y, p2.z);

              _count += v1.distanceTo(v2);

              if (_count > _half) {
                const dis = _count - _half;
                _center = v1.lerp(v2, 1 - dis / v1.distanceTo(v2));
                break;
              }
            }
          } else {
            _center.x = option.points[0].x;
            _center.y = option.points[0].y;
            _center.z = option.points[0].z;
          }
          return _center;
        };
        //============================================================

 

posted @ 2025-07-24 16:30  SimoonJia  阅读(13)  评论(0)    收藏  举报