Created
October 10, 2018 14:40
-
-
Save akirayou/4362b64d88b63142ad60cd0778b9c05d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
[JsonProperty("host")] | |
public string[] Host { 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) | |
{ | |
Led = new byte[len * 3]; | |
Host = _host; | |
Debug.Log(Host); | |
udp = new UdpClient(); | |
udp.Connect(Host, 13531); | |
SetOffset(200); | |
SetMaxDelay(100); | |
} | |
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 len, int startPos) | |
{ | |
// Debug.Log("send"+len.ToString()); | |
byte[] d = new byte[5 + len*3]; | |
d[0] = sendIdx; | |
int ms = (int)(Time.fixedTime * 1000) % 65536; | |
d[1] = (byte)(ms / 256); | |
d[2] = (byte)(ms % 256); | |
d[3] = (byte)(startPos/8); | |
d[4] = (byte)(len / 2); | |
System.Array.Copy(data, startPos*3, d, 5, len); | |
udp.Send(d, d.Length); | |
} | |
private void SendCommand(byte[] data,byte commandNo) | |
{ | |
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] = commandNo; | |
d[4] = 0; | |
System.Array.Copy(data, 0, d, 5, data.Length); | |
udp.Send(d, d.Length); | |
sendIdx++; | |
} | |
public void SetOffset(int offset) | |
{ | |
SendCommand(new byte[] { (byte)(offset / 256), (byte)(offset % 256) }, 0); | |
} | |
public void SetMaxDelay(int delay) | |
{ | |
SendCommand(new byte[] { (byte)(delay / 256), (byte)(delay % 256) }, 1); | |
} | |
public void Send() | |
{ | |
int dataLen = Led.Length/3; | |
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, chunkLen, i); | |
} | |
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.0f; | |
float delta ; | |
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(data.Host[i], 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()); | |
delta = 0; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
delta += Time.deltaTime; | |
if (delta > timeSpan) | |
{ | |
while(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