基本上我有一个类根据sqlite数据库和计时器的值来绘制我的汽车动画。这是代码:
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Globalization;
using System.Collections.Generic;
public class Test : MonoBehaviour {
public class NodeData
{
public int time;
public int posX;
public int posY;
public int posZ;
public float temp;
}
public class VehicleData
{
public VehicleData()
{
time = new List<float>();
posX = new List<float>();
}
public List<float> time;
public List<float> posX;
}
private int maxLength = 700;//10500/15;
private int maxWidth = 13;//225/15;//(int) Math.Ceiling(200.0f/15.0f)+1;
private int maxHeight = 15;//150/15;
private int maxTime = 61;
public GameObject MainHoldingobj;
public Texture crosshair;
private Main getfromMain;
private float dividedValue;
float minTemp = 100;
float maxTemp;
List<VehicleData> vehicleData;
float[][,,] data;
int[,,] lastTimeData;
Material mat;
public Material transMat;
GameObject[,,] cubes;
ParticleSystem.Particle[,,] tempPart = new ParticleSystem.Particle[50,10,40];
//NodeData[][,,] testYo = new NodeData[maxLength/gridBoxSize,maxHeight/gridBoxSize,maxWidth/gridBoxSize];
public float oneSecTimer;
public float timer;
[Range(0,1)]
public float alphaValue;
public float carTransparency = 0.5f;
public bool lockTime = true;
public bool showAll = true;
public bool showCar = true;
public GameObject RcCube;
Vector3 vehicleSize = new Vector3(450/100,135/66,165/80);
int[] carCounter;
float[] carPosCounter;
SqliteDatabase sqlDB;
DataTable Heatmapdt;
DataTable abmdt;
string query = "";
public enum dataType
{
air,
ground,
building
}
public float getDataTypeAlpha(dataType type)
{
//return transparency based on type range 0-1
switch (type)
{
case dataType.air :
return 0.1f;
break;
case dataType.ground :
return 1.0f;
break;
case dataType.building :
return 0.5f;
break;
// no such type
default:
return 0.0f;
}
}
void readVehicleData ()
{
int id;
float tempX;
float tempTime;
for (int i = 0; i < abmdt.Rows.Count; i++) {
id = int.Parse(abmdt[i]["ID"].ToString());
if(id > vehicleData.Count-1)
{
VehicleData tempData = new VehicleData();
tempX = (float.Parse(abmdt[i]["PosX"].ToString()));
tempTime = float.Parse(abmdt[i]["Time"].ToString());
tempData.time.Add(tempTime/1000);
tempData.posX.Add(tempX*100.0f/15.0f);
vehicleData.Add(tempData);
}
else{
vehicleData[int.Parse(abmdt[i]["ID"].ToString ())].time.Add(float.Parse(abmdt[i]["Time"].ToString())/1000);
vehicleData[int.Parse(abmdt[i]["ID"].ToString ())].posX.Add((float.Parse(abmdt[i]["PosX"].ToString()))*100.0f/15.0f);
}
}
carCounter = new int[vehicleData.Count];
carPosCounter = new float[vehicleData.Count];
for (int i=0; i<vehicleData.Count; i++) {
carCounter[i] = -1;
carPosCounter[i] = 0;
}
// Debug.Log (maxX + " " + maxY + " " + maxZ + " " + maxTemp + " " + maxTime);
}
void readStreamLineData ()
{
var reader = new StreamReader (File.OpenRead ("ccs_data2/heatmap_output_all_vehicles_a.csv")); //heatmap_output_all_one_cm
int maxX = 0, maxY=0, maxZ =0;
float maxTemp = 0, maxTime = 0;
while (!reader.EndOfStream)
{
NodeData tempData = new NodeData();
var line = reader.ReadLine ();
var values = line.Split (',');
if (values.Length == 5)
{
if (int.TryParse (values [0], out tempData.time)
&& int.TryParse (values [1], out tempData.posX)
&& int.TryParse (values [2], out tempData.posY)
&& int.TryParse (values [3], out tempData.posZ)
&& float.TryParse (values [4], NumberStyles.Float, CultureInfo.InvariantCulture, out tempData.temp))
{
// if(tempData.posX > maxX)
// maxX =tempData.posX;
//
// if(tempData.posY > maxY)
// maxY =tempData.posY;
//
// if(tempData.posZ > maxZ)
// maxZ =tempData.posZ;
//
// if(tempData.temp > maxTemp)
// maxTemp =tempData.temp;
//
// if(tempData.time > maxTime)
// maxTime =tempData.time;
//
// if(tempData.posX > maxLength-1)
// Debug.Log("Alert x");
// else if(tempData.posY > maxHeight-1)
// Debug.Log("Alert y");
// else if(tempData.posZ > maxWidth-1)
// Debug.Log("Alert z");
// else if(tempData.time/1000 > maxTime-1)
// Debug.Log("Alert time");
data[tempData.time/1000][tempData.posX,tempData.posY,tempData.posZ] = tempData.temp;
}
else
{
continue;
}
}
}
// Debug.Log (maxX + " " + maxY + " " + maxZ + " " + maxTemp + " " + maxTime);
}
void findMinMax()
{
for(int t =0;t<maxTime;t++)
{
for(int x =0;x<maxLength;x++)
{
for(int y =0;y<maxHeight;y++)
{
for(int z =0;z<maxWidth;z++)
{
if(data[t][x,y,z] < minTemp && data[t][x,y,z] !=0)
minTemp = data[t][x,y,z];
if(data[t][x,y,z] > maxTemp && data[t][x,y,z] <100)
{
//Debug.Log ("maxTemp: + " + maxTemp + "Data: " + data[t][x,y,z]);
maxTemp = data[t][x,y,z];
//Debug.Log ("maxTemp2: + " + maxTemp + "Data2: " + data[t][x,y,z]);
}
}
}
}
}
//dividedValue = (int)((maxTemp - minTemp)/0.7f);
dividedValue = ((maxTemp - minTemp)/0.7f);
}
void createCubes()
{
for(int x =0;x<50;x++)
{
for(int y =0;y<maxHeight;y++)
{
for(int z =10;z<30;z++)
{
cubes[x,y,z] = new GameObject();
cubes[x,y,z] = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubes[x,y,z].transform.position = new Vector3(x, y, z);
cubes[x,y,z].GetComponent<Renderer>().material= new Material(transMat);
}
}
}
}
void createParticles()
{
for(int x =0;x<50;x++)
{
for(int y =0;y<maxHeight;y++)
{
for(int z =10;z<30;z++)
{
tempPart[x,y,z].position = new Vector3(x, y, z);
}
}
}
}
void initializeData()
{
vehicleData = new List<VehicleData> ();
cubes = new GameObject[maxLength, maxHeight, maxWidth];
data = new float[maxTime][,,];
lastTimeData = new int[maxLength,maxHeight,maxWidth];
for (int i = 0; i < maxTime; i++)
data[i] = new float[maxLength,maxHeight,maxWidth];
for(int t =0;t<maxTime;t++)
{
for(int x =0;x<maxLength;x++)
{
for(int y =0;y<maxHeight;y++)
{
for(int z =0;z<maxWidth;z++)
{
data[t][x,y,z] = 0;
}
}
}
}
//last data time
for (int x =0; x<maxLength; x++)
{
for (int y =0; y<maxHeight; y++)
{
for (int z =0; z<maxWidth; z++)
{
lastTimeData[x,y,z] = 0;
}
}
}
}
// Use this for initialization
void Start () {
//tt = mCam.GetComponent<testingtime> ();
getfromMain = MainHoldingobj.GetComponent<Main> ();
sqlDB = new SqliteDatabase("Test01");
query = "SELECT * FROM Heatmap WHERE Time ="+((int)(detailedTimer * 1000));
Heatmapdt = sqlDB.ExecuteQuery(query);
string query2 = "SELECT * FROM abm WHERE ID = 0";
abmdt = sqlDB.ExecuteQuery (query2);
initializeData();
readStreamLineData();
readVehicleData ();
//createCubes();
//createParticles();
findMinMax();
}
// Update is called once per frame
void Update () {
// for(int x =0;x<50;x++)
// {
// for(int y =0;y<maxHeight;y++)
// {
// for(int z =10;z<30;z++)
// {
// cubes[x,y,z].GetComponent<Renderer>().material.color = returnColour((int)timer,x,y,z);
// }
// }
// }
//
// for(int x =0;x<50;x++)
// {
// for(int y =0;y<maxHeight;y++)
// {
// for(int z =10;z<30;z++)
// {
// tempPart[x,y,z].color = returnColour((int)timer,x,y,z);
// }
// }
// }
// if(!showAll)
// {
// for(int x =0;x<maxLength;x++)
// {
// for(int y =0;y<maxHeight;y++)
// {
// for(int z =0;z<maxWidth;z++)
// {
// if( data[(int)timer][x,y,z] !=0)
// {
// cubes[x,y,z].SetActive(true);
// }
// else
// {
// cubes[x,y,z].SetActive(false);
// }
// }
// }
// }
// }
}
public float detailedTimer = 0;
void FixedUpdate()
{
if(!lockTime)
{
if(timer+1 >maxTime)
{
timer = 0;
detailedTimer = 0;
for(int i=0; i< vehicleData.Count;i++)
{ carCounter[i] = -1;
carPosCounter[i] = 0;
}
}
if(oneSecTimer <1)
oneSecTimer +=Time.fixedDeltaTime;
else
{
oneSecTimer = 0;
//timer ++;
}
// detailedTimer = (float)tt.playtime;
detailedTimer = getfromMain.timer;
timer = getfromMain.timer;
//detailedTimer += Time.fixedDeltaTime;
//update each car
for(int i=0; i< vehicleData.Count;i++)
{
if(carCounter[i]+1 < vehicleData[i].time.Count)
{
if(getfromMain.timer < vehicleData[i].time[carCounter[i]+1])
{
if(carCounter[i] != -1)
{
//interpolation so that it will move smoothly?
//carPosCounter[i] +=
carPosCounter[i] = 0;
}
}
else
{
carCounter[i]++;
//check if it has exceeded a lot? put some space
carPosCounter[i] = 0;
}
}
}
}
//Debug.Log (timer);
}
void OnDrawGizmosSelected() {
//
// for(int x =0;x<maxLength;x++)
// {
// for(int y =0;y<maxHeight;y++)
// {
// for(int z =0;z<maxWidth;z++)
// {
//
// if(showAll)
// {
// Gizmos.color = returnColour((int)timer,x,y,z);
// Gizmos.DrawCube(new Vector3(x,y,z), new Vector3(1, 1, 1));
// }
// else
// {
// if( data[(int)timer][x,y,z] !=0)
// {
// Gizmos.color = returnColour((int)timer,x,y,z);
// Gizmos.DrawCube(new Vector3(x,y,z), new Vector3(1, 1, 1));
// }
// }
//
// }
// }
// }
// Gizmos.color = new Color32(255, 255, 255, 155);
//
// Gizmos.DrawCube(new Vector3(0,10,0), new Vector3(1, 1, 1));
// Debug.Log ("hi");
}
void OnPostRender()
{
if (!mat)
{
mat = new Material ("Shader \"Lines/Colored Blended\" {" +
"SubShader {" +
" Tags { \"Queue\" = \"Transparent\" }" +
" Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
//" Blend One One " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
// mat = new Material(shader);
mat.hideFlags = HideFlags.HideAndDontSave;
mat.shader.hideFlags = HideFlags.HideAndDontSave;
}
//draw cubes
GL.PushMatrix ();
mat.SetPass (0);
// GL.Begin (GL.QUADS);
// for(int x =0;x<maxLength;x++)
// {
// for(int y =0;y<maxHeight;y++)
// {
// for(int z =0;z<maxWidth;z++)
// {
// if(showAll)
// {
// GL.Color( returnColour((int)timer,x,y,z));
// drawCube(x,y,z,1.0f);
// }
//
// else
// {
// if(data[(int)timer][x,y,z] !=0)
// {
// GL.Color( returnColour((int)timer,x,y,z));
// drawCube(x,y,z,1.0f);
// }
// }
//
//
// }
// }
// }
// GL.End ();
GL.PopMatrix ();
//draw car
GL.PushMatrix ();
mat.SetPass (0);
GL.Begin (GL.QUADS);
int tempi = 0;
for(int i =0;i<vehicleData.Count;i++)
{
// if(vehicleData[i].time[] < getfromMain.timer)
// continue;
// else
// tempi = i;
if(showCar)
{
if((carCounter[i] != -1) && (carCounter[i] < (vehicleData[i].time.Count-1)) )//&& vehicleData[i].time[carCounter[i]] < maxTime
{
GL.Color(new Color(1,0,0,carTransparency));
drawCar(vehicleData[i].posX[carCounter[i]]+carPosCounter[i]);
Vector3 mampos = new Vector3(0,1.5f,1.5f);
mampos.x = (vehicleData[i].posX[carCounter[i]]+carPosCounter[i])+1.5f;
RcCube.transform.position = mampos;
}
}
}
GL.End ();
GL.PopMatrix ();
}
void drawCube(int x,int y,int z, float size)
{
size = size/2;
//GL.Color( returnColour((int)timer,0,0,0));
GL.Vertex3(x + size,y + size, z + size);
GL.Vertex3(x - size,y + size, z + size);
GL.Vertex3(x - size,y + size, z - size);
GL.Vertex3(x + size,y + size, z - size);
GL.Vertex3(x + size,y - size, z + size);
GL.Vertex3(x - size,y - size, z + size);
GL.Vertex3(x - size,y - size, z - size);
GL.Vertex3(x + size,y - size, z - size);
GL.Vertex3(x + size,y + size, z + size);
GL.Vertex3(x + size,y - size, z + size);
GL.Vertex3(x + size,y - size, z - size);
GL.Vertex3(x + size,y + size, z - size);
GL.Vertex3(x - size,y + size, z + size);
GL.Vertex3(x - size,y - size, z + size);
GL.Vertex3(x - size,y - size, z - size);
GL.Vertex3(x - size,y + size, z - size);
GL.Vertex3(x + size,y + size, z + size);
GL.Vertex3(x - size,y + size, z + size);
GL.Vertex3(x - size,y - size, z + size);
GL.Vertex3(x + size,y - size, z + size);
GL.Vertex3(x + size,y + size, z - size);
GL.Vertex3(x - size,y + size, z - size);
GL.Vertex3(x - size,y - size, z - size);
GL.Vertex3(x + size,y - size, z - size);
}
void drawCar(float x)
{
GL.Vertex3(x + vehicleSize.x,vehicleSize.y, vehicleSize.z);
GL.Vertex3(x,vehicleSize.y, vehicleSize.z);
GL.Vertex3(x,vehicleSize.y, 1);
GL.Vertex3(x + vehicleSize.x,vehicleSize.y, 1);
GL.Vertex3(x + vehicleSize.x,0, vehicleSize.z);
GL.Vertex3(x,0, vehicleSize.z);
GL.Vertex3(x,0, 1);
GL.Vertex3(x + vehicleSize.x,0, 1);
GL.Vertex3(x + vehicleSize.x,vehicleSize.y, vehicleSize.z);
GL.Vertex3(x + vehicleSize.x,0, vehicleSize.z);
GL.Vertex3(x + vehicleSize.x,0, 1);
GL.Vertex3(x + vehicleSize.x,vehicleSize.y, 1);
GL.Vertex3(x,vehicleSize.y, vehicleSize.z);
GL.Vertex3(x,0, vehicleSize.z);
GL.Vertex3(x,0, 1);
GL.Vertex3(x,vehicleSize.y, 1);
GL.Vertex3(x + vehicleSize.x,vehicleSize.y, vehicleSize.z);
GL.Vertex3(x,vehicleSize.y, vehicleSize.z);
GL.Vertex3(x,0, vehicleSize.z);
GL.Vertex3(x + vehicleSize.x,0, vehicleSize.z);
GL.Vertex3(x + vehicleSize.x,vehicleSize.y, 1);
GL.Vertex3(x,vehicleSize.y, 1);
GL.Vertex3(x,0, 1);
GL.Vertex3(x + vehicleSize.x,0, 1);
}
Color returnColour(int t,int x,int y,int z)
{
if ((t + 1) < maxTime) {
float temp = 0;
temp = data [t] [x, y, z];
if (temp != 0)
lastTimeData [x, y, z] = t;
else
temp = data [lastTimeData [x, y, z]] [x, y, z];
float temp2 = 0;
if (data [t + 1] [x, y, z] != 0)
temp2 = data [t + 1] [x, y, z];
else
temp2 = data [lastTimeData [x, y, z]] [x, y, z];
// float value = maxTemp - temp;
// float value2 = maxTemp - temp2;
//
// Color lerping =Color.Lerp(HSBColor.ToColor(new HSBColor(value/dividedValue,1.0f,1.0f,alphaValue)),HSBColor.ToColor(new HSBColor(value2/dividedValue,1.0f,1.0f,alphaValue)),oneSecTimer);
// if (temp < 100)
// return lerping;
// else
// //return new Color(1,1,1,1);
// return HSBColor.ToColor (new HSBColor (1, 1.0f, 1.0f, alphaValue));//full spectrum
// temp = data[x,y,z].temp[id];
float value = maxTemp - temp;
Color lerping = HSBColor.ToColor (new HSBColor (value / dividedValue, 1.0f, 1.0f, alphaValue));
return lerping;
// float value = temp/(maxTemp - temp);
// if (temp < 50)
// return HSBColor.ToColor (new HSBColor (value * 0.7f, 1.0f, 0.5f));
// else
// return new Color(1,0,0,1);
// if(temp > 50)
// return new Color(1,0,0,0.5f); //red
// else if(temp > 45)
// return new Color(1,0.5f,0,0.5f); //
// else if(temp > 40)
// return new Color(0,1,0,0.5f); //green
// else if(temp > 35)
// return new Color(0,1,0.5f,0.5f); //
// else
// return new Color(0,0,1,0.5f); //blue
} else
return new Color (0, 0, 1, 0.5f);
}
}
代码相当混乱,并且周围有很多未使用的东西。您需要查看的主要类是VehicleData,ReadVehicleData,Start,fixedupdate和onpostrender。
所以问题是每当我改变详细信息的值时,动画就停在那里我希望能够用详细的值来控制动画但是为了做到这一点我需要知道carcounter的值[i]并且在那段时间里有车辆[i]。任何人都可以帮我这个吗?