5 using System.Collections.Generic;
10 [UseWith(typeof(LineBase))]
13 [Header(
"Strip Mesh Settings")]
19 private float uvOffset = 0f;
21 private Mesh stripMesh;
22 private MeshRenderer stripMeshRenderer;
23 private Material lineMatInstance;
24 private List<Vector3> positions =
new List<Vector3>();
25 private List<Vector3> forwards =
new List<Vector3>();
26 private List<Color> colors =
new List<Color>();
27 private List<float> widths =
new List<float>();
29 private GameObject meshRendererGameObject;
33 if (LineMaterial == null)
35 Debug.LogError(
"Line material cannot be null.");
40 lineMatInstance =
new Material(LineMaterial);
43 if (stripMesh == null)
45 stripMesh =
new Mesh();
48 if (stripMeshRenderer == null)
50 meshRendererGameObject =
new GameObject(
"Strip Mesh Renderer");
51 stripMeshRenderer = meshRendererGameObject.AddComponent<MeshRenderer>();
54 stripMeshRenderer.sharedMaterial = lineMatInstance;
55 stripMeshRenderer.shadowCastingMode =
UnityEngine.Rendering.ShadowCastingMode.Off;
56 stripMeshRenderer.receiveShadows =
false;
57 stripMeshRenderer.lightProbeUsage =
UnityEngine.Rendering.LightProbeUsage.Off;
59 MeshFilter stripMeshFilter = stripMeshRenderer.EnsureComponent<MeshFilter>();
60 stripMeshFilter.sharedMesh = stripMesh;
63 private void OnDisable()
65 if (lineMatInstance != null)
67 GameObject.Destroy(lineMatInstance);
70 if (meshRendererGameObject != null)
72 GameObject.Destroy(meshRendererGameObject);
73 stripMeshRenderer = null;
81 stripMeshRenderer.enabled =
false;
85 stripMeshRenderer.enabled =
true;
91 for (
int i = 0; i <= NumLineSteps; i++)
93 float normalizedDistance = (1f / (NumLineSteps - 1)) * i;
94 positions.Add(Source.GetPoint(normalizedDistance));
95 colors.Add(GetColor(normalizedDistance));
96 widths.Add(GetWidth(normalizedDistance));
97 forwards.Add(Source.GetRotation(normalizedDistance) * Vector3.down);
100 GenerateStripMesh(positions, colors, widths, uvOffset, forwards, stripMesh);
103 public static void GenerateStripMesh(List<Vector3> positionList, List<Color> colorList, List<float> thicknessList,
float uvOffsetLocal, List<Vector3> forwardList, Mesh mesh)
105 int vertexCount = positionList.Count * 2;
106 int colorCount = colorList.Count * 2;
107 int uvCount = positionList.Count * 2;
109 if (stripMeshVertices == null || stripMeshVertices.Length != vertexCount)
111 stripMeshVertices =
new Vector3[vertexCount];
113 if (stripMeshColors == null || stripMeshColors.Length != colorCount)
115 stripMeshColors =
new Color[colorCount];
117 if (stripMeshUvs == null || stripMeshUvs.Length != uvCount)
119 stripMeshUvs =
new Vector2[uvCount];
122 for (
int x = 0; x < positionList.Count; x++)
124 Vector3 forward = forwardList[x / 2];
125 Vector3 right = Vector3.Cross(forward, Vector3.up).normalized;
126 float thickness = thicknessList[x / 2] / 2;
127 stripMeshVertices[2 * x] = positionList[x] - right * thickness;
128 stripMeshVertices[2 * x + 1] = positionList[x] + right * thickness;
129 stripMeshColors[2 * x] = colorList[x];
130 stripMeshColors[2 * x + 1] = colorList[x];
132 float uv = uvOffsetLocal;
133 if (x == positionList.Count - 1 && x > 1)
135 float dist_last = (positionList[x - 2] - positionList[x - 1]).magnitude;
136 float dist_cur = (positionList[x] - positionList[x - 1]).magnitude;
137 uv += 1 - dist_cur / dist_last;
140 stripMeshUvs[2 * x] =
new Vector2(0, x - uv);
141 stripMeshUvs[2 * x + 1] =
new Vector2(1, x - uv);
144 int numTriangles = ((positionList.Count * 2 - 2) * 3);
145 if (stripMeshTriangles == null || stripMeshTriangles.Length != numTriangles)
147 stripMeshTriangles =
new int[numTriangles];
150 for (
int i = 0; i < positionList.Count * 2 - 3; i += 2, j++)
152 stripMeshTriangles[i * 3] = j * 2;
153 stripMeshTriangles[i * 3 + 1] = j * 2 + 1;
154 stripMeshTriangles[i * 3 + 2] = j * 2 + 2;
156 stripMeshTriangles[i * 3 + 3] = j * 2 + 1;
157 stripMeshTriangles[i * 3 + 4] = j * 2 + 3;
158 stripMeshTriangles[i * 3 + 5] = j * 2 + 2;
162 mesh.vertices = stripMeshVertices;
163 mesh.uv = stripMeshUvs;
164 mesh.triangles = stripMeshTriangles;
165 mesh.colors = stripMeshColors;
166 mesh.RecalculateBounds();
167 mesh.RecalculateNormals();
170 private static Vector3[] stripMeshVertices = null;
171 private static Color[] stripMeshColors = null;
172 private static Vector2[] stripMeshUvs = null;
173 private static int[] stripMeshTriangles = null;
177 public class CustomEditor : MRTKEditor { }