LoginSignup
11
13

More than 5 years have passed since last update.

星を表示する(1) CSVデータを読み込む

Last updated at Posted at 2018-02-24

今回はcsvファイルからデータを読み込んでみます。

こちらに星の位置、視等級などのCSVデータがパブリックドメインで置いてありましたので、使わせていただきました。

スクリーンショット 2018-02-24 20.32.01.png

hip_lite_major.csv ファイルを ダウンロードし、UnityのAssetsの下の適当な場所に入れます。

スクリーンショット 2018-02-24 20.36.49.png

内容
1 HIP番号:1~120416
2 赤経:時(整数)
3 赤経:分(整数)
4 赤経:秒(小数)
5 赤緯:符号(0:- 1:+)
6 赤緯:度(整数)
7 赤緯:分(整数)
8 赤緯:秒(小数)
9 視等級:等級(小数)

となっているので、
列1からHIP番号を、
列2−8から地球から見た方角を、
列9から明るさを取得します。

ParticleSetTest.cs
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace ParticleTest{
    public class ParticleSetTest : MonoBehaviour
    {
        // データ型の定義
        public struct HipData
        {
            public int hipId;
            public Vector3 pos;
            public Color color;
            public float magnitude; // 等級

            public HipData(int _id, Vector3 _pos, Color _color, float _magnitude)
            {
                hipId = _id;
                pos = _pos;
                color = _color;
                magnitude = _magnitude;
            }
            public HipData(HipData _data)
            {
                hipId = _data.hipId;
                pos = _data.pos;
                color = _data.color;
                magnitude = _data.magnitude;
            }
        }

        // 読み込むデータ
        [SerializeField] TextAsset lightFile = null;
        // 読み込んだデータを格納するリスト
        public List<HipData> hipList;

        // Use this for initialization
        void Start()
        {
            hipList = createHipList(lightFile);
        }

        // Update is called once per frame
        void Update()
        {
            if (hipList != null)
            {
                foreach(HipData hip in hipList){
                    Debug.DrawLine(Vector3.zero,hip.pos*500f,Color.white*(1f/hip.magnitude));
                }
            }
        }

        // ファイルからデータを読み込みデータリストに格納する
        List<HipData> createHipList(TextAsset _lightsFile)
        {
            List<HipData> list = new List<HipData>();
            StringReader sr = new StringReader(_lightsFile.text);
            while (sr.Peek() > -1)
            {
                string lineStr = sr.ReadLine();
                HipData data;
                if (stringToHipData(lineStr, out data))
                {
                    list.Add(data);
                }
            }
            sr.Close();
            return list;
        }

        // CSV文字列からデータ型に変換
        bool stringToHipData(string _hipStr, out HipData data)
        {
            bool ret = true;
            data = new HipData();
            // カンマ区切りのデータを文字列の配列に変換
            string[] dataArr = _hipStr.Split(',');
            try
            {
                // 文字列をint,floatに変換する
                int hipId = int.Parse(dataArr[0]);
                float hlH = float.Parse(dataArr[1]);
                float hlM = float.Parse(dataArr[2]);
                float hlS = float.Parse(dataArr[3]);
                int hsSgn = int.Parse(dataArr[4]);
                float hsH = float.Parse(dataArr[5]);
                float hsM = float.Parse(dataArr[6]);
                float hsS = float.Parse(dataArr[7]);
                float mag = float.Parse(dataArr[8]);
                Color col = Color.gray;
                float hDeg = (360f / 24f) * (hlH + hlM / 60f + hlS / 3600f);
                float sDeg = (hsH + hsM / 60f + hsS / 3600f) * (hsSgn == 0 ? -1f : 1f);
                Quaternion rotL = Quaternion.AngleAxis(hDeg, Vector3.up);
                Quaternion rotS = Quaternion.AngleAxis(sDeg, Vector3.right);
                Vector3 pos = rotL * rotS * Vector3.forward;
                data = new HipData(hipId, pos, Color.white, mag);
            }
            catch
            {
                ret = false;
                Debug.Log("data err");
            }
            return ret;
        }
    }
}

上記スクリプトを空のオブジェクトにアタッチし、lightFile に hip_lite_major.csvをドラッグ&ドロップします。

スクリーンショット 2018-02-24 20.54.01.png

実行するとデータが示す方向に(放射状に)デバッグラインが表示されます

スクリーンショット 2018-02-24 20.55.57.png

星を表示する(1) CSVデータを読み込む
星を表示する(2) 星をパーティクルで表示する
星を表示する(3) 星座線を表示する
星を表示する(4) データを変換する
星を表示する(5) 星を点群メッシュで表示する

11
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
13