Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Highlight Cube Face

Discussion in 'Made With Unity' started by Jacob-Williams, Dec 10, 2009.

  1. Jacob-Williams

    Jacob-Williams

    Joined:
    Jan 30, 2009
    Posts:
    267
    I was looking around yesterday for a script that would allow me to highlight a cube face similar to the way Cheetah3D does. To my surprise, it has been asked about quite a bit on the forums, but I couldn't find any working code. So after a couple hours of research (as I know nothing about meshes), I managed to figure it out.

    Code (csharp):
    1.  
    2. var highlightColor : Color;
    3. private var lastMesh : Mesh;
    4. private var lastHitNormal : Vector3;
    5.  
    6. function Update () {
    7. var hit : RaycastHit;
    8.     if (Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit)){
    9.         HighlightSelectedFace(hit);
    10.     }
    11.     else {
    12.         RemoveHighlights();
    13.     }
    14. }
    15.  
    16.  
    17. function HighlightSelectedFace(hit : RaycastHit){
    18.     if (lastHitNormal != Vector3.zero){
    19.         RemoveHighlights();
    20.     }
    21.     var meshCollider = hit.collider as MeshCollider;
    22.     if (meshCollider == null || meshCollider.sharedMesh == null)
    23.     return;
    24.  
    25.     var mesh : Mesh = meshCollider.sharedMesh;
    26.     var vertices = mesh.vertices;
    27.     var normals = mesh.normals;
    28.     var triangles = mesh.triangles;
    29.     var colors = new Color[vertices.Length];
    30.  
    31.     for (var i=0;i<vertices.length;i++){
    32.         if (normals[i] == hit.normal){
    33.             colors[i] = highlightColor;
    34.         }
    35.         else {
    36.             colors[i] = Color.white;   
    37.         }
    38.     }
    39.  
    40.     mesh.colors = colors;
    41.     lastMesh = mesh;
    42.     lastHitNormal = hit.normal;
    43.  
    44. }
    45.  
    46.  
    47. function RemoveHighlights(){
    48.     if (lastMesh){
    49.         var mesh : Mesh = lastMesh;
    50.         var vertices = mesh.vertices;
    51.         var normals = mesh.normals;
    52.         var triangles = mesh.triangles;
    53.         var originalColors = mesh.colors;
    54.         var colors = new Color[vertices.Length];
    55.  
    56.         for (var i=0;i<vertices.length;i++){
    57.             colors[i] = Color.white;
    58.         }
    59.  
    60.         mesh.colors = colors;
    61.         lastMesh = null;
    62.         lastHitNormal = Vector3.zero;
    63.     }
    64. }
    65.  
    The example below uses a Unity cube primitive with a mesh collider. The code is pretty simple and fast. I am sure it can be improved, but it is enough to get you started.

    Click here for the webplayer.

    I also came to realize that very few of the default Unity shaders support vertex colors, so below I have attached my shader. It is a simple shader from the wiki that I hacked an outline into.

    Enjoy!
     

    Attached Files:

    wxxhrt and tapticc like this.