Skip to content

Instantly share code, notes, and snippets.

@SoylentGraham
Created June 7, 2020 12:09
Show Gist options
  • Save SoylentGraham/b4a5b9c08d64ffe4c1ee3a68e6756993 to your computer and use it in GitHub Desktop.
Save SoylentGraham/b4a5b9c08d64ffe4c1ee3a68e6756993 to your computer and use it in GitHub Desktop.
Broken precision using vec3 in struct, only on android webgl glsl (chromium)
precision highp float;
struct TRay
{
vec3 Pos;
vec3 Dir;
};
vec3 ScreenToWorld(float2 uv,float z)
{
float x = mix( -1.0, 1.0, uv.x );
float y = mix( 1.0, -1.0, uv.y );
vec4 ScreenPos4 = vec4( x, y, z, 1.0 );
vec4 CameraPos4 = ScreenToCameraTransform * ScreenPos4;
vec4 WorldPos4 = CameraToWorldTransform * CameraPos4;
vec3 WorldPos = WorldPos4.xyz / WorldPos4.w;
return WorldPos;
}
TRay GetWorldRay()
{
float Near = 0.01;
float Far = 100.0;
TRay Ray;
Ray.Pos = ScreenToWorld( uv, Near );
Ray.Dir = ScreenToWorld( uv, Far ) - Ray.Pos;
return Ray;
}
main()
{
TRay Ray = GetWorldRay();
// Ray pos & dir seems low precision (varies based on angle in matricies, when w gets high)
Ray.Pos = ScreenToWorld(uv, 0.01);
Ray.Dir = ScreenToWorld(uv, 100.0) - Ray.Pos;
// now ray dir is fine!
gl_FragColor = Dir;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment