(200320) How to develop AR applicaion in iPhone?

  1. window > package manager > download AR Foundation and ARkit XR Plugin and AR subsystems
  2. Hierarchy tab > right click > XR > AR Session Origin and AR Session
  3. Project tab > create C# scripts > named it ARTapToPlaceObject
ARTapToPlaceObject.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

[RequireComponent(typeof(ARRaycastManager))]
public class ARTapToPlaceObject : MonoBehaviour
{
    public GameObject gameObjectToInstantiate;

    private GameObject spawnedObject;
    private ARRaycastManager _arRaycastManager;
    private Vector2 touchPosition;

    static List<ARRaycastHit> hits = new List<ARRaycastHit>(); 

    private void Awake()
    {
        _arRaycastManager = GetComponent<ARRaycastManager>();
    }

    bool TryGetTouchPosition(out Vector2 touchPosition)
    {
        if (Input.touchCount > 0)
        {
            touchPosition = Input.GetTouch(0).position;
            return true;
        }

        touchPosition = default;
        return false;
    }

    void Update()
    {
        if(!TryGetTouchPosition(out Vector2 touchPosition))
            return;
        if(_arRaycastManager.Raycast(touchPosition, hits, TrackableType.PlaneWithinPolygon))
        {
            var hitPose = hits[0].pose;

            if(spawnedObject == null)
            {
                spawnedObject = Instantiate(gameObjectToInstantiate, hitPose.position, hitPose.rotation);
            }
            else
            {
                spawnedObject.transform.position = hitPose.position;
            }
        }
    }
}

4. Hierarchy tab > click AR Session Origin > Inspector tab > Add component > add AR Plane manager and AR Tap To Place Object

5. Project tab > create brick > move to Game Object of AR Session origin’s AR tab to Place Object

6. Hierarchy tab > create empty game object and named it ‘ARplane’ > add Mesh Collider

And finally detect a plane and place cube.

(191219) How to visualize indoor data?

I signed up placenote.com and got the API address for the navigation. And also found the arrow data from navigation kit. So I connected the location data into navigation.

Yet the problem is that placenote spatial information is built for IOS application. As I am developing application for Magic Leap, the application should connected with Lumin application. I’m still looking for the solution.

(191215) How to visualize the city data?

