I would like to compute a projective transformation to texture an arbitrary quad in webgl (with three.js and shaders if possible/necessary).
This is what I want to obtain, taken from this answer.
Everything is well described in the post, so I suppose that with a bit of work I could solve the problem. Here is a pseudo-code of the solution:
precompute A matrix (should be trivial since texture coordinates are in [0,1] interval)
compute B matrix according to the vertex positions (not possible in the vertex shader since we need the four coordinates of the points)
use B in the fragment shader to compute the correct texture coordinate at each pixel
However I am wondering if there is an easier method to do that in webgl.
---- Links to related topics ----
There is a similar way to solve the problem mathematically described here, but since it a solution to compute a many to many point mapping, it seems an overkill to me.
I thought that this is a solution in OpenGL but realized it is a solution to perform a simple perspective correct interpolation, which is luckily enabled by default.
I found many things on trapezoids, which is a simple version of the more general problem I want to solve: 1, 2 and 3. I first though that those would help, but instead they lead me to a lot of reading and misunderstanding.
Finally, this page describes a solution to solve the problem, but I was skeptical that it is the simplest and most common solution. Now I think it might be correct !
---- Conclusion ----
I have been searching a lot for the solution, not because it is a particularly complex problem, but because I was looking for a simple and typical/common solution. I though it is an easy problem solved in many cases (every video mapping apps) and that there would be trivial answers.
Ok I managed to do it with three.js and coffeescript (I had to implement the missing Matrix3 functions):
and here is the fragment shader:
Additional note
Maptastic is a Javascript/CSS projection mapping utility. https://github.com/glowbox/maptasticjs