'전체 글'에 해당되는 글 183건

  1. 2012.07.10 HLSL 함수
  2. 2012.06.25 Blending
  3. 2012.06.08 Blending modes..참고 자료
TA/Unity2012. 7. 10. 09:29

http://chulin28ho.egloos.com/5106486

 

[펌]HLSL 함수 by 김윤정

명령어 모음

원문 : http://blog.stnzone.com/darkman/entry/HLSL-함수

abs value abs(value a) 절대치 (성분마다).
acos acos(x) x 의 각 성분의 역코사인을 돌려준다. 각 성분은,[-1, 1] 의 범위로 한다.
all all(x) x 의 모든 성분이 0 이외의 값인지 아닌지를 테스트한다.
any any(x) x 의 몇개의 성분이 0 이외의 값인지 아닌지를 테스트한다.
asin asin(x) x 의 각 성분의 역정현을 돌려준다. 각 성분은,[-pi/2, pi/2] 의 범위로 한다.
atan atan(x) x 의 각 성분의 역탄젠트를 돌려준다. 반환값은,[-pi/2, pi/2] 의 범위이다.
atan2 atan2(y, x) y/x 의 역탄젠트를 돌려준다. y 와 x 의 부호를 사용해 [-pi, pi] 의 범위에 있는 반환값의 상한을 판단한다. atan2 는, x 가 0 으로 동일하고, y 가 0 으로 동일하지 않은 경우에서도, 원점 이외의 각 점에 대해서 충분히 정의되고 있다.
ceil ceil(x) x 이상의 최소의 정수를 돌려준다.
clamp clamp(x, min, max) x 를 [min, max] 의 범위에 제한한다.
clip clip(x) x 의 몇개의 성분이 0 보다 작은 경우, 현재의 픽셀을 파기한다. x 의 각 성분이 면으로부터의 거리를 나타내는 경우, 이 함수를 사용해, 클립면을 시뮬레이션 한다.
cos cos(x) x 의 코사인을 돌려준다.
cosh cosh(x) x 의 쌍곡코사인을 돌려준다.
cross cross(a, b) 2 개의 3D 벡터 a 와 b 의 외적을 돌려준다.
D3DCOLORtoUBYTE4 D3DCOLORtoUBYTE4(x) 4D 벡터 x 의 성분을 교체 및 스케일링 해, 일부 하드웨어에 있는 UBYTE4 지원의 부족을 보정한다.
ddx ddx(x) 스크린 공간의 x 좌표에 대해, x 의 편미분을 돌려준다.
ddy ddy(x) 스크린 공간의 y 좌표에 대해, x 의 편미분을 돌려준다.
degrees degrees(x) x 를 라디안 단위로부터 도수로 변환한다.
determinant determinant(m) 서방 행렬 m 의 행렬식을 돌려준다.
distance distance(a, b) 2 개의 점 a 와 b 간의 거리를 돌려준다.
dot dot(a, b) 2 개의 벡터 a 와 b 의 내적을 돌려준다.
exp exp(x) e 를 바닥으로 하는 지수 ex 를 돌려준다.
exp2 value exp2(value a) 2 를 바닥으로 하는 지수 (성분마다).
faceforward faceforward(n, i, ng) -n * sign(dot(i, ng))를 돌려준다.
floor floor(x) x 이하의 최대의 정수를 돌려준다.
fmod fmod(a, b) a = i * b + f 가 되는 것 같은, a / b 의 부동 소수점수(실수)의 잉여 f 를 돌려준다. 여기서, i 는 정수, f 는 x 와 부호가 같아, 그 절대치는 b 의 절대치보다 작다.
frac frac(x) f 가 0 보다 크고, 1 보다 작은 값이 되는 것 같은, x 의 소수부 f 를 돌려준다.
frc value frc(value a) 소수부 (성분마다).
frexp frexp(x, out exp) x 의 가수와 지수를 돌려준다. frexp 는 가수를 돌려주어, 지수는 출력 인수 exp 에 저장 된다. x 가 0 의 경우, 함수는 가수와 지수의 양쪽 모두에 0 을 돌려준다.
fwidth fwidth(x) abs(ddx(x)) +abs(ddy(x))를 돌려준다.
isfinite isfinite(x) x 가 유한의 경우는 TRUE 를 돌려준다. 그 이외의 경우는 FALSE 를 돌려준다.
isinf isinf(x) x 가 +INF 나 -INF 의 경우는 TRUE 를 돌려준다. 그 이외의 경우는 FALSE 를 돌려준다.
isnan isnan(x) x 가 NAN 나 QNAN 의 경우는 TRUE 를 돌려준다. 그 이외의 경우는 FALSE 를 돌려준다.
ldexp ldexp(x, exp) x * 2exp 를 돌려준다.
len float len(value a) 벡터의 길이.
length length(v) 벡터 v 의 길이를 돌려준다.
lerp lerp(a, b, s) a + s(b - a)를 돌려준다. 이 함수는, s 가 0 의 경우는 a 를 돌려주어, 1 의 경우는 b 를 돌려주도록, a 와 b 의 사이를 선형 보간 한다.
lit lit(ndotl, ndoth, m) 조명의 벡터 (앰비언트, 디퓨즈, 스펙큐러, 1)를 돌려준다. 앰비언트 = 1; 디퓨즈 = (ndotl < 0) ? 0 : ndotl; 스펙큐러 = (ndotl < 0) || (ndoth < 0) ? 0 : (ndoth * m);
log log(x) x 의, 바닥이 e 의 자연대수를 돌려준다. x 가 부의 경우, 이 함수는 무한을 돌려준다. x 가 0 의 경우, +INF 를 돌려준다.
log10 log10(x) x 의, 바닥이 10 의 자연대수를 돌려준다. x 가 부의 경우, 이 함수는 무한을 돌려준다. x 가 0 의 경우, +INF 를 돌려준다.
log2 log2(x) x 의, 바닥이 2 의 자연대수를 돌려준다. x 가 부의 경우, 이 함수는 무한을 돌려준다. x 가 0 의 경우, +INF 를 돌려준다.
max max(a, b) a 와 b 의 큰 (분)편을 선택한다.
min min(a, b) a 와 b 가 작은 (분)편을 선택한다.
modf modf(x, out ip) 값 x 를, 각각이 x 와 같은 부호를 가진 소수부와 정수부로 나눈다. x 의 부호 첨부 소수부가 반환된다. 정수부는 출력 인수 ip 에 저장 된다.
mul mul(a, b) a 와 b 의 사이의 행렬 곱셈을 실행한다. a 가 벡터의 경우, 행 벡터로서 처리한다. b 가 벡터의 경우, 열로서 처리한다. 내부 넓이의 a 열과 b 행은 동일해야 한다. a 행 x b 열의 넓이를 얻을 수 있다.
noise noise(x) 처리되지 않다.
normalize normalize(v) 정규화된 벡터 v / length(v)를 돌려준다. v 의 길이가 0 의 경우, 결과는 무한이 된다.
pow pow(x, y) xy 를 돌려준다.
radians radians(x) x 를 도수로부터 라디안 단위로 변환한다.
reflect reflect(i, n) 입사 방향 i, 표면 법선 n 로 했을 경우의, v = i - 2 * dot(i, n) * n 에 의해 구할 수 있는, 반사 벡터 v 를 돌려준다.
refract refract(i, n, eta) 입사 방향 i, 표면 법선 n, 굴절 eta 의 상대 인덱스가 주어졌을 경우의, 굴절 벡터 v 를 돌려준다. i 와 n 의 사이의 입사각이 지정된 eta 보다 너무 크면 (0,0,0)를 돌려준다.
round round(x) x 를 가장 가까운 정수에 말다.
rsqrt rsqrt(x) 1 / sqrt(x)를 돌려준다.
saturate saturate(x) x 를 [0, 1] 의 범위에 제한한다.
sign sign(x) x 의 부호를 요구한다. x 가 0 보다 작은 경우는 -1, 0 으로 동일한 경우는 0, 0 보다 큰 경우는 1 을 돌려준다.
sin sin(x) x 의 정현을 돌려준다.
sincos sincos(x, out s, out c) x 의 정현과 코사인을 돌려준다. sin(x)는 출력 인수 s 에 저장 되어 cos(x)는 출력 인수 c 에 저장 된다.
sinh sinh(x) x 의 쌍곡정현을 돌려준다.
smoothstep smoothstep(min, max, x) x < min 의 경우는 0 을 돌려준다. x > max 의 경우는 1 을 돌려준다. x 가 [min, max] 의 범위내이면, 0 으로 1 의 사이의 매끄러운 에르미트 보간을 돌려준다.
sqrt value sqrt(value a) 제곱근 (성분마다).
step step(a, x) (x >= a) ? 1 : 0 을 돌려준다.
tan tan(x) x 의 탄젠트를 돌려준다.
tanh tanh(x) x 의 쌍곡탄젠트를 돌려준다.
tex1D tex1D(s, t) 1D 의 텍스처 참조. s 는 샘플러 또는 sampler1D 개체. t 는 스칼라-.
tex1D tex1D(s, t, ddx, ddy) 미분을 지정한, 1D 의 텍스처 참조. s 는 샘플러 또는 sampler1D 개체. t, ddx, ddy 는 스칼라-.

