服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - unity实现玻璃效果

unity实现玻璃效果

2022-09-06 11:18神码编程 C#

这篇文章主要为大家详细介绍了unity实现玻璃效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了unity实现玻璃效果的具体代码,供大家参考,具体内容如下

一、使用cubemap,做一个假反射

unity实现玻璃效果

shader代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
shader "custom/glassshader" {
 properties {
 _maincolor("main color",color)=(1,1,1,1)
 _maintex ("base (rgb)", 2d) = "white" {}
 _cube("cube",cube)=""{}
 }
 subshader {
 tags {"rendertype"="opaque"}
 lod 200
 
 //cull off
 
 cgprogram
 #pragma surface surf lambert alpha
 
 fixed4 _maincolor;
 sampler2d _maintex;
 samplercube _cube;
 
 struct input {
 float2 uv_maintex;
 float3 worldrefl;
 };
 
 void surf (input in, inout surfaceoutput o) {
 half4 c = tex2d (_maintex, in.uv_maintex);
 o.albedo = c.rgb*_maincolor.rgb;
 o.emission=texcube(_cube,in.worldrefl).rgb;
 o.alpha = c.a*_maincolor.a;
 }
 endcg
 }
 
 fallback "diffuse"
}

二、使用grabpass,抓取屏幕纹理,实现实时反射

shader代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
shader "unlit/grabglass"
{
 properties
 {
 _color("main color",color)=(1,1,1,1)
 _maintex ("texture", 2d) = "white" {}
 }
 subshader
 {
 
 
 tags {"queue"="transparent" "rendertype"="opaque" }//opaque
 lod 100
 
 //绘制半透明物体 关闭深度缓存
 zwrite off
 //透明混合
 blend srcalpha oneminussrcalpha
 
 //如果没有命名,则可以用_grabtexture来读取,不过开销很大,应用到特殊效果时才去应用
 grabpass
 {
 "_grabtex"
 }
 
 pass
 {
 cgprogram
 #pragma vertex vert
 #pragma fragment frag
 // make fog work
 #pragma multi_compile_fog
 
 #include "unitycg.cginc"
 
 struct appdata
 {
 float4 vertex : position;
 float2 uv : texcoord0;
 };
 
 struct v2f
 {
 float2 uv : texcoord0;
 unity_fog_coords(1)
 float4 vertex : sv_position;
 };
 
 sampler2d _maintex;
 float4 _maintex_st;
 fixed4 _color;
 sampler2d _grabtex;
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(unity_matrix_mvp, v.vertex);
 o.uv = transform_tex(v.uv, _maintex);
 unity_transfer_fog(o,o.vertex);
 return o;
 }
 
 fixed4 frag (v2f i) : sv_target
 {
 // sample the texture
 fixed4 col = tex2d(_maintex, i.uv)*_color;
 
 // apply fog
 unity_apply_fog(i.fogcoord, col);
 //调整一下uv
 float2 uv=i.uv;
 uv.x=1-uv.x;
 return col*tex2d(_grabtex,uv);
 }
 endcg
 }
 }
}

效果如下:

unity实现玻璃效果

三、使用摄像机实现实时反射

因为grabpass,相对来说消耗较大,只建议用于一些特殊效果,于是这里就借助辅助摄像机,来实现实时反射效果,当然这需要多写一个脚本,同时需要在辅助摄像机中屏蔽玻璃本身

shader代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
shader "unlit/cameraglass"
{
 properties
 {
 _maintex ("texture", 2d) = "white" {}
 }
 subshader
 {
 tags { "rendertype"="opaque" }
 lod 100
 
 pass
 {
 cgprogram
 #pragma vertex vert
 #pragma fragment frag
 // make fog work
 #pragma multi_compile_fog
 
 #include "unitycg.cginc"
 
 struct appdata
 {
 float4 vertex : position;
 float2 uv : texcoord0;
 };
 
 struct v2f
 {
 float2 uv : texcoord0;
 unity_fog_coords(1)
 float4 vertex : sv_position;
 };
 
 sampler2d _maintex;
 float4 _maintex_st;
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(unity_matrix_mvp, v.vertex);
 o.uv = transform_tex(v.uv, _maintex);
 unity_transfer_fog(o,o.vertex);
 return o;
 }
 
 fixed4 frag (v2f i) : sv_target
 {
 // sample the texture 需要调整一下uv
 fixed4 col = tex2d(_maintex, 1-i.uv);
 // apply fog
 unity_apply_fog(i.fogcoord, col);
 return col;
 }
 endcg
 }
 }
}

