Last active
December 19, 2019 01:50
-
-
Save sortofsleepy/dcc8bffbc0d2f9830fee743411f288e1 to your computer and use it in GitHub Desktop.
Useful glsl snippits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Helps to scale image uvs to fit variable surfaces. | |
| // Note - still need to test but sounds promising and basic idea appeared to work based on case study. | |
| // adapted from description by @lhbzr | |
| // https://tympanus.net/codrops/2019/12/18/case-study-portfolio-of-bruno-arizio/ | |
| vec2 scaleToFit(vec2 resolution,vec2 imageResolution){ | |
| vec2 ratio = vec2( | |
| min((resolution.x / resolution.y) / (imageResolution.x / imageResolution.y), 1.0), | |
| min((resolution.y / resolution.x) / (imageResolution.y / imageResolution.x), 1.0) | |
| ); | |
| return vec2( | |
| vUv.x * ratio.x + (1.0 - ratio.x) * 0.5, | |
| vUv.y * ratio.y + (1.0 - ratio.y) * 0.5 | |
| ); | |
| } | |
| // from | |
| // https://www.gamedev.net/forums/topic/593196-hlsl-texture-scale-shader-how-to-scale-from-origin/ | |
| vec2 scaleUVs(vec2 uv, float scale){ | |
| vec2 center = vec2(0.5); | |
| return (uv - center) * scale + center; | |
| } | |
| vec4 rescaleImg(sampler2D image, float zoomRatio, vec2 zoomUV){ | |
| return texture2D(image, zoomUV/zoomRatio + vec2((zoomRatio - 1.)/2.)/zoomRatio); | |
| } | |
| // note thi sstill needs more testing. | |
| // referenced from | |
| // https://gamedev.stackexchange.com/questions/87029/is-it-possible-to-use-unnormalized-texture-coordinates-from-a-gles2-glsl-fragmen | |
| vec2 pixelToNormalizedUVs(float x, float y, vec2 renderSize){ | |
| return vec2((x + 0.5) / renderSize.x, (y + 0.5) / renderSize.y); | |
| } | |
| // scale a vector | |
| mat2 scale(vec2 _scale){ | |
| return mat2(_scale.x,0.0, | |
| 0.0,_scale.y); | |
| } | |
| /* | |
| example | |
| uv -= vec2(0.5); | |
| uv = scale( vec2(_scale,0.8) ) * uv; | |
| uv += vec2(0.5); | |
| */ | |
| // desaturates a color. | |
| // @param color {vec3} vec3 containing color value | |
| // @param desaturation {float} the amount of desaturation to apply | |
| // reference https://github.com/jamieowen/glsl-blend/blob/master/_temp/conversion/desaturate.glsl | |
| vec4 desaturate(vec3 color, float desaturation) | |
| { | |
| vec3 grayXfer = vec3(0.3, 0.59, 0.11); | |
| vec3 gray = vec3(dot(grayXfer, color)); | |
| return vec4(mix(color, gray, desaturation), 1.0); | |
| } | |
| // helper function to calculate uv for screen space. | |
| vec2 screenspaceUV(vec2 resolution){ | |
| return gl_FragCoord.xy / resolution; | |
| } | |
| // adapted from | |
| // https://www.shadertoy.com/view/4sXSWs | |
| vec4 grain(vec2 uv, float strength){ | |
| float x = (uv.x + 4.0 ) * (uv.y + 4.0 ) * (10.0); | |
| return vec4(mod((mod(x, 13.0) + 1.0) * (mod(x, 123.0) + 1.0), 0.01)-0.005) * strength; | |
| } | |
| // generates a grid - useful for simple GPU particle simulations. | |
| vec3 generateGrid(float width, float height, vec2 resolution){ | |
| vec2 uv = gl_FragCoord/resolution.xy; | |
| uv.x *= width; | |
| uv.y *= height; | |
| vec2 ipos = floor(uv); | |
| return vec3( | |
| fract(sin(dot(ipos.xy, | |
| vec2(12.9898,78.233)))* | |
| 43758.5453123); | |
| ); | |
| } | |
| // when origin is set to lower left, centers position to the center of the screen. | |
| // pass in position + resolution. | |
| vec3 centerToScreen(vec3 pos, vec2 iResolution){ | |
| pos.xyz *= iResolution.y; | |
| pos.x += iResolution.x / 2.0; | |
| pos.y += iResolution.y / 2.0; | |
| return pos; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment