当前位置:网站首页>Fuzzy background of unity (take you to appreciate the hazy beauty of women)
Fuzzy background of unity (take you to appreciate the hazy beauty of women)
2022-06-24 19:58:00 【Hua Weiyun】
Recently, the project has used fuzzy background effect . The implementation method is :shader. Code from Baidu . Next, let's introduce the implementation process of fuzzy background . First of all, you need one that your ancestors have written shader( Of course , If you have the ability , You can do it yourself !)
If you don't have one, you can download shaders and corresponding shader.
Fuzzy background shader
Blur background shader
shader as follows :
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 How to use :
Import downloaded shaders and for the project shader
Usually we build Material Folder to store shaders 
establish Shader Folder storage shader Code 
New background Image, Name it bg, And assign it with the specified picture resources , as follows :
Create a new fuzzy material background Image, Name it mask,SourceImage Specify any value ;Material Specifies the shader to download 
The effect is as follows :
Adjust the of the shader Size Can adjust the degree of blur . The bigger the value is. , The more blurred the effect .
Insert picture description here
take size Value pull to 5 The effect is as follows :

边栏推荐
- What do I mean when I link Mysql to report this error?
- Unity mobile game performance optimization spectrum CPU time-consuming optimization divided by engine modules
- Jsup supports XPath
- How to select the ECS type and what to consider?
- LCD12864 (ST7565P) Chinese character display (STM32F103)
- 数字孪生行业案例:智慧港口数字化
- IBPS开源表单设计器有什么功能?
- Ask a question. Adbhi supports the retention of 100 databases with the latest IDs. Is this an operation like this
- RF_DC系统时钟设置GEN1/GEN2
- Based on STM32F103 0.96 inch OLED LCD driver (IIC communication)
猜你喜欢

工作6年,月薪3W,1名PM的奋斗史

Technology implementation | Apache Doris cold and hot data storage (I)

How to customize cursor position in wechat applet rotation chart

An accident caused by a MySQL misoperation cannot be withstood by High Availability!

Example analysis of corrplot related heat map beautification in R language

Vs2017 setting function Chinese Notes

Win7 10 tips for installing Office2010 five solutions for installing MSXML components

php OSS文件读取和写入文件,workerman生成临时文件并输出浏览器下载

Obstacle avoidance sensor module (stm32f103c8t6)

R for Data Science (notes) -- data transformation (used by filter)
随机推荐
Vs2017 setting function Chinese Notes
Volcano becomes spark default batch scheduler
UART communication (STM32F103 library function)
Does finkcdc support sqlserver2008?
Programmers spend most of their time not writing code, but...
Error in Android connection database query statement
Apache+php+mysql environment construction is super detailed!!!
Ribbon源码分析之@LoadBalanced与LoadBalancerClient
What are the functions of IBPs open source form designer?
Unityshader world coordinates do not change with the model
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
Instruction rearrangement concept
Bytebase joins Alibaba cloud polardb open source database community
Landcover100, planned land cover website
PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
Some small requirements for SQL Engine for domestic database manufacturers
How to customize cursor position in wechat applet rotation chart
Starring develops httpjson access point + Database
Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!
Kubernetes集群部署