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

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)

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

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.

Shader "Unlit/OutlineTest"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }

        Pass
        {
            Cull Front

            Offset -5, 0

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            float4 vert(float4 vertex : POSITION) : SV_POSITION
            {
                return UnityObjectToClipPos(vertex);
            }

            fixed4 frag() : SV_Target
            {

                return fixed4(1, .843, 0, 1);
            }

            ENDCG
        }

    }
}

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.

It’s great that POOKSHANK gave you a good start.

This asset is popular and free, which supports a number of additional modes, and handles the material list management for you: https://assetstore.unity.com/packages/tools/particles-effects/quick-outline-115488 Drop a single component on whatever parent object you like, and it does the rest with all the renderers in all children.

1 Like

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 :slight_smile:

ty so much, i didn’t understand, how can I apply this?

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.

9769989--1399881--outline.jpg

i know the basics, just i didn’t know to how to apply. thank you <3

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

2 Likes