202404010 学习目标:
- 刷课至4.4
学习内容:
4.3 片段的舍弃 Clip
-
Clip(x):如果X<0,则舍弃掉此片段
-
溶解效果
-
修改溶解效果的Tilling Offset
Shader "taecg/Character Unlit"
{
properties
{
[ Header (Base) ]
[NoScaleOffset] _MainTex ( "MainTex" , 2D ) = "White" { }
_Color ( "Color",color ) = (0,0,0,0)
[ Space (10) ]
[ Header (Dissolve) ]
_DissovlveTex(DissolveTex(R)", 2D ) = "White" { }
_Clip ( "Clip",Range(0,1)) = 0
}
SubShader
{
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG/cginc"
spmpler 2D _MainTex;
fixed4 _color ;
sampler2D _DissolveTex;float4_DissolveTex_ST;
fixed _Clip;
struct appdata
{
float4 vertex : POSITION ;
float4 uv : TEXCOORD1;
} ;
struct v2f
{
float4 pos : SV_POSITION ;
float4 uv : TEXCOORD;
} ;
v2f vert (appdata v)
{
v2f o ;
o.pos = UnityObjectToClipPos ( v.vertex ) ;
o.uv.xy = v.uv.xy ;
// o.uv.zw = v.uv * DissolveTex_ST.xy+_DissolveTex_ST.zw ;
o.uv.zw = TRANSFORM_TEX( v.uv , _DissolveTex );
return 0 ;
}
fixed4 frag (v2f i) : sv_TARGET
{
fixed4 c;
fixed4 tex = tex2D( _MainTex, i.uv.xy);
c = tex ;
c + = _Color;
fixed4 dissolveTex = tex2D( _DissolveTex,i.uv.zw);
Clip( dissolveTex .r - _Clip );
return c;
}
ENGCG
}
}
}
4.4 图形计算器
- www.desmos.com
- www.geogebra.org/graphing(用的人比较多)
4.4 优化溶解效果
-
希望体现火烧效果
-
溶解边缘有不同颜色变化
-
smoothstep ( min, max, x ) 在两个值之间进行平滑过渡 高消耗
- 如果x比min小,返回0
- 如果x比max大,返回1
- 如果x处于范围 [min,max] 中间,则范围0和1之间的平滑值
-
saturate (x)线性过渡 低消耗
- 如果x比o小,返回0
- 如果x比1大,返回1
- 如果x处于范围 [0,1] 中间,返回x
float smoothstep(min,max,x) { float t=saturate(x-min)/(max-min); return t*t*(3-2*t); }
-
溶解原理:
Shader "taecg/Character Unlit"
{
properties
{
[ Header (Base) ]
[NoScaleOffset] _MainTex ( "MainTex" , 2D ) = "White" { }
_Color ( "Color",color ) = (0,0,0,0)
[ Space (10) ]
[ Header (Dissolve) ]
_DissovlveTex(DissolveTex(R)", 2D ) = "White" { }
[ NoScaleOffset ]_RampTex ( " RampTex( RGB ) " ,2D ) = "White" { }
_Clip ( "Clip",Range(0,1)) = 0
}
SubShader
{
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG/cginc"
spmpler 2D _MainTex;
fixed4 _color ;
sampler2D _DissolveTex;float4_DissolveTex_ST ;
sampler _RampTex ;
fixed _Clip;
struct appdata
{
float4 vertex : POSITION ;
float4 uv : TEXCOORD1;
} ;
struct v2f
{
float4 pos : SV_POSITION ;
float4 uv : TEXCOORD;
} ;
v2f vert (appdata v)
{
v2f o ;
o.pos = UnityObjectToClipPos ( v.vertex ) ;
o.uv.xy = v.uv.xy ;
// o.uv.zw = v.uv * DissolveTex_ST.xy+_DissolveTex_ST.zw ;
o.uv.zw = TRANSFORM_TEX( v.uv , _DissolveTex );
return 0 ;
}
fixed4 frag (v2f i) : sv_TARGET
{
fixed4 c;
fixed4 tex = tex2D( _MainTex, i.uv.xy);
c = tex ;
c + = _Color;
fixed4 dissolveTex = tex2D( _DissolveTex,i.uv.zw);
Clip( dissolveTex .r - _Clip );
fixed dissolveValue = saturate (( dissolveTex.r - _Clip ) / ( _Clip + 0.1 - _Clip )) ;
fixed4 rampTex = tex1D(_RampTex , dissolveValue ) ;
c+ = rampTex ;
return c ;
}
ENGCG
}
}
}
学习产出:
- 4.3
- 4.4