-
Notifications
You must be signed in to change notification settings - Fork 189
/
VAT_Utilies.hlsl
201 lines (168 loc) · 5.32 KB
/
VAT_Utilies.hlsl
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
float3 VAT_unpackAlpha(float alpha)
{
//decode float to float2
alpha *= 1024;
// alpha = 0.8286 * 1024;
float2 f2;
f2.x = floor(alpha / 32.0) / 31.5;
f2.y = (alpha - (floor(alpha / 32.0)*32.0)) / 31.5;
//decode float2 to float3
float3 f3;
f2 *= 4;
f2 -= 2;
float f2dot = dot(f2,f2);
f3.xy = sqrt(1 - (f2dot/4.0)) * f2;
f3.z = 1 - (f2dot/2.0);
f3 = clamp(f3, -1.0, 1.0);
return f3;
}
float2 VAT_uvPosition(float2 uvIndex, int numOfFrames, float speed, float time, float2 paddedRatio)
{
float2 uvPosition;
float FPS = 24.0;
float FPS_div_Frames = FPS / numOfFrames;
float timeInFrames = frac(speed * time);
timeInFrames = ceil(timeInFrames * numOfFrames);
timeInFrames /= numOfFrames;
timeInFrames += (1/numOfFrames);
uvPosition.x = uvIndex.x * paddedRatio.x;
uvPosition.y = (1 - (timeInFrames * paddedRatio.y)) + (1 - ((1 - uvIndex.y) * paddedRatio.y));
return uvPosition;
}
// Rigid VAT
void VAT_Rigid_float(
float3 restPosition,
float3 restNormal,
float3 vertexColor,
float2 uvIndex,
SamplerState texSampler,
Texture2D positionMap,
Texture2D rotationMap,
float2 positionBounds,
float2 pivotBounds,
float time,
float speed,
int numOfFrames,
float2 paddedRatio,
out float3 outPosition,
out float3 outNormal
)
{
float2 uvPosition = VAT_uvPosition(uvIndex, numOfFrames, speed, time, paddedRatio);
float4 texturePos = positionMap.SampleLevel(texSampler, uvPosition, 0);
float4 textureRot = rotationMap.SampleLevel(texSampler, uvPosition, 0);
texturePos.xyz = lerp(positionBounds.x, positionBounds.y, texturePos.xyz);
float3 pivot = lerp(pivotBounds.x, pivotBounds.y, vertexColor.xyz);
float3 atOrigin = restPosition - pivot;
//calculate rotation
textureRot *= 2.0;
textureRot -= 1.0;
float4 quat = 0;
quat = textureRot;
float3 rotated = 2.0 * cross(quat.xyz, cross(quat.xyz, atOrigin) + quat.w * atOrigin);
float3 rotatedNormal = restNormal + 2.0 * cross(quat.xyz, cross(quat.xyz, restNormal) + quat.w * restNormal);
float effectiveFrame = ceil(frac(speed * time) * numOfFrames);
if (effectiveFrame <= 1.0 || effectiveFrame >= (float)numOfFrames) {
outPosition = restPosition;
} else {
outPosition = atOrigin + rotated + texturePos;
}
outNormal = rotatedNormal;
}
// Soft VAT
void VAT_Soft_float(
float3 restPosition,
float2 uvIndex,
SamplerState texSampler,
Texture2D positionMap,
Texture2D normalMap,
Texture2D colorMap,
float2 positionBounds,
float time,
float speed,
int numOfFrames,
float2 paddedRatio,
bool packNorm,
out float3 outPosition,
out float3 outNormal,
out float3 outColor
)
{
float2 uvPosition = VAT_uvPosition(uvIndex, numOfFrames, speed, time, paddedRatio);
float4 texturePos = positionMap.SampleLevel(texSampler, uvPosition, 0);
float4 textureN = normalMap.SampleLevel(texSampler, uvPosition, 0);
float4 textureCd = colorMap.SampleLevel(texSampler, uvPosition, 0);
texturePos.xyz = lerp(positionBounds.x, positionBounds.y, texturePos.xyz);
//calculate normal
if (packNorm){
outNormal = VAT_unpackAlpha(texturePos.w);
} else {
outNormal = textureN * 2 - 1;
}
outPosition = restPosition + texturePos;
outColor = textureCd.xyz;
}
// Fluid VAT
void VAT_Fluid_float(
float2 uvIndex,
SamplerState texSampler,
Texture2D positionMap,
Texture2D normalMap,
Texture2D colorMap,
float2 positionBounds,
float time,
float speed,
int numOfFrames,
float2 paddedRatio,
bool packNorm,
out float3 outPosition,
out float3 outNormal,
out float3 outColor
)
{
float2 uvPosition = VAT_uvPosition(uvIndex, numOfFrames, speed, time, paddedRatio);
float4 texturePos = positionMap.SampleLevel(texSampler, uvPosition, 0);
float4 textureN = normalMap.SampleLevel(texSampler, uvPosition, 0);
float4 textureCd = colorMap.SampleLevel(texSampler, uvPosition, 0);
texturePos.xyz = lerp(positionBounds.x, positionBounds.y, texturePos.xyz);
//calculate normal
if (packNorm){
outNormal = VAT_unpackAlpha(texturePos.w);
} else {
outNormal = textureN * 2 - 1;
}
outPosition = texturePos;
outColor = textureCd.xyz;
}
// Sprite VAT
void VAT_Sprite_float(
float2 uvIndex,
float2 uv,
SamplerState texSampler,
Texture2D positionMap,
Texture2D colorMap,
float2 positionBounds,
float2 widthHeight,
float time,
float speed,
int numOfFrames,
float2 paddedRatio,
bool packNorm,
matrix MV,
out float3 outPosition,
out float3 outNormal,
out float3 outColor
)
{
float2 uvPosition = VAT_uvPosition(uvIndex, numOfFrames, speed, time, paddedRatio);
float4 texturePos = positionMap.SampleLevel(texSampler, uvPosition, 0);
float4 textureCd = colorMap.SampleLevel(texSampler, uvPosition, 0);
texturePos.xyz = lerp(positionBounds.x, positionBounds.y, texturePos.xyz);
outNormal = float3(1.0, 0.0, 0.0);
//create camera facing billboard based on uv coordinates
float3 cameraF = float3(0.5 - uv.x, uv.y - 0.5, 0);
cameraF *= float3(widthHeight.x, widthHeight.y, 1);
cameraF = mul(cameraF, MV);
outPosition = cameraF + texturePos.xyz;
outColor = textureCd.xyz;
}