- Home /
Outline diffuse shader (toon) : no results?
Hi,
I would like to use a shader to get the edges with a specific color (http://wiki.unity3d.com/index.php/OutlinedDiffuse). (The code only works with "vert", there is no "frag" function, but since it is on the wiki, I guess it is also working like that).
EDIT #2:
Thanks whydoitdoit for the link. Would you know how to avoid the outline effect inside an intersection between 2 rectangles? The 2 rectangles are perfectly perpendicular in Blender...
Also how could I change the color of the front "frag", I tried with return float4(70, 70, 30, 1);
but it triggers a warning : "Shader warning in 'Toon2': Program 'vert', implicit truncation of vector type (compiling for d3d11_9x)"
I have got this result without the lightcolor with :
//c.rgb = lightColor * c.rgb * 2;
c.rgb = c.rgb * 2;
The image :
Thanks again
[3]: /storage/temp/10419-settings.png
@whydoitdoit : thanks whydoitdoit, I really appreciate your help, and your website! Having the community that share, and a website like yours to share your knowledge is a great motivation for me. I edited my post with an image of my result : can I ask you how it is possible to -not- have the outline inside an intersection between 2 rectangles? And how to change the outline color (with a float4?) ? Thanks a lot
So you can do that by not writing to the depth buffer on the first pass, or by offsetting the depth buffer backwards during that pass (or forwards in the other pass) - however, it ends up looking odd when you put two characters next to each other, because their outlines will merge and they won't be outlined against each other very well.
Converted the toon shader to an answer (because it's too unwieldy as a comment :)
Answer by whydoidoit · Apr 26, 2013 at 05:53 AM
Shader "Custom/OutlineToonShader" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Bump ("Bump", 2D) = "bump" {}
_ColorMerge ("Color Merge", Range(0.1,20000)) = 8
_Ramp ("Ramp Texture", 2D) = "white" {}
_Outline ("Outline", Range(0, 0.15)) = 0.08
_OutllineColor ("OutlineColor", Color) = (0,0,0,1)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass {
Cull Front
Lighting Off
ZWrite On
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float3 tangent : TANGENT;
};
struct v2f
{
float4 pos : POSITION;
};
float _Outline;
float4 _OutlineColor;
v2f vert (a2v v)
{
v2f o;
float4 pos = mul( UNITY_MATRIX_MV, v.vertex);
float3 normal = mul( (float3x3)UNITY_MATRIX_IT_MV, v.normal);
normal.z = -0.4;
pos = pos + float4(normalize(normal),0) * _Outline;
o.pos = mul(UNITY_MATRIX_P, pos);
return o;
}
float4 frag (v2f IN) : COLOR
{
return _OutlineColor;
}
ENDCG
}
Pass {
Cull Back
Lighting On
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform float4 _LightColor0;
sampler2D _MainTex;
sampler2D _Bump;
sampler2D _Ramp;
float4 _MainTex_ST;
float4 _Bump_ST;
float _Tooniness;
float _ColorMerge;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 tangent : TANGENT;
};
struct v2f
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float3 lightDirection;
};
v2f vert (a2v v)
{
v2f o;
//Create a rotation matrix for tangent space
TANGENT_SPACE_ROTATION;
//Store the light's direction in tangent space
o.lightDirection = mul(rotation, ObjSpaceLightDir(v.vertex));
//Transform the vertex to projection space
o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
//Get the UV coordinates
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
o.uv2 = TRANSFORM_TEX (v.texcoord, _Bump);
return o;
}
float4 frag(v2f i) : COLOR
{
//Get the color of the pixel from the texture
float4 c = tex2D (_MainTex, i.uv);
//Merge the colours
c.rgb = (floor(c.rgb*_ColorMerge)/_ColorMerge);
//Get the normal from the bump map
float3 n = UnpackNormal(tex2D (_Bump, i.uv2));
//Based on the ambient light
float3 lightColor = UNITY_LIGHTMODEL_AMBIENT.xyz;
//Work out this distance of the light
float lengthSq = dot(i.lightDirection, i.lightDirection);
//Fix the attenuation based on the distance
float atten = 1.0 / (1.0 + lengthSq);
//Angle to the light
float diff = saturate (dot (n, normalize(i.lightDirection)));
//Perform our toon light mapping
diff = tex2D(_Ramp, float2(diff, 0.5));
//Update the colour
lightColor += _LightColor0.rgb * (diff * atten);
//Product the final color
c.rgb = lightColor * c.rgb * 2;
return c;
}
ENDCG
}
}
FallBack "Diffuse"
}
The one above has an _OutlineColor that can be changed.
Thanks a lot, ZWrite Off on the first pass and the Outline Color work perfectly! Thank you very much!
Your answer
Follow this Question
Related Questions
Ztest shader issues 0 Answers
QuickOutline: how can I change the shader to work with objects with two materials? 3 Answers
Shader Edit Help: 2D Outline Coloring 2 Answers
Better Outlines 0 Answers
Interior Outlines? 0 Answers