Outline Rendering?

Hi guys and gals,I’ve looked around but can’t find any info on how to draw intersecting line in two intersectant gameobject?
Seems like the image:

Hi, I believe your issue could be solved by scripting shader in Unity. You issue actually requires an IMAGE EFFECT rather than a shader for models. Since I have nearly seen nothing about outlining objects either, I am going to talk about both in the following.
[75723-屏幕快照-2016-08-09-下午23151.png|75723]
[75726-屏幕快照-2016-08-09-下午30734.png|75726]

  1. How to outline a SINGLE object?
    First, create a new shader in your project and open it to edit. I have written one for you and you may take this for reference:

    Shader “Mistral/Outline”
    {
    Properties
    {
    _Outline ( “The scale of the Outline”, Range ( 0, 0.1 ) ) = 0.02
    _Factor ( “The Factor”, Range ( 0, 0.1) ) = 0.02
    _Color ( “The Color of your Outline”, Color ) = (1, 1, 1, 1)
    }

     SubShader
     {
     	Pass
     	{
     		Tags { "LightMode" = "Always" }
     		Cull Front
     		ZWrite On
    
     		CGPROGRAM
    
     		#pragma vertex vert
     		#pragma fragment frag
    
     		#include "UnityCG.cginc"
    
     		uniform float _Outline;
     		uniform float _Factor;
     		uniform fixed4 _Color;
    
     		struct vertexOutput
     		{
     			float4 pos : SV_POSITION;
     		};
    
     		vertexOutput vert ( appdata_full v )
     		{
     			vertexOutput o;
    
     			o.pos = mul ( UNITY_MATRIX_MVP, v.vertex );
     			float3 dir = normalize ( v.vertex.xyz );
     			float3 dir2 = v.normal;
     			dir = lerp ( dir, dir2, _Factor );
     			dir = mul ( ( float3x3 )UNITY_MATRIX_IT_MV, dir );
     			float2 offset = TransformViewToProjection ( dir.xy );
     			offset = normalize ( offset );
     			o.pos.xy += offset * o.pos.z * _Outline;
    
     			return o;
     		}
    
     		fixed4 frag (vertexOutput i) : COLOR
     		{
     			return _Color;
     		}
     		ENDCG
     	}
    
     	Pass
     	{
     		Tags {"LightMode" = "ForwardBase"}
    
     		CGPROGRAM
    
     		#pragma vertex vert
     		#pragma fragment frag
    
     		#include "UnityCG.cginc"
    
     		uniform fixed4 _LightColor0;
    
     		struct vertexOutput
     		{
     			float4 pos : SV_POSITION;
     			float4 col : COLOR;
     		};
    
     		vertexOutput vert ( appdata_full v )
     		{
     			vertexOutput o;
     			o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    
     			float3 normalDirection = normalize ( mul ( float4 ( v.normal, 0.0 ), _World2Object ).xyz );
     			float3 lightDirection = normalize ( _WorldSpaceLightPos0.xyz );
     			float3 diffuseRelection = dot ( normalDirection, lightDirection );
     			o.col = fixed4 ( diffuseRelection, 1.0 ) * _LightColor0;
     			o.pos = mul ( UNITY_MATRIX_MVP, v.vertex );
    
     			return o;
     		}
    
     		fixed4 frag ( vertexOutput i ) : COLOR
     		{
     			return i.col;
     		}
    
     		ENDCG
    
     	}
     }
    

    }

Second, create a new Material, and set its shader to “Mistral/Outline” (The one indicated in the shader that I have written. If you wanna the path of it to be “BALA/bala/blbl”, you may edit the first line of my shader.)

Last, set the material of the object that you wanna to outline to the new material that you have just created. So now everything will be fine.

Please note that the shader above is only for a test and it supports only one light only. If you want the shader and the material to be fully functional you need to learn something more about Unity ShaderLab. You may get some useful stuff on Unity Official Online Manual.

  1. How to outline INTERLEAVED objects?

Well in this case you must apply a POSTPROCESSING, namely an image effect. What you have to do is to detect the edges in the rendered raw frame, highlight the edges, and finally output.

