当前位置:网站首页>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 Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
- 一起探索云服务之云数据库
- MATLAB實現Huffman編碼譯碼含GUI界面
- The Oracle message permission under the local Navicat connection liunx is insufficient
- Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)
- 《通信软件开发与应用》课程结业报告
- 30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
- .NET MAUI 性能提升
- Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
- MySQL安装常见报错处理大全
猜你喜欢
Rationaldmis2022 advanced programming macro program
正在运行的Kubernetes集群想要调整Pod的网段地址
[system design] index monitoring and alarm system
Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本
【全栈计划 —— 编程语言之C#】基础入门知识一文懂
Excel公式知多少?
18 basic introduction to divider separator component of fleet tutorial (tutorial includes source code)
超标量处理器设计 姚永斌 第9章 指令执行 摘录
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
Talk about SOC startup (IX) adding a new board to uboot
随机推荐
[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation
Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
【全栈计划 —— 编程语言之C#】基础入门知识一文懂
Up meta - Web3.0 world innovative meta universe financial agreement
MATLAB实现Huffman编码译码含GUI界面
Common locking table processing methods in Oracle
Software design - "high cohesion and low coupling"
Cmu15445 (fall 2019) project 2 - hash table details
Rationaldmis2022 advanced programming macro program
Internet Protocol
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
Ask about the version of flinkcdc2.2.0, which supports concurrency. Does this concurrency mean Multiple Parallelism? Now I find that mysqlcdc is full
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
Poor math students who once dropped out of school won the fields award this year
@Bean与@Component用在同一个类上,会怎么样?
VIM command mode and input mode switching
In my limited software testing experience, a full-time summary of automation testing experience
UP Meta—Web3.0世界创新型元宇宙金融协议
Talk about SOC startup (VII) uboot startup process III