using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataPlotter_1215 : MonoBehaviour
{
    Renderer rend;
    float duration = 0.1f;
    private int k;
    private float colorChangeTime = 1;
    private float colorChangeTimer = 0;
    GameObject cub;
    List<GameObject> cubs;

    // Name of the input file, no extension
    public string inputfile_origin;
    public string inputfile_moved;

    // List for holding data from CSV reader
    private List<Dictionary<string, object>> o_pointList;
    private List<Dictionary<string, object>> m_pointList;

    // indices for columns to be assigned
    public int oColumnx;
    public int oColumny;
    public int oColumnz;

    public int mColumnx;
    public int mColumny;
    public int mColumnz;

    // Full column names
    private string o_xName;
    private string o_yName;
    private string o_zName;

    private string m_xName;
    private string m_yName;
    private string m_zName;

    // z value
    private float ox;
    private float oy;
    private float oz;

    private float mx;
    private float my;
    private float mz;

    // The prefab for the data point to be instantiated
    public GameObject originprefab;
    public GameObject movedprefab;

    // color value
    int a = 0;
    int b = 0;
    int c = 0;

    // Use this for initialization
    void Start()
    {
        SetLocation();
    }

    private void SetLocation()
    {
        // Set pointlist to results of function Reader with argument inputfile
        o_pointList = CSVReader.Read(inputfile_origin);
        m_pointList = CSVReader.Read(inputfile_moved);

        //Declare list of string, fill with keys (column names)
        List<string> o_columnList = new List<string>(o_pointList[1].Keys);
        List<string> m_columnList = new List<string>(m_pointList[1].Keys);

        //assign column name from columnlist to name variables
        o_xName = o_columnList[oColumnx];
        o_yName = o_columnList[oColumny];
        o_zName = o_columnList[oColumnz];
        m_zName = m_columnList[mColumnz];

        GameObject cub;

        //Loop through Pointlist
        for (var i = 0; i < o_pointList.Count; i++)
        {
            // Get value in poinList at ith "row", in "column" Name
            ox = System.Convert.ToSingle(o_pointList[i][o_xName]);
            oy = System.Convert.ToSingle(o_pointList[i][o_yName]);
            oz = System.Convert.ToSingle(o_pointList[i][o_zName]);
            mz = System.Convert.ToSingle(m_pointList[i][m_zName]);

            //color
            ChangeAbc();
            Color differentColor = new Color(a, b, c);

            //instantiate the prefab with coordinates defined above
            cub = Instantiate(originprefab, new Vector3(ox, oy, oz), Quaternion.identity);

            cub.transform.position = new Vector3(ox, oy + (oy), oz);
            cub.transform.localScale = new Vector3(1, 2 * oy, 1);
            cub.GetComponent<Renderer>().material.color = differentColor;
        }
    }

    private void ChangeAbc()
    {
        for (k = 0; k < o_pointList.Count; k++)
        {
            float colorValue = System.Convert.ToSingle(o_pointList[k][o_yName]);

            float minValue = Mathf.Min(oy);
            Debug.Log(minValue);
            float maxValue = Mathf.Max(oy);
            Debug.Log(maxValue);
            float length = (maxValue - minValue) / 5;
            Debug.Log(length);

            a = 0;
            b = 1;
            c = 1;

            //float lerp = Mathf.PingPong(Time.time, duration) / duration; //Color.Lerp(color2, color2, lerp);
            
            if (colorValue > (minValue + 4 * length))
            {
                float greenValue = 125 - ((colorValue - (minValue + 4 * length)) / length) * 125;
                float newGreen = greenValue / 255;
                a = 1;
                b = (int)newGreen;
                c = 0;
            }
            if (colorValue > (minValue + 3 * length))
            {
                float greenValue2 = 255 - ((colorValue - (minValue + 3 * length)) / length) * 125;
                float newGreen2 = greenValue2 / 255;

                a = 1;
                b = (int)newGreen2;
                c = 0;
            }
            if (colorValue > (minValue + 2 * length))
            {
                float redValue1 = ((colorValue - (minValue + 2 * length)) / length) * 255;
                float newRed1 = redValue1 / 255;

                a = (int)newRed1;
                b = 1;
                c = 0;
            }
            if (colorValue > (minValue + length))
            {
                float blueValue1 = 255 - (colorValue - (minValue + length)) / length * 125;
                float newBlue1 = blueValue1 / 255;

                a = 0;
                b = 1;
                c = (int)newBlue1;
            }
            if ((minValue + length) > colorValue)
            {
                a = 0;
                b = 1;
                c = 1;
            }
            
        }
    }
}

(191212) How to visualize the city data?