tex1Dproj tex1Dproj(s, t) 1D 의 투영 텍스처 참조. s 는 샘플러 또는 sampler1D 개체. t 는 4D 벡터. t 는, 참조가 실행되기 직전의 성분으로 나눗셈 된다.
tex1Dbias tex1Dbias(s, t) 1D 의 바이어스 텍스처 참조. s 는 샘플러 또는 sampler1D 개체. t 는 4D 벡터. 참조를 실행하기 전에, 밉레벨에 t.w 의 바이어스를 걸칠 수 있다.
tex2D tex2D(s, t) 2D 의 텍스처 참조. s 는 샘플러 또는 sampler2D 개체. t 는 2D 텍스처 좌표.
tex2D tex2D(s, t, ddx, ddy) 미분을 지정한, 2D 의 텍스처 참조. s 는 샘플러 또는 sampler2D 개체. t, ddx, ddy 는 2D 벡터.
tex2Dproj tex2Dproj(s, t) 2D 의 투영 텍스처 참조. s 는 샘플러 또는 sampler2D 개체. t 는 4D 벡터. t 는, 참조가 실행되기 직전의 성분으로 나눗셈 된다.
tex2Dbias tex2Dbias(s, t) 2D 의 바이어스 텍스처 참조. s 는 샘플러 또는 sampler2D 개체. t 는 4D 벡터. 참조를 실행하기 전에, 밉레벨에 t.w 의 바이어스를 걸칠 수 있다.
tex3D tex3D(s, t) 3D 의 볼륨 텍스처 참조. s 는 샘플러 또는 sampler3D 개체. t 는 3D 텍스처 좌표.
tex3D tex3D(s, t, ddx, ddy) 미분을 지정한, 3D 의 볼륨 텍스처 참조. s 는 샘플러 또는 sampler3D 개체. t, ddx, ddy 는 3D 벡터.
tex3Dproj tex3Dproj(s, t) 3D 의 투영 볼륨 텍스처 참조. s 는 샘플러 또는 sampler3D 개체. t 는 4D 벡터. t 는, 참조가 실행되기 직전의 성분으로 나눗셈 된다.
tex3Dbias tex3Dbias(s, t) 3D 의 바이어스 텍스처 참조. s 는 샘플러 또는 sampler3D 개체. t 는 4D 벡터. 참조를 실행하기 전에, 밉레벨에 t.w 의 바이어스를 걸칠 수 있다.
texCUBE texCUBE(s, t) 3D 의 큐브 텍스처 참조. s 는 샘플러 또는 samplerCUBE 개체. t 는 3D 텍스처 좌표.
texCUBE texCUBE(s, t, ddx, ddy) 미분을 지정한, 3D 의 큐브 텍스처 참조. s 는 샘플러 또는 samplerCUBE 개체. t, ddx, ddy 는 3D 벡터.
texCUBEproj texCUBEproj(s, t) 3D 투영의 큐브 텍스처 참조. s 는 샘플러 또는 samplerCUBE 개체. t 는 4D 벡터. t 는, 참조가 실행되기 직전의 성분으로 나눗셈 된다.
texCUBEbias texCUBEbias(s, t) 3D 의 바이어스 큐브 텍스처 참조. s 는 샘플러 또는 samplerCUBE 개체. t 는 4D 벡터. 참조를 실행하기 전에, 밉레벨에 t.w 의 바이어스를 걸칠 수 있다.
transpose transpose(m) 행렬 m 의 전치행렬을 돌려준다. 입력의 넓이가 m 행 x m 열의 경우, 결과는 넓이 m 열 x m 행이 된다.

