Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Last active July 23, 2016 18:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugi-cho/6076993 to your computer and use it in GitHub Desktop.
Save sugi-cho/6076993 to your computer and use it in GitHub Desktop.
#ifndef COONSCURVE_INCLUDED
#define COONSCURVE_INCLUDED
float3 coons(float t, float3 p0, float3 p1, float3 v0, float3 v1)
{
float3 a = 2*p0 - 2*p1 + v0 + v1;
float3 b = -3*p0 + 3*p1 - 2*v0 - v1;
float t2 = t*t;
float t3 = t2*t;
return a*t3 + b*t2 + v0*t + p0;
}
float2 coons(float t, float2 p0, float2 p1, float2 v0, float2 v1)
{
float2 a = 2*p0 - 2*p1 + v0 + v1;
float2 b = -3*p0 + 3*p1 - 2*v0 - v1;
float t2 = t*t;
float t3 = t2*t;
return a*t3 + b*t2 + v0*t + p0;
}
float coons(float t, float p0, float p1, float v0, float v1)
{
float a = 2*p0 - 2*p1 + v0 + v1;
float b = -3*p0 + 3*p1 - 2*v0 - v1;
float t2 = t*t;
float t3 = t2*t;
return a*t3 + b*t2 + v0*t + p0;
}
#endif
#ifndef EASING_INCLUDED
#define EASING_INCLUDED
//easing func from jQuery Easing Plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
float easeInQuad(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return c*(t/=d)*t + b;
}
float easeOutQuad(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return -c *(t/=d)*(t-2) + b;
}
float easeInOutQuad(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
if ((t/=d/2) < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
}
float easeInCubic(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return c*(t/=d)*t*t + b;
}
float easeOutCubic(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return c*((t=t/d-1)*t*t + 1) + b;
}
float easeInOutCubic(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
if ((t/=d/2) < 1) return c/2*t*t*t + b;
return c/2*((t-=2)*t*t + 2) + b;
}
float easeInQuart(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return c*(t/=d)*t*t*t + b;
}
float easeOutQuart(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return -c * ((t=t/d-1)*t*t*t - 1) + b;
}
float easeInOutQuart(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
return -c/2 * ((t-=2)*t*t*t - 2) + b;
}
float easeInQuint(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return c*(t/=d)*t*t*t*t + b;
}
float easeOutQuint(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return c*((t=t/d-1)*t*t*t*t + 1) + b;
}
float easeInOutQuint(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
return c/2*((t-=2)*t*t*t*t + 2) + b;
}
float easeInSine(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return -c * cos(t/d * (3.14159265359/2)) + c + b;
}
float easeOutSine(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return c * sin(t/d * (3.14159265359/2)) + b;
}
float easeInOutSine(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return -c/2 * (cos(3.14159265359*t/d) - 1) + b;
}
float easeInExpo(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return (t==0) ? b : c * pow(2, 10 * (t/d - 1)) + b;
}
float easeOutExpo(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return (t==d) ? b+c : c * (-pow(2, -10 * t/d) + 1) + b;
}
float easeInOutExpo(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
if (t==0) return b;
if (t==d) return b+c;
if ((t/=d/2) < 1) return c/2 * pow(2, 10 * (t - 1)) + b;
return c/2 * (-pow(2, -10 * --t) + 2) + b;
}
float easeInCirc(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return -c * (sqrt(1 - (t/=d)*t) - 1) + b;
}
float easeOutCirc(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
return c * sqrt(1 - (t=t/d-1)*t) + b;
}
float easeInOutCirc(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
if ((t/=d/2) < 1) return -c/2 * (sqrt(1 - t*t) - 1) + b;
return c/2 * (sqrt(1 - (t-=2)*t) + 1) + b;
}
float easeInElastic(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
float s=1.70158;float p=0;float a=c;
if (t==0) return b; if ((t/=d)==1) return b+c; if (p == 0) p=d*.3;
if (a < abs(c)) { a=c; s=p/4; }
else s = p/(2*3.14159265359) * asin (c/a);
return -(a*pow(2,10*(t-=1)) * sin( (t*d-s)*(2*3.14159265359)/p )) + b;
}
float easeOutElastic(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
float s=1.70158;float p=0;float a=c;
if (t==0) return b; if ((t/=d)==1) return b+c; if (p ==0) p=d*.3;
if (a < abs(c)) { a=c; s=p/4; }
else s = p/(2*3.14159265359) * asin (c/a);
return a*pow(2,-10*t) * sin( (t*d-s)*(2*3.14159265359)/p ) + c + b;
}
float easeInOutElastic(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
float s=1.70158;float p=0;float a=c;
if (t==0) return b; if ((t/=d/2)==2) return b+c; if (p ==0) p=d*(.3*1.5);
if (a < abs(c)) { a=c; s=p/4; }
else s = p/(2*3.14159265359) * asin (c/a);
if (t < 1) return -.5*(a*pow(2,10*(t-=1)) * sin( (t*d-s)*(2*3.14159265359)/p )) + b;
return a*pow(2,-10*(t-=1)) * sin( (t*d-s)*(2*3.14159265359)/p )*.5 + c + b;
}
float easeInBack(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
float s = 1.70158;
return c*(t/=d)*t*((s+1)*t - s) + b;
}
float easeOutBack(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
float s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
float easeInOutBack(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
float s = 1.70158;
if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}
float easeOutBounce(float x, float t, float b, float c, float d) {
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
} else {
return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
}
}
float easeOutBounce(float x){
return easeOutBounce(x, x, 0, 1, 1);
}
float easeInBounce(float x, float t, float b, float c, float d) {
return c - easeOutBounce (x, d-t, 0, c, d) + b;
}
float easeInBounce(float x){
return easeInBounce(x, x, 0, 1, 1);
}
float easeInOutBounce(float x, float t, float b, float c, float d) {
if (t < d/2) return easeInBounce (x, t*2, 0, c, d) * .5 + b;
return easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
}
float easeInOutBounce(float x){
return easeInOutBounce(x, x, 0, 1, 1);
}
//g:gravity b:bouncy
float easeOutBounceCustomized(float x, float g, float b){
float p1 = sqrt(1/g);
float p2 = 2*sqrt(b/g);
float p3 = 2*sqrt(b*b/g);
float p4 = 2*sqrt(b*b*b/g);
float p5 = 2*sqrt(b*b*b*b/g);
if(x < p1){
return g*x*x;
} else if(x < p1+p2){
return g*(x-=(p1+p2/2))*x+(1-b);
} else if(x < p1+p2+p3){
return g*(x-=(p1+p2+p3/2))*x+(1-b*b);
} else if(x < p1+p2+p3+p4){
return g*(x-=(p1+p2+p3+p4/2))*x+(1-b*b*b);
} else if(x < p1+p2+p3+p4+p5){
return g*(x-=(p1+p2+p3+p4+p5/2))*x+(1-b*b*b*b);
}
return 1;
}
#endif // EASING_INCLUDED
#ifndef NOISE_INCLUDED
#define NOISE_INCLUDED
//
// Description : Array and textureless GLSL 2D simplex noise function.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
float4 mod289(float4 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
float3 mod289(float3 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
float2 mod289(float2 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
float mod289(float x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
float permute(float x) {
return mod289(((x*34.0)+1.0)*x);
}
float3 permute(float3 x) {
return mod289(((x*34.0)+1.0)*x);
}
float4 permute(float4 x) {
return mod289(((x*34.0)+1.0)*x);
}
float4 taylorInvSqrt(float4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
float taylorInvSqrt(float r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
float4 grad4(float j, float4 ip)
{
const float4 ones = float4(1.0, 1.0, 1.0, -1.0);
float4 p,s;
p.xyz = floor( frac (float3(j,j,j) * ip.xyz) * 7.0) * ip.z - 1.0;
p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
//s = p;//float4(lessThan(p, float4(0.0)));
if(p.x<0)
s.x = 1;
else
s.x = 0;
if(p.y<0)
s.y = 1;
else
s.y = 0;
if(p.z<0)
s.z = 1;
else
s.z = 0;
if(p.w<0)
s.w = 1;
else
s.w = 0;
p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
return p;
}
float snoise(float2 v)
{
const float4 C = float4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
-0.577350269189626, // -1.0 + 2.0 * C.x
0.024390243902439); // 1.0 / 41.0
// First corner
float2 i = floor(v + dot(v, C.yy) );
float2 x0 = v - i + dot(i, C.xx);
// Other corners
float2 i1;
//i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
//i1.y = 1.0 - i1.x;
i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0);
// x0 = x0 - 0.0 + 0.0 * C.xx ;
// x1 = x0 - i1 + 1.0 * C.xx ;
// x2 = x0 - 1.0 + 2.0 * C.xx ;
float4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
// Permutations
i = mod289(i); // Avoid truncation effects in permutation
float3 p = permute( permute( i.y + float3(0.0, i1.y, 1.0 ))
+ i.x + float3(0.0, i1.x, 1.0 ));
float3 m = max(0.5 - float3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
m = m*m ;
m = m*m ;
// Gradients: 41 points uniformly over a line, mapped onto a diamond.
// The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
float3 x = 2.0 * frac(p * C.www) - 1.0;
float3 h = abs(x) - 0.5;
float3 ox = floor(x + 0.5);
float3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt( a0*a0 + h*h );
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
// Compute final noise value at P
float3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
float snoise(float3 v)
{
const float2 C = float2(1.0/6.0, 1.0/3.0) ;
const float4 D = float4(0.0, 0.5, 1.0, 2.0);
// First corner
float3 i = floor(v + dot(v, C.yyy) );
float3 x0 = v - i + dot(i, C.xxx) ;
// Other corners
float3 g = step(x0.yzx, x0.xyz);
float3 l = 1.0 - g;
float3 i1 = min( g.xyz, l.zxy );
float3 i2 = max( g.xyz, l.zxy );
// x0 = x0 - 0.0 + 0.0 * C.xxx;
// x1 = x0 - i1 + 1.0 * C.xxx;
// x2 = x0 - i2 + 2.0 * C.xxx;
// x3 = x0 - 1.0 + 3.0 * C.xxx;
float3 x1 = x0 - i1 + C.xxx;
float3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
float3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
// Permutations
i = mod289(i);
float4 p = permute( permute( permute(
i.z + float4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + float4(0.0, i1.y, i2.y, 1.0 ))
+ i.x + float4(0.0, i1.x, i2.x, 1.0 ));
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float n_ = 0.142857142857; // 1.0/7.0
float3 ns = n_ * D.wyz - D.xzx;
float4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
float4 x_ = floor(j * ns.z);
float4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
float4 x = x_ *ns.x + ns.yyyy;
float4 y = y_ *ns.x + ns.yyyy;
float4 h = 1.0 - abs(x) - abs(y);
float4 b0 = float4( x.xy, y.xy );
float4 b1 = float4( x.zw, y.zw );
//float4 s0 = float4(lessThan(b0,0.0))*2.0 - 1.0;
//float4 s1 = float4(lessThan(b1,0.0))*2.0 - 1.0;
float4 s0 = floor(b0)*2.0 + 1.0;
float4 s1 = floor(b1)*2.0 + 1.0;
float4 sh = -step(h, float4(0,0,0,0));
float4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
float4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
float3 p0 = float3(a0.xy,h.x);
float3 p1 = float3(a0.zw,h.y);
float3 p2 = float3(a1.xy,h.z);
float3 p3 = float3(a1.zw,h.w);
//Normalise gradients
float4 norm = taylorInvSqrt(float4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
// Mix final noise value
float4 m = max(0.6 - float4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
m = m * m;
return 42.0 * dot( m*m, float4( dot(p0,x0), dot(p1,x1),
dot(p2,x2), dot(p3,x3) ) );
}
// (sqrt(5) - 1)/4 = F4, used once below
#define F4 0.309016994374947451
float snoise(float4 v)
{
const float4 C = float4( 0.138196601125011, // (5 - sqrt(5))/20 G4
0.276393202250021, // 2 * G4
0.414589803375032, // 3 * G4
-0.447213595499958); // -1 + 4 * G4
// First corner
float4 i = floor(v + dot(v, float4(F4,F4,F4,F4)) );
float4 x0 = v - i + dot(i, C.xxxx);
// Other corners
// Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
float4 i0;
float3 isX = step( x0.yzw, x0.xxx );
float3 isYZ = step( x0.zww, x0.yyz );
// i0.x = dot( isX, float3( 1.0 ) );
i0.x = isX.x + isX.y + isX.z;
i0.yzw = 1.0 - isX;
// i0.y += dot( isYZ.xy, float2( 1.0 ) );
i0.y += isYZ.x + isYZ.y;
i0.zw += 1.0 - isYZ.xy;
i0.z += isYZ.z;
i0.w += 1.0 - isYZ.z;
// i0 now contains the unique values 0,1,2,3 in each channel
float4 i3 = clamp( i0, 0.0, 1.0 );
float4 i2 = clamp( i0-1.0, 0.0, 1.0 );
float4 i1 = clamp( i0-2.0, 0.0, 1.0 );
// x0 = x0 - 0.0 + 0.0 * C.xxxx
// x1 = x0 - i1 + 1.0 * C.xxxx
// x2 = x0 - i2 + 2.0 * C.xxxx
// x3 = x0 - i3 + 3.0 * C.xxxx
// x4 = x0 - 1.0 + 4.0 * C.xxxx
float4 x1 = x0 - i1 + C.xxxx;
float4 x2 = x0 - i2 + C.yyyy;
float4 x3 = x0 - i3 + C.zzzz;
float4 x4 = x0 + C.wwww;
// Permutations
i = mod289(i);
float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
float4 j1 = permute( permute( permute( permute (
i.w + float4(i1.w, i2.w, i3.w, 1.0 ))
+ i.z + float4(i1.z, i2.z, i3.z, 1.0 ))
+ i.y + float4(i1.y, i2.y, i3.y, 1.0 ))
+ i.x + float4(i1.x, i2.x, i3.x, 1.0 ));
// Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
// 7*7*6 = 294, which is close to the ring size 17*17 = 289.
float4 ip = float4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;
float4 p0 = grad4(j0, ip);
float4 p1 = grad4(j1.x, ip);
float4 p2 = grad4(j1.y, ip);
float4 p3 = grad4(j1.z, ip);
float4 p4 = grad4(j1.w, ip);
// Normalise gradients
float4 norm = taylorInvSqrt(float4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
p4 *= taylorInvSqrt(dot(p4,p4));
// Mix contributions from the five corners
float3 m0 = max(0.6 - float3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0);
float2 m1 = max(0.6 - float2(dot(x3,x3), dot(x4,x4) ), 0.0);
m0 = m0 * m0;
m1 = m1 * m1;
return 49.0 * ( dot(m0*m0, float3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
+ dot(m1*m1, float2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;
}
float3 snoise3D(float3 v){
float3 n = float3(
snoise(float2(v.x, v.y)),
snoise(float2(v.y, v.z)),
snoise(float2(v.z, v.x))
);
return n;
}
float curlX(float3 v, float d){
return (
(snoise3D(float3(v.x,v.y+d,v.z)).z - snoise3D(float3(v.x,v.y-d,v.z)).z)
-(snoise3D(float3(v.x,v.y,v.z+d)).y - snoise3D(float3(v.x,v.y,v.z-d)).y)
) /2/d;
}
float curlY(float3 v, float d){
return (
(snoise3D(float3(v.x,v.y,v.z+d)).x - snoise3D(float3(v.x,v.y,v.z-d)).x)
-(snoise3D(float3(v.x+d,v.y,v.z)).z - snoise3D(float3(v.x-d,v.y,v.z)).z)
) /2/d;
}
float curlZ(float3 v, float d){
return (
(snoise3D(float3(v.x+d,v.y,v.z)).y - snoise3D(float3(v.x-d,v.y,v.z)).y)
-(snoise3D(float3(v.x,v.y+d,v.z)).x - snoise3D(float3(v.x,v.y-d,v.z)).x)
) /2/d;
}
#endif // NOISE_INCLUDED
#ifndef PHOTOSHOPMATH_INCLUDED
#define PHOTOSHOPMATH_INCLUDED
/*
** Copyright (c) 2012, Romain Dura romain@shazbits.com
**
** Permission to use, copy, modify, and/or distribute this software for any
** purpose with or without fee is hereby granted, provided that the above
** copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
** WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
** SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
** ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
** IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
** Photoshop & misc math
** Blending modes, RGB/HSL/Contrast/Desaturate, levels control
**
** Romain Dura | Romz
** Blog: http://mouaif.wordpress.com
** Post: http://mouaif.wordpress.com/?p=94
*/
/*
** Desaturation
*/
float4 Desaturate(float3 color, float Desaturation)
{
float3 grayXfer = float3(0.3, 0.59, 0.11);
float d = dot(grayXfer, color);
float3 gray = float3(d,d,d);
return float4(lerp(color, gray, Desaturation), 1.0);
}
/*
**HSV
*/
float3 rgb2hsv(float3 c)
{
float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
float3 hsv2rgb(float3 c)
{
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
/*
** Hue, saturation, luminance
*/
float3 RGBToHSL(float3 color)
{
float3 hsl; // init to 0 to avoid warnings ? (and reverse if + remove first part)
float fmin = min(min(color.r, color.g), color.b); //Min. value of RGB
float fmax = max(max(color.r, color.g), color.b); //Max. value of RGB
float delta = fmax - fmin; //Delta RGB value
hsl.z = (fmax + fmin) / 2.0; // Luminance
if (delta == 0.0) //This is a gray, no chroma...
{
hsl.x = 0.0; // Hue
hsl.y = 0.0; // Saturation
}
else //Chromatic data...
{
if (hsl.z < 0.5)
hsl.y = delta / (fmax + fmin); // Saturation
else
hsl.y = delta / (2.0 - fmax - fmin); // Saturation
float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;
float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;
float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;
if (color.r == fmax )
hsl.x = deltaB - deltaG; // Hue
else if (color.g == fmax)
hsl.x = (1.0 / 3.0) + deltaR - deltaB; // Hue
else if (color.b == fmax)
hsl.x = (2.0 / 3.0) + deltaG - deltaR; // Hue
if (hsl.x < 0.0)
hsl.x += 1.0; // Hue
else if (hsl.x > 1.0)
hsl.x -= 1.0; // Hue
}
return hsl;
}
float HueToRGB(float f1, float f2, float hue)
{
if (hue < 0.0)
hue += 1.0;
else if (hue > 1.0)
hue -= 1.0;
float res;
if ((6.0 * hue) < 1.0)
res = f1 + (f2 - f1) * 6.0 * hue;
else if ((2.0 * hue) < 1.0)
res = f2;
else if ((3.0 * hue) < 2.0)
res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;
else
res = f1;
return res;
}
float3 HSLToRGB(float3 hsl)
{
float3 rgb;
if (hsl.y == 0.0)
rgb = float3(hsl.z,hsl.z,hsl.z); // Luminance
else
{
float f2;
if (hsl.z < 0.5)
f2 = hsl.z * (1.0 + hsl.y);
else
f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);
float f1 = 2.0 * hsl.z - f2;
rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0));
rgb.g = HueToRGB(f1, f2, hsl.x);
rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0));
}
return rgb;
}
/*
** Contrast, saturation, brightness
** Code of this function is from TGM's shader pack
** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057
*/
// For all settings: 1.0 = 100% 0.5=50% 1.5 = 150%
float3 ContrastSaturationBrightness(float3 color, float brt, float sat, float con)
{
// Increase or decrease theese values to adjust r, g and b color channels seperately
const float AvgLumR = 0.5;
const float AvgLumG = 0.5;
const float AvgLumB = 0.5;
const float3 LumCoeff = float3(0.2125, 0.7154, 0.0721);
float3 AvgLumin = float3(AvgLumR, AvgLumG, AvgLumB);
float3 brtColor = color * brt;
float d = dot(brtColor, LumCoeff);
float3 intensity = float3(d,d,d);
float3 satColor = lerp(intensity, brtColor, sat);
float3 conColor = lerp(AvgLumin, satColor, con);
return conColor;
}
float3 BlendLighten(float3 baseColor, float3 blendColor){
return max(blendColor, baseColor);
}
float3 BlendLighten(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendLighten(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendDarken(float3 baseColor, float3 blendColor){
return min(blendColor, baseColor);
}
float3 BlendDarken(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendDarken(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendLinearBurn(float3 baseColor, float3 blendColor){
return max(baseColor + blendColor - 1.0, 0.0);
}
float3 BlendLinearBurn(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendLinearBurn(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendLinearDodge(float3 baseColor, float3 blendColor){
return min(baseColor + blendColor, 1.0);
}
float3 BlendLinearDodge(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendLinearDodge(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendLinearLight(float3 baseColor, float3 blendColor){
return (blendColor < 0.5) ? BlendLinearBurn(baseColor, (2.0 * blendColor)) : BlendLinearDodge(baseColor, (2.0 * (blendColor - 0.5)));
}
float3 BlendLinearLight(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendLinearLight(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendScreen(float3 baseColor, float3 blendColor){
return (1.0 - ((1.0 - baseColor) * (1.0 - blendColor)));
}
float3 BlendScreen(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendScreen(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendOverLay(float3 baseColor, float3 blendColor){
return (baseColor < 0.5) ? (2.0 * baseColor * blendColor) : (1.0 - 2.0 * (1.0 - baseColor) * (1.0 - blendColor));
}
float3 BlendOverLay(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendOverLay(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendSoftLight(float3 baseColor, float3 blendColor){
return ((blendColor < 0.5) ? (2.0 * baseColor * blendColor + baseColor * baseColor * (1.0 - 2.0 * blendColor)) : (sqrt(baseColor) * (2.0 * blendColor - 1.0) + 2.0 * baseColor * (1.0 - blendColor)));
}
float3 BlendSoftLight(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendSoftLight(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendColorDodge(float3 baseColor, float3 blendColor){
return (blendColor == 1.0) ? blendColor : min(baseColor / (1.0 - blendColor), 1.0);
}
float3 BlendColorDodge(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendColorDodge(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendColorBurn(float3 baseColor, float3 blendColor){
return ((blendColor == 0.0) ? blendColor : max((1.0 - ((1.0 - baseColor) / blendColor)), 0.0));
}
float3 BlendColorBurn(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendColorBurn(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendVividLight(float3 baseColor, float3 blendColor){
return ((blendColor < 0.5) ? BlendColorBurn(baseColor, (2.0 * blendColor)) : BlendColorDodge(baseColor, (2.0 * (blendColor - 0.5))));
}
float3 BlendVividLight(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendVividLight(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendPinLight(float3 baseColor, float3 blendColor){
return (blendColor < 0.5) ? BlendDarken(baseColor, (2.0 * blendColor)) : BlendLighten(baseColor, (2.0 *(blendColor - 0.5)));
}
float3 BlendPinLight(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendPinLight(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendHardLerp(float3 baseColor, float3 blendColor){
return ((BlendVividLight(baseColor, blendColor) < 0.5) ? 0.0 : 1.0);
}
float3 BlendHardLerp(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendHardLerp(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendReflect(float3 baseColor, float3 blendColor){
return ((blendColor == 1.0) ? blendColor : min(baseColor * baseColor / (1.0 - blendColor), 1.0));
}
float3 BlendReflect(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendReflect(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendNegation(float3 baseColor, float3 blendColor){
return (float3(1,1,1) - abs(float3(1,1,1) - baseColor - blendColor));
}
float3 BlendNegation(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendNegation(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendExclusion(float3 baseColor, float3 blendColor){
return (baseColor + blendColor - 2.0 * baseColor * blendColor);
}
float3 BlendExclusion(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendExclusion(baseColor, blendColor.rgb),blendColor.a);
}
float3 BlendPhoenix(float3 baseColor, float3 blendColor){
return (min(baseColor, blendColor) - max(baseColor, blendColor) + float3(1,1,1));
}
float3 BlendPhoenix(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendPhoenix(baseColor, blendColor.rgb),blendColor.a);
}
// Hue Blend mode creates the result color by combining the luminance and saturation of the baseColor color with the hue of the blendColor color.
float3 BlendHue(float3 baseColor, float3 blendColor)
{
float3 baseHSL = RGBToHSL(baseColor);
return HSLToRGB(float3(RGBToHSL(blendColor).r, baseHSL.g, baseHSL.b));
}
float3 BlendHue(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendHue(baseColor, blendColor.rgb),blendColor.a);
}
// Saturation Blend mode creates the result color by combining the luminance and hue of the base color with the saturation of the blendColor color.
float3 BlendSaturation(float3 baseColor, float3 blendColor)
{
float3 baseHSL = RGBToHSL(baseColor);
return HSLToRGB(float3(baseHSL.r, RGBToHSL(blendColor).g, baseHSL.b));
}
float3 BlendSaturation(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendSaturation(baseColor, blendColor.rgb),blendColor.a);
}
// Color Mode keeps the brightness of the base color and applies both the hue and saturation of the blendColor color.
float3 BlendColor(float3 baseColor, float3 blendColor)
{
float3 blendHSL = RGBToHSL(blendColor);
return HSLToRGB(float3(blendHSL.r, blendHSL.g, RGBToHSL(baseColor).b));
}
float3 BlendColor(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendColor(baseColor, blendColor.rgb),blendColor.a);
}
// Luminosity Blend mode creates the result color by combining the hue and saturation of the base color with the luminance of the blend color.
float3 BlendLuminosity(float3 baseColor, float3 blendColor)
{
float3 baseHSL = RGBToHSL(baseColor);
return HSLToRGB(float3(baseHSL.r, baseHSL.g, RGBToHSL(blendColor).b));
}
float3 BlendLuminosity(float3 baseColor, float4 blendColor){
return lerp(baseColor, BlendLuminosity(baseColor, blendColor.rgb),blendColor.a);
}
float3 HSLShift(half3 baseColor, half3 shift){
half3 hsl = RGBToHSL(baseColor);
hsl = hsl + shift.xyz;
hsl.yz = saturate(hsl.yz);
return HSLToRGB(hsl);
}
float3 HSLShift(float3 baseColor, float4 shift){
return lerp(baseColor, HSLShift(baseColor, shift.rgb),shift.a);
}
float3 HSVShift(half3 baseColor, half3 shift){
half3 hsv = rgb2hsv(baseColor);
hsv = hsv + shift.xyz;
hsv.yz = saturate(hsv.yz);
return hsv2rgb(hsv);
}
float3 HSVShift(float3 baseColor, float4 shift){
return lerp(baseColor, HSVShift(baseColor, shift.rgb),shift.a);
}
/*
** Gamma correction
** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/
*/
#define GammaCorrection(color, gamma) pow(color, 1.0 / gamma)
/*
** Levels control (input (+gamma), output)
** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/
*/
#define LevelsControlInputRange(color, minInput, maxInput) min(max(color - float3(minInput,minInput,minInput), float3(0,0,0)) / (float3(maxInput,maxInput,maxInput) - float3(minInput,minInput,minInput)), float3(1,1,1))
#define LevelsControlInput(color, minInput, gamma, maxInput) GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma)
#define LevelsControlOutputRange(color, minOutput, maxOutput) lerp(float3(minOutput,minOutput,minOutput), float3(maxOutput,maxOutput,maxOutput), color)
#define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput) LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput)
#endif
#ifndef RANDOM_INCLUDED
#define RANDOM_INCLUDED
float rand( float2 co ){
return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
}
float3 rand3( float2 seed ){
float t = sin(seed.x + seed.y * 1e3);
return float3(frac(t*1e4), frac(t*1e6), frac(t*1e5));
}
float srand(float2 v, float s){
v *= s;
float2 p = frac(v);
v = floor(v);
float
r00 = rand(v),
r01 = rand(v + float2(0,1)),
r10 = rand(v + float2(1,0)),
r11 = rand(v + float2(1,1));
return lerp(lerp(r00, r10, p.x), lerp(r01, r11, p.x), p.y);
}
float3 srand3(float2 v, float s){
v *= s;
float2 p = frac(v);
v = floor(v);
float3
r00 = rand3(v),
r01 = rand3(v + float2(0,1)),
r10 = rand3(v + float2(1,0)),
r11 = rand3(v + float2(1,1));
return lerp(lerp(r00, r10, p.x), lerp(r01, r11, p.x), p.y);
}
#endif // RANDOM_INCLUDED
#ifndef TRANSFORM_INCLUDED
#define TRANSFORM_INCLUDED
// calculating formula from
// http://www.cg.info.hiroshima-cu.ac.jp/~miyazaki/knowledge/tech07.html
float3 rotateX(float3 v, float angle){
float s,c;
sincos(angle, s, c);
float4x4 rot = float4x4(
1, 0, 0, 0,
0, c,-s, 0,
0, s, c, 0,
0, 0, 0, 1
);
return mul(rot, float4(v,1)).xyz;
}
float3 rotateX(float3 v, float angle, float3 center){
v -= center;
v = rotateX(v, angle);
v += center;
return v;
}
float3 rotateY(float3 v, float angle){
float s,c;
sincos(angle, s, c);
float4x4 rot = float4x4(
c, 0, s, 0,
0, 1, 0, 0,
-s, 0, c, 0,
0, 0, 0, 1
);
return mul(rot, float4(v,1)).xyz;
}
float3 rotateY(float3 v, float angle, float3 center){
v -= center;
v = rotateY(v, angle);
v += center;
return v;
}
float3 rotateZ(float3 v, float angle){
float s,c;
sincos(angle, s, c);
float4x4 rot = float4x4(
c,-s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
return mul(rot, float4(v,1)).xyz;
}
float3 rotateZ(float3 v, float angle, float3 center){
v -= center;
v = rotateZ(v, angle);
v += center;
return v;
}
float3 rotate(float3 v, float3 axis, float angle){
float s,c;
sincos(angle, s, c);
float
nx = axis.x,
ny = axis.y,
nz = axis.z;
float4x4 rot = float4x4(
nx*nx*(1-c)+c, nx*ny*(1-c)-nz*s, nz*nx*(1-c)+ny*s, 0,
nx*ny*(1-c)+nz*s, ny*ny*(1-c)+c, ny*nz*(1-c)-nx*s, 0,
nz*nx*(1-c)-ny*s, ny*nz*(1-c)+nx*s, nz*nz*(1-c)+c, 0,
0,0,0,1
);
return mul(rot, float4(v,1)).xyz;
}
float3 rotate(float3 v, float3 axis, float angle, float3 center){
v -= center;
v = rotate(v, axis, angle);
v += center;
return v;
}
float2 rotate2D(float2 v, float angle){
float s,c;
sincos(angle, s, c);
float3x3 rot = float3x3(
c,-s, 0,
s, c, 0,
0, 0, 1
);
return mul(rot, float3(v,1)).xy;
}
float2 rotate2D(float2 v, float angle, float2 center){
v -= center;
v = rotate2D(v, angle);
v += center;
return v;
}
#endif // TRANSFORM_INCLUDED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment