Skip to content

Instantly share code, notes, and snippets.

@manthrax
Created March 4, 2024 07:25
Show Gist options
  • Save manthrax/df155eb9a63c84413f165582cc80bddf to your computer and use it in GitHub Desktop.
Save manthrax/df155eb9a63c84413f165582cc80bddf to your computer and use it in GitHub Desktop.
Warp 3d plane to 2d screen space GLSL:
uniform sampler2D uTexture;
uniform float uAspect; //Aspect ratio of the screen
uniform float uProgress;
...
at the end of the vertex shader:
vec4 worldPosition = projectionMatrix * modelViewMatrix * vec4(pos, 1.);
vec4 screenPosition = vec4((uv-.5)*2.,0.,1.);
ivec2 texDim = textureSize(uTexture,0);
float imageAspect = float(texDim.x) / float(texDim.y);
if(imageAspect > uAspect)
screenPosition.y = screenPosition.y * uAspect / imageAspect;
else
screenPosition.x = screenPosition.x / uAspect * imageAspect;
gl_Position = mix(worldPosition,screenPosition, uProgress);
@manthrax
Copy link
Author

manthrax commented Mar 4, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment