Hi,
I have attached a Blender screenshot with what I want to accomplish:
- its the drawing of the orange outlineline.
How can I render the outline of a 3D objekt with Unity?
Hi,
I have attached a Blender screenshot with what I want to accomplish:
How can I render the outline of a 3D objekt with Unity?
Stick this pass in your shader and the properties will let you control the width and color.
Properties {
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline Width", Float) = 0.005
}
Pass {
Name "Outline"
Tags { "LightMode" = "Always"}
Lighting Off
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : SV_POSITION;
};
float _Outline;
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
norm.x *= UNITY_MATRIX_P[0][0];
norm.y *= UNITY_MATRIX_P[1][1];
o.pos.xy += norm.xy * o.pos.z * _Outline;
return o;
}
float4 _OutlineColor;
float4 frag(v2f i) :COLOR {
return float4(_OutlineColor.rgb,0);
}
ENDCG
}
The effect is to replace the outside edge with a color, not to create a variable-pixel-width border. It matches what would happen if you were manually to select the edges, but updates based on viewing angle.
… put the object into a unique camera (set up culling masks accordingly) and use the edge detection image effect on that one (?)
EDIT: normals sensitivity should be 0 then …
Thanks, I tried to add the pass to another shader and this is what turned out:
Here is the shader:
hader “Test” {
Properties {
_Color (“Main Color”, Color) = (1,1,1,0.5)
_SpecColor (“Spec Color”, Color) = (1,1,1,1)
_Emission (“Emmisive Color”, Color) = (0,0,0,0)
_Shininess (“Shininess”, Range (0.01, 1)) = 0.7
_MainTex (“Base (RGB)”, 2D) = “white” { }
_OutlineColor (“Outline Color”, Color) = (0,0,0,1)
_Outline (“Outline Width”, Float) = 0.005
}
SubShader {
Pass {
Material {
Diffuse [_Color]
Ambient [_Color]
Shininess [_Shininess]
Specular [_SpecColor]
Emission [_Emission]
}
Lighting On
SeparateSpecular On
SetTexture [_MainTex] {
constantColor [_Color]
Combine texture * primary DOUBLE, texture * constant
}
}
Pass {
Name “Outline”
Tags { “LightMode” = “Always”}
Lighting Off
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include “UnityCG.cginc”
struct v2f
{
float4 pos : SV_POSITION;
};
float _Outline;
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
norm.x *= UNITY_MATRIX_P[0][0];
norm.y *= UNITY_MATRIX_P[1][1];
o.pos.xy += norm.xy * o.pos.z * _Outline;
return o;
}
float4 _OutlineColor;
float4 frag(v2f i) :COLOR {
return float4(_OutlineColor.rgb,0);
}
ENDCG
}
}
}
What do I need to make with the shader to make the model look like this with orange outline:
It won’t look exactly like that image, that effect’s a bit harder to achieve. But it’ll be sort of close.
In your material properties, try increasing the Outline Width a bit. Changing the Outline Color will change the colour of the outline (set it to an orange colour).
Thanks,
I somewhy had to change the line width to negative value to get the right effect, so with -0.1 it looks like this:
:), works now
Now I need to apply such rendering to a 2D flat mesh to render it’s outlines of subobjects that are precisely next to each other:
Here is the footage from Unity with having the same shader applied to the material of part of the flat white human shape, but somewhy it does not render the outline as it does with the 3D model.
And this is kind of what I want to achieve (altough in blender the orange line does not display properly since the 2 meshes are exactly next to each other):