Search Unity

Question How to change the neighboring color of selected triangle?

Discussion in 'Editor & General Support' started by ilyasGefeasoft, Nov 30, 2022.

  1. ilyasGefeasoft

    ilyasGefeasoft

    Joined:
    Oct 12, 2022
    Posts:
    1
    Hello, by the help of stack overflow (Unity | mesh.colors won't color my custom mesh object I was able to solve the problem where I can see the colorized triangle's face after being selected using ray-cast in unity as shown in the image attached. I was wondering how I can improve my method by making the neighbors of selected triangle also change its color as well. For example, as shown in the image the selected triangle's neighbors to change its color to white and be there only for a few seconds.





    Code (CSharp):
    1. using System.Linq;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class MyRayDraw : MonoBehaviour
    8. {
    9.     public GameObject spherePrefab;
    10.  
    11.     // Better to reference those already in the Inspector
    12.     [SerializeField] private MeshFilter meshFilter;
    13.     [SerializeField] private MeshRenderer meshRenderer;
    14.     [SerializeField] private MeshCollider meshCollider;
    15.  
    16.     private Mesh mesh;
    17.  
    18.     private void Awake()
    19.     {
    20.         if (!meshFilter) meshFilter = GetComponent<MeshFilter>();
    21.         if (!meshRenderer) meshRenderer = GetComponent<MeshRenderer>();
    22.         if (!meshCollider) meshCollider = GetComponent<MeshCollider>();
    23.  
    24.         mesh = meshFilter.mesh;
    25.  
    26.         // create new colors array where the colors will be created
    27.         var colors = new Color[mesh.vertices.Length];
    28.         for(var i = 0; i < colors.Length; i++)
    29.         {
    30.             colors[i] = new Color(1f, 0f, 0.07f);
    31.         }
    32.  
    33.         // assign the array of colors to the Mesh.
    34.         mesh.colors = colors;
    35.  
    36.     }
    37.  
    38.     private void Update()
    39.     {
    40.         if (!Input.GetMouseButtonDown(0)) return;
    41.  
    42.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    43.         RaycastHit hit;
    44.  
    45.  
    46.         if (Physics.Raycast(ray, out hit))
    47.         {
    48.             Debug.Log(hit.triangleIndex);
    49.             //cube.transform.position = hit.point;
    50.  
    51.             // Get current vertices, triangles, uvs and colors
    52.             var vertices = mesh.vertices;
    53.             var triangles = mesh.triangles;
    54.             //var uv = _mesh.uv;
    55.             var colors = mesh.colors;
    56.  
    57.             // Get the vert indices for this triangle
    58.             var vertIndex_1 = triangles[hit.triangleIndex * 3 + 0];
    59.             var vertIndex_2 = triangles[hit.triangleIndex * 3 + 1];
    60.             var vertIndex_3 = triangles[hit.triangleIndex * 3 + 2];
    61.  
    62.             // Get the positions for the vertices
    63.             var vertPos_1 = vertices[vertIndex_1];
    64.             var vertPos_2 = vertices[vertIndex_2];
    65.             var vertPos_3 = vertices[vertIndex_3];
    66.          
    67.             // Now for all three vertices we first check if any other triangle if using it
    68.             // by simply count how often the indices are used in the triangles list
    69.             var verticesOccur_1 = 0;
    70.             var verticesOccur_2 = 0;
    71.             var verticesOccur_3 = 0;
    72.  
    73.             for (var i = 0; i < triangles.Length; i++)
    74.             {
    75.                 if (triangles[i] == vertIndex_1) verticesOccur_1++;
    76.                 if (triangles[i] == vertIndex_2) verticesOccur_2++;
    77.                 if (triangles[i] == vertIndex_3) verticesOccur_3++;
    78.             }
    79.          
    80.  
    81.             // Create copied Lists so we can dynamically add entries
    82.             var newVertices = vertices.ToList();
    83.             var newTriangles = triangles.ToList();
    84.             //var newUV = uv.ToList();
    85.             var newColors = colors.ToList();
    86.  
    87.             // Now if a vertex is shared we need to add a new individual vertex
    88.             // and also an according entry for the color array
    89.             // and update the vertex index
    90.             // otherwise we will simply use the vertex we already have
    91.             if (verticesOccur_1 >= 1)
    92.             {
    93.  
    94.                 newVertices.Add(vertPos_1);
    95.                // newUV.Add(vert1UV);
    96.                 newColors.Add(Color.green);
    97.                 vertIndex_1 = newVertices.Count - 1;
    98.             }
    99.  
    100.             if (verticesOccur_2 >= 1)
    101.             {
    102.                 newVertices.Add(vertPos_2);
    103.                 //newUV.Add(vert2UV);
    104.                 newColors.Add(Color.green);
    105.                 vertIndex_2 = newVertices.Count - 1;
    106.             }
    107.  
    108.             if (verticesOccur_3 >= 1)
    109.             {
    110.                 newVertices.Add(vertPos_3);
    111.                 //newUV.Add(vert3UV);
    112.                 newColors.Add(Color.green);
    113.                 vertIndex_3 = newVertices.Count - 1;
    114.             }
    115.  
    116.  
    117.         // Update the indices of the hit triangle to use the (eventually) new
    118.             // vertices instead
    119.             triangles[hit.triangleIndex * 3 + 0] = vertIndex_1;
    120.             triangles[hit.triangleIndex * 3 + 1] = vertIndex_2;
    121.             triangles[hit.triangleIndex * 3 + 2] = vertIndex_3;
    122.          
    123.             // Now we can simply add the new triangle
    124.             newTriangles.Add(vertIndex_1);
    125.             newTriangles.Add(vertIndex_2);
    126.             newTriangles.Add(vertIndex_3);
    127.          
    128.             // And update the mesh
    129.             mesh.vertices = newVertices.ToArray();
    130.             mesh.triangles = newTriangles.ToArray();
    131.             //_mesh.uv = newUV.ToArray();
    132.             mesh.colors = newColors.ToArray();
    133.          
    134.  
    135.  
    136.             // color these vertices
    137.             newColors[vertIndex_1] = Color.red;
    138.             newColors[vertIndex_2] = Color.red;
    139.             newColors[vertIndex_3] = Color.red;
    140.  
    141.             // Recalculate the normals
    142.             mesh.RecalculateNormals();
    143.         }
    144.         else
    145.         {
    146.             Debug.Log("no hit");
    147.         }
    148.     }
    149. }