脚本代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using unityengine;
using system.collections;
 
public class renderglasstexture : monobehaviour {
 
 /// <summary>
 /// 辅助摄像机
 /// 原理:就是将辅助摄像机所看到的内容渲染到玻璃物体上,所以就实现了实时反射的镜面效果
 /// 因为玻璃也是场景中的物体,所以辅助摄像机也会看见他
 /// 所以最好能将玻璃物体单独放在一个层级中,让辅助摄像机不去渲染他
 /// </summary>
 public camera cam;
 private rendertexture rendertex;
 /// <summary>
 /// 玻璃shader
 /// </summary>
 public shader glassshader;
 /// <summary>
 /// 玻璃材质
 /// </summary>
 private material m_glassmaterial;
 protected material glassmaterial
 {
  get
  {
   if (m_glassmaterial == null)
   {
    m_glassmaterial = new material(glassshader);
   }
   return m_glassmaterial;
  }
 }
 
 // use this for initialization
 void start () {
  rendertex = new rendertexture(screen.width, screen.height, 16);
  cam.targettexture = rendertex;
 }
 
 //在摄像机开始裁剪场景之前调用
 void onprecull()
 {
  glassmaterial.settexture("_maintex", rendertex);
 }
 
 //在相机完成场景渲染后调用
 void onpostrender()
 {
  glassmaterial.settexture("_maintex", null);
 }
}

效果如下:

unity实现玻璃效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq992817263/article/details/50773052

延伸 · 阅读

精彩推荐
  • C#如何使用C#程序给PDF文件添加编辑域

    如何使用C#程序给PDF文件添加编辑域

    本文主要给大家分享的是通过C#操作PDF类库iTextSharp来实现在在PDF文档中填写日期或签名之类的能编辑的文本域,非常的简单实用,有需要的小伙伴可以参考...

    冯威9332021-12-18
  • C#C#获取动态生成的CheckBox值

    C#获取动态生成的CheckBox值

    checkbox是VS2012的常用控件之一,可以方便的为某些功能取消或启用,下面教你如何简单使用checkbox。本文通过两种方法给大家介绍,需要的朋友一起看看吧...

    C#教程网9012021-10-27
  • C#WPF基础教程之元素绑定详解

    WPF基础教程之元素绑定详解

    这篇文章主要给大家介绍了关于WPF基础教程之元素绑定的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或工作具有一定的参考学习价值,需...

    SmilelyCoding4292022-03-07
  • C#WPF/Silverlight实现图片局部放大的方法分析

    WPF/Silverlight实现图片局部放大的方法分析

    这篇文章主要介绍了WPF/Silverlight实现图片局部放大的方法,结合实例形式分析了WPF/Silverlight针对图片属性操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun6522021-12-29
  • C#Unity3D实现鼠标控制视角转动

    Unity3D实现鼠标控制视角转动

    这篇文章主要为大家详细介绍了Unity3D实现鼠标控制视角转动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    JeterPong10682022-07-06
  • C#C# Soap调用WebService的实例

    C# Soap调用WebService的实例

    下面小编就为大家带来一篇C# Soap调WebService的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    梦想总是这么遥远5092022-02-16
  • C#详解C#正则表达式Regex常用匹配

    详解C#正则表达式Regex常用匹配

    这篇文章主要介绍了C#正则表达式Regex常用匹配,利用Regex类实现验证,感兴趣的小伙伴们可以参考一下...

    柔城9352021-11-05
  • C#c#中list.FindAll与for循环的性能对比总结

    c#中list.FindAll与for循环的性能对比总结

    这篇文章主要给大家总结介绍了关于c#中list.FindAll与for循环的性能,文中通过详细的示例代码给大家介绍了这两者之间的性能,对大家的学习或工作具有一...

    阮玉峰5962022-01-25