http://wiki.unity3d.com/index.php/Silhouette-Outlined_Diffuse

A variant of Outlined Diffuse 3 showing outlines only at the exteriors of the object. Useful for showing
which character is selected in a strategy game, for example.


Notice the left foot is far too down the ground.


The outline is merely the object in question but scaled upwards, rendered in a single color, and it always shows on top of everything (like the tree here), except the object it is outlining. You can remove the "showing on top" effect by removing all occurrences
of "ZTest Always"

Shader "Outlined/Silhouetted Diffuse" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (0.0, 0.03)) = .005
_MainTex ("Base (RGB)", 2D) = "white" { }
}
 
CGINCLUDE
#include "UnityCG.cginc"
 
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
 
struct v2f {
float4 pos : POSITION;
float4 color : COLOR;
};
 
uniform float _Outline;
uniform float4 _OutlineColor;
 
v2f vert(appdata v) {
// just make a copy of incoming vertex data but scaled according to normal direction
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
 
o.pos.xy += offset * o.pos.z * _Outline;
o.color = _OutlineColor;
return o;
}
ENDCG
 
SubShader {
Tags { "Queue" = "Transparent" }
 
// note that a vertex shader is specified here but its using the one above
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Off
ZWrite Off
ZTest Always
ColorMask RGB // alpha not used
 
// you can choose what kind of blending mode you want for the outline
Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
//Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
 
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
 
half4 frag(v2f i) :COLOR {
return i.color;
}
ENDCG
}
 
Pass {
Name "BASE"
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
Material {
Diffuse [_Color]
Ambient [_Color]
}
Lighting On
SetTexture [_MainTex] {
ConstantColor [_Color]
Combine texture * constant
}
SetTexture [_MainTex] {
Combine previous * primary DOUBLE
}
}
}
 
SubShader {
Tags { "Queue" = "Transparent" }
 
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite Off
ZTest Always
ColorMask RGB
 
// you can choose what kind of blending mode you want for the outline
Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
//Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
 
CGPROGRAM
#pragma vertex vert
#pragma exclude_renderers gles xbox360 ps3
ENDCG
SetTexture [_MainTex] { combine primary }
}
 
Pass {
Name "BASE"
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
Material {
Diffuse [_Color]
Ambient [_Color]
}
Lighting On
SetTexture [_MainTex] {
ConstantColor [_Color]
Combine texture * constant
}
SetTexture [_MainTex] {
Combine previous * primary DOUBLE
}
}
}
 
Fallback "Diffuse"
}

Bumped version:

Shader "Outlined/Silhouetted Bumped Diffuse" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (0.0, 0.03)) = .005
_MainTex ("Base (RGB)", 2D) = "white" { }
_BumpMap ("Bumpmap", 2D) = "bump" {}
}
 
CGINCLUDE
#include "UnityCG.cginc"
 
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
 
struct v2f {
float4 pos : POSITION;
float4 color : COLOR;
};
 
uniform float _Outline;
uniform float4 _OutlineColor;
 
v2f vert(appdata v) {
// just make a copy of incoming vertex data but scaled according to normal direction
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
 
o.pos.xy += offset * o.pos.z * _Outline;
o.color = _OutlineColor;
return o;
}
ENDCG
 
SubShader {
Tags { "Queue" = "Transparent" }
 
// note that a vertex shader is specified here but its using the one above
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Off
ZWrite Off
ZTest Always
 
// you can choose what kind of blending mode you want for the outline
Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
//Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
 
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
 
half4 frag(v2f i) : COLOR {
return i.color;
}
ENDCG
}
 
 
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
sampler2D _MainTex;
sampler2D _BumpMap;
uniform float3 _Color;
void surf(Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb * _Color;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
 
}
 
SubShader {
Tags { "Queue" = "Transparent" }
 
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite Off
ZTest Always
Offset 15,15
 
// you can choose what kind of blending mode you want for the outline
Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
//Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
 
CGPROGRAM
#pragma vertex vert
#pragma exclude_renderers gles xbox360 ps3
ENDCG
SetTexture [_MainTex] { combine primary }
}
 
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
sampler2D _MainTex;
sampler2D _BumpMap;
uniform float3 _Color;
void surf(Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb * _Color;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
 
}
 
Fallback "Outlined/Silhouetted Diffuse"
}

Outline Only Variant

The thing that does the trick here is "Blend Zero One" which is to completely forego rendering our object and use only the destination color (i.e. whatever is behind the object). In effect, the object itself is invisible, but we still let the outline render
itself. So that's what we're left with: only the outline.

Shader "Outlined/Silhouette Only" {
Properties {
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (0.0, 0.03)) = .005
}
 
CGINCLUDE
#include "UnityCG.cginc"
 
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
 
struct v2f {
float4 pos : POSITION;
float4 color : COLOR;
};
 
uniform float _Outline;
uniform float4 _OutlineColor;
 
v2f vert(appdata v) {
// just make a copy of incoming vertex data but scaled according to normal direction
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
 
o.pos.xy += offset * o.pos.z * _Outline;
o.color = _OutlineColor;
return o;
}
ENDCG
 