In order to do so, you must enable the Depth Texture Mode of your camera, since edge detecting is based on the depthTex of the original frame. However the thread is both simple and clear: grab the adjacent texels in the original frame and judge whether they form an edge or not.

Here is the Image Effect Shader Code:

Shader "Hidden/Image Effects/EdgeOutline"
{
	Properties
	{
		_MainTex ( "Base Frame ( RGB )", RECT ) = "white" {}
	}

	SubShader
	{
		Pass
		{
			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			uniform sampler2D _MainTex;
			uniform float4 _MainTex_TexelSize;
			sampler2D _CameraDepthTexture;
			sampler2D _CameraNormalsTexture;

			struct vertexOutput
			{
				float4 pos : POSITION;
				float2 uv[4] : TEXCOORD0;
			};

			vertexOutput vert ( appdata_img v )
			{
				vertexOutput o;

				o.pos = mul ( UNITY_MATRIX_MVP, v.vertex );
				float2 uv = MultiplyUV ( UNITY_MATRIX_TEXTURE0, v.texcoord );
				o.uv[0] = uv;
				o.uv[1] = uv;
				o.uv[2] = uv + float2 (-_MainTex_TexelSize.x, -_MainTex_TexelSize.y);
				o.uv[3] = uv + float2 (_MainTex_TexelSize.x, -_MainTex_TexelSize.y);

				return o;
			}

			fixed4 frag (vertexOutput i) : COLOR
			{
				fixed4 original = tex2D ( _MainTex, i.uv[0] );

				float centerDepth = UNITY_SAMPLE_DEPTH ( tex2D ( _CameraDepthTexture, i.uv[1] ) );
				float3 centerNormal = tex2D ( _CameraNormalsTexture, i.uv[1] ) * 2.0 - 1.0;

				float d1 = UNITY_SAMPLE_DEPTH ( tex2D ( _CameraDepthTexture, i.uv[2] ) );
				float3 n1 = tex2D ( _CameraNormalsTexture, i.uv[2] ) * 2.0 - 1.0;

				float d2 = UNITY_SAMPLE_DEPTH ( tex2D ( _CameraDepthTexture, i.uv[3] ) );
				float3 n2 = tex2D ( _CameraNormalsTexture, i.uv[3] ) * 2.0 - 1.0;

				fixed isSameDepth1 = abs ( d1 - centerDepth ) < 0.01 * centerDepth;
				fixed isSameDepth2 = abs ( d2 - centerDepth ) < 0.01 * centerDepth;
				fixed isSameNormal1 = 1 - dot ( n1, centerNormal ) < 0.051;
				fixed isSameNormal2 = 1 - dot ( n2, centerNormal ) < 0.051;


				original = original * isSameDepth1 * isSameDepth2 * isSameNormal1 * isSameNormal2;
				return original;
			}

			ENDCG
		}
	}

}

Please note that this Image Effect requires something from our Camera, and thus the C# script should not be copied from the so-called “Image Effect C# Template”. Here is my code:

using UnityEngine;
using System.Collections;

public class EdgeDetectEffect : MonoBehaviour 
{
	public Shader EdgeDetectShader;
	public Material material
	{
		get
		{
			if (mat == null) 
			{
				mat = new Material ( EdgeDetectShader );
				mat.hideFlags = HideFlags.HideAndDontSave;
			}
			return mat;
		}
	}

	private Material mat;

	void Start ()
	{
		if (!SystemInfo.supportsImageEffects) 
		{
			enabled = false;
			return;
		}
		if (!EdgeDetectShader && !EdgeDetectShader.isSupported) 
		{
			enabled = false;
			return;
		}
		GetComponent <Camera> ().depthTextureMode = DepthTextureMode.DepthNormals;
	}

	void OnRenderImage (RenderTexture src, RenderTexture dst)
	{
		if (EdgeDetectShader != null)
			Graphics.Blit (src, dst, material);
		else
			Graphics.Blit (src, dst);
	}
}

Add EdgeDetect Component to the camera and drag the shader into the slot, you will see the result.

I hope my solution will be of help, and if you still have further questions, please feel free to contact me.

GL &HF!

Have you tried the built-in edge detect normals image effect? It appears to achieve exactly what you want: https://docs.unity3d.com/Manual/script-EdgeDetectEffectNormals.html