-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetaBallPrimitives.hlsli
218 lines (193 loc) · 6.15 KB
/
MetaBallPrimitives.hlsli
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
#ifndef METABALLPRIMITIVES_H
#define METABALLPRIMITIVES_H
#include "RaytracingShaderHelper.hlsli"
// MetaBall resources
StructuredBuffer<Metaball> g_metaballs : register(t4, space0);
// Calculate field potential from all active metaballs.
float CalculateMetaballsPotential(in float3 position, in Metaball blobs[MAX_METABALLS_PER_CAL], in UINT nActiveMetaballs)
{
float sumFieldPotential = 0;
for (UINT j = 0; j < nActiveMetaballs; j++)
{
float dummy;
sumFieldPotential += CalculateMetaballPotential(position, blobs[j], dummy);
}
return sumFieldPotential;
}
bool RayMetaballIntersectionTest(in Ray localRay, in MetaBall metaBall, inout float thit)
{
float tmin = INFINITY;
float tmax = -INFINITY;
float _thit, _tmax;
if (RaySolidSphereIntersectionTest(localRay, _thit, _tmax, metaBall.center, metaBall.radius))
{
thit = _thit;
return true;
}
return false;
}
float3 CalculateMetaballNormal(in float3 position, in Metaball blob)
{
float3 direction = position - blob.center;
float distance = length(direction);
if (distance < blob.radius)
{
float d = blob.radius - distance;
float r = d / blob.radius;
float len = 30 / (d * distance) * r * r * r * (r - 1) * (r - 1);
return direction * len;
}
return float3(0, 0, 0);
}
// Calculate a normal via central differences.
float3 CalculateMetaballNormal(in float3 position, in Metaball blobs[MAX_METABALLS_PER_CAL], in UINT nActiveMetaballs)
{
/*
float e = 0.5773 * 0.00001;
return normalize(float3(
CalculateMetaballsPotential(position + float3(-e, 0, 0), blobs, nActiveMetaballs) -
CalculateMetaballsPotential(position + float3(e, 0, 0), blobs, nActiveMetaballs),
CalculateMetaballsPotential(position + float3(0, -e, 0), blobs, nActiveMetaballs) -
CalculateMetaballsPotential(position + float3(0, e, 0), blobs, nActiveMetaballs),
CalculateMetaballsPotential(position + float3(0, 0, -e), blobs, nActiveMetaballs) -
CalculateMetaballsPotential(position + float3(0, 0, e), blobs, nActiveMetaballs)));
*/
float3 norm = { 0, 0, 0 };
for (UINT j = 0; j < nActiveMetaballs; ++j)
{
norm += CalculateMetaballNormal(position, blobs[j]);
}
return normalize(norm);
}
bool RayMetaBallPostIntersectionTest(in Ray localRay, in RayPayload payload, out float thit, out float3 normal)
{
const float3 Translation = float3(0.0f, 1.0f, 0.0f);
UINT numOfMetaBalls = 0;
float tmin = INFINITY;
float tmax = -INFINITY;
uint j = 0;
float smin = INFINITY;
float start = RayTMin();
MetaBall metaBalls[MAX_METABALLS_PER_CAL];
const float innerRatio = 0.74f;
for (int i = 0; i < MAX_INDEX_BUFFER_LENGTH; i++) {
if (j >= MAX_METABALLS_PER_CAL || !(payload.bit[i / 32] & (1U << (i % 32)))){
continue;
}
MetaBall m = g_metaballs[i];
m.center += Translation;
//metaBalls[i].center = mul(WorldToObject3x4(), float4(metaBalls[i].center, 1.0f));
//metaBalls[i].radius = length(mul(WorldToObject3x4(), float4(metaBalls[i].radius, 0.0f, 0.0f, 0.0f)));
float _thit, _tmax;
if (RaySolidSphereIntersectionTest(localRay, _thit, _tmax, m.center, m.radius))
{
#ifdef INTERVAL_REFINEMENT
if (_thit > smin || _thit < start) {
continue;
}
float _shit, _smax;
// test inner
if (RaySolidSphereIntersectionTest(localRay, _shit, _smax, m.center, m.radius * innerRatio)) {
smin = min(_shit, smin);
}
#endif
tmin = min(_thit, tmin);
tmax = max(_tmax, tmax);
metaBalls[j++] = m;
}
}
numOfMetaBalls = j;
tmin = max(tmin, RayTMin());
tmax = min(tmax, RayTCurrent());
UINT MAX_STEPS = 256;
float t = tmin;
float minTStep = (tmax - tmin) / (MAX_STEPS);
UINT iStep = 0;
const float Threshold = 0.1f;
#ifdef PREFER_PERFORMANCE
float3 position = localRay.origin + t * localRay.direction;
float sumFieldpotential = 0;
for (i = 0; i < numOfMetaBalls; i++)
{
float distance;
float tempPotential = CalculateMetaballPotential(position, metaBalls[i], distance);
sumFieldpotential += tempPotential;
}
bool fromInside = sumFieldpotential > Threshold;
#else
bool fromInside = false;
#endif
while (iStep++ < MAX_STEPS)
{
float3 position = localRay.origin + t * localRay.direction;
float sumFieldpotential = 0;
for (UINT j = 0; j < numOfMetaBalls; j++)
{
float distance;
float tempPotential = CalculateMetaballPotential(position, metaBalls[j], distance);
sumFieldpotential += tempPotential;
}
if (!fromInside && sumFieldpotential >= Threshold)
{
normal = CalculateMetaballNormal(position, metaBalls, numOfMetaBalls);
if (IsAValidHit(localRay, t, normal))
{
thit = t;
return true;
}
}
if (fromInside && sumFieldpotential <= Threshold)
{
normal = CalculateMetaballNormal(position, metaBalls, numOfMetaBalls);
if (IsAValidHit(localRay, t, normal))
{
thit = t;
return true;
}
}
t += minTStep;
}
return false;
}
/*
bool ShadowRayMetaBallPostIntersectionTest(in Ray localRay, in ShadowRayPayload payload, out float thit)
{
UINT numOfMetaBalls = payload.indexCount;
float tmin = INFINITY;
float tmax = -INFINITY;
MetaBall metaBalls[MAX_INDEX_BUFFER_LENGTH];
for (UINT i = 0; i < numOfMetaBalls; i++) {
metaBalls[i] = g_metaballs[payload.indexBuffer[i]];
float _thit, _tmax;
if (RaySolidSphereIntersectionTest(localRay, _thit, _tmax, metaBalls[i].center, metaBalls[i].radius))
{
tmin = min(_thit, tmin);
tmax = max(_tmax, tmax);
}
}
tmin = max(tmin, RayTMin());
tmax = min(tmax, RayTCurrent());
UINT MAX_STEPS = 128;
float t = tmin;
float minTStep = (tmax - tmin) / (MAX_STEPS / 1);
UINT iStep = 0;
const float Threshold = 0.25f;
while (iStep++ < MAX_STEPS)
{
float3 position = localRay.origin + t * localRay.direction;
float sumFieldpotential;
for (UINT j = 0; j < numOfMetaBalls; j++)
{
float distance;
float tempPotential = CalculateMetaballPotential(position, metaBalls[j], distance);
sumFieldpotential += tempPotential;
}
if (sumFieldpotential >= Threshold)
{
return true;
}
t += minTStep;
}
return false;
}*/
#endif /*METABALLPRIMITIVES_H*/