I'm trying to apply a gradient texture to a model using a gradient texture derived from CSS. The idea is that a user could adjust the stops/colors of a gradient and then apply the gradient to a model to match the current camera view as it's rotated around. I've had a very hard time understanding how to implement something like this tutorial.
I've created a very simple example with a hard coded gradient image and Suzanne the monkey, which you can find here:
https://github.com/abogartz/projection-mapping
(To run this, you can use the provided Browser-Sync setup or just run a simple server on index.html)
Right now, the Suzanne model applies the texture as per its own UVs. This results in a gradient that is not linear across the face:
What I would like is to use "projection mapping" instead, where the gradient starts from the leftmost vertex and ends at the rightmost, no matter how the camera is rotated (I'll save the camera matrix on a user action and use that as a uniform later).
The result should be more like this (of course with lighting,etc)
My current shader looks like this:
<script id='fragmentShader' type='x-shader/x-fragment'>
uniform vec2 u_mouse;
uniform vec2 u_resolution;
uniform float u_time;
uniform sampler2D u_gradient_tex;
varying vec2 vUv;
void main() {
gl_FragColor = texture2D(u_gradient_tex,vUv);
}
Obviously, the vUv varying is not what I want, so how do I calculate the projection coordinate instead?