February 19, 2023

Unity Shader 学习 Texture Repetition

Unity Shader 学习 Texture Repetition

目的

为了解决三向和双向的重复问题

参考资料

Coding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
Shader "Unlit/Biplanar_Texture_Repetition"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_NoramlTex ("Noraml Texture", 2D) = "white" {}
_shapek ("shapek", Range(0,20)) = 1
_BlendRatio ("BlendRatio",Float ) = 1
_TriplanarBlendSharpness("Blend Sharpness", float)=1
}
SubShader
{
Tags { "RenderType"="Opaque" }


Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 normal : NORMAL;
float4 tangent : TANGENT;
};

struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 posWS : TEXCOORD1;
float3 nDirWS : TEXCOORD2;
float3 tDirWS : TEXCOORD3; // 世界空间切线方向
float3 bDirWS : TEXCOORD4;
};
Texture2D _MainTex;
SamplerState sampler_MainTex;
float4 _MainTex_ST;
Texture2D _NoramlTex;
//sampler2D _MainTex;float4 _MainTex_ST;
//sampler2D _NoramlTex;
float _shapek;
float _BlendRatio;
float _TriplanarBlendSharpness;

v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos( v.vertex );
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.posWS = mul(unity_ObjectToWorld, v.vertex);
o.nDirWS = UnityObjectToWorldNormal(v.normal); // 法线方向 OS>WS
o.tDirWS = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz); // 切线方向 OS>WS
o.bDirWS = normalize(cross(o.nDirWS, o.tDirWS) * v.tangent.w);
return o;
}
//哈希算法
float4 hash4(float2 p)
{
float t1 = 1.0 + dot(p, float2(37.0, 17.0));
float t2 = 2.0 + dot(p, float2(11.0, 47.0));
float t3 = 3.0 + dot(p, float2(41.0, 29.0));
float t4 = 4.0 + dot(p, float2(23.0, 31.0));
return frac(sin(float4(t1, t2, t3, t4)) * 103.0);
}
// 清除纹理本体
float4 texNoTileTech(Texture2D baseTex, SamplerState baseTexSampler, float2 uv, float2 blendRatio)
{
// 获取整数部分,即哪个tile
float2 iuv = floor(uv);
// 获取小数部分,确认采样点在单个tile中的位置
float2 fuv = frac(uv);

// 产生四次不同的变换
float4 ofa = hash4(iuv + float2(0.0, 0.0));
float4 ofb = hash4(iuv + float2(1.0, 0.0));
float4 ofc = hash4(iuv + float2(0.0, 1.0));
float4 ofd = hash4(iuv + float2(1.0, 1.0));

// 计算ddx,ddy避免UV跳跃
float2 dx = ddx(uv);
float2 dy = ddy(uv);

// 镜像处理
ofa.zw = sign(ofa.zw - 0.5);
ofb.zw = sign(ofb.zw - 0.5);
ofc.zw = sign(ofc.zw - 0.5);
ofd.zw = sign(ofd.zw - 0.5);

float2 uva = uv * ofa.zw + ofa.xy, dxa = dx * ofa.zw, dya = dy * ofa.zw;
float2 uvb = uv * ofb.zw + ofb.xy, dxb = dx * ofb.zw, dyb = dy * ofb.zw;
float2 uvc = uv * ofc.zw + ofc.xy, dxc = dx * ofc.zw, dyc = dy * ofc.zw;
float2 uvd = uv * ofd.zw + ofd.xy, dxd = dx * ofd.zw, dyd = dy * ofd.zw;

// 采样和混合
float2 b = smoothstep(blendRatio, 1.0 - blendRatio, fuv);

float4 sampler1 = baseTex.SampleGrad(baseTexSampler, uva, dxa, dya);
float4 sampler2 = baseTex.SampleGrad(baseTexSampler, uvb, dxb, dyb);
float4 sampler3 = baseTex.SampleGrad(baseTexSampler, uvc, dxc, dyc);
float4 sampler4 = baseTex.SampleGrad(baseTexSampler, uvd, dxd, dyd);

return lerp(lerp(sampler1, sampler2, b.x), lerp(sampler3, sampler4, b.x), b.y);
//return float4(uva,0,0);
}
fixed4 frag (v2f i) : SV_Target
{
//数据构建
float3 p = i.posWS;
float3 dpdx = ddx(p);
float3 dpdy = ddy(p);
float3 n = abs(i.nDirWS);
uint3 ma = (n.x>n.y&&n.x>n.z) ? uint3(0,1,2):
(n.y>n.z) ? uint3(1,2,0):
uint3(2,0,1);

uint3 mi = (n.x<n.y&&n.x<n.z) ? uint3(0,1,2):
(n.y<n.z) ? uint3(1,2,0):
uint3(2,0,1);
uint3 me = uint3(3,3,3)-mi-ma;

float2 w = float2(n[ma.x],n[me.x]);
w = saturate((w-0.5)/(1-0.5));
float k = _shapek/8.0;
w = pow(w,_TriplanarBlendSharpness);
w=pow(w,float2(k,k));
// sample the texture
float4 x = _MainTex.SampleGrad(sampler_MainTex,float2(p[ma.y],p[ma.z]),float2(dpdx[ma.y],dpdx[ma.z]),float2(dpdy[ma.y],dpdy[ma.z]));
float4 y = _MainTex.SampleGrad(sampler_MainTex,float2(p[me.y],p[me.z]),float2(dpdx[me.y],dpdx[me.z]),float2(dpdy[me.y],dpdy[me.z]));
//fixed4 col = tex2D(_MainTex, i.uv);
//_MainTex.Sample(sampler_MainTex, i.uv);
// apply fog

fixed4 cx = texNoTileTech(_MainTex,sampler_MainTex,float2(p[ma.y],p[ma.z]),_BlendRatio);
fixed4 cy = texNoTileTech(_MainTex,sampler_MainTex,float2(p[me.y],p[me.z]),_BlendRatio);
float4 col = (cx*w.x+cy*w.y)/(w.x+w.y);
//float4 col = (x*w.x+y*w.y)/(w.x+w.y);

//ramp world y
//NORMAL

fixed4 Nx = texNoTileTech(_NoramlTex,sampler_MainTex,float2(p[ma.y],p[ma.z]),_BlendRatio);
fixed4 Ny = texNoTileTech(_NoramlTex,sampler_MainTex,float2(p[me.y],p[me.z]),_BlendRatio);
//float4 Nx = tex2Dgrad(_NoramlTex,float2(p[ma.y],p[ma.z]),float2(dpdx[ma.y],dpdx[ma.z]),float2(dpdy[ma.y],dpdy[ma.z]));
//float4 Ny = tex2Dgrad(_NoramlTex,float2(p[me.y],p[me.z]),float2(dpdx[me.y],dpdx[me.z]),float2(dpdy[me.y],dpdy[me.z]));
float3 n1 = UnpackNormal(Nx);
float3 n2 = UnpackNormal(Ny);

float3 nW;
float3 n1W = float3(n1.y+i.nDirWS[ma.z],n1.x+i.nDirWS[ma.y],abs(n1.z)*i.nDirWS[ma.x]);
float3 n2W = float3(n2.y+i.nDirWS[me.z],n2.x+i.nDirWS[me.y],abs(n2.z)*i.nDirWS[me.x]);

n1W = float3(n1W[ma.z],n1W[ma.y],n1W[ma.x]);
n2W = float3(n2W[me.z],n2W[me.y],n2W[me.x]);
//nW = normalize(n1*w.x+n2*w.y);
nW = normalize(n1W*w.x+n2W*w.y);
float3 normalT = mul(nW,float3x3(i.tDirWS,i.bDirWS,i.nDirWS));
half3 lDirWS = _WorldSpaceLightPos0.xyz;

half ndotl = dot(nW, lDirWS);


return fixed4(col.rgb*ndotl,1);

}
ENDCG
}
}
}

关于本文

本文作者 Master Gong Sheng, 许可由 CC BY-NC 4.0.