void OnEnable() { // Tire marks manager must be located at the origin with no rotation transform.position = Vector3.zero; transform.rotation = Quaternion.identity; // Create the mesh components for drawing the marks MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>(); if (meshFilter == null) meshFilter = gameObject.AddComponent<MeshFilter>(); MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>(); if (meshRenderer == null) { meshRenderer = gameObject.AddComponent<MeshRenderer>(); meshRenderer.shadowCastingMode = ShadowCastingMode.Off; meshRenderer.material = material; } if (maxMarks < 10) maxMarks = 10; // Initialize mark points array m_markPoints = new MarkPoint[maxMarks*2]; for (int i = 0, c = m_markPoints.Length; i < c; i++) m_markPoints[i] = new MarkPoint(); m_markCount = 0; m_markArraySize = m_markPoints.Length; // Initialize the mesh source arrays. // These will be edited and passed to the mesh filter in runtime. m_vertices = new Vector3[maxMarks * 4]; m_normals = new Vector3[maxMarks * 4]; m_tangents = new Vector4[maxMarks * 4]; m_colors = new Color[maxMarks * 4]; m_uvs = new Vector2[maxMarks * 4]; m_triangles = new int[maxMarks * 6]; m_values = new Vector2[maxMarks]; m_segmentCount = 0; m_segmentArraySize = maxMarks; m_segmentsUpdated = false; // Elements that will be invariant for (int i = 0; i < m_segmentArraySize; i++) { m_uvs[i * 4 + 0] = new Vector2(0, textureOffsetY); m_uvs[i * 4 + 1] = new Vector2(1, textureOffsetY); m_uvs[i * 4 + 2] = new Vector2(0, 1-textureOffsetY); m_uvs[i * 4 + 3] = new Vector2(1, 1-textureOffsetY); m_triangles[i * 6 + 0] = i * 4 + 0; m_triangles[i * 6 + 2] = i * 4 + 1; m_triangles[i * 6 + 1] = i * 4 + 2; m_triangles[i * 6 + 3] = i * 4 + 2; m_triangles[i * 6 + 5] = i * 4 + 1; m_triangles[i * 6 + 4] = i * 4 + 3; } // Initialize the mesh m_mesh = new Mesh(); m_mesh.MarkDynamic(); m_mesh.vertices = m_vertices; m_mesh.normals = m_normals; m_mesh.tangents = m_tangents; m_mesh.colors = m_colors; m_mesh.triangles = m_triangles; m_mesh.uv = m_uvs; m_mesh.RecalculateBounds(); meshFilter.mesh = m_mesh; }
void AddSegment(MarkPoint first, MarkPoint second) { int segmentIndex = (m_segmentCount % m_segmentArraySize) * 4; m_vertices[segmentIndex + 0] = first.posl; m_vertices[segmentIndex + 1] = first.posr; m_vertices[segmentIndex + 2] = second.posl; m_vertices[segmentIndex + 3] = second.posr; m_normals[segmentIndex + 0] = first.normal; m_normals[segmentIndex + 1] = first.normal; m_normals[segmentIndex + 2] = second.normal; m_normals[segmentIndex + 3] = second.normal; m_tangents[segmentIndex + 0] = first.tangent; m_tangents[segmentIndex + 1] = first.tangent; m_tangents[segmentIndex + 2] = second.tangent; m_tangents[segmentIndex + 3] = second.tangent; m_colors[segmentIndex + 0].a = first.intensity; m_colors[segmentIndex + 1].a = first.intensity; m_colors[segmentIndex + 2].a = second.intensity; m_colors[segmentIndex + 3].a = second.intensity; m_values[segmentIndex/4] = new Vector2(first.intensity, second.intensity); // The very first segment moves all vertices to that position. // This allows proper calculation of the bounds later. if (m_segmentCount == 0) { Vector3 v = m_vertices[0]; for (int i = 4, c = m_vertices.Length; i < c; i++) m_vertices[i] = v; } m_segmentCount++; m_segmentsUpdated = true; }