Home Game Development shaders – Generate Uniform distances in Distance Field with out Square Textures?

### shaders – Generate Uniform distances in Distance Field with out Square Textures?

Following this Godot Tutorial I’ve been implementing a shader for 2D international illumination. In the tutorial it mentions including a UV to World side ratio change throughout the raymarching perform to right distances which are biased in direction of both X or Y axis (relying on decision). This technique works, however is it attainable to use this correction throughout the distance subject shader itself?

Hopefully most of this is sensible for those who’ve been paying consideration up till now. One presumably obscure half is perhaps remodeling the UV to world side ratio. We want to do that if our viewport is rectangular or our distances shall be totally different relying on whether or not they’re biased in direction of the X or X axis. We’ll must translate again to UV house earlier than sampling any enter textures.

My authentic thought was to use this remodel to the coordinates themselves throughout the distance subject shader, however I’ve not gotten anyplace with that implementation.

This is my present distance subject fragment shader: (converts the output distance to a vec2 for elevated precision–because I’m utilizing GameMaker which makes use of 8-bit RGBA parts for floor textures which can’t be modified)

``````various vec2 in_Coord; // UV coordinate of the present pixel fragment.
uniform vec2 in_Resol; // Width,Height of the render decision.

// Converts a float to a normalized vec2().
float V2_F16(vec2 v) { return v.x + (v.y / 255.0); }
// Converts a normalized vec2() to a float.
vec2 F16_V2(float f) { return vec2(flooring(f * 255.0) / 255.0, fract(f * 255.0)); }

void most important() {
// Distance Field from Jump Flood texture.
vec4 jfuv = texture2D(gm_BaseTexture, in_Coord);
vec2 jumpflood = vec2(V2_F16(jfuv.rg),V2_F16(jfuv.ba));
gl_FragColor = vec4(F16_V2(distance(in_Coord, jumpflood)), 0.0, 1.0);
}
``````

FIGURE A: (Rectangular Viewport/Resolution)

FIGURE B: (Square Viewport/Resolution)

The sq. output produces a way more uniform lighting output whereas the oblong viewport has a biased x-component when sampling the space texture which causes mild to unfold horizontally extra. (FIGBURE C) While the sq. viewport is extra visually uniform. (FIGURE D)

FIGURE C:

FIGURE D: