[Metaballs] Do all the texture generation in the fragment shader
This commit is contained in:
parent
8b4f2d8cc2
commit
dfa603012f
2 changed files with 45 additions and 14 deletions
|
@ -24,6 +24,13 @@ typedef struct {
|
|||
float2 textureCoordinate;
|
||||
} RasterizerData;
|
||||
|
||||
typedef struct {
|
||||
int2 size;
|
||||
int numberOfBalls;
|
||||
} Parameters;
|
||||
|
||||
typedef half3 Ball;
|
||||
|
||||
vertex RasterizerData
|
||||
passthroughVertexShader(uint vid [[vertex_id]],
|
||||
constant Vertex* vertexes [[buffer(0)]])
|
||||
|
@ -35,19 +42,36 @@ passthroughVertexShader(uint vid [[vertex_id]],
|
|||
return out;
|
||||
}
|
||||
|
||||
float sampleAtPoint(float2, constant Ball*, uint);
|
||||
|
||||
fragment float4
|
||||
sampleToColorShader(RasterizerData in [[stage_in]],
|
||||
texture2d<half> samples [[texture(0)]])
|
||||
sampleToColorShader(RasterizerData in [[stage_in]],
|
||||
constant Parameters& parameters [[buffer(0)]],
|
||||
constant Ball* balls [[buffer(1)]])
|
||||
{
|
||||
constexpr sampler textureSampler(mag_filter::linear, min_filter::linear);
|
||||
const half4 sample = samples.sample(textureSampler, in.textureCoordinate);
|
||||
const float sample = sampleAtPoint(in.textureCoordinate, balls, parameters.numberOfBalls);
|
||||
|
||||
float4 out;
|
||||
if (sample.r > 1.0) {
|
||||
if (sample > 1.0) {
|
||||
out = float4(0.0, 1.0, 0.0, 0.0);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
out = float4(0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
float
|
||||
sampleAtPoint(float2 point,
|
||||
constant Ball* balls,
|
||||
uint count)
|
||||
{
|
||||
float sample = 0.0;
|
||||
for (uint i = 0; i < count; i++) {
|
||||
constant Ball& ball = balls[i];
|
||||
float r2 = ball.z * ball.z; // Radius stored in z coordinate.
|
||||
float xDiff = point.x - ball.x;
|
||||
float yDiff = point.y - ball.y;
|
||||
sample += r2 / ((xDiff * xDiff) + (yDiff * yDiff));
|
||||
}
|
||||
return sample;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue