Skip to content

Instantly share code, notes, and snippets.

@akirayou
Created October 10, 2018 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirayou/9116f7de324fa7de47891ac46052257d to your computer and use it in GitHub Desktop.
Save akirayou/9116f7de324fa7de47891ac46052257d to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Globalization;
using System.Net.Sockets;
public partial class Data
{
[JsonProperty("level")]
public float[][] Level { get; set; }
[JsonProperty("pos")]
public float[][][] Pos { get; set; }
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters = {
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
public class UnitData
{
string Host;
byte[] Led;
UdpClient udp;
byte sendIdx = 0;
public UnitData(string _host, int len)
{
Init(_host, len);
}
public void Init(string _host,int len)
{
udp = new UdpClient();
udp.Connect(_host, 13531);
Led = new byte[len * 3];
Host = _host;
}
public void SetColor(int index,byte r,byte g,byte b)
{
Led[index * 3] = r;
Led[index * 3+1] = g;
Led[index * 3+2] = b;
}
private void send1(byte[] data,int idx,int len,byte startPos_Q)
{
byte[] d=new byte[5+data.Length];
d[0] = sendIdx;
int ms=(int)(Time.fixedTime*1000) % 65536 ;
d[1] = (byte)(ms / 256);
d[2] = (byte)(ms % 256);
d[3] = startPos_Q;
d[4] = (byte)(data.Length / 2);
System.Array.Copy(data,idx, d,5 ,len);
udp.Send(d, d.Length);
}
public void Send()
{
int dataLen = Led.Length;
int chunkMax= ((1472-6)/3/8)*8;
for(int i = 0; i < dataLen; i += chunkMax)
{
int chunkLen = dataLen - i;
if (chunkMax < chunkLen) chunkLen = chunkMax;
send1(Led, i, chunkLen,(byte)(i/8));
}
sendIdx++;
}
}
public class Led
{
public Vector3 Pos;
GameObject Obj;
UnitData Unit;
Material Material;
int Index;
public Led(UnitData unit,int index, Vector3 pos, GameObject LedObj)
{
Init( unit, index, pos, LedObj);
}
public void Init(UnitData unit,int index,Vector3 pos,GameObject LedObj)
{
Unit = unit;
Obj = LedObj;
Material=Obj.GetComponent<Renderer>().material;
Pos = pos;
Index = index;
}
public void SetColor(Color32 col)
{
Unit.SetColor(Index, (byte)(col.r * 255), (byte)(col.g * 255), (byte)(col.b * 255));
Material.SetColor("_EmissionColor",col);
}
}
public class Setting : MonoBehaviour {
public GameObject prefLED;
Data data;
// Use this for initialization
UnitData[] Units = { };
List<Led> Leds=new List<Led>();
public float timeSpan = 1 / 15;
float delta = 0;
IEnumerator GetFromWWW()
{
WWW www = new WWW("http://127.0.0.1:8000/pos.json");
while (!www.isDone)
{ // ダウンロードの進捗を表示
//print(Mathf.CeilToInt(www.progress * 100));
yield return null;
}
if (!string.IsNullOrEmpty(www.error))
{ // ダウンロードでエラーが発生した
print(www.error);
}
else
{ // ダウンロードが正常に完了した
//print(JsonUtility.ToJson(new Data()));
data = JsonConvert.DeserializeObject<Data>(www.text, Converter.Settings);
Units = new UnitData[data.Pos.Length];
for (int i = 0; i < data.Pos.Length; i++)
{
Units[i]=new UnitData("127.0.0.1", data.Pos[i].Length);
for (int j = 0; j < data.Pos[i].Length; j++)
{
if (data.Level[i][j] < 0.5) continue;
Vector3 pos = new Vector3(data.Pos[i][j][0], data.Pos[i][j][2]*0, data.Pos[i][j][1]);
GameObject o = Instantiate(prefLED);
Leds.Add(new Led(Units[i], j, pos, o));
o.transform.position = pos;
}
}
}
}
void Start () {
StartCoroutine(GetFromWWW());
}
// Update is called once per frame
void Update () {
delta += Time.deltaTime;
if (delta > timeSpan)
{
delta -= timeSpan;
foreach (Led led in Leds)
{
led.SetColor(new Color(Random.Range(0.1f, 1), Random.Range(0.1f, 1), Random.Range(0.1f, 1)));
}
foreach (UnitData unit in Units){
unit.Send();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment