Unity3D中shader 轮廓描边效果实现代码

 更新时间:2017年03月14日 10:25:08   投稿:lqh  
这篇文章主要介绍了Unity3D中shader 轮廓描边效果的相关资料,需要的朋友可以参考下

Unity3D中shader 轮廓描边效果

想利用Unity3D中shader这个功能实现描边轮廓边框效果该怎么做呢,相信有很多搞开发的人想知道,为此下面就给大家介绍下方法。

  Shade实现描边效果,如下图中的3D球效果图

          Shade代码如下:

Shader "Outlined/Silhouetted Diffuse" { 
 Properties { 
 _Color ("Main Color", Color) = (1,1,1,1) 
 _OutlineColor ("Outline Color", Color) = (0,0,0,1) //改变这个能改变轮廓边的颜色 
 _Outline ("Outline width", Range (0.0, 0.03)) = 0.008 //改变这个能改变轮廓边的粗细 
 _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" 
} 

  以上就是Unity3D中利用shader这个功能实现描边轮廓边框效果的方法,不会的话就直接那这串代码去尝试吧。

       感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Scala函数式编程专题--函数思想介绍

    Scala函数式编程专题--函数思想介绍

    这篇文章主要介绍了Scala函数式编程的的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • YOLO v4常见的非线性激活函数详解

    YOLO v4常见的非线性激活函数详解

    这篇文章主要介绍了YOLO v4常见的非线性激活函数,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05
  • 8个基于Lucene的开源搜索引擎(推荐)

    8个基于Lucene的开源搜索引擎(推荐)

    Lucene是一种功能强大且被广泛使用的搜索引擎,以下列出8种基于Lucene的搜索引擎,你可以想象他们有多么强大
    2017-04-04
  • vscode安装git及项目开发过程

    vscode安装git及项目开发过程

    这篇文章主要介绍了vscode安装git及项目开发过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • K8S二进制部署的K8S(1.15)部署hpa功能

    K8S二进制部署的K8S(1.15)部署hpa功能

    这篇文章主要介绍了K8S二进制部署的K8S(1.15)部署hpa功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • 分享Git常用7大技巧和命令

    分享Git常用7大技巧和命令

    Git 是一个非常强大的工具,它包含丰富的工具用以维护项目,我们在日期工作中会经常用到一些常用命令,本文先为大家介绍下面七个Git常用技巧和命令
    2018-09-09
  • 汇编优化提示

    汇编优化提示

    暑假瞄了一些汇编优化的文章,感觉这篇有点意思。尽管英文水平不咋地,还是倔起牛劲翻译了下。肯定有不好的地方,大家海涵~英文原文附件给出~如果有什么错误还望批评指正~另外,如果admin感觉可以加精的话就麻烦下了
    2012-07-07
  • 浅谈测试驱动开发TDD之争

    浅谈测试驱动开发TDD之争

    在软件行业中,神仙打架的名场面,那就不得不提的是2014年的那场——测试驱动开发(TDD)之争。
    2021-05-05
  • Idea 无法引用类问题解决办法

    Idea 无法引用类问题解决办法

    这篇文章主要介绍了 Idea 无法引用类问题解决办法的相关资料,需要的朋友可以参考下
    2017-03-03
  • 浅谈架构模式变迁之从分层架构到微服务架构

    浅谈架构模式变迁之从分层架构到微服务架构

    一般地,架构模式大致可以分成两类,单体架构(monolithic architecture)和分布式架构(distributed architecture)。
    2021-05-05

最新评论