Hello everyone! how to put a custom shader in depth only on the floor?
I'm trying to use the depth of the scene to put the shader exclusively on the floor, but apparently I'm doing something wrong
Links
https://www.dropbox.com/s/4ghun92frlcg7hz/IMG_9960.PNG?dl=0
https://www.dropbox.com/home?preview=-2362988581602429186.MP4
PostProcess.metal
float linearizeDepth(float sampleDepth, float4x4 viewMatrix) {
constexpr float kDepthEpsilon = 1e-5f;
float d = max(kDepthEpsilon, sampleDepth);
d = abs(-viewMatrix[3][2] / d);
return d;
}
constexpr sampler textureSampler(address::clamp_to_edge, filter::bicubic);
float getDepth(float2 coords, constant InputArgs *args, texture2d<float, access::sample> inDepth,depth2d<float, access::sample> arDepth) {
float2 arDepthCoords = args->orientationTransform * coords + args->orientationOffset;
float realDepth = arDepth.sample(textureSampler, arDepthCoords);
float virtualDepth = linearizeDepth(inDepth.sample(textureSampler, coords)[0], args->viewMatrix);
bool realFragment = (virtualDepth <= FLT_EPSILON);
if (realFragment) { virtualDepth = realDepth; }
return min(virtualDepth, realDepth);
}
float3 getDirection(float2 screenCoord, constant InputArgs *args) {
float3 top = mix(args->topLeft.xyz, args->topRight.xyz, screenCoord.x);
float3 bottom = mix(args->bottomLeft.xyz, args->bottomRight.xyz, screenCoord.x);
return normalize(mix(bottom, top, screenCoord.y));
}
float3 worldCoordsForDepth(float depth, float2 screenCords, constant InputArgs *args) {
float3 centerDirection = getDirection(float2(0.5, 0.5), args);
float3 direction = getDirection(screenCords, args);
float depth2 = depth / dot(direction, centerDirection);
return direction * depth2 + args->viewTranslation.xyz;
}
[[kernel]]
void postProcess(uint2 gid [[thread_position_in_grid]],
texture2d<half, access::read> inputTexture [[texture(0)]],
texture2d<float, access::sample> inDepth [[texture(1)]],
texture2d<half, access::write> outputTexture [[texture(2)]],
depth2d<float, access::sample> arDepth [[texture(3)]],
constant InputArgs *args [[buffer(0)]]) {
float2 screenCoords = float2(float(gid[0]) / float(outputTexture.get_width()),
float(gid[1]) / float(outputTexture.get_height()));
float rawDepth = getDepth(screenCoords, args, inDepth, arDepth);
float3 worldCoords = worldCoordsForDepth(rawDepth, screenCoords, args);
float depth = rawDepth;
depth = 1 - pow(1 / (pow(depth, args->intensity) + 1), args->falloff);
depth = clamp(depth, 0.0, 1.0);
half4 nearColor = inputTexture.read(gid);
float blend = pow(1 - depth, args->exponent);
half4 color = half4(0.0);
float2 frag = worldCoords.xz;
frag *= 1.0 - 0.2 * cos (frag) * sin (3.14159 * 0.5 * inDepth.sample(textureSampler, float2(0.0)).x);
frag *= 5.0;
float random = rand (floor(frag));
float2 black = smoothstep(1.0, 0.8, cos(frag * 3.14159 * 2.0));
float3 finalColor = hsv2rgb (float3 (random, 1.0, 1.0));
finalColor *= black.x * black.y * smoothstep (1.0, 0.0, length (fract (frag) - 0.5));
finalColor *= 0.5 + 0.5 * cos (random + random * args->time + args->time + 3.14159 * 0.5 * inDepth.sample(textureSampler, float2(0.7)).x);
color = blend * nearColor + (1.0 - blend) * half4(half3(finalColor), 1.0);
}
I really hope for help of understanding this one