Matcap Rim Lighting for 3ds max viewport shading.
Ver.0.5
Ver.0.85
This shader support for 3dsmax direcX driver and opengl view port with Nitro view port driver DX9 only.
//////////////////////////--Based Shader--/////////////////////////////
// Shader by Charles Hollemeersch http://charles.hollemeersch.net ///
// Edited: 12/13/2009 - Leonardo Covarrubias - For use in Maya ///
// Public domain. ///
///////////////////////////////////////////////////////////////////////
//////////////////////////--AddOn Shader--/////////////////////////////
// Shader by JP.Lee http://leegoonz.blogspot.com ////////////////////
// Edited: 03/05/2014 - Lee JungPyo - For use in 3D studio max ///
// Thanks Charles Hollemeersch with Leonardo Covarrubias ///
//////////////////////////--Customized Author by Leegoonz--////////////
string ParamID = "0x003";
////////////////////////
// Matrices
////////////////////////
float4x4 WorldView : WORLDVIEW;
float4x4 WorldViewProjection : WORLDVIEWPROJ;
////////////////////////
// User interface
////////////////////////
float BumpHeight <
string UIName = "Bumpmap Strength";
string UIWidget = "slider";
float UIMin = 0.1;
float UIMax = 3.0;
float UIStep = 0.01;
> = 1.0;
float Alpha <
string UIName = "Global Opacity";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.01;
> = 1.0;
bool flipGreen
<
string UIName = "Invert Normal Map Green Channel?";
> = false;
texture diffuseMap : Diffuse <
string UIName = "Diffuse Map";
string ResourceName = "";
string ResourceType = "2D";
>;
texture normalMap : Normal <
string UIName = "Normal Map";
string ResourceName = "";
string ResourceType = "2D";
>;
texture litSphereMap : LitSphere <
string UIName = "LitSphere Map";
string ResourceName = "./matball01.jpg";
string ResourceType = "2D";
>;
texture litSphereRim : LitSphere <
string UIName = "Rim Map";
string ResourceName = "./matball01.jpg";
string ResourceType = "2D";
>;
float matCapStrengthUI<
string UIName = "LightCap Power";
string UIWidget = "slider";
float UIMin = 0.1;
float UIMax = 5.0;
float UIStep = 0.01;
> = 1.0;
float LightCapRimStrengthUI<
string UIName = "Rim Power";
string UIWidget = "slider";
float UIMin = 0.1;
float UIMax = 3.0;
float UIStep = 0.01;
> = 1.0;
float4 RimColor : COLOR <
string UIName = "Rim Color";
string UIWidget = "none";
> = { 0.5f, 0.5f, 0.5f , 1.0f};
float contrastMatRimPower<
string UIName = "LightCap Contrast";
string UIWidget = "slider";
float UIMin = -3.0;
float UIMax = 3.0;
float UIStep = 0.01;
> = 0.25;
////////////////////////
// Samplers
////////////////////////
sampler DiffuseSampler = sampler_state {
Texture = ;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
SRGBTexture = FALSE;
};
sampler NormalSampler = sampler_state {
Texture = ;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler LightSampler = sampler_state {
Texture = ;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
//SRGBTexture = FALSE;
};
sampler LitSphereRimSampler = sampler_state {
Texture = ;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
////////////////////////
// Exchange structs
////////////////////////
struct a2v
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
float3 Tangent : TANGENT;
float3 Binormal : BINORMAL;
float3 Normal : NORMAL;
};
struct v2f
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
float3 TexCoord1 : TEXCOORD1;
float3 TexCoord2 : TEXCOORD2;
float3 TexCoord3 : TEXCOORD3;
};
////////////////////////
// Vertex shader
////////////////////////
v2f BumpReflectVS(a2v IN)
{
v2f OUT;
// Pos to NDC
OUT.Position = mul(float4(IN.Position.xyz,1), WorldViewProjection);
// Texcoords (for normal map)
OUT.TexCoord.xy = IN.TexCoord;
// Tangent space vectors
float3 vtan = IN.Tangent;
float3 vbinorm = IN.Binormal;
float3 vnorm = IN.Normal;
OUT.TexCoord1.xyz = mul(vtan,WorldView);
OUT.TexCoord2.xyz = mul(vbinorm,WorldView);
OUT.TexCoord3.xyz = mul(vnorm,WorldView);
return OUT;
}
////////////////////////
// Pixel shader
////////////////////////
float4 BumpReflectPS(v2f IN) : COLOR {
float4 outColor;
//float4 diffuseTex = tex2D(DiffuseSampler, IN.TexCoord.xy);
//float4 diffuseTex = pow(tex2D(DiffuseSampler, IN.TexCoord.xy), 2.2);
float4 diffuseTex = tex2D(DiffuseSampler, IN.TexCoord.xy);
//diffuseTex = diffuseTex * diffuseTex;
//float3 texNormal = tex2D(NormalSampler, IN.TexCoord.xy).xyz; float3 texNormal = tex2D(NormalSampler, IN.TexCoord.xy).xyz; if(flipGreen)texNormal.g = 1-texNormal.g; texNormal.rgb = texNormal.rgb * 2.0 - 1.0; // Fixes normals if no normal map texture is supplied if ( dot(texNormal,texNormal) > 2.0 ) { texNormal = float4(0,0,1,1); } // The normalizes can probably go away... but meh... //float3 T = pow((normalize(IN.TexCoord1.xyz) * BumpHeight) , BumpHeight); float3 T = normalize(IN.TexCoord1.xyz) * BumpHeight; //float3 B = pow((normalize(IN.TexCoord2.xyz) * BumpHeight) , BumpHeight); float3 B = normalize(IN.TexCoord2.xyz) * BumpHeight; float3 N = normalize(IN.TexCoord3.xyz); // Put in world space and renormalize (after scaling) float3 worldNorm = normalize(N + texNormal.y * T + texNormal.x * B); // Swap it around a bit... worldNorm.y = - worldNorm.y; float4 matCap; //matCap = pow(tex2D(LightSampler, worldNorm.xy * 0.5 + 0.5) , 2.2); matCap = tex2D(LightSampler, worldNorm.xy * 0.5 + 0.5); matCap = pow(matCap, 2.2); //matCap = matCap * matCap; float4 LightCapRimTex; LightCapRimTex = tex2D(LitSphereRimSampler, worldNorm.xy * 0.5 + 0.5); LightCapRimTex *= RimColor; float matCapStrength; matCapStrength = matCapStrengthUI; float LightCapRimStrength; LightCapRimStrength = LightCapRimStrengthUI; //outColor = (((matCap * 2.0 - 1) * 0.5 + 0.5 * matCapStrength) + (LightCapRimTex * LightCapRimStrengthUI)) * diffuseTex; float contrastMatRim; contrastMatRim = contrastMatRimPower; //outColor = ((pow(matCap, 1 / 2.2) * 0.5 + contrastMatRim * matCapStrength) + (LightCapRimTex * LightCapRimStrengthUI)) * pow(diffuseTex, 1 / 2.2); //outColor = (pow(diffuseTex, 1 / 2.2)) * (matCap * 0.5 + contrastMatRim * matCapStrength) + (LightCapRimTex * LightCapRimStrengthUI); //outColor = (pow(diffuseTex, 1 / 2.2)) * (matCap * matCapStrength + contrastMatRim) + (LightCapRimTex * LightCapRimStrengthUI); outColor = diffuseTex * (matCap * matCapStrength + contrastMatRim) + (LightCapRimTex * LightCapRimStrengthUI); //outColor = sqrt(outColor); outColor = pow(outColor , 0.45454545); matCap.a *= Alpha; outColor.a = matCap.a; return outColor; } //////////////////////// // Pixel shader //////////////////////// technique Matballz_AddOn_JPLee < string Script = "Pass=p0;"; > /*not sure what these annotations do just copy pasted them from somewhere... */ { pass p0 < string Script = "Draw=geometry;"; > { SRGBWRITEENABLE = FALSE; VertexShader = compile vs_2_0 BumpReflectVS(); PixelShader = compile ps_2_0 BumpReflectPS(); } }diffuseTex = pow(diffuseTex , 2.2);
Input gamma correction formula ::
pow(x , 2.2) = X ^ 2.2
X ^ 2.2 를 풀어 쓰면

이걸 데카르트 좌표계로 보면.
return gamma correction formula ::
pow(x , 1/2.2) = X ^ 0.45454545
Game Developer Leegoon copyright all right reserved since 2010.





감마 보정 추가.
ReplyDelete멧켑 텍스처 병합 콘트라스트 변수 추가.
ReplyDeleteLightCap contrast formula change.
ReplyDeleteRemove gamma correction for matcap.