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

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

服务器之家 - 编程语言 - C# - Unity Shader实现2D游戏迷雾

Unity Shader实现2D游戏迷雾

2022-09-06 11:24Emmmwzh C#

这篇文章主要为大家详细介绍了Unity Shader实现2D游戏迷雾,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了unity shader实现2d游戏迷雾的具体代码,供大家参考,具体内容如下

先看效果吧。

Unity Shader实现2D游戏迷雾

Unity Shader实现2D游戏迷雾

我使用的是屏幕后处理效果,首先先去photoshop做一张图片如下,用画笔点一个点就可以了,使用它来对摄像机截取的图片进行处理。

Unity Shader实现2D游戏迷雾

在摄像机上添加脚本文件

?
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
using system.collections;
using system.collections.generic;
using unityengine;
 
public class testscript : monobehaviour
{
 [range(0,3)]
 public float lerp = 0;//使用它来调整可视区域的大小
 public texture2d masktex;
 public shader screanshader;
 public material getmaterial
 {
  get
  {
   if(_material ==null) _material = new material(screanshader);
   return _material;
  }
 }
 private material _material = null;
 //src是摄像机截取到的照片,dest是处理过的图片
 void onrenderimage(rendertexture src, rendertexture dest)
 {
  getmaterial.settexture("_maintex", src);
  getmaterial.settexture("_masktex", masktex);
  getmaterial.setfloat("_lerp", lerp);
  graphics.blit(src, dest, getmaterial);
 }
}

对应的shader,思路就是把masktex的颜色翻转一下然后直接乘上去就可以了,小数越乘越小,越小颜色越黑。

?
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
shader "wzhhh/myshader2" {
 properties{
 _maintex("maintex",2d) = "white"{}
 _masktex("masktex",2d) = "white"{}
 _lerp("lerp",range(0,3)) = 1
 }
 subshader{
 pass{
 tags{ "lightmode" = "forwardbase" }
 
 cgprogram
 #include "lighting.cginc"
 #pragma vertex vert
 #pragma fragment frag
 sampler2d _masktex;
 sampler2d _maintex;
 float4 _maintex_st;
 float _alphabase;
 float _lerp;
 struct a2v {
 float4 vertex : position;
 float2 texcoord : texcoord0;
 };
 struct v2f {
 float4 pos : sv_position;
 fixed2 uv : texcoord0;
 };
 v2f vert(a2v i) {
 v2f o;
 o.pos = unityobjecttoclippos(i.vertex);
 o.uv = transform_tex(i.texcoord, _maintex);
 return o;
 }
 fixed4 frag(v2f o) :sv_target{
 fixed4 color = tex2d(_masktex, o.uv);
 color.r = 1 - color.r;
 color.g = 1 - color.g;
 color.b = 1 - color.b;
 fixed4 color2 = tex2d(_maintex, o.uv);
 color2.r *= color.r*_lerp;
 color2.g *= color.g*_lerp;
 color2.b *= color.b*_lerp;
 return color2;
 }
 endcg
 }
 }
}

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

原文链接:https://blog.csdn.net/qq_30858573/article/details/78992785

延伸 · 阅读

精彩推荐
  • C#C#根据年月日计算星期几的函数

    C#根据年月日计算星期几的函数

    这篇文章主要为大家详细介绍了C#实现根据年月日计算星期几的函数,感兴趣的小伙伴们可以参考一下...

    草下飞11012021-12-02
  • C#MVC设定默认路由为指定的Area下的某个action

    MVC设定默认路由为指定的Area下的某个action

    今天小编就为大家分享一篇关于MVC设定默认路由为指定的Area下的某个action,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一...

    chenqiangdage7082022-03-08
  • C#C#实现字符串倒序的写法

    C#实现字符串倒序的写法

    这篇文章主要为大家详细介绍了C#实现字符串倒序的多种写法,以LINQ写法最为简洁,感兴趣的朋友可以参考一下...

    zhangbaochong6672021-11-22
  • C#C#实现Check Password和锁定输错密码锁定账户功能

    C#实现Check Password和锁定输错密码锁定账户功能

    C#实现的Check Password,并根据输错密码的次数分情况锁定账户:如果输入错误3次,登录账户锁定5分钟并提示X点X分后重试登录,具体实现代码感兴趣的朋友...

    農碼一生11122022-08-17
  • C#C#代码实现PDF文档操作类

    C#代码实现PDF文档操作类

    本篇文章给大家介绍使用pdf文档操作C#代码,本文代码非常简单,代码附有注释,需要注意的是:需要添加itextsharp.dll引用才可以正常通过编译,感兴趣的朋...

    C#教程网3772021-11-01
  • C#WPF实现2048小游戏

    WPF实现2048小游戏

    这篇文章主要为大家详细介绍了WPF实现2048小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xddc8522022-02-20
  • C#Unity实现简单虚拟摇杆

    Unity实现简单虚拟摇杆

    这篇文章主要为大家详细介绍了Unity实现简单虚拟摇杆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    ancoloo3792022-08-31
  • C#C# Dictionary和SortedDictionary的简介

    C# Dictionary和SortedDictionary的简介

    今天小编就为大家分享一篇关于C# Dictionary和SortedDictionary的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小...

    Czhenya6952022-03-03