-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuv.fxh
268 lines (233 loc) · 6.69 KB
/
uv.fxh
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#define UV_FXH
////////////////////////////////////////////////////////////////
//
// UV and Texture Mapping functions
//
////////////////////////////////////////////////////////////////
#ifndef TWOPI
#define TWOPI 6.28318531
#endif
#ifndef PI
#define PI 3.14159265
#endif
// convert between uv and screen space (vvvv)
float2 screenToUV(float2 p)
{
p.y *= -1;
return p *.5 + .5;
};
float2 UVToScreen(float2 uv)
{
uv.y = 1- uv.y;
return uv = uv * 2 - 1;
};
float2 cubicUV(float3 pos, float3 norm)
{
norm = float3(abs(norm.x), abs(norm.y), abs(norm.z));
if (norm.x > norm.y && norm.x > norm.z) //project on x axis
return float2(pos.z, -pos.y)+.5;
else if (norm.y > norm.x && norm.y > norm.z) //project on y axis
return float2(pos.x, -pos.z)+.5;
else return float2(pos.x, -pos.y)+.5; // project on z axis
};
float2 sphericalUV(float3 norm)
{
float2 result;
float r;
r = norm.x * norm.x + norm.y * norm.y + norm.z * norm.z;
if (r > 0)
{
r = sqrt(r);
float p, y;
p = asin(norm.y/r) / TWOPI;
y = 0;
if (norm.z != 0) y = atan2(-norm.x, -norm.z);
else if (norm.x > 0) y = -PI / 2;
else y = PI / 2;
y /= TWOPI;
result = float2(-y,-(p+.25)*2);
}
else result = 0;
return result;
};
//TODO should use pos+norms
float2 cylindricalUV(float3 pos)
{
float2 uv;
uv.y = -pos.y-.5;
if (length(pos) > 0)
{
if (pos.z != 0) uv.x = atan2(pos.x, -pos.z);
else if (pos.x > 0) uv.x = -PI / 2;
else uv.x = PI / 2;
uv.x /= TWOPI;
}
else uv.x = 0;
return uv;
};
//UV Interface and Classes definitions
//Usage:////////////////////////////////////////////////////////////////////////////////////////////////
// iUVMode uvMode <string linkclass="UVmap,PlanarXY,PlanarXZ,PlanarZY,Cubic,Spherical,Cylindrical";>;
//
//uvMode.Map(pos,norm,uv);
////////////////////////////////////////////////////////////////////////////////////////////////////////
interface iUVMode
{
float2 Map(float3 pos, float3 norm, float2 uv);
};
class cUVmap : iUVMode
{
float2 Map(float3 pos, float3 norm, float2 uv) { return uv; }
};
class cPlanarXY : iUVMode
{
float2 Map(float3 pos, float3 norm, float2 uv) { return float2(pos.x, -pos.y)+.5; }
};
class cPlanarXZ : iUVMode
{
float2 Map(float3 pos, float3 norm, float2 uv) { return float2(pos.x, -pos.z)+.5; }
};
class cPlanarZY : iUVMode
{
float2 Map(float3 pos, float3 norm, float2 uv) { return float2(pos.z, -pos.y)+.5; }
};
class cCubic : iUVMode
{
float2 Map(float3 pos, float3 norm, float2 uv)
{
return cubicUV(pos, norm);
}
};
class cSpherical : iUVMode
{
float2 Map(float3 pos, float3 norm, float2 uv)
{
return sphericalUV(norm);
}
};
class cCylindrical : iUVMode
{
float2 Map(float3 pos, float3 norm, float2 uv)
{
return cylindricalUV(pos);
}
};
cUVmap UVmap;
cPlanarXY PlanarXY;
cPlanarXZ PlanarXZ;
cPlanarZY PlanarZY;
cCubic Cubic;
cSpherical Spherical;
cCylindrical Cylindrical;
// Triplaner Texture mapping
float4 triPlane(Texture2D tex, SamplerState s, float3 p, float3 n, float scale, float k)
{
p *= scale;
float3 m = pow( abs( n ), k );
float4 x = tex.Sample( s, p.yz );
float4 y = tex.Sample( s, p.zx );
float4 z = tex.Sample( s, p.xy );
return (x*m.x + y*m.y + z*m.z) / (m.x + m.y + m.z);
}
// Triplaner Texture mapping w/ gradients
float4 triPlane(Texture2D tex, SamplerState s, float3 p, float3 n, float3 gx, float3 gy, float scale = 1.0, float k = 4.0)
{
p *= scale;
gx *= scale;
gy *= scale;
float3 m = pow( abs( n ), k );
float4 x = tex.SampleGrad(s, p.yz, gx.yz, gy.yz);
float4 y = tex.SampleGrad(s, p.zx, gx.zx, gy.zx);
float4 z = tex.SampleGrad(s, p.xy, gx.xy, gy.xy);
return (x*m.x + y*m.y + z*m.z) / (m.x + m.y + m.z);
}
// Pretty sure this is dodgy
float3 triPlaneNormal(Texture2D tex, SamplerState s, float3 p, float3 n, float scale, float k)
{
p*= scale;
float3 Tangent1 = normalize(float3(n.x, 1, n.y));
float3 Tangent2 = normalize(float3(n.y, 1, n.z));
float3 Tangent3 = normalize(float3(n.z, 1, n.x));
float3x3 TBN1, TBN2, TBN3;
TBN1[0] = Tangent1;
TBN1[1] = cross(Tangent1, n);
TBN1[2] = n;
TBN2[0] = Tangent2;
TBN2[1] = cross(Tangent2, n);
TBN2[2] = n;
TBN3[0] = Tangent3;
TBN3[1] = cross(Tangent3, n);
TBN3[2] = n;
float3 m = pow( abs( n ), k );
float3 n1 = tex.Sample( s, p.yz ).rgb * 2.0 - 1.0;;
float3 n2 = tex.Sample( s, p.zx ).rgb * 2.0 - 1.0;;
float3 n3 = tex.Sample( s, p.xy ).rgb * 2.0 - 1.0;;
// Transform normals into world space
n1 = mul(n1, TBN1);
n2 = mul(n2, TBN2);
n3 = mul(n3, TBN1);
return normalize((n1*m.x + n2*m.y + n3*m.z) / (m.x + m.y + m.z));
}
// Shader code by Inigo Quilez
//http://www.iquilezles.org/www/articles/texturerepetition/texturerepetition.htm
float4 sampleNoTile(Texture2D tex, in float2 uv, SamplerState samp, float lod = 0.0 )
{
float2 p = floor( uv );
float2 f = frac( uv );
// voronoi contribution
float4 va = 0.0;
float wt = 0.0;
for( int j=-1; j<=1; j++ )
for( int i=-1; i<=1; i++ )
{
float2 g = float2( float(i), float(j) );
//float4 o = hash4( p + g );
// Hash Function
float2 pg = p + g;
float4 o = frac(sin(float4( 1.0+dot(pg,float2(37.0,17.0)),
2.0+dot(pg,float2(11.0,47.0)),
3.0+dot(pg,float2(41.0,29.0)),
4.0+dot(pg,float2(23.0,31.0))))*103.0);
float2 r = g - f + o.xy;
float d = dot(r,r);
float w = exp(-5.0*d );
float4 c = tex.SampleLevel(samp, uv + o.zw, lod);
va += w*c;
wt += w;
}
// normalization
return va/wt;
}
float4 sampleGradNoTile(Texture2D tex, in float2 uv, SamplerState samp )
{
float2 p = floor( uv );
float2 f = frac( uv );
// derivatives (for correct mipmapping)
float2 myddx = ddx( uv );
float2 myddy = ddy( uv );
// voronoi contribution
float4 va = 0.0;
float wt = 0.0;
for( int j=-1; j<=1; j++ )
for( int i=-1; i<=1; i++ )
{
float2 g = float2( float(i), float(j) );
//float4 o = hash4( p + g );
// Hash Function
float2 pg = p + g;
float4 o = frac(sin(float4( 1.0+dot(pg,float2(37.0,17.0)),
2.0+dot(pg,float2(11.0,47.0)),
3.0+dot(pg,float2(41.0,29.0)),
4.0+dot(pg,float2(23.0,31.0))))*103.0);
float2 r = g - f + o.xy;
float d = dot(r,r);
float w = exp(-5.0*d );
float4 c = tex.SampleGrad( samp, uv + o.zw, myddx, myddy );
va += w*c;
wt += w;
}
// normalization
return va/wt;
}
////////////////////////////////////////////////////////////////
// EOF