'TA > Unity' 카테고리의 다른 글

유니티 C# 공부 모음  (0) 2013.05.07
Texture Matrix in Fixed Function  (0) 2012.07.18
Blending modes..참고 자료  (0) 2012.06.08
기초적인 조명쉐이더  (0) 2012.06.08
정 반사광  (0) 2012.06.08
Posted by 프리랜서 디자이너

ShaderLab syntax: Blending

Blending은 투명한 물체를 만들기 위해 사용됩니다.

그래픽들이 표현될 때 모든 쉐이더가 실행되고 모든 텍스쳐가 적용되어진 후에 픽셀은 스크린에 쓰여집니다. 그들이 이미 있는 것과 어떻게 결합되는지는Blend 명령에 의해 조절됩니다.

Syntax

Blend Off
블렌딩 기능 취소
Blend SrcFactor DstFactor
블렌딩을 구성 & 활성화. 생성된 색상은 SrcFactor를 곱합니다. 스크린에 이미 있던 색상은 DstFactor 에 의해 곱해지고 두 개는 함께 더해집니다.

Properties

모든 다음의 속성들은SrcFactor와DstFactor 둘 다를 위해서 유효합니다. Source 는 계산되어지는 색상을 의미하고 Destination 는 이미 스크린에 있는 색상을 의미합니다.

