Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // some util functions
- float decodeFloat(in vec4 color) {
- return color.r * (255.0 * 256.0 * 256.0) + color.g * 255.0 * 256.0 + color.b * 255.0;
- }
- /**
- * Reconstructs the 3D position in world space from given screen uv coordinates and depth from
- * the zBuffer.
- */
- vec3 getPosition(in float zBuffer, in vec2 uv, in mat4 viewProjectionMatrixInverse){
- vec4 pos = vec4(uv, zBuffer, 1.0) * 2.0 - 1.0;
- pos = viewProjectionMatrixInverse * pos;
- return pos.xyz / pos.w;
- }
- /**
- * approximates the real distance in [0 - 1] - but is not entirely independent
- * of the camera position - so use with caution!
- **/
- float getDepthByZBuffer(in float zBuffer, in mat4 projectionMatrix) {
- float normalized_depth = zBuffer * 2.0 - 1.0;
- return max(0.0, projectionMatrix[2][3] / (normalized_depth - projectionMatrix[1][1]));
- }
- float getDistanceByZBuffer(in float zBuffer, in float near, in float far) {
- float z_n = 2.0 * zBuffer - 1.0;
- return 2.0 * near * far / (far + near - z_n * (far - near));
- }
- /**
- * calculates a linear interpretation of the (non linear) depth buffer in
- * the range of [0 - 1].
- **/
- float linearizeDepth(in float z, in float near, in float far) {
- return (2.0 * near) / (far + near - z * (far - near));
- }
- vec4 getAttributeAt(in sampler2D texture, in ivec2 position) {
- return texelFetch(texture, position, 0);
- }
- /**
- * symmetric modulo
- */
- float modulo(float a, float x) {
- float result = mod(a, x);
- if (result < 0) {
- result = x + result;
- }
- return abs(result);
- }
- /**
- * returns a signmoid distribution from a given
- * value between 0 and 1 projected to a output
- * value between 0 and 1
- *
- * the multiplicant defines how courvy the projection
- * is: 10 ist normal -> while 20 or above makes it more S shaped
- **/
- float sigmoid(float mult, float x) {
- return 1 / (1 + pow(2.718281828459045235, -((x - 0.5) * mult)));
- }
- /**
- * converts rgb to hsv
- * h is in the range of [0-1]
- **/
- vec3 rgb2hsv(vec3 c) {
- vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
- vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
- vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
- float d = q.x - min(q.w, q.y);
- float e = 1.0e-10;
- return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
- }
- /**
- * converts hsv to rgb
- **/
- vec3 hsv2rgb(vec3 c) {
- vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
- vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
- return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
- }
- /**
- * mixes in a cubic interpolation insted of a linear one
- **/
- vec4 mixCubic(in vec4 valA, in vec4 valB, in float i) {
- float pos = pow(i, 1.5);
- return mix(valA, valB, pos);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement