基本原理:被遮挡的部分关闭深度写入, 显示透明效果;未被遮挡的部分不关闭深度测试,显示正常贴图效果,即使用两个Pass即可。

Pass1:关闭深度写入(ZWrite Off),深度测试渲染较远的物体,即模型被物体遮挡的部分(ztest greater)。

Pass2:开启深度写入,正常渲染。

 1 Shader "xj/Character/Normal" {
 2     Properties {
 3         _Color ("Main Color", Color) = (1,1,1,1)
 4         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
 5         _intensity ("Intensity", float ) = 1.0
 6 
 7         _RimColor("RimColor",Color) = (0,1,1,1)
 8         _RimPower ("Rim Power", Range(0.1,8.0)) = 1.0
 9     }
10 
11 
12     SubShader
13     {
14         LOD 300  
15         Tags { "Queue" = "Geometry+500" "RenderType"="Opaque" } 
16         Pass
17         {
18             Blend SrcAlpha One
19             ZWrite off
20             Lighting off
21  
22             ztest greater
23  
24             CGPROGRAM
25             #pragma vertex vert
26             #pragma fragment frag
27             #include "UnityCG.cginc"
28  
29             float4 _RimColor;
30             float _RimPower;
31             
32             struct appdata_t {
33                 float4 vertex : POSITION;
34                 float2 texcoord : TEXCOORD0;
35                 float4 color:COLOR;
36                 float4 normal:NORMAL;
37             };
38  
39             struct v2f {
40                 float4  pos : SV_POSITION;
41                 float4  color:COLOR;
42             } ;
43             v2f vert (appdata_t v)
44             {
45                 v2f o;
46                 o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
47                 float3 viewDir = normalize(ObjSpaceViewDir(v.vertex)); 
48                 //视线与法线垂直的部分(点乘为0)即是外轮廓,加重描绘
49                 float rim = 1 - saturate(dot(viewDir,v.normal )); 
50                 o.color = _RimColor*pow(rim,_RimPower);
51                 return o;
52             }
53             float4 frag (v2f i) : COLOR
54             {
55                 return i.color; 
56             }
57             ENDCG
58         }
59         Pass {  
60             CGPROGRAM
61                 #pragma vertex vert
62                 #pragma fragment frag
63                 #pragma fragmentoption ARB_precision_hint_fastest
64                 
65                 #include "UnityCG.cginc"
66 
67                 struct appdata_t {
68                     float4 vertex : POSITION;
69                     half2 texcoord : TEXCOORD0;
70                 };
71 
72                 struct v2f {
73                     float4 vertex : SV_POSITION;
74                     half2 texcoord : TEXCOORD0;
75                 };
76 
77                 sampler2D _MainTex;
78                 fixed4 _Color;
79                 fixed _intensity;
80                 
81                 v2f vert (appdata_t v)
82                 {
83                     v2f o;
84                     o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
85                     o.texcoord = v.texcoord;
86                     return o;
87                 }
88                 
89                 fixed4 frag (v2f i) : SV_Target
90                 {
91                     fixed4 col = tex2D(_MainTex, i.texcoord) * _Color * _intensity;
92                     return col;
93                 }
94             ENDCG
95         }
96     }
97 }

 

posted @ 2018-12-15 17:23 Mr. Ant 阅读(1067) 评论(0) 推荐(0) 编辑
摘要: 基本原理:使用Projector和Camera结合实现的动态阴影,使用Camera渲染出一张模型轮廓图,再用Projector投射到场景的接收物体上。 思路: 1、创建一个GameObject用来挂载Projector和Camera。 2、生成一张RenderTexture贴图,命名为mShadow 阅读全文
posted @ 2018-12-15 16:43 Mr. Ant 阅读(515) 评论(0) 推荐(0) 编辑
摘要: 基本思路:Shader用两个Pass,一个渲染描边部分,一个渲染物体部分。 Pass1:剔除正面,渲染背面,把顶点延法线方向外围扩展一定宽度,用来表现描边的粗细,这部分用自己设定的颜色。 Pass2:剔除背面,渲染正面,正常渲染所看到的物体模型。 阅读全文
posted @ 2018-12-15 15:25 Mr. Ant 阅读(1117) 评论(0) 推荐(0) 编辑
摘要: 基本思路:unity有一种很容易实现光影效果的神器,那就是unity自带的projector。 首先需要用到两张贴图: 一张投影需要的光圈贴图,如下: 一张根据远近距离显示暗淡的贴图(左至右,白渐变黑),如下: 主要用到两个参数:_Projector 和 _ProjectorClip mul(_Pr 阅读全文
posted @ 2018-12-15 15:18 Mr. Ant 阅读(1125) 评论(0) 推荐(0) 编辑
摘要: Shader深度渲染队列Queue预定义值:Background(1000)、Geometry(2000)、AlphaTest(2450)、Transparent(3000)、Overlay(4000)。 渲染优先顺序: Queue值越小越先渲染,后渲染的物体会覆盖先渲染的物体。 测试:我想让远处的 阅读全文
posted @ 2018-10-24 20:55 Mr. Ant 阅读(6208) 评论(1) 推荐(1) 编辑
摘要: 实现原理:雨和雪的实现基本类似,天空中飘的雨和雪,地上的水花和雪花,增加一个下落速度和风速,就形成了天气了。 天气雪实现步骤: 1、预生成Mesh:随机生成多个空中飘落的雪网格和地面上的雪花网格。 2、预生成雪shader和雪花shader,雪shader支持雪的显示,雪花shader支持序列帧动画 阅读全文
posted @ 2018-06-25 11:59 Mr. Ant 阅读(985) 评论(0) 推荐(0) 编辑
摘要: 实现原理:主要思想是设置显示uv纹理的大小,并逐帧修改图片的uv坐标。 实现步骤 1、我们首先用_Time.y和速度属性_Speed相乘得到模拟的时间。 2、然后我们用time除以_HorizontalAmount的结果值作为当前的行索引,除法结果的余数则是列索引。 3、接下来,我们根据行索引和列索 阅读全文
posted @ 2018-06-23 10:45 Mr. Ant 阅读(1934) 评论(0) 推荐(0) 编辑
摘要: 一、模型打包流程 二、模型创建流程 三、模型API框架 阅读全文
posted @ 2018-06-21 15:09 Mr. Ant 阅读(623) 评论(0) 推荐(0) 编辑
摘要: Mesh概念:Mesh是Unity中的一个组件,称为网格组件。通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而多边形实际上是由多个三角形拼接而成的。所以一个3D模型的表面其实是由多个彼此相连的三角面构成。三维空间中,构成这些三角形的点和边的集合就是Mesh。 Mesh组成: 1、顶点 阅读全文
posted @ 2018-06-21 14:32 Mr. Ant 阅读(4180) 评论(0) 推荐(0) 编辑
摘要: 前提:本文默认你安装了unity5.6版本,不是这个版本的没有Gradle(new)选项,也默认你安装了Android Studio并配置好了环境变量。 Gradle(new):打包Android Studio工程。 ADT(legacy):打包Eclipse工程。 1、按下图所标注进行工程设置: 阅读全文
posted @ 2018-05-15 20:56 Mr. Ant 阅读(2943) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示