void MeshSetup() { var meshFilter = gameObject.GetComponent <MeshFilter>(); if (meshFilter.sharedMesh != null) { DestroyImmediate(meshFilter.sharedMesh, true); } mesh = new Mesh(); mesh.name = name; meshFilter.sharedMesh = mesh; mesh.vertices = Vertices.ToArray(); mesh.triangles = Triangles.ToArray(); mesh.uv = UV.ToArray(); var type = XReflectionUtils.GetPrefabType(gameObject); if (type == "Prefab") { XReflectionUtils.AddObjectToObject(mesh, gameObject); } }
void InitMesh() { //-----------------Head-----------------// var mesh = new Mesh(); mesh1.sharedMesh = mesh; List <Vector3> Vertices = new List <Vector3> (); List <Vector2> UV = new List <Vector2> (); List <int> Triangles = new List <int> (); List <Color> Colors = new List <Color> (); Vector3 lastLT = Vector3.zero; Vector3 lastLD = Vector3.zero; Vector3 leftTop; Vector3 leftDown; Vector3 rightDown; Vector3 rightTop; float y = step * yWidthCurve.Evaluate(0.02f) / 2; float y2 = step * yWidthCurve.Evaluate(0.01f) / 2; var p = GetBezierCurve(position0, position1, position2, position3, -headWid); var np = GetBezierCurve(position0, position1, position2, position3, 0f); rightTop = new Vector3(step, step * y2 * 0.5f) + np; rightDown = new Vector3(step, -y2 * 0.5f) + np; leftTop = new Vector3(-step, step * y * 0.5f) + p; leftDown = new Vector3(-step, -y * 0.5f) + p; Vertices.Add(leftTop); Vertices.Add(leftDown); Vertices.Add(rightTop); Vertices.Add(rightDown); UV.Add(new Vector2(0, 1)); UV.Add(new Vector2(0, 0)); UV.Add(new Vector2(1, 1)); UV.Add(new Vector2(1, 0)); Triangles.AddRange(new List <int> { 0, 2, 3 }); Triangles.AddRange(new List <int> { 0, 3, 1 }); mesh.vertices = Vertices.ToArray(); mesh.uv = UV.ToArray(); mesh.triangles = Triangles.ToArray(); mesh.name = mesh1.name; var type = XReflectionUtils.GetPrefabType(gameObject); if (type == "Prefab") { XReflectionUtils.AddObjectToObject(mesh, mesh1.gameObject); } //-----------------Trail-----------------// Vertices.Clear(); UV.Clear(); Triangles.Clear(); Colors.Clear(); mesh = new Mesh(); mesh2.sharedMesh = mesh; int currIndex = 0; float UVDelta = 1.0f / UVCount; for (int i = 0; i < stepCount; i++) { float t = i * 1.0f / stepCount; float nextT = (i + 1) * 1.0f / stepCount; int uvIndex = i; float leftUV = uvIndex * UVDelta; float rightUV = (uvIndex + 1) * UVDelta; Color leftColor = gradient.Evaluate(t); Color rightColor = gradient.Evaluate(nextT); p = GetBezierCurve(position0, position1, position2, position3, t); y = step * yWidthCurve.Evaluate(t) / 2; if (lastLT != Vector3.zero) { leftTop = lastLT; } else { leftTop = new Vector3(step, step * y * 0.5f) + p; Vertices.Add(leftTop); UV.Add(new Vector2(leftUV, 1)); Colors.Add(leftColor); } if (lastLD != Vector3.zero) { leftDown = lastLD; } else { leftDown = new Vector3(step, -y * 0.5f) + p; Vertices.Add(leftDown); UV.Add(new Vector2(leftUV, 0)); Colors.Add(leftColor); } if (i == 0) { rightTop = new Vector3(step, step * y * 0.5f) + p; rightDown = new Vector3(step, -y * 0.5f) + p; } else { rightTop = new Vector3(step, step * y * 0.5f) + p; rightDown = new Vector3(step, -y * 0.5f) + p; } Vertices.Add(rightTop); UV.Add(new Vector2(rightUV, 1)); Colors.Add(rightColor); Vertices.Add(rightDown); UV.Add(new Vector2(rightUV, 0)); Colors.Add(rightColor); lastLT = new Vector3(rightTop.x, rightTop.y * 0.5f, rightTop.z); lastLD = new Vector3(rightDown.x, rightDown.y * 0.5f, rightDown.z); currIndex = i * 2; Triangles.AddRange(new List <int> { currIndex, currIndex + 2, currIndex + 3 }); Triangles.AddRange(new List <int> { currIndex, currIndex + 3, currIndex + 1 }); } mesh.vertices = Vertices.ToArray(); mesh.uv = UV.ToArray(); mesh.triangles = Triangles.ToArray(); mesh.name = mesh2.name; mesh.colors = Colors.ToArray(); type = XReflectionUtils.GetPrefabType(gameObject); if (type == "Prefab") { XReflectionUtils.AddObjectToObject(mesh, mesh2.gameObject); } }