One

1의 값 – 소스 또는 데스티네이션 색상이 온전히 오게 합니다.

 

Zero

0의 값 – 소스 또는데스티네이션 값을 삭제하게 합니다.

 

SrcColor

이 스테이지의 값은 소스 색상 값에 의해 곱해집니다.

 

SrcAlpha

이 스테이지의 값은 소스 알파 값에 의해 곱해집니다.

 

DstColor

이 스테이지의 값은 프레임 버퍼 소스 색상 값에 의해 곱해집니다.

 

DstAlpha

이 스테이지의 값은 프레임 버퍼 소스 알파 값에 의해 곱해집니다.

 

OneMinusSrcColor

이 스테이지의 값은 (1 – 소스 색상)에 의해 곱해집니다.

 

OneMinusSrcAlpha

이 스테이지의 값은 (1 – 소스 알파)에 의해 곱해집니다.

 

OneMinusDstColor

이 스테이지의 값은 (1 – 데스티네이션 색상)에 의해 곱해집니다.

 

OneMinusDstAlpha

이 스테이지의 값은 (1 – 데스티네이션 알파)에 의해 곱해집니다.

 

Details

아래는 가장 흔한 블렌드 타입입니다:

Blend SrcAlpha OneMinusSrcAlpha     // Alpha blending
Blend One One                       // Additive
Blend One OneMinusDstColor          // Soft Additive
Blend DstColor Zero                 // Multiplicative
Blend DstColor SrcColor             // 2x Multiplicative

Example

이것은 스크린에 이미 무엇이 있던 하나의 텍스쳐를 추가하는 쉐이더의 작은 예입니다:

Shader "Simple Additive" {
    Properties {
        _MainTex ("Texture to blend", 2D) = "black" {}
    }
    SubShader {
        Tags { "Queue" = "Transparent" }
        Pass {
            Blend One One
            SetTexture [_MainTex] { combine texture }
        }
    }
}

'TA > Unity Shader 레퍼런스' 카테고리의 다른 글

Performance Tips when Writing Shaders  (0) 2012.07.12
Writing vertex and fragment shaders  (0) 2012.07.12
Fallback  (0) 2012.05.21
RenderTech-VertexLit  (0) 2012.05.17
RenderTech-ForwardRendering  (0) 2012.05.17
Posted by 프리랜서 디자이너
TA/Unity2012. 6. 8. 14:12

http://mouaif.wordpress.com/2009/01/05/photoshop-math-with-glsl-shaders/

 

 

PhotoshopMathFP.glsl

Photoshop math with GLSL shaders

January 5, 2009

