Skip to content

Instantly share code, notes, and snippets.

@ghidra
Last active February 12, 2019 22:26
Show Gist options
  • Save ghidra/0d1b93488255527efc19b8640f259c0c to your computer and use it in GitHub Desktop.
Save ghidra/0d1b93488255527efc19b8640f259c0c to your computer and use it in GitHub Desktop.
//rotate 2d vector
vec2 rotateUV(vec2 uv, float angle)
{
float rad = angle*(3.14159265359/180.0);
float c = cos(rad);
float s = sin(rad);
return vec2( uv.x * c - uv.y * s, uv.x * s + uv.y * c);
}
//get a triplanar interpolated texture look up
vec4 textureTriPlanar(sampler2D tex,vec3 pos,vec3 norm)
{
///blend
vec3 blending = abs(norm);
blending = normalize(max(blending, 0.00001)); // Force weights to sum to 1.0
float b = (blending.x + blending.y + blending.z);
blending /= vec3(b);
///Sample
vec4 xtex = texture(tex, pos.yz);
vec4 ytex = texture(tex, pos.xz);
vec4 ztex = texture(tex, pos.xy);
return (xtex * blending.x + ytex * blending.y + ztex * blending.z);
}
///
float luminance(vec3 c){return dot(c, vec3(.2126, .7152, .0722));}
///
float bias(float t, float b){return (t / ((((1.0/b) - 2.0)*(1.0 - t))+1.0));}
float gain(float t,float g)
{
if(t < 0.5)
{
return bias(t * 2.0,g)/2.0;
}else{
return bias(t * 2.0 - 1.0,1.0 - g)/2.0 + 0.5;
}
}
float rand(vec3 co){return fract(sin(dot(co ,vec3(12.9898,78.233,-23.3562))) * 43758.5453);}
float fit(float v, float l1, float h1, float l2, float h2){return l2 + (v - l1) * (h2 - l2) / (h1 - l1);}
//https://gist.github.com/neilmendoza/4512992
mat3 rotationMatrix(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c);
}
mat3 rotationMatrix(vec3 x, vec3 y, vec3 z)
{
return mat3(x.x,x.y,x.z,
y.x,y.y,y.z,
z.x,z.y,z.z);
}
mat3 eulerMatrix(float x, float y, float z)
{
float sx = sin(x*(3.14125/180.0));
float cx = cos(x*(3.14125/180.0));
float sy = sin(y*(3.14125/180.0));
float cy = cos(y*(3.14125/180.0));
float sz = sin(z*(3.14125/180.0));
float cz = cos(z*(3.14125/180.0));
return mat3(cx*cy, cx*sy*sz-sz*cz, cx*sy*cz+sx*sz,
sz*cy, sx*sy*sz+cx*cz, sz*sy*cz-cx*sz,
-sy, cy*sz, cy*cz);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment