Image Effect: Edge Detect Normals Colours [rel]

(Now updated to work up to 5.4.0f3)
As I was working on my project, I was looking into edge detecting and the current image effects edge detection doesn’t offer the ability to change the colours of the edges it detects. Instead it only renders it black.

I’ve edited the shader and editor to add another image effect called Edge Detection Color. It allows you to edit the colour of the edges, changing them from black to any other color, selectable in the editor. The required files are attached to this post.

How to install: Unity Image Effects files are required (Import Unity Effects package)
Put EdgeDetectionColorsEditor file in the Editor/ImageEffects folder,
Put the other two (EdgeDetectColor.cs & EdgeDetectNormalsColor.shader) anywhere else.
Add EdgeDetectionColour to camera through Image Effects > Edge Detection > EdgeDetectionColor / searching for EdgeDetectionColor

Note: only works for triangle depth normals and robert cross depth normals

A quick example of what it lets you do:
Before:


After:

2016887–194303–EdgeDetectionColorsEditor.cs (2.36 KB)
2733371–194301–EdgeDetectNormalsColor.shader (9.7 KB)
2733371–194300–EdgeDetectionColor.cs (2.78 KB)

5 Likes

@pmurph03 this is just want I wanted, but I’m getting an error: “Missing shader in Main Camera when” when I attach the script to my camera, do you have any suggestions as to how I can fix this?

I was able to get it to work by adding:

public override bool CheckResources ()
{
	CheckSupport(true);

	if (edgeDetectShader == null)
	{
		edgeDetectShader = (Shader)Resources.Load ("EdgeDetectNormalsColor");
	}
	.
	.
	.
}

pmurph03 hey, thanks for scripts!
very helpfull for me, looks so nice

@pmurph03
THANK YOU! This is exactly what i have been looking for. I was using the unity edge detection scrpt, and it wasnt doing the job like i wanted. this is exactly perfect. You are the man!

This is excellent. I was really struggling on how to do this so thank you.

Does anyone know if it is at all possible to either - 1) have the color key off say for instance an objects material color or 2) exclude certain objects from the edge detection effect?

I’ve followed the instructions, but I’m getting an error:

Unfortunately, I can’t figure out what the issue is. Anyone had the same problem?

EDIT: Fixed the issue. I misplaced the shader, now it works just fine. Sorry for the inconvenience!

Nice work.

Thanks.

When trying out your scripts, I’m receiving two errors.

Assets/Editor/ImageEffects/EdgeDetectionEditor.cs(8,11): error CS0101: The namespace UnityStandardAssets.ImageEffects' already contains a definition for EdgeDetectionEditor’

NullReferenceException: Object reference not set to an instance of an object
UnityStandardAssets.ImageEffects.EdgeDetectionColor.OnRenderImage (UnityEngine.RenderTexture source, UnityEngine.RenderTexture destination) (at Assets/Scripts/EdgeDetectionColor.cs:81)

Seeing as how i’m trying to learn how shaders and things work, I’m not sure how to go about finding the cause of these issues. The first error really has me confused, since I have searched the EdgeDetectionEditor.cs script and find no instance of “EdgeDetectionEditor” in the script itself. So i’m not sure where it’s finding that definition.

@Rottiger I know it’s been several months since you solved your problem, but do you remember what you did exactly? I have the exact problem you did, and I’ve tried a bunch of different things (including shuffling the shader around) but I can’t seem to fix it. (Also I’m on Unity Personal v5.3.3f1, if that has any significance)

#1. You can’t have two scripts with the same name in your project. Either delete the duplicate or rename one, both the file and the namespace.

#2. Look at line 81 of the script just like the error tells you. Likely you have a missing pointer and the script is trying to utilize some component but you haven’t told it what to use.

Bump.

1 Like

@Kayos Yeah, the issue seems to be that it can’t track down the shader. I was able to get it to work by doing the following:

  • Place your “EdgeDetectNormalsColor.shader” in Standard Assets > Effects > ImageEffects > Shader
  • Add the following line of code to “EdgeDetectionColor.cs” at line 40/41 (just after CheckSupport(true); )

edgeDetectShader = Shader.Find (“Hidden/EdgeDetectColors”);

1 Like

I apologize for the lack of support! I didn’t realize that anyone had used this, so I’m very sorry for the late update.

I just quickly updated this shader to work in Unity 5.4.0f3, as well as fixing some bugs that people pointed out. I’ll also update the original post to include more information about proper installation. Original post attachments have also been updated to the latest version.

Attached are files for 5.4.0f3

2733371–194300–EdgeDetectionColor.cs (2.78 KB)
2733371–194301–EdgeDetectNormalsColor.shader (9.7 KB)
2016887–194303–EdgeDetectionColorsEditor.cs (2.36 KB)

1 Like

Thank you pmurph03. Works well on mobiles too.

Hi, I’m unity 5.6 and I kept the EdgeDetection image effect but it has a bad issue: the thickness of lines changes based on screen size.
Does this effect have the same issue?

I believe if you modify the script that goes on the camera to multiply the sample distance based on screen resolution there shouldn’t be a change of percieved thickness based on screen size.

Hi pmurph, I am using Unity 5.6, I added these files in the correct place as well as importing Unity’s image effects but I get the error:
The type or namespace name `PostEffectsBase’ could not be found.

Am I missing something else? This class didn’t seem to be included in Unity’s Effects package.

Cheers!

Okay I found the issue, I ended up on a wild goose chase back to the Legacy image effects which aren’t included in the newer versions of Unity: https://www.assetstore.unity3d.com/en/#!/content/83913

The effect is perfect, thanks for releasing this!

How get the edge detect shader to work on backfaces as well?
It seems to ignore backfaces.
Crease effect also ignores back faces.

Other image effects like bloom or contrast enhance do process back faces.