shader utility functions and logic

Remap:

//将[a,b]的取值映射到[c,d]的范围内
float Remap(float a,float b,float c,float d,float val){
    
    
	    return (val-a)/(b-a) * (d-c) + c;
}

Keep the aspect ratio: (uv length and width are both [0,1])

//将uv移动到中心(0.5,0.5) 并保持宽高比
vec2 uvcenter = (fragCoord/iResolution.xy - 0.5)*iResolution.xy/iResolution.y;
//仅保持宽高比
vec2 uv = (fragCoord/iResolution.xy)*iResolution.xy/iResolution.y

Continuous values ​​become segmented values:

float x = floor(uv.xsize)/size;
insert image description here
float x = ceil(uv.x
size)/size;
insert image description here
float x = fract(uv.x*size)/size;
insert image description here

Generate random numbers:

Generate random numbers from 0-1:

float rand(float3 z){
    
    
                return frac(sin(dot(z.xyz,float3(12.9898,78.233,53.539))) * 43758.5453);
}

Construction of a rotation matrix for a rotation angle around an axis

According to the rotation axis axis rotation angle (radian)

float3x3 RotMatrixByAxisAngle(float angle,float3 axis){
    
    
                float c,s;
                sincos(angle,s,c);

                float t = 1-c;
                float x = axis.x;
                float y = axis.y;
                float z = axis.z;

                return float3x3(
                    t*x*x +c,   t*x*y - s*z,    t*x*z + s*y,
                    t*x*y + s*z,    t*y*y +c,   t*y*z - s*x,
                    t*x*z - s*y,    t*y*z +s*x,    t*z*z+c
                );
            }

Find the gray level corresponding to the RGB color value of the target pixel

This formula is the BT709 brightness conversion formula from RGB to YUV, and it is an image grayscale processing formula based on human perception.
Why are the three coefficients not 0.5, 0.5, 0.5, but 0.2125, 0.7154, 0.0721. That's because the human eye has different sensitivities to the three colors of red, green and blue, so a weighted average is required when calculating the gray level. This coefficient is mainly derived from the different sensitivities of human eyes to the three primary colors of R, G, and B. Similar formula: Y = 0.299 R + 0.587 G + 0.114*B

            fixed3 luminance(fixed3 colorpixel){
    
    
                fixed lumin = 0.2125 * colorpixel.r + 0.7154 * colorpixel.g + 0.0721 * colorpixel.b;
                return fixed3(lumin,lumin,lumin);
            }

Guess you like

Origin blog.csdn.net/suixinger_lmh/article/details/125097680