Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced moiré a pattern reduction in HLSL / GLSL procedural textures shader - antialiasing

I am working on a procedural texture, it looks fine, except very far away, the small texture pixels disintegrate into noise and moiré patterns.

I have set out to find a solution to average and quantise the scale of the pattern far away and close up, so that close by it is in full detail, and far away it is rounded off so that one pixel of a distant mountain only represents one colour found there, and not 10 or 20 colours at that point.

It is easy to do it by rounding the World_Position that the volumetric texture is based on using an if statement i.e.:

    if( camera-pixel_distance > 1200 meters ) {wpos = round(wpos/3)*3;}//---round far away pixels
    return texturefucntion(wpos);

the result of rounding far away textures is that they will look like this, except very far away: the trouble with this is i have to make about 5 if conditions for the various distances, and i have to estimate a random good rounding value

I tried to make a function that cuts the distance of the pixel into distance steps, and applies a LOD devider to the pixel_worldposition value to make it progressively rounder at distance but i got nonsense results, actually the HLSL was totally flipping out. here is the attempt:

float cmra= floor(_WorldSpaceCameraPos/500)*500;  //round camera distance by steps of 500m
float dst=  (1-distance(cmra,pos)/4500)*1000 ; //maximum faraway view is 4500 meters
pos= floor(pos/dst)*dst;//close pixels are rounded by 1000, far ones rounded by 20,30 etc

it returned nonsense patterns that i could not understand.

Are there good documented algorithms for smoothing and rounding distance texture artifacts? can i use the scren pixel resolution, combined with the distance of the pixel, to round each pixel to one color that stays a stable color?

like image 918
LifeInTheTrees Avatar asked Mar 21 '23 12:03

LifeInTheTrees


1 Answers

Are you familiar with the GLSL (and I would assume HLSL) functions dFdx() and dFdy() or fwidth()? They were made specifically to solve this problem. From the GLSL Spec:

genType dFdy (genType p) Returns the derivative in y using local differencing for the input argument p. These two functions are commonly used to estimate the filter width used to anti-alias procedural textures.

and

genType fwidth (genType p) Returns the sum of the absolute derivative in x and y using local differencing for the input argument p, i.e.: abs (dFdx (p)) + abs (dFdy (p));

like image 108
user1118321 Avatar answered Apr 06 '23 09:04

user1118321