SubShader {
Tags { "Queue" = "Transparent" }
 
Pass {
Name "BASE"
Cull Back
Blend Zero One
 
// uncomment this to hide inner details:
//Offset -8, -8
 
SetTexture [_OutlineColor] {
ConstantColor (0,0,0,0)
Combine constant
}
}
 
// note that a vertex shader is specified here but its using the one above
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
 
// you can choose what kind of blending mode you want for the outline
//Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
 
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
 
half4 frag(v2f i) :COLOR {
return i.color;
}
ENDCG
}
 
 
}
 
Fallback "Diffuse"
}

Silhouette-Outlined Diffuse的更多相关文章

  1. CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection)

    CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection) 2016-08-13 由于CSh ...

  2. 【译】Unity3D Shader 新手教程(5/6) —— Bumped Diffuse Shader

    本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 动机 如果你满足以下条件,我建议你阅读这篇教程: 你想学习片段着色器(Fragment Shader). 你想实现 ...

  3. [Unity3D] Normal map、Diffuse map 和 Speculer map

    Normal map : Normal map (法线贴图) 它的作用是模拟出高模上的一些细节纹理,特别是将高模上的圆滑和粗糙度投射到低模上,让低模也有高模的效果. 因为高模的面数非常多,导入引擎后电 ...

  4. Unity3D 开发之shader教程(浅谈光照之漫反射diffuse)

    在游戏开发过程中,光照应该是必不可少部分,当然,这是指大多数的稍微大型一些的3D游戏会需要,给模型或者山山水水加上光照,会看上去更加的真实,获得更好的体验.一个本身不发光物体显示什么颜色,在于本身反射 ...

  5. u3d_Shader_effects笔记3 half diffuse 和 ramp texture

    1.前面的心情 每次写博客,先写心情也好,就当是小日记了吧.现在已经懒到不想动笔和纸来写日记了.近两天公司的活较少,晚上直接回来了,没有留公司.在公司看代码,不做工,就困... 哎,小辉哥家的老房子后 ...

  6. Tranparent/cutout/diffuse

    Shader "Tranparent/cutout/diffuse" { Properties {   //  _Color ("Main Color", Co ...

  7. 开源文件比较工具:WinMerge、KDiff3、diffuse

    为了寻找免费的BeyondCompare的替代品,最后经过实用,找到如下一些: 1.diffuse 感受:如果仅仅是比较两个文本类的文件,这个软件也就够用了. 安装好后,对着文件点击右键,会出现“Op ...

  8. <Learning How to Learn>Week One: Focused versus Diffuse Thinking

    1-1 Introduction to the focused and diffuse modes (4:40) 两种思考的模式:focused mode以及diffuse mode focused ...

  9. 开源的文件比较工具:WinMerge,KDiff3,diffuse

    为了寻找免费的BeyondCompare的替代品,最后经过实用,找到如下一些: 1.diffuse 感受:如果仅仅是比较两个文本类的文件,这个软件也就够用了. 安装好后,对着文件点击右键,会出现&qu ...

随机推荐

  1. LeetCode——Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  2. imagick图片压缩。

    选择一个合适的图片处理扩展包. 常见的扩展如GD,imagick,Gmagick. 老古董的GD丢掉吧,效率很低,而且压缩的图片体积很大=.=   imagick是个不错的选择,在PHP的图片处理扩展 ...

  3. ME51N, ME52N 创建采购申请的一个BADI

    ME51N ME52N创建修改采购申请时的一个BADI (2013-07-11 16:50:58) 转载▼ 标签: 采购申请 me51n me52 badi me_process_req_cust 分 ...

  4. 采集练习(十二) python 采集之 xbmc 酷狗电台插件

    前段时间买了个树莓派才知道有xbmc这么强大的影音软件(后来我逐渐在 电脑.手机和机顶盒上安装xbmc),在树莓派上安装xbmc后树莓派就成为了机顶盒,后面在hdpfans论坛发现了jackyspy  ...

  5. HDU - 1541 Stars 【树状数组】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...

  6. SQL语句编写注意问题

    下面就某些SQL语句的where子句编写中需要注意的问题作详细介绍.在这些where子句中,即使某些列存在索引,但是由于编写了劣质的SQL,系统在运行该SQL语句时也不能使用该索引,而同样使用全表扫描 ...

  7. Too many open files解决方案及原理

    以下是我解决Too many open files异常时学习的知识的理解和总结,如有不正确指出,敬请指出! 此问题中文搜索雷同,你可以尝试以下关键字:"file descriptor lea ...

  8. Hive- Hive安装

    Hive安装 1.1下载Hive安装包 官网:http://hive.apache.org/downloads.html 个人建议到这里下载:http://apache.forsale.plus/ 1 ...

  9. 分享知识-快乐自己:Hibernate 关联映射

    关联关系映射--概念: 关联关系是使用最多的一种关系,非常重要.在内存中反映为实体关系,映射到DB中为主外键关系. 实体间的关联,即对外键的维护.关联关系的发生,即对外键数据的改变. 外键:外面的主键 ...

  10. 51nod1671【货物运输】

    开始天真的我以为这道题和运输计划是一样的套路.于是写了一发,debug后发现过了第一个点,十分开心的交了一发,结果只过了第一个点.后来发现这个并不是一样的,因为修建黑洞之后路径法变了,而运输计划没有( ...