一直覺得warp挺適用,但是用起來有時候效果挺奇怪的
還有directional warp貌似兩個產生的效果截然不同
於是嘗試用u3d 材質實現下
directional warp
這個節點主要功能是用灰度信息推開像素,常常用來分割連續貼圖,用於讓磚塊之類紋理看起來更不連續
根據灰度扭曲圖像,所以有方向性,灰度決定扭曲的距離
warp
用高度圖來扭曲圖像做邊緣破損效果,因爲對坡度變化越大的地方效果越劇烈,沒有坡度的地方沒有效果
有檢測邊緣的效果,棋盤格輸入,只能扭曲棋盤格構成的線框,而且無法改變本身
用有漸變的高度圖像作爲輸入,那麼根據圖像梯度進行扭曲,就是漸變劇烈的地方扭曲,顏色本身不能決定扭曲程度
下面是substance designer中兩個對比圖
、
可以看到上面區塊明顯有位移了
下面的只是有細線,扭曲數值都調大了
先可以模擬direction warp 也可以看到其實這個運算比warp快,這個就是材質中常用的uv扭曲方式
float2 vertex_uv = float2(1.,1.);
float4 displace = tex2D(_MainTex2, i.uv);
float displace_k = displace.r * _Number;
float2 uv_displaced = float2(i.uv.x*displace_k - displace_k/2.0, i.uv.y*displace_k - displace_k / 2.0);
uv_displaced = float2(i.uv.x - displace_k/2.0, i.uv.y - displace_k / 2.0);//direction
可以看到效果基本一樣
warp因爲有邊緣檢測 所以略有不同
但是不知道substance designer在裏面還有什麼處理不能完全還原,只能是大概
下面是用sobel材質改的
只是做了提取扭曲部分的灰度,並沒有用第二張圖來扭曲
不過可以參考上面的來寫
Shader "Custom/test" {
Properties{
_MainTex("MainTex", 2D) = "white" {}
_Size("Size", range(1,2048)) = 256
}
SubShader{
pass {
Tags{ "LightMode" = "ForwardBase" }
Cull off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _Size;
sampler2D _MainTex;
float4 _MainTex_ST;
struct v2f {
float4 pos:SV_POSITION;
float2 uv_MainTex:TEXCOORD0;
};
v2f vert(appdata_full v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv_MainTex = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
float4 frag(v2f i) :COLOR
{
float mc = tex2D(_MainTex, i.uv_MainTex).r;
float4 c = 0;
float mc00 = tex2D(_MainTex, i.uv_MainTex - fixed2(1, 1) / _Size).r- mc;
float mc10 = tex2D(_MainTex, i.uv_MainTex - fixed2(0, 1) / _Size).r- mc;
float mc20 = tex2D(_MainTex, i.uv_MainTex - fixed2(-1, 1) / _Size).r- mc;
float mc01 = tex2D(_MainTex, i.uv_MainTex - fixed2(1, 0) / _Size).r- mc;
float mc21 = tex2D(_MainTex, i.uv_MainTex - fixed2(-1, 0) / _Size).r- mc;
float mc02 = tex2D(_MainTex, i.uv_MainTex - fixed2(1, -1) / _Size).r- mc;
float mc12 = tex2D(_MainTex, i.uv_MainTex - fixed2(0, -1) / _Size).r- mc;
float mc22 = tex2D(_MainTex, i.uv_MainTex - fixed2(-1, -1) / _Size).r- mc;
if (mc00 != 0.0)c = (c<abs(mc00)) ? abs(mc00) : c;
if (mc10 != 0.0)c = (c<abs(mc10)) ? abs(mc10) : c;
if (mc20 != 0.0)c = (c<abs(mc20)) ? abs(mc20) : c;
if (mc01 != 0.0)c = (c<abs(mc01)) ? abs(mc01) : c;
if (mc21 != 0.0)c = (c<abs(mc21)) ? abs(mc21) : c;
if (mc02 != 0.0)c = (c<abs(mc02)) ? abs(mc02) : c;
if (mc12 != 0.0)c = (c<abs(mc12)) ? abs(mc12) : c;
if (mc22 != 0.0)c = (c<abs(mc22)) ? abs(mc22) : c;
return c;
}
ENDCG
}
}
}
通過對比發現圓形範圍不是很正常
網格和其他倒是類似
所以還是需要探索下,估計還有其他算法