Having started learning shaders and experimenting with tools like Shadertoy, I am attempting to make stereoscopic (anaglyph or autostereogram) shaders as an exercise. For this, I need to displace each pixel to the left/right depending on its depth value.
I am trying to generate a double image, based on the original image and the depth map, with each image pixel displaced to the left and right by a value depending on the depth map - so background pixels are displaced more/less (depending on the mode) than foreground pixels.
It would be easy to get the color of the pixel, say, 30 px to the left or right, if I needed a displacement of 30 for this particular pixel. The problem is, this would not actually work: instead, I need to change the color of the pixel 30 px to the left or right, and displace the color of the current pixel there.
One solution would be to use a
for loop and check each pixel in the possible displacement range for its depth value. However this has an impact on performance that becomes untenable for large displacement values. With an average eye distance of 65 mm, this can easily require hundreds of pixels of displacement range.
Another solution (as per this answer) would be to generate two viewpoints and combine them, but I want to avoid doing so here. I am searching for a solution based on a single image and its depth map instead.
Is there a another, more efficient way to displace pixels in such a way using shaders?
Note: I am using Shadertoy for ease of use, but if a solution exists but is not usable in it, for example requiring a 3D engine to implement, it is still of interest.