using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataPlotter : MonoBehaviour
{
    Renderer rend;
    float duration = 0.1f;
    private int k;
    private float colorChangeTime = 1;
    private float colorChangeTimer = 0;

    // Name of the input file, no extension
    public string inputfile_origin;
    public string inputfile_moved;

    // List for holding data from CSV reader
    private List<Dictionary<string, object>> o_pointList;
    private List<Dictionary<string, object>> m_pointList;
   
    // indices for columns to be assigned
    public int oColumnx;
    public int oColumny;
    public int oColumnz;

    public int mColumnx;
    public int mColumny;
    public int mColumnz;

    // Full column names
    private string o_xName;
    private string o_yName;
    private string o_zName;

    private string m_xName;
    private string m_yName;
    private string m_zName;

    // z value
    private float ox;
    private float oy;
    private float oz;

    private float mx;
    private float my;
    private float mz;

    // The prefab for the data point to be instantiated
    public GameObject originprefab;
    public GameObject movedprefab;

    // Use this for initialization
    void Start()
    {
        SetLocation();
    }

    private void SetLocation()
    {
        // Set pointlist to results of function Reader with argument inputfile
        o_pointList = CSVReader.Read(inputfile_origin);
        m_pointList = CSVReader.Read(inputfile_moved);

        //Declare list of string, fill with keys (column names)
        List<string> o_columnList = new List<string>(o_pointList[1].Keys);
        List<string> m_columnList = new List<string>(m_pointList[1].Keys);

        /*/print number of keys list (using .count)
        Debug.Log("There are " + o_columnList.Count + "column in origin csv");
        foreach (string o_key in o_columnList)
            Debug.Log("Column name is " + o_key);

        Debug.Log("There are " + m_columnList.Count + "column in moved csv");
        foreach (string m_key in m_columnList)
            Debug.Log("Column name is " + m_key);*/

        //assign column name from columnlist to name variables
        o_xName = o_columnList[oColumnx];
        o_yName = o_columnList[oColumny];
        o_zName = o_columnList[oColumnz];

        m_xName = m_columnList[mColumnx];
        m_yName = m_columnList[mColumny];
        m_zName = m_columnList[mColumnz];

        //Loop through Pointlist
        for (var i = 0; i < o_pointList.Count; i++)
        {
            // Get value in poinList at ith "row", in "column" Name
            ox = System.Convert.ToSingle(o_pointList[i][o_xName]);
            oy = System.Convert.ToSingle(o_pointList[i][o_yName]);
            oz = System.Convert.ToSingle(o_pointList[i][o_zName]);

            //instantiate the prefab with coordinates defined above
            Instantiate(originprefab, new Vector3(ox, oy, oz), Quaternion.identity);
        }

        for (var j = 0; j < m_pointList.Count; j++)
        {
            // Get value in poinList at ith "row", in "column" Name
            mx = System.Convert.ToSingle(m_pointList[j][m_xName]);
            my = System.Convert.ToSingle(m_pointList[j][m_yName]);
            mz = System.Convert.ToSingle(m_pointList[j][m_zName]);

            //instantiate the prefab with coordinates defined above
            Instantiate(movedprefab, new Vector3(mx, my, mz), Quaternion.identity);
        }
    }

    void Update()
    {
        colorChangeTimer += Time.deltaTime;
        if (colorChangeTimer >= colorChangeTime)
        {
            colorChangeTimer = 0;
            ChangeColor();
        }
    }

    private void ChangeColor()
    {
        for (int k = 0; k < o_pointList.Count; k++)
        {
            float colorValue = System.Convert.ToSingle(m_pointList[k][m_zName]);
            int val = (int)colorValue;

            float minValue = Mathf.Min(mz);
            float maxValue = Mathf.Max(mz);

            float length = (maxValue - minValue) / 5;

            int min = (int)minValue;
            int max = (int)maxValue;
            int len = (int)length;

            Renderer rend = movedprefab.GetComponent<Renderer>();

            if (val > (min + 4*len))
            {
                int greenValue = 125 - ((val - (min + 4*len)) / len) * 125;
                float newGreen = greenValue / 255f;

                Debug.Log(val);

                Color color1 = new Color(1, newGreen, 0);

                float lerp = Mathf.PingPong(Time.time, duration) / duration;
                rend.sharedMaterial.color = Color.Lerp(color1, color1, lerp);
            }
            if (val > (min + 3*len))
            {
                int greenValue2 = 255 - ((val - (min + 3*len)) / len) * 125;
                float newGreen2 = greenValue2 / 255f;

                Debug.Log(val);

                Color color2 = new Color(1, newGreen2, 0);

                float lerp = Mathf.PingPong(Time.time, duration) / duration;
                rend.sharedMaterial.color = Color.Lerp(color2, color2, lerp);
            }
            if (val > (min + 2*len))
            {
                int redValue1 = ((val - (min + 2 * len)) / len) * 255;
                float newRed1 = redValue1 / 255f;
                Color color3 = new Color(newRed1, 1, 0);

                Debug.Log(val);

                float lerp = Mathf.PingPong(Time.time, duration) / duration;
                rend.sharedMaterial.color = Color.Lerp(color3, color3, lerp);
            }
            if (val > (min + len))
            {
                int blueValue1 = 255 - (val - (min + len)) / len * 125;
                float newBlue1 = blueValue1 / 255f;
                Color color4 = new Color(0, 1, newBlue1);

                Debug.Log(val);

                float lerp = Mathf.PingPong(Time.time, duration) / duration;
                rend.sharedMaterial.color = Color.Lerp(color4, color4, lerp);
            }
            if ((min + len) > val)
            {
                Debug.Log(val);

                Color color5 = new Color(0, 1, 1);
                float lerp = Mathf.PingPong(Time.time, duration) / duration;
                rend.sharedMaterial.color = Color.Lerp(color5, color5, lerp);
            }

            k++;
        }
    }

}

