Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How can i create and add a material which the selected/raycast object's edges are highlighted

Discussion in 'Game Design' started by Gamer_Halil, Apr 10, 2024.

  1. Gamer_Halil

    Gamer_Halil

    Joined:
    Mar 29, 2024
    Posts:
    13
    Q: How to create the material what highlight selected/raycast objects and how to apply it?
    ...like in that game Prison Simulator? Actually i know the code block that with raycast and gameObject set/hide
    func 
    but idk how to create materials what highlight objects's edges.
    (check the photos pls)

    Ekran görüntüsü 2024-04-11 003746.png Ekran görüntüsü 2024-04-11 004835.png
     
  2. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    384
    the absolute easiest method in shader would be a two pass shader where you render the backface in gold and push it out using offset command. i might write a simple unlit one if you're interested, as i could crank it out in like 5 minutes
     
  3. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    384
    ok, here's what i whipped up, if you want to prevent the outline artifacts that happen when an object is far away you'll have to do less outlining at a distance, though i assume you're only outlining things that are close to the player.

    Code (CSharp):
    1. Shader "Unlit/OutlineTest"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             // make fog work
    18.             #pragma multi_compile_fog
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 UNITY_FOG_COORDS(1)
    32.                 float4 vertex : SV_POSITION;
    33.             };
    34.  
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.  
    38.             v2f vert (appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.vertex = UnityObjectToClipPos(v.vertex);
    42.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    43.                 UNITY_TRANSFER_FOG(o,o.vertex);
    44.                 return o;
    45.             }
    46.  
    47.             fixed4 frag (v2f i) : SV_Target
    48.             {
    49.                 // sample the texture
    50.                 fixed4 col = tex2D(_MainTex, i.uv);
    51.                 // apply fog
    52.                 UNITY_APPLY_FOG(i.fogCoord, col);
    53.                 return col;
    54.             }
    55.             ENDCG
    56.         }
    57.  
    58.         Pass
    59.         {
    60.             Cull Front
    61.  
    62.             Offset -5, 0
    63.  
    64.             CGPROGRAM
    65.  
    66.             #pragma vertex vert
    67.             #pragma fragment frag
    68.             #include "UnityCG.cginc"
    69.  
    70.             float4 vert(float4 vertex : POSITION) : SV_POSITION
    71.             {
    72.                 return UnityObjectToClipPos(vertex);
    73.             }
    74.  
    75.             fixed4 frag() : SV_Target
    76.             {
    77.  
    78.                 return fixed4(1, .843, 0, 1);
    79.             }
    80.  
    81.             ENDCG
    82.         }
    83.  
    84.     }
    85. }
    86.  
    an easy way to apply this on top of existing shaders is by removing the first pass and adding it as a second material in your mesh renderer.
     
    Last edited: Apr 11, 2024
  4. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    384
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,598
    POOKSHANK likes this.
  6. Gamer_Halil

    Gamer_Halil

    Joined:
    Mar 29, 2024
    Posts:
    13
    firstly, thank you so much for your kind and helper answers. for the first way, idk how to work with shaders. how to apply, how to write and so on... in which language did you write the shaders? also where can i learn them?
    also how can i edit this code for to make more thin corners, colors et.
    thank you so much again :)
     
  7. Gamer_Halil

    Gamer_Halil

    Joined:
    Mar 29, 2024
    Posts:
    13
    ty so much, i didn't understand, how can I apply this?
     
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,598
    If you don't understand that sentence, you should probably start looking at Unity Learn resources to get your way around. Try the Roll-a-Ball tutorial series.

    But you literally just add this component to a model. Done. That's it. (It won't show up in Scene view, just in Game view while playing.) And if you disable it or remove it, the outline goes away.

    outline.png
     
  9. Gamer_Halil

    Gamer_Halil

    Joined:
    Mar 29, 2024
    Posts:
    13
    i know the basics, just i didn't know to how to apply. thank you <3
     
  10. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    384

    i'm not sure if you're going to use this anymore, as halley's solution is very simple, but the language is hlsl/shaderlab and to control thickness you would change the values in the offset command (the -10). to change color you change the value returned at the end of the shader, where it says return float4. a float4 represents an rgba color.

    this below will answer most basic questions about writing shaders, and it's how i learned at the beginning.
    ShaderLab FAQ - Unity Forum
     
    Gamer_Halil and halley like this.