当前位置:网站首页>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 :

边栏推荐
- LCD12864 (ST7565P) Chinese character display (STM32F103)
- Clustered index (clustered index), nonclustered index (nonclustered index)
- Apache+PHP+MySQL环境搭建超详细!!!
- IBPS开源表单设计器有什么功能?
- Q1: error in JMeter filename must not be null or empty
- Server lease error in Hong Kong may lead to serious consequences
- Oracle create tablespaces and tables
- Dataworks development ODPs SQL development production environment automatic completion of ProjectName
- Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!
- How to select the ECS type and what to consider?
猜你喜欢

Example analysis of corrplot related heat map beautification in R language
思源笔记工具栏中的按钮名称变成了 undefined,有人遇到过吗?

Fundamentals of performance testing -- definitions of common terms

Volcano成Spark默认batch调度器

Geoscience remote sensing data collection online

Digital twin industry case: Digital Smart port

对国产数据库厂商提几个关于SQL引擎的小需求

怎么使用R包ggtreeExtra绘制进化树

What are the functions of IBPs open source form designer?

Technology implementation | Apache Doris cold and hot data storage (I)
随机推荐
Zadig + cave Iast: let safety dissolve in continuous delivery
Ribbon源码分析之@LoadBalanced与LoadBalancerClient
Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!
[video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer
Methods for comparing float types in the kernel
Programmers spend most of their time not writing code, but...
What other data besides SHP data
IP address to integer
RF_DC系统时钟设置GEN1/GEN2
Fabric ledger data block structure analysis (I): how to analyze the smart contract transaction data in the ledger
【云驻共创】ModelBox隔空作画 绘制你的专属画作
JVM tuning
What type of datetime in the CDC SQL table should be replaced
What is CNN (convolutional neural network)
Some ideas about chaos Engineering
Q1: error in JMeter filename must not be null or empty
Application DDoS attack principle and defense method
Database index can improve query efficiency. Ask what will improve, what is the difference between inapplicable index and index use, and what will happen.
Get to know the data structure of redis - hash
8 challenges of BSS application cloud native deployment