主要使用噪声和透明度测试,从噪声图中读取某个通道的值,然后使用该值进行透明度测试。
主要代码如下:
fixed cutout = tex2D(_NoiseTex, i.uvNoiseTex).r;
clip(cutout - _Threshold);
边缘颜色
如果纯粹这样镂空,则效果太朴素了,因此通常要在镂空边缘上弄点颜色来模拟火化、融化等效果。
1. 纯颜色
第一种实现很简单,首先定义_EdgeLength和_EdgeColor两个属性来决定边缘多长范围要显示边缘颜色;然后在代码中找到合适的范围来显示边缘颜色。
主要代码如下:
//Properties
_EdgeLength("Edge Length", Range(0.0, 0.2)) = 0.1
_EdgeColor("Border Color", Color) = (1,1,1,1)
...
//Fragment
if(cutout - _Threshold < _EdgeLength)
return _EdgeColor;
2. 两种颜色混合
第一种纯颜色的效果并不太好,更好的效果是混合两种颜色,来实现一种更加自然的过渡效果。
主要代码如下:
if(cutout - _Threshold < _EdgeLength)
{
float degree = (cutout - _Threshold) / _EdgeLength;
return lerp(_EdgeFirstColor, _EdgeSecondColor, degree);
}
3. 边缘颜色混合物体颜色
为了让过渡更加自然,我们可以进一步混合边缘颜色和物体原本的颜色。
主要代码如下:
float degree = saturate((cutout - _Threshold) / _EdgeLength); //需要保证在[0,1]以免后面插值时颜色过亮
fixed4 edgeColor = lerp(_EdgeFirstColor, _EdgeSecondColor, degree);
fixed4 col = tex2D(_MainTex, i.uvMainTex);
fixed4 finalColor = lerp(edgeColor, col, degree);
return fixed4(finalColor.rgb, 1);
4. 使用渐变纹理
为了让边缘颜色更加丰富,我们可以进而使用渐变纹理:
然后我们就可以利用degree来对这条渐变纹理采样作为我们的边缘颜色:
float degree = saturate((cutout - _Threshold) / _EdgeLength);
fixed4 edgeColor = tex2D(_RampTex, float2(degree, degree));
fixed4 col = tex2D(_MainTex, i.uvMainTex);
fixed4 finalColor = lerp(edgeColor, col, degree);
return fixed4(finalColor.rgb, 1);