I usualy play with Photoshop to try post-processing effects on photos or game screenshots, it’s a lot faster than coding directly anything in shaders, but at the end I wanted to see my effects running in real-time. So I adapted a big part of the C-like code from this famous Photoshop blending mode math page + missing blending modes to GLSL (and now HLSL!) code and I added a few other useful things from Photoshop, such as Hue/Saturation/Luminance conversion, desaturation, contrast.

For example, I tried combining a few things in my Editor:

photoshopmath_tn

photoshopmath_editor_tn

Translating Photoshop operations on layers gives this kind of code:

uniform sampler2D Tex;
uniform sampler1D GradientMap;
uniform sampler1D GradientGround;

varying vec2 uv;

void main()
{

vec3 color = texture2D(Tex, uv).xyz;

// Split-tone
vec4 colorDesat = Desaturate(color, 1.0);
vec3 splitColor = texture1D(GradientMap, colorDesat.r).rgb;
vec3 pass1 = BlendColor(color, splitColor);

// Vertical gradient
vec4 verticalGradientColor = texture1D(GradientGround, uv.y);
vec3 pass2 = mix(pass1, BlendColor(pass1, verticalGradientColor.rgb), verticalGradientColor.a);

// Luminosity
vec3 pass3 = mix(pass2, BlendLuminosity(pass2, color + vec3(0.08)), 0.5);

// Linear light at 40%
vec3 pass4 = mix(pass3, BlendLinearLight(pass3, color), 0.4);

// Final
gl_FragColor = vec4(pass4, 1.0);

}

Here is the list of blending modes and functions I got:

Blending modes:

  • Normal
  • Lighten
  • Darken
  • Multiply
  • Average
  • Add
  • Substract
  • Difference
  • Negation
  • Exclusion
  • Screen
  • Overlay
  • SoftLight
  • HardLight
  • ColorDodge
  • ColorBurn
  • LinearDodge
  • LinearBurn
  • LinearLight
  • VividLight
  • PinLight
  • HardMix
  • Reflect
  • Glow
  • Phoenix
  • Hue
  • Saturation
  • Color
  • Luminosity

Functions:

  • Desaturation
  • RGBToHSL (RGB to Hue/Saturation/Luminance)
  • HSLToRGB (Hue/Saturation/Luminance to RGB)
  • Contrast

 

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*
** Photoshop & misc math
** Blending modes, RGB/HSL/Contrast/Desaturate, levels control
**
** Romain Dura | Romz
** Blog: http://blog.mouaif.org
** Post: http://blog.mouaif.org/?p=94
*/


/*
** Desaturation
*/

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);
}


/*
** Hue, saturation, luminance
*/

vec3 RGBToHSL(vec3 color)
{
 vec3 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;
}

vec3 HSLToRGB(vec3 hsl)
{
 vec3 rgb;
 
 if (hsl.y == 0.0)
  rgb = vec3(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%
vec3 ContrastSaturationBrightness(vec3 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 vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721);
 
 vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB);
 vec3 brtColor = color * brt;
 vec3 intensity = vec3(dot(brtColor, LumCoeff));
 vec3 satColor = mix(intensity, brtColor, sat);
 vec3 conColor = mix(AvgLumin, satColor, con);
 return conColor;
}


/*
** Float blending modes
** Adapted from here: http://www.nathanm.com/photoshop-blending-math/
** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results
*/

#define BlendLinearDodgef    BlendAddf
#define BlendLinearBurnf    BlendSubstractf
#define BlendAddf(base, blend)   min(base + blend, 1.0)
#define BlendSubstractf(base, blend)  max(base + blend - 1.0, 0.0)
#define BlendLightenf(base, blend)   max(blend, base)
#define BlendDarkenf(base, blend)   min(blend, base)
#define BlendLinearLightf(base, blend)  (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5))))
#define BlendScreenf(base, blend)   (1.0 - ((1.0 - base) * (1.0 - blend)))
#define BlendOverlayf(base, blend)  (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)))
#define BlendSoftLightf(base, blend)  ((blend < 0.5) ? (2.0 * base * blend + base * base * (1.0 - 2.0 * blend)) : (sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend)))
#define BlendColorDodgef(base, blend)  ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0))
#define BlendColorBurnf(base, blend)  ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0))
#define BlendVividLightf(base, blend)  ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5))))
#define BlendPinLightf(base, blend)  ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5))))
#define BlendHardMixf(base, blend)  ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0)
#define BlendReflectf(base, blend)   ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0))


