当前位置:网站首页>Introduction and application of smoothstep in unity: optimization of dissolution effect
Introduction and application of smoothstep in unity: optimization of dissolution effect
2022-07-07 12:02:00 【Blowing breeze and three drops of water】
Unity in SmoothStep Introduction and Application : Optimization of dissolution effect
Last article Use RampTex To add a unsatisfactory edge color change to the dissolve effect , In this article, we will further optimize , And do some simple analysis of the corresponding principle .
Today's main content is as follows :
- Introduction to difference function :
smoothstep
- Use the difference function to improve the dissolution effect
- Realize further optimization of the scheme
Old rules , Let's see the end result first :
Introduction to difference function : smoothstep
smoothstep(edge_low, edge_up, x)
function :
[edge_low, edge_up]
Is a specified range of differencesx
Is any real number- The result of the function is :
if x < edge_low; return 0
.if x > edge_up; return 1
.- If
x
be inedge_low
andedge_up
Between , Then return tox
stay[0, 1]
Mapping values in the range- For example, the specified range is
[0, 10]
,x=5
, We map it to[0, 1]
after , The corresponding mapping value is0.5
- For example, the specified range is
[0, 100]
,x=5
, We map it to[0, 1]
after , The corresponding mapping value is0.05
- In essence, I will
[edge_low, edge_up]
Mapping to[0, 1]
, And then findx
stay[0, 1]
Mapping values in - Note that the instructions here use Linear mapping , and
smoothstep
Linear mapping is not used , But after linear mapping, use curve to smooth the result , This result is not much different from linear mapping , We can simply use linear mapping to understand
- For example, the specified range is
x
The mapping of functions within the scope is :- The linear mapping function is : k 1 = ( x − a ) ( b − a ) { a < = x < = b } k_{1}=\dfrac{(x-a)}{(b-a)}\{a<=x<=b\} k1=(b−a)(x−a){ a<=x<=b}
- First line linear mapping , get
x
The mapping value of k 1 k_1 k1 - Then use the curve to smooth this value : k = 3 k 1 2 − 2 k 1 3 { a < x < b } k=3k_{1}^{2}-2k_{1}^{3}\{a<x<b\} k=3k12−2k13{ a<x<b}
The code implementation is as follows :
float smoothstep (float edge0, float edge1, float x)
{
if (x < edge0)
return 0;
if (x > edge1)
return 1;
// Linear mapping
x = (x - edge0) / (edge1 - edge0);
// smooth
return x * x * (3 - 2 * x);
}
The above figure shows the use of smoothstep
Function , The specified range is [a=1, b=a+1=2]
, You can see :
- When
x <= 1
when , Function value is0
- When
x >= 2
when , Function value is1
- When
1 < x < 2
when , The function value is scaled to[0, 1]
Between- The blue lines are linear mappings
- The red line is the result of smoothing with curve after linear mapping
- Generally speaking, there is little difference between the two
Use the difference to improve the scheme
We don't set how many circles there are this time , Completely based on RampTex The gradient level of , RampTex How many layer , How many layers are there on our edge .
in other words , We need to RampTex Mapped to the small area of the dissolution edge , As specified in our last article 0~0.12
Range .
To put it simply , It's about putting RampTex Draw this 0~0.12
Within the scope of .
Here we use 0~0.1
.
In mathematical terms , Is to be in [0, 1]
Noise texture value in this range , Mapped to the edge [0, 0.1]
within , Combined with the difference function introduced above , Our goal is :
// x=dissolveCol.r, {0<=x<=1}
smoothsetp(0, 0.1, x)
Of course , there [0, 0.1]
It's not the dissolving edge .
In the previous post , We introduced , dissolveCol.r < _DissolveThreshold
Is to dissolve pixels , So from dissolveCol.r == _DissolveThreshold
The first pixel is the pixel that dissolves the edge , As for the final range , We need to specify ourselves , For example, the value here is 0.1
.
So our call needs to be optimized as :
// x=dissolveCol.r, Sample self noise texture , {0<=x<=1}
// a=_DissolveThreshold, b=_DissolveThreshold+0.1
// among a Is the lower bound of dissolution , b Is the width of the edge of the dissolved pixel
y = smoothsetp(a, b, x)
The above call means :
We need to calculate the dissolution value x
, The new sampling coordinates are obtained by processing the difference function y
, adopt y
stay RampTex After sampling the color, it is attached to the primary color of the object :
x < a
: This pixel dissolves , abandon , Don't mindx > b
: At this point, the function returns1
, stay RampTex Black is sampled on , Because the black value is 0, It is equivalent to that the last pixel keeps the primary colora < x < b
: At this time, the function returns a new one after linear mapping and smoothing[0, 1]
Mapping values between- What we should pay attention to here is : because
x
Itself is in[0, 1]
Between , Processed resultsy
Still processing[0, 1]
Between , So it is easy to misunderstand , Need to know - None of the conditions here involve
=
Part of , Because in shader Dealing with boundaries , One pixel has little difference , And there may be performance differences
- What we should pay attention to here is : because
After this little treatment , We can be within the specified width pixels of the edge position , Draw completely RampTex Represents the color of the .
Realize further optimization of the scheme
You think it's over ? naive …
because RampTex It is essentially a one-dimensional texture , So our first optimization is , Replace its sampler , This can improve certain performance :
sampler _RampTex;
fixed4 rampColor = tex1D(_RampTex, smoothstep(_DissolveThreshold, _DissolveThreshold + 0.1, dissolveCol.r));
The second optimization has been mentioned above , We are in the difference , Used smoothstep
function , This function will do linear mapping first , Then smooth the curve , However, there is little difference between the result of smooth curve and that of linear mapping , But there is an additional curve calculation , So when our requirements are not particularly high , Just use the result of linear mapping .
First, let's introduce the function : saturate
.
saturate(x)
The delta function is going to be :
- When
x <= 0
when , Function value is0
- When
x >= 1
when , Function value is1
- When
0 < x < 1
when , returnx
Then realize linear mapping and use saturate
:
// k = (x - a) / (b - a);
k = (dissolveCol.r - _DissolveThreshold) / (_DissolveThreshold + 0.1 - _DissolveThreshold);
fixed4 rampColor = tex1D(_RampTex, saturate(k));
All right. , At this point, our dissolution effect is finally introduced .
Here is the complete code :
Shader "Dissolve"
{
Properties
{
[NoScaleOffset]_MainTex ("Texture", 2D) = "white" {}
_DissolveTex("DissolveTex", 2D) = "white" {}
_DissolveThreshold("DissolveThreshold", Range(0, 1)) = 0
_RampTex("RampTex", 2D) = "" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _DissolveTex;
float4 _DissolveTex_ST;
fixed _DissolveThreshold;
sampler _RampTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
// o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
o.uv.xy = v.uv;
o.uv.zw = TRANSFORM_TEX(v.uv, _DissolveTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 dissolveCol = tex2D(_DissolveTex, i.uv.zw);
// Sample color from noise texture , If the value [ Less than threshold ] Then discard this segment
// For example, the threshold is 0.1, All pixels sampled on the noise texture r Less than 0.1 All the fragments will be discarded
// That is, the dark color on the noise texture (->0) Start dissolving first , A pale color (->1) Finally dissolve
clip(dissolveCol.r - _DissolveThreshold);
// fixed4 rampColor = tex1D(_RampTex, smoothstep(_DissolveThreshold, _DissolveThreshold + 0.1, dissolveCol.r));
// k = (x-a)/(b-a);
// k = (dissolveCol.r - _DissolveThreshold) / (_DissolveThreshold + 0.1 - _DissolveThreshold)
fixed4 rampColor = tex1D(_RampTex, saturate((dissolveCol.r - _DissolveThreshold) * 10));
fixed4 col = tex2D(_MainTex, i.uv.xy);
col += rampColor;
return col;
}
ENDCG
}
}
}
summary
After the introduction of three articles , We know each other completely Unity How to make and optimize the dissolution effect in .
In the whole process , We not only understand the principle of dissolution effect itself , Also through this special effect , Touched in Shader Noise texture and gradient texture are common in , And use these two tools to optimize the dissolution effect .
At the same time, we also analyzed Shader Commonly used smoothstep
Functions and saturate
function , In short, the harvest is full .
Okay , That's what we're talking about today , I hope that's helpful .
边栏推荐
- R language uses image of magick package_ Mosaic functions and images_ The flatten function stacks multiple pictures together to form a stack layers on top of each other
- Use references
- .NET MAUI 性能提升
- Ask about the version of flinkcdc2.2.0, which supports concurrency. Does this concurrency mean Multiple Parallelism? Now I find that mysqlcdc is full
- 问下flinkcdc2.2.0的版本,支持并发,这个并发是指多并行度吗,现在发现,mysqlcdc全
- sql里,我想设置外键,为什么出现这个问题
- Matlab implementation of Huffman coding and decoding with GUI interface
- The running kubernetes cluster wants to adjust the network segment address of pod
- 112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
猜你喜欢
Fleet tutorial 19 introduction to verticaldivider separator component Foundation (tutorial includes source code)
<No. 8> 1816. Truncate sentences (simple)
MATLAB實現Huffman編碼譯碼含GUI界面
2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
Improve application security through nonce field of play integrity API
Programming examples of stm32f1 and stm32subeide -315m super regenerative wireless remote control module drive
UP Meta—Web3.0世界创新型元宇宙金融协议
请查收.NET MAUI 的最新学习资源
随机推荐
Reasons for the failure of web side automation test
R language uses the quantile function to calculate the quantile of the score value (20%, 40%, 60%, 80%), uses the logical operator to encode the corresponding quantile interval (quantile) into the cla
In my limited software testing experience, a full-time summary of automation testing experience
Poor math students who once dropped out of school won the fields award this year
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
In depth learning autumn recruitment interview questions collection (1)
Improve application security through nonce field of play integrity API
Rationaldmis2022 advanced programming macro program
The Oracle message permission under the local Navicat connection liunx is insufficient
小红书微服务框架及治理等云原生业务架构演进案例
千人規模互聯網公司研發效能成功之路
Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
[filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
源代码防泄密中的技术区别再哪里
大佬们有没有人遇到过 flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取
Camera calibration (2): summary of monocular camera calibration