void CreateMesh(ref List <Vector3> vertexbuf, GameObject feaObj) { List <float> lstTopPartLen = new List <float>(); List <float> lstBottomPartLen = new List <float>(); var road2Sidelen = SpeedRoadUtils.GetRoad2SideLen(vertexbuf, ref lstTopPartLen, ref lstBottomPartLen); var roadCenterlen = (road2Sidelen.x + road2Sidelen.y) / 2; string roadtype = "s" + s2e_ways.ToString() + "e" + e2s_ways.ToString(); if (roadCenterlen - SpeedRoad.RoadZebraCrossingLength * 2 > 0) // 两端斑马线最少占用长度 { GameObject zebraCrossingHead = SpeedRoadUtils.CreateMesh_Part(ref vertexbuf, feaObj, SpeedRoad.RoadZebraCrossingLength, "zchead", ref vertexZebraCrossingHead); SetTexture(vertexZebraCrossingHead, zebraCrossingHead, "ZebraCrossing", 4); GameObject zebraCrossingTail = SpeedRoadUtils.CreateMesh_Part(ref vertexbuf, feaObj, SpeedRoad.RoadZebraCrossingLength, "zctail", ref vertexZebraCrossingTail, false); SetTexture(vertexZebraCrossingTail, zebraCrossingTail, "ZebraCrossing", 4); road2Sidelen = SpeedRoadUtils.GetRoad2SideLen(vertexbuf, ref lstTopPartLen, ref lstBottomPartLen); roadCenterlen = (road2Sidelen.x + road2Sidelen.y) / 2; bool bNotOneWay = e2s_ways > 0 && s2e_ways > 0; if (bNotOneWay) // 双向车道 { if (roadCenterlen - SpeedRoad.RoadWaitingLength * 2 > 0) // 分割双向候车区 { GameObject waitingHead = SpeedRoadUtils.CreateMesh_Part(ref vertexbuf, feaObj, SpeedRoad.RoadWaitingLength, "waitinghead", ref vertexWaitingHead); SetTextureWait(vertexWaitingHead, waitingHead, true, e2s_ways); GameObject waitingTail = SpeedRoadUtils.CreateMesh_Part(ref vertexbuf, feaObj, SpeedRoad.RoadWaitingLength, "waitingtail", ref vertexWaitingTail, false); SetTextureWait(vertexWaitingTail, waitingTail, false, s2e_ways); } } else // 单行道 { if (roadCenterlen - SpeedRoad.RoadWaitingLength > 0) // 分割单向候车区 { if (e2s_ways > 0) { GameObject waitingHead = SpeedRoadUtils.CreateMesh_Part(ref vertexbuf, feaObj, SpeedRoad.RoadWaitingLength, "waitinghead", ref vertexWaitingHead); SetTextureWait(vertexWaitingHead, waitingHead, true, e2s_ways, true); } else if (s2e_ways > 0) { GameObject waitingTail = SpeedRoadUtils.CreateMesh_Part(ref vertexbuf, feaObj, SpeedRoad.RoadWaitingLength, "waitingtail", ref vertexWaitingTail, false); SetTextureWait(vertexWaitingTail, waitingTail, false, s2e_ways, true); } else { Assert.IsTrue(false); } } } } GameObject road = SpeedRoadUtils.CreateSegment(vertexbuf, feaObj, "road"); SetTexture(vertexbuf, road, roadtype, 1); vertexRoad = vertexbuf; }
void SetTexture(List <Vector3> vertex, GameObject part, string texname, float repeatV) { var tex = SpeedRoadTexMgr.Instance.GetTex(texname); part.GetComponent <MeshRenderer>().material.mainTexture = tex; part.GetComponent <MeshRenderer>().material.shader = Shader.Find("Transparent/Diffuse"); List <float> lstTopPartLen = new List <float>(); List <float> lstBottomPartLen = new List <float>(); var len = SpeedRoadUtils.GetRoad2SideLen(vertex, ref lstTopPartLen, ref lstBottomPartLen); Vector2[] uv1 = new Vector2[vertex.Count]; float repeatU = 1; if (part.name == "road") { repeatU = (len.x + len.y) / 2 / SpeedRoad.RoadwayWidth; } uv1[0].Set(0, repeatV); uv1[1].Set(0, 0); Vector2 offset = Vector2.zero; for (int i = 2; i <= vertex.Count - 2; i += 2) { var ratetop = (offset.x + lstTopPartLen[i / 2 - 1]) / len.x; uv1[i].Set(repeatU * ratetop, repeatV); var ratebottom = (offset.y + lstBottomPartLen[i / 2 - 1]) / len.y; uv1[i + 1].Set(repeatU * ratebottom, 0); offset.x += lstTopPartLen[i / 2 - 1]; offset.y += lstBottomPartLen[i / 2 - 1]; } uv1[vertex.Count - 2].Set(repeatU, repeatV); uv1[vertex.Count - 1].Set(repeatU, 0); part.GetComponent <MeshFilter>().mesh.uv = uv1; }
void SetTextureWait(List <Vector3> vertex, GameObject part, bool bhead, int ways, bool oneway = false) { List <float> lstTopPartLen = new List <float>(); List <float> lstBottomPartLen = new List <float>(); var len = SpeedRoadUtils.GetRoad2SideLen(vertex, ref lstTopPartLen, ref lstBottomPartLen); Vector2[] uv1 = new Vector2[vertex.Count]; if (bhead) { uv1[0].Set(0, 1); uv1[1].Set(0, 0); Vector2 offset = Vector2.zero; for (int i = 2; i <= vertex.Count - 2; i += 2) { var ratetop = (offset.x + lstTopPartLen[i / 2 - 1]) / len.x; uv1[i].Set(ratetop, 1); var ratebottom = (offset.y + lstBottomPartLen[i / 2 - 1]) / len.y; uv1[i + 1].Set(ratebottom, 0); offset.x += lstTopPartLen[i / 2 - 1]; offset.y += lstBottomPartLen[i / 2 - 1]; } uv1[vertex.Count - 2].Set(1, 1); uv1[vertex.Count - 1].Set(1, 0); } else { uv1[0].Set(1, 0); uv1[1].Set(1, 1); Vector2 offset = Vector2.zero; for (int i = 2; i <= vertex.Count - 2; i += 2) { var ratetop = (offset.x + lstTopPartLen[i / 2 - 1]) / len.x; uv1[i].Set(1 - ratetop, 0); var ratebottom = (offset.y + lstBottomPartLen[i / 2 - 1]) / len.y; uv1[i + 1].Set(1 - ratebottom, 1); offset.x += lstTopPartLen[i / 2 - 1]; offset.y += lstBottomPartLen[i / 2 - 1]; } uv1[vertex.Count - 2].Set(0, 0); uv1[vertex.Count - 1].Set(0, 1); } part.GetComponent <MeshFilter>().mesh.uv = uv1; if (oneway) { Texture2D tex = SpeedRoadTexMgr.Instance.GetTex("wait" + ways.ToString() + "ways"); // Color32[] colmain = tex.GetPixels32(); // var te = new Texture2D(tex.width, tex.height); // te.SetPixels32(colmain); // // // Texture2D arrowtex = SpeedRoadTexMgr.Instance.GetTex("arrowforward"); // Color32[] colarrow = arrowtex.GetPixels32(); // te.SetPixels32(colarrow); // for (int m = 0; m < 128; m++) // { // for (int n = 0; n < 128; n++) // { // te.SetPixel(m, n, Color.red); // } // } // te.Apply(); part.GetComponent <MeshRenderer>().material.mainTexture = tex; part.GetComponent <MeshRenderer>().material.shader = Shader.Find("Transparent/Diffuse"); } else { Material mat = part.GetComponent <MeshRenderer>().material; var shader = Shader.Find("speedroad/waitarea"); mat.shader = shader; var textop = SpeedRoadTexMgr.Instance.GetTex("wait" + ways.ToString() + "waystop"); Texture2D texbottom = SpeedRoadTexMgr.Instance.GetTex("wait" + ways.ToString() + "waysbottom"); mat.SetTexture("_Tex1", textop); mat.SetTexture("_Tex2", texbottom); var repeatU = (len.x + len.y) / 2 / SpeedRoad.RoadwayWidth; mat.SetTextureScale("_Tex2", new Vector2(repeatU, 1)); } }