Plusプラン限定レッスン

三角関数の演習4 - 緯度・経度の計算

// ページの読み込みを待つ
window.addEventListener('load', init);

function init() {
  // サイズを指定
  let width = 960;
  let height = 540;

  // 角度
  let rot = 0;

  // レンダラーを作成
  let renderer = new THREE.WebGLRenderer({
    canvas: document.querySelector('#myCanvas')
  });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(width, height);

  // シーンを作成
  let scene = new THREE.Scene();

  // 画面上にグリッド(格子)を配置
  let grid = new THREE.GridHelper(1000, 10);
  scene.add(grid);

  // 画面上に3軸を配置
  let axes = new THREE.AxesHelper(1000);
  scene.add(axes);

  // カメラを作成
  let camera = new THREE.PerspectiveCamera(45, width / height);
  camera.lookAt(new THREE.Vector3(0, 0, 0));

  // 平行光源
  let directionalLight = new THREE.DirectionalLight(0xFFFFFF);
  directionalLight.position.set(1, 1, 1);
  scene.add(directionalLight);// シーンに追加

  // 環境光源
  let ambientLight = new THREE.AmbientLight(0x999999);
  scene.add(ambientLight);

  // 球体を作成
  let geometry = new THREE.SphereGeometry(300, 30, 30);
  // 画像を読み込む
  let loader = new THREE.TextureLoader();
  let texture = loader.load('imgs/earthmap1k.jpg');
  // マテリアルにテクスチャーを設定
  let material = new THREE.MeshStandardMaterial({
    map: texture
  });
  // メッシュを作成
  let mesh = new THREE.Mesh(geometry, material);
  // 3D空間にメッシュを追加
  scene.add(mesh);


  /** 主要都市緯度経度一覧 */
  let citiesPoints = [
    [35, 139], // 日本
    [51.2838, 0], // ロンドン
    [39, -116], // アメリカ
    [34, 118], // 中国
  ];
  // リスト分のポイントをプロット
  for (let i = 0; i < citiesPoints.length; i++) {
    // 緯度
    let latitude = citiesPoints[i][0];
    // 経度
    let longitude = citiesPoints[i][1];

    // ---------------------------------------------
    // 緯度と経度から、都市の場所に球体を配置しましょう
    // ▼ここから
    // ---------------------------------------------



    // ---------------------------------------------
    // ▲ここまで
    // ---------------------------------------------
  }


  tick();

  // 毎フレーム時に実行されるループイベントです
  function tick() {

    rot += 1;

    // ラジアンに変換
    let radian = rot * Math.PI / 180;

    // ライトの座標に反映
    camera.position.x = 1000 * Math.cos(radian);
    camera.position.y = 500;
    camera.position.z = 1000 * Math.sin(radian);

    // カメラは常に中央を向くように指定
    camera.lookAt(new THREE.Vector3(0, 0, 0));

    // レンダリング
    renderer.render(scene, camera);
    requestAnimationFrame(tick);
  }
}

/**
 * 緯度経度から位置を算出します。
 * @param {number} latitude 緯度です。
 * @param {number} longitude 経度です。
 * @param {number} radius 半径です。
 * @returns {Vector3} 3Dの座標です。
 */
function translateGeoCoords(latitude, longitude, radius) {
  // 仰角
  let phi = (latitude) * Math.PI / 180;
  // 方位角
  let theta = (longitude - 180) * Math.PI / 180;

  let x = -(radius) * Math.cos(phi) * Math.cos(theta);
  let y = (radius) * Math.sin(phi);
  let z = (radius) * Math.cos(phi) * Math.sin(theta);

  return new THREE.Vector3(x, y, z);
}

この続きはPlusプラン限定です