(191127) How to read data from excel to unity? (4)

I wrote a code which change color by csv file.

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using System.IO;

public class EnergyColorNew : MonoBehaviour
{
    Renderer rend;
    readonly float duration = 0.1f;
    private string[] colorData;
    private int colorIndex = 0;
    public float colorChangeTime = 1;
    private float colorChangeTimer = 0;
    public GameObject buil;

    void Start()
    {
        rend = GetComponent<Renderer>();
        ReadCSVFile();
    }

    private void Update()
    {
        colorChangeTimer += Time.deltaTime;
        if (colorChangeTimer >= colorChangeTime)
        {
            colorChangeTimer = 0;
            ChangeColor();
        }
    }

    private void ChangeColor()
    {
        if (colorIndex >= colorData.Length)
            colorIndex = 0;

        string[] row = colorData[colorIndex++].Split(new char[] { ',' });
        

        string valueOne = row[3];
        
        //Debug.Log(valueOne);

        int value = int.Parse(valueOne);
        Debug.Log(value);
        
        if (100000 > value && value > 70000)
        {
            int greenValue = 125 - ((value - 700000) / 551312) * 125;
            float newGreen = greenValue / 255f;
            Color color1 = new Color(1, newGreen, 0);

            float lerp = Mathf.PingPong(Time.time, duration) / duration;
            rend.material.color = Color.Lerp(color1, color1, lerp);
            Debug.Log("the value is 100000 > value && value > 70000");
        }
        if (70000 > value && value > 20000)
        {
            int greenValue2 = 255 - ((value - 200000) / 500000) * 125;
            float newGreen2 = greenValue2 / 255f;
            Color color2 = new Color(1, newGreen2, 0);

            float lerp = Mathf.PingPong(Time.time, duration) / duration;
            rend.material.color = Color.Lerp(color2, color2, lerp);
            Debug.Log("the value is 70000 > value && value > 20000");
        }
        if (value > 100000)
        {
            int redValue1 = ((value - 100000) / 100000) * 255;
            float newRed1 = redValue1 / 255f;
            Color color3 = new Color(newRed1, 1, 0);

            float lerp = Mathf.PingPong(Time.time, duration) / duration;
            rend.material.color = Color.Lerp(color3, color3, lerp);
            Debug.Log("the value is value > 100000");
        }
        if (20000 > value && value > 10000)
        {
            int blueValue1 = 255 - ((value - 10000) / 90000) * 125;
            float newBlue1 = blueValue1 / 255f;
            Color color4 = new Color(0, 1, newBlue1);

            float lerp = Mathf.PingPong(Time.time, duration) / duration;
            rend.material.color = Color.Lerp(color4, color4, lerp);
            Debug.Log("the value is 20000 > value && value > 10000");
        }
        if(10000 > value)
        {
            Color color5 = new Color(0, 1, 1);
            float lerp = Mathf.PingPong(Time.time, duration) / duration;
            rend.material.color = Color.Lerp(color5, color5, lerp);
            Debug.Log("the color is 0, 1, 1");
        }
    }

    // Update is called once per frame
    void ReadCSVFile()
    {
        TextAsset energyData = Resources.Load<TextAsset>("energyData");

        colorData = energyData.text.Split(new char[] { '\n' });
    }
}