当前位置:网站首页>unity之模糊背景(带你欣赏女人的朦胧美)
unity之模糊背景(带你欣赏女人的朦胧美)
2022-06-24 18:58:00 【华为云】
最近项目用到了模糊背景效果。实现方法为:shader。代码来自百度。接下来给大家介绍一下模糊背景的实现过程。首先你需要一个先人已经写好的shader(当然,如果你有能力,也可以自己实现!)
没有的可以通过以下链接下载材质球和对应的shader。
模糊背景shader
模糊背景材质球
shader如下:
Shader "FT/Blur/Back"{ Properties { _Color ("Main Color", Color) = (1,1,1,1) _Size ("Size", Range(0, 20)) = 1 } Category { // We must be transparent, so other objects are drawn before this one. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType" = "Plane" "CanUseSpriteAtlas" = "True" } SubShader { // Horizontal blur GrabPass { Tags { "LightMode" = "Always" } } Pass { Tags { "LightMode" = "Always" } Name "BackBlurHor" CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; float4 color : COLOR; }; struct v2f { float4 vertex : POSITION; float4 uvgrab : TEXCOORD0; float4 color : COLOR; }; v2f vert (appdata_t v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); #if UNITY_UV_STARTS_AT_TOP float scale = -1.0; #else float scale = 1.0; #endif o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5; o.uvgrab.zw = o.vertex.zw; o.color = v.color; return o; } sampler2D _GrabTexture; float4 _GrabTexture_TexelSize; float4 _MainTex_TexelSize; float _Size; uniform float4 _Color; half4 GrabPixel(v2f i, float weight, float kernel){ kernel = sign(sign(abs(i.uvgrab.x)) + sign(abs(i.uvgrab.y))) * kernel; return tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernel * _Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight; } half4 frag( v2f i ) : COLOR { half4 sum = half4(0,0,0,0); sum += GrabPixel(i, 0.05, -4.0); sum += GrabPixel(i, 0.09, -3.0); sum += GrabPixel(i, 0.12, -2.0); sum += GrabPixel(i, 0.15, -1.0); sum += GrabPixel(i, 0.18, 0.0); sum += GrabPixel(i, 0.15, +1.0); sum += GrabPixel(i, 0.12, +2.0); sum += GrabPixel(i, 0.09, +3.0); sum += GrabPixel(i, 0.05, +4.0); float4 col5 = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab)); fixed decayFactor = sign(sign(abs(i.uvgrab.x)) + sign(abs(i.uvgrab.y))); sum = lerp(col5, sum, decayFactor) * i.color * _Color; return sum; } ENDCG } // Vertical blur GrabPass { Tags { "LightMode" = "Always" } } Pass { Tags { "LightMode" = "Always" } Name "BackBlurVer" CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float2 texcoord: TEXCOORD0; float4 color : COLOR; }; struct v2f { float4 vertex : POSITION; float4 uvgrab : TEXCOORD0; float4 color : COLOR; }; v2f vert (appdata_t v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); #if UNITY_UV_STARTS_AT_TOP float scale = -1.0; #else float scale = 1.0; #endif o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5; o.uvgrab.zw = o.vertex.zw; o.color = v.color; return o; } sampler2D _GrabTexture; float4 _GrabTexture_TexelSize; float _Size; uniform float4 _Color; half4 GrabPixel(v2f i, float weight, float kernel){ kernel = sign(sign(abs(i.uvgrab.x)) + sign(abs(i.uvgrab.y))) * kernel; return tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernel * _Size, i.uvgrab.z, i.uvgrab.w))) * weight; } half4 frag( v2f i ) : COLOR { half4 sum = half4(0,0,0,0); sum += GrabPixel(i, 0.05, -4.0); sum += GrabPixel(i, 0.09, -3.0); sum += GrabPixel(i, 0.12, -2.0); sum += GrabPixel(i, 0.15, -1.0); sum += GrabPixel(i, 0.18, 0.0); sum += GrabPixel(i, 0.15, +1.0); sum += GrabPixel(i, 0.12, +2.0); sum += GrabPixel(i, 0.09, +3.0); sum += GrabPixel(i, 0.05, +4.0); float4 col5 = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab)); fixed decayFactor = sign(sign(abs(i.uvgrab.x)) + sign(abs(i.uvgrab.y))); sum = lerp(col5, sum, decayFactor) * i.color * _Color; return sum; } ENDCG } } }}unity中使用方法:
为工程导入下载的材质球和shader
通常我们新建Material文件夹存放材质球
创建Shader文件夹存放shader代码
新建背景Image,命名为bg,并为其赋上指定的图片资源,如下:
新建模糊材质背景Image,命名为mask,SourceImage指定任意值即可;Material指定下载的材质球
效果如下:
调节材质球的Size可以调节模糊程度。值越大,效果越模糊。
在这里插入图片描述
将size值拉到5效果如下:

边栏推荐
- How to use JWT authentication in thinkphp6
- How to select the ECS type and what to consider?
- Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
- Geoscience remote sensing data collection online
- Bytebase rejoint la communauté de base de données open source d'alicloud polardb
- Digital twin industry case: Digital Smart port
- The difference between the lazy man mode and the hungry man mode
- JVM tuning
- Bytebase 加入阿裏雲 PolarDB 開源數據庫社區
- Jsup supports XPath
猜你喜欢

Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!

Experience of MDM master data project implementation for manufacturing projects

Saltstack state state file configuration instance

Geoscience remote sensing data collection online

A detailed explanation of the implementation principle of go Distributed Link Tracking

Understanding openstack network

Nodered has no return value after successfully inserting into the database (the request cannot be ended)

Volcano becomes spark default batch scheduler

Vs2017 add header file path method

60 个神级 VS Code 插件!!
随机推荐
Write a positive integer to the node and return a floating-point number multiplied by 0.85 when reading the node
Capacitive inching touch switch module control (stm32f103c8t6)
8 challenges of BSS application cloud native deployment
Experience of MDM master data project implementation for manufacturing projects
Unityshader world coordinates do not change with the model
MySQL binlog data source configuration document, please share
Bytebase 加入阿里云 PolarDB 开源数据库社区
R语言corrplot相关热图美化实例分析
Understanding openstack network
JVM tuning
[R tidyverse] use of select verb
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map
IBPS开源表单设计器有什么功能?
Maps are grouped according to the values of the passed in parameters (similar to database groupby)
The agile way? Is agile development really out of date?
物联网?快来看 Arduino 上云啦
How to use JWT authentication in thinkphp6
Apache+php+mysql environment construction is super detailed!!!
程序员大部分时间不是写代码,而是。。。
R for Data Science (notes) -- data transformation (used by filter)