/*
** Vector3 blending modes
*/

// Component wise blending
#define Blend(base, blend, funcf)   vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b))

#define BlendNormal(base, blend)   (blend)
#define BlendLighten    BlendLightenf
#define BlendDarken    BlendDarkenf
#define BlendMultiply(base, blend)   (base * blend)
#define BlendAverage(base, blend)   ((base + blend) / 2.0)
#define BlendAdd(base, blend)   min(base + blend, vec3(1.0))
#define BlendSubstract(base, blend)  max(base + blend - vec3(1.0), vec3(0.0))
#define BlendDifference(base, blend)  abs(base - blend)
#define BlendNegation(base, blend)  (vec3(1.0) - abs(vec3(1.0) - base - blend))
#define BlendExclusion(base, blend)  (base + blend - 2.0 * base * blend)
#define BlendScreen(base, blend)   Blend(base, blend, BlendScreenf)
#define BlendOverlay(base, blend)   Blend(base, blend, BlendOverlayf)
#define BlendSoftLight(base, blend)  Blend(base, blend, BlendSoftLightf)
#define BlendHardLight(base, blend)  BlendOverlay(blend, base)
#define BlendColorDodge(base, blend)  Blend(base, blend, BlendColorDodgef)
#define BlendColorBurn(base, blend)  Blend(base, blend, BlendColorBurnf)
#define BlendLinearDodge   BlendAdd
#define BlendLinearBurn   BlendSubstract
// Linear Light is another contrast-increasing mode
// If the blend color is darker than midgray, Linear Light darkens the image by decreasing the brightness. If the blend color is lighter than midgray, the result is a brighter image due to increased brightness.
#define BlendLinearLight(base, blend)  Blend(base, blend, BlendLinearLightf)
#define BlendVividLight(base, blend)  Blend(base, blend, BlendVividLightf)
#define BlendPinLight(base, blend)   Blend(base, blend, BlendPinLightf)
#define BlendHardMix(base, blend)   Blend(base, blend, BlendHardMixf)
#define BlendReflect(base, blend)   Blend(base, blend, BlendReflectf)
#define BlendGlow(base, blend)   BlendReflect(blend, base)
#define BlendPhoenix(base, blend)   (min(base, blend) - max(base, blend) + vec3(1.0))
#define BlendOpacity(base, blend, F, O)  (F(base, blend) * O + blend * (1.0 - O))


// Hue Blend mode creates the result color by combining the luminance and saturation of the base color with the hue of the blend color.
vec3 BlendHue(vec3 base, vec3 blend)
{
 vec3 baseHSL = RGBToHSL(base);
 return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b));
}

// Saturation Blend mode creates the result color by combining the luminance and hue of the base color with the saturation of the blend color.
vec3 BlendSaturation(vec3 base, vec3 blend)
{
 vec3 baseHSL = RGBToHSL(base);
 return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b));
}

// Color Mode keeps the brightness of the base color and applies both the hue and saturation of the blend color.
vec3 BlendColor(vec3 base, vec3 blend)
{
 vec3 blendHSL = RGBToHSL(blend);
 return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b));
}

// Luminosity Blend mode creates the result color by combining the hue and saturation of the base color with the luminance of the blend color.
vec3 BlendLuminosity(vec3 base, vec3 blend)
{
 vec3 baseHSL = RGBToHSL(base);
 return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b));
}


/*
** 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 - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0))
#define LevelsControlInput(color, minInput, gamma, maxInput)    GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma)
#define LevelsControlOutputRange(color, minOutput, maxOutput)    mix(vec3(minOutput), vec3(maxOutput), color)
#define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput)  LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput)

'TA > Unity' 카테고리의 다른 글

Texture Matrix in Fixed Function  (0) 2012.07.18
HLSL 함수  (0) 2012.07.10
기초적인 조명쉐이더  (0) 2012.06.08
정 반사광  (0) 2012.06.08
unity shader 3가지 방식 정리  (0) 2012.06.01
Posted by 프리랜서 디자이너