public override void refresh() { base.refresh(); if (needsRefresh) { return; } if (line == null) { // not yet fully initialized needsRefresh = true; return; } var lineRenderer = line.GetComponent <LineRenderer>(); var col = Misc.ArrayToColor(Color); lineRenderer.material = defaultMaterial; lineRenderer.material.color = col; List <Vector3> positions = new List <Vector3>(); for (int i = 0; i < XData.Length; i++) { var pos = new Vector3(XData[i], YData[i], 0); if (ZData != null && ZData.Length == XData.Length) { pos.z = ZData[i]; } pos = convertSizeToPixels(pos, MUnits.data, true); positions.Add(pos); } lineRenderer.positionCount = XData.Length; lineRenderer.SetPositions(positions.ToArray()); }
public override void refresh() { base.refresh(); if (needsRefresh) { return; } if (String != null && String.Length > 0) { if (textObj == null) { // not fully initialized Debug.LogWarning("Text obj not initialized before refresh"); init(); } var textMesh = textObj.GetComponent <TextMeshPro>(); var scaleMultiplier = 1f / transform.lossyScale.magnitude; //TODO: maybe do this resizing in update() to account for scale changes textMesh.fontSize = Mathf.RoundToInt(FontSize[0] * scaleMultiplier / 20f); textMesh.autoSizeTextContainer = true; textMesh.text = String; switch (HorizontalAlignment) { case "left": switch (VerticalAlignment) { case "bottom": textMesh.alignment = TextAlignmentOptions.BottomLeft; break; case "top": textMesh.alignment = TextAlignmentOptions.TopLeft; break; default: textMesh.alignment = TextAlignmentOptions.MidlineLeft; Debug.LogWarningFormat("Unsupported Alignment: {0} {1}", HorizontalAlignment, VerticalAlignment); break; } break; case "center": switch (VerticalAlignment) { case "bottom": textMesh.alignment = TextAlignmentOptions.Bottom; break; case "top": textMesh.alignment = TextAlignmentOptions.Top; break; default: textMesh.alignment = TextAlignmentOptions.Center; Debug.LogWarningFormat("Unsupported Alignment: {0} {1}", HorizontalAlignment, VerticalAlignment); break; } break; case "right": switch (VerticalAlignment) { case "bottom": textMesh.alignment = TextAlignmentOptions.BottomRight; break; case "top": textMesh.alignment = TextAlignmentOptions.TopRight; break; default: textMesh.alignment = TextAlignmentOptions.Right; Debug.LogWarningFormat("Unsupported Alignment: {0} {1}", HorizontalAlignment, VerticalAlignment); break; } break; } textMesh.color = Misc.ArrayToColor(Color); var textRenderer = textMesh.GetComponent <Renderer>(); //textRenderer.material.color = Misc.ArrayToColor(Color); textRenderer.enabled = true; } }
public override void refresh() { base.refresh(); if (needsRefresh) { return; } if (mesh != null) { Destroy(mesh); } mesh = new Mesh(); var meshFilter = transform.gameObject.AddComponent <MeshFilter>(); meshFilter.mesh = mesh; var meshRenderer = transform.gameObject.AddComponent <MeshRenderer>(); if (FaceVertexCData != null) { meshRenderer.material = defaultVertexColorMaterial; } else { meshRenderer.material = defaultMaterial; } if (FaceColor.array != null) { meshRenderer.material.color = Misc.ArrayToColor(FaceColor.array); } if (EdgeColor.str != null) { if (EdgeColor.str != "none") { Debug.LogErrorFormat("Unsupported EdgeColor {0}", EdgeColor.str); } // else do nothing } else { Debug.Assert(EdgeColor.array != null & EdgeColor.array.Length == 3); // add second material as wireframe to show both underlying color and mesh edges var wireframeMaterial = defaultWireframeMaterial; wireframeMaterial.color = Misc.ArrayToColor(EdgeColor.array); meshRenderer.materials = new Material[] { meshRenderer.material, defaultWireframeMaterial }; } //TODO: add second wireframe material if edge color is not none // convert coordinate systems and from float[3] to Vector3 var convertedVertices = new Vector3[Vertices.Length]; for (int i = 0; i < Vertices.Length; i++) { convertedVertices[i] = convertSizeToPixels(Misc.ArrayToVec3(Vertices[i]), MUnits.data, true); } mesh.vertices = convertedVertices; // convert from (numFaces by numTriangles) to (numFaces*numTriangles vector) as expected by Unity var numVerticesPerFace = Faces[0].Length; Debug.Assert(numVerticesPerFace == 3); // quadilaterials and other size face elements not currently supported var convertedFaces = new int[Faces.Length * numVerticesPerFace]; //Debug.LogFormat("Size of faces: {0},{1}", Faces.Length, Faces[0].Length); for (int i = 0; i < Faces.Length; i++) { Debug.Assert(Faces[i].Length == numVerticesPerFace); for (int j = 0; j < numVerticesPerFace; j++) { convertedFaces[numVerticesPerFace * i + j] = Faces[i][j] - 1; // subtract 1 to convert to zero-based indexing } } //Debug.LogFormat("Max index: {0}", Mathf.Max(convertedFaces)); //Debug.LogFormat("Size of Vertices: {0},{1}", Vertices.Length, Vertices[0].Length); MeshTopology meshTopol = MeshTopology.Triangles; if (numVerticesPerFace == 3) { meshTopol = MeshTopology.Triangles; } //else if (numVerticesPerFace == 4) // meshTopol = MeshTopology.Quads; else { Debug.LogErrorFormat("Unsupported number of vertices per face: {0}", meshTopol); } mesh.SetIndices(convertedFaces, meshTopol, 0); //mesh.triangles = convertedFaces; if (FaceVertexCData != null) { // convert scalar maps into colormap into RGB colors if (FaceVertexCData.Length > 0 && (FaceVertexCData[0].Length == 1)) { // assume if one element needs to be converted, all do FaceVertexCData = mapIntoColormap((float [][])FaceVertexCData); } var colors = new List <Color>(); if (FaceVertexCData.Length == 1) { // replicate single color Debug.Assert(FaceVertexCData[0].Length == 3); for (int i = 0; i < Vertices.Length; i++) { colors.Add(Misc.ArrayToColor(FaceVertexCData[0])); } } else { Debug.Assert(FaceVertexCData.Length == Vertices.Length); // for now, only support coloring by vertex (not face) //TODO: add support for coloring by face if FaceVertexCData.Length == numFaces for (int i = 0; i < Vertices.Length; i++) { colors.Add(Misc.ArrayToColor(FaceVertexCData[i])); } } mesh.SetColors(colors); } // duplicate faces and flip normals Misc.MakeMeshDoubleSided(ref mesh); }
public void UpdateMarker() { if (line != null) { Destroy(line); } if (sphere != null) { Destroy(sphere); } switch (Marker) { case "o": if (!renderAsSphere) { line = new GameObject(); line.transform.SetParent(transform); line.transform.localPosition = Vector3.zero; line.transform.localScale = Vector3.one; line.name = "MarkerLine"; var lineRenderer = line.AddComponent <LineRenderer>(); lineRenderer.useWorldSpace = false; lineRenderer.widthMultiplier = 0.005f * LineWidth; lineRenderer.material = material; lineRenderer.receiveShadows = false; if (MarkerEdgeColor.Length == 3) { lineRenderer.material.color = Misc.ArrayToColor(MarkerEdgeColor); } float radius = Mathf.Sqrt(MarkerSize / Mathf.PI) * 2; float z = 0; lineRenderer.positionCount = numLinesPerCircle + 1; for (int i = 0; i < (numLinesPerCircle + 1); i++) { float angle = Mathf.Deg2Rad * i * 360f / numLinesPerCircle; float x = Mathf.Sin(angle) * radius; float y = Mathf.Cos(angle) * radius; lineRenderer.SetPosition(i, new Vector3(x, y, z)); } } else { sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.SetParent(transform); sphere.transform.localPosition = Vector3.zero; sphere.transform.localScale = Vector3.one * Mathf.Sqrt(MarkerSize / Mathf.PI) * 4; sphere.name = "MarkerSphere"; sphere.GetComponent <SphereCollider>().enabled = false; var mr = sphere.GetComponent <MeshRenderer>(); mr.material = material; mr.receiveShadows = false; if (MarkerEdgeColor.Length == 3) { mr.material.color = Misc.ArrayToColor(MarkerEdgeColor); } else if (MarkerFaceColor.Length == 3) { mr.material.color = Misc.ArrayToColor(MarkerFaceColor); } sphere.name = "MarkerSphere"; } break; default: Debug.LogErrorFormat("Unsupported marker type: {0}", Marker); break; } }