2009-04-19 18:07:22 +00:00
|
|
|
// 3D world rendering shader
|
|
|
|
// Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
|
|
|
|
// Vertex input data
|
|
|
|
struct VertexData
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
float3 pos : POSITION;
|
|
|
|
float4 color : COLOR0;
|
|
|
|
float2 uv : TEXCOORD0;
|
|
|
|
float3 normal : NORMAL; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Pixel input data
|
|
|
|
struct PixelData
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
float4 pos : POSITION;
|
|
|
|
float4 color : COLOR0;
|
|
|
|
float2 uv : TEXCOORD0;
|
2012-04-17 19:13:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//mxd
|
|
|
|
// Pixel input data for light pass
|
|
|
|
struct LitPixelData
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
float4 pos : POSITION;
|
|
|
|
float4 color : COLOR0;
|
|
|
|
float2 uv : TEXCOORD0;
|
|
|
|
float3 pos_w : TEXCOORD1; //mxd. pixel position in world space
|
|
|
|
float3 normal : TEXCOORD2; //mxd. normal
|
2009-04-19 18:07:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Modulation color
|
|
|
|
float4 modulatecolor;
|
|
|
|
|
|
|
|
// Highlight color
|
|
|
|
float4 highlightcolor;
|
|
|
|
|
|
|
|
// Matrix for final transformation
|
|
|
|
float4x4 worldviewproj;
|
|
|
|
|
2012-04-17 19:13:47 +00:00
|
|
|
//mxd
|
|
|
|
float4x4 world;
|
|
|
|
float4 vertexColor;
|
|
|
|
//light
|
|
|
|
float4 lightPosAndRadius;
|
2012-05-20 00:56:59 +00:00
|
|
|
float4 lightColor; //also used as fog color
|
|
|
|
//fog
|
|
|
|
float4 cameraPos; //w is set to fade factor (distance, at wich fog color completely overrides pixel color)
|
2012-04-17 19:13:47 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Texture input
|
|
|
|
texture texture1;
|
|
|
|
|
|
|
|
// Filter settings
|
|
|
|
dword minfiltersettings;
|
|
|
|
dword magfiltersettings;
|
2010-10-03 11:51:38 +00:00
|
|
|
dword mipfiltersettings;
|
|
|
|
float maxanisotropysetting;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Texture sampler settings
|
|
|
|
sampler2D texturesamp = sampler_state
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
Texture = <texture1>;
|
|
|
|
MagFilter = magfiltersettings;
|
|
|
|
MinFilter = minfiltersettings;
|
|
|
|
MipFilter = mipfiltersettings;
|
|
|
|
MipMapLodBias = 0.0f;
|
|
|
|
MaxAnisotropy = maxanisotropysetting;
|
2009-04-19 18:07:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Vertex shader
|
2014-12-03 23:15:26 +00:00
|
|
|
PixelData vs_main(VertexData vd)
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
PixelData pd;
|
|
|
|
// Fill pixel data input
|
|
|
|
pd.pos = mul(float4(vd.pos, 1.0f), worldviewproj);
|
|
|
|
pd.color = vd.color;
|
|
|
|
pd.uv = vd.uv;
|
|
|
|
// Return result
|
|
|
|
return pd;
|
2012-04-17 19:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//mxd. same as vs_main, but uses vertexColor var instead of actual vertex color. used in models rendering
|
2014-12-03 23:15:26 +00:00
|
|
|
PixelData vs_customvertexcolor(VertexData vd)
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
PixelData pd;
|
|
|
|
// Fill pixel data input
|
|
|
|
pd.pos = mul(float4(vd.pos, 1.0f), worldviewproj);
|
|
|
|
pd.color = vertexColor;
|
|
|
|
pd.color.a = 1.0f;
|
|
|
|
pd.uv = vd.uv;
|
|
|
|
// Return result
|
|
|
|
return pd;
|
2012-04-17 19:13:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
LitPixelData vs_customvertexcolor_fog(VertexData vd)
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
LitPixelData pd;
|
|
|
|
// Fill pixel data input
|
|
|
|
pd.pos = mul(float4(vd.pos, 1.0f), worldviewproj);
|
|
|
|
pd.pos_w = mul(float4(vd.pos, 1.0f), world);
|
|
|
|
pd.color = vertexColor;
|
|
|
|
pd.color.a = 1.0f;
|
|
|
|
pd.uv = vd.uv;
|
|
|
|
pd.normal = vd.normal;
|
|
|
|
// Return result
|
|
|
|
return pd;
|
2012-05-20 00:56:59 +00:00
|
|
|
}
|
|
|
|
|
2012-04-17 19:13:47 +00:00
|
|
|
//mxd. light pass vertex shader
|
2014-12-03 23:15:26 +00:00
|
|
|
LitPixelData vs_lightpass(VertexData vd)
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
LitPixelData pd;
|
|
|
|
pd.pos = mul(float4(vd.pos, 1.0f), worldviewproj);
|
|
|
|
//pd.pos_w = mul(vd.pos, (float3x3)world);
|
|
|
|
pd.pos_w = mul(float4(vd.pos, 1.0f), world);
|
|
|
|
pd.color = vd.color;
|
|
|
|
pd.uv = vd.uv;
|
|
|
|
pd.normal = vd.normal;
|
|
|
|
|
|
|
|
// Return result
|
|
|
|
return pd;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normal pixel shader
|
|
|
|
float4 ps_main(PixelData pd) : COLOR
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
float4 tcolor = tex2D(texturesamp, pd.uv);
|
|
|
|
return tcolor * pd.color * modulatecolor;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Full-bright pixel shader
|
|
|
|
float4 ps_fullbright(PixelData pd) : COLOR
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
float4 tcolor = tex2D(texturesamp, pd.uv);
|
|
|
|
tcolor.a *= pd.color.a;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
// Blend texture color and modulation color
|
|
|
|
return tcolor * modulatecolor;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normal pixel shader with highlight
|
|
|
|
float4 ps_main_highlight(PixelData pd) : COLOR
|
|
|
|
{
|
|
|
|
float4 tcolor = tex2D(texturesamp, pd.uv);
|
|
|
|
|
|
|
|
// Blend texture color, vertex color and modulation color
|
|
|
|
float4 ncolor = tcolor * pd.color * modulatecolor;
|
|
|
|
float4 hcolor = float4(highlightcolor.rgb, ncolor.a);
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
//return lerp(ncolor, hcolor, highlightcolor.a);
|
|
|
|
return float4(hcolor.rgb * highlightcolor.a + (ncolor.rgb - 0.4f * highlightcolor.a), ncolor.a + 0.25f); //tcolor.a
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Full-bright pixel shader with highlight
|
|
|
|
float4 ps_fullbright_highlight(PixelData pd) : COLOR
|
|
|
|
{
|
|
|
|
float4 tcolor = tex2D(texturesamp, pd.uv);
|
|
|
|
|
|
|
|
// Blend texture color and modulation color
|
|
|
|
float4 ncolor = tcolor * modulatecolor;
|
|
|
|
float4 hcolor = float4(highlightcolor.rgb, ncolor.a);
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
//return lerp(ncolor, hcolor, highlightcolor.a);
|
|
|
|
return float4(hcolor.rgb * highlightcolor.a + (ncolor.rgb - 0.4f * highlightcolor.a), ncolor.a + 0.25f); //tcolor.a
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
2012-05-20 00:56:59 +00:00
|
|
|
//mxd. This adds fog color to current pixel color
|
2014-12-03 23:15:26 +00:00
|
|
|
float4 getFogColor(LitPixelData pd, float4 color)
|
|
|
|
{
|
2012-05-20 00:56:59 +00:00
|
|
|
float fogdist = max(16.0f, distance(pd.pos_w, cameraPos.xyz));
|
|
|
|
float fogfactor = min(1.0f, fogdist / cameraPos.w);
|
|
|
|
//float fogfactor = exp2(cameraPos.w * fogdist);
|
|
|
|
if(fogfactor == 1.0f) //texture color completly replaced by fog color
|
2013-09-11 09:47:53 +00:00
|
|
|
return float4(lightColor.rgb, color.a);
|
2012-05-20 00:56:59 +00:00
|
|
|
return float4(lightColor.rgb * fogfactor + color.rgb * (1.0f - fogfactor), color.a);
|
|
|
|
}
|
|
|
|
|
|
|
|
//mxd. Shaders with fog calculation
|
|
|
|
// Normal pixel shader
|
2014-12-03 23:15:26 +00:00
|
|
|
float4 ps_main_fog(LitPixelData pd) : COLOR
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
float4 tcolor = tex2D(texturesamp, pd.uv);
|
|
|
|
return getFogColor(pd, tcolor * pd.color * modulatecolor);
|
2012-05-20 00:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normal pixel shader with highlight
|
2014-12-03 23:15:26 +00:00
|
|
|
float4 ps_main_highlight_fog(LitPixelData pd) : COLOR
|
|
|
|
{
|
2012-05-20 00:56:59 +00:00
|
|
|
float4 tcolor = tex2D(texturesamp, pd.uv);
|
|
|
|
|
|
|
|
// Blend texture color, vertex color and modulation color
|
|
|
|
float4 ncolor = tcolor * pd.color * modulatecolor;
|
|
|
|
float4 hcolor = float4(highlightcolor.rgb, ncolor.a);
|
2013-03-18 13:52:27 +00:00
|
|
|
return getFogColor(pd, float4(hcolor.rgb * highlightcolor.a + (ncolor.rgb - 0.4f * highlightcolor.a), ncolor.a + 0.25f)); //tcolor.a
|
2012-05-20 00:56:59 +00:00
|
|
|
}
|
|
|
|
|
2012-04-17 19:13:47 +00:00
|
|
|
//mxd: used to draw bounding boxes
|
2014-12-03 23:15:26 +00:00
|
|
|
float4 ps_constant_color(PixelData pd) : COLOR
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
return vertexColor;
|
2012-04-17 19:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//mxd. dynamic light pixel shader pass, dood!
|
2014-12-03 23:15:26 +00:00
|
|
|
float4 ps_lightpass(LitPixelData pd) : COLOR
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
//is face facing away from light source?
|
|
|
|
if(dot(pd.normal, (lightPosAndRadius.xyz - pd.pos_w)) < -0.1f) // (lightPosAndRadius.xyz - pd.pos_w) == direction from light to current pixel
|
|
|
|
clip(-1);
|
|
|
|
|
|
|
|
//is pixel in light range?
|
|
|
|
float dist = distance(pd.pos_w, lightPosAndRadius.xyz);
|
|
|
|
if(dist > lightPosAndRadius.w)
|
|
|
|
clip(-1);
|
|
|
|
|
|
|
|
//is pixel tranparent?
|
|
|
|
float4 tcolor = tex2D(texturesamp, pd.uv);
|
|
|
|
if(tcolor.a == 0.0f)
|
|
|
|
clip(-1);
|
|
|
|
|
|
|
|
//if it is - calculate color at current pixel
|
|
|
|
float4 lightColorMod = float4(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
|
|
lightColorMod.rgb = lightColor.rgb * max(lightPosAndRadius.w - dist, 0.0f) / lightPosAndRadius.w;
|
2014-12-03 23:15:26 +00:00
|
|
|
if(lightColorMod.r > 0.0f || lightColorMod.g > 0.0f || lightColorMod.b > 0.0f)
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
lightColorMod.rgb *= lightColor.a;
|
|
|
|
if(lightColor.a > 0.4f) //Normal, vavoom or negative light
|
|
|
|
return tcolor * lightColorMod;
|
|
|
|
return lightColorMod; //Additive light
|
|
|
|
}
|
|
|
|
clip(-1);
|
|
|
|
return lightColorMod; //should never get here
|
2012-04-17 19:13:47 +00:00
|
|
|
}
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Technique for shader model 2.0
|
2014-12-03 23:15:26 +00:00
|
|
|
technique SM20
|
|
|
|
{
|
2009-04-19 18:07:22 +00:00
|
|
|
// Normal
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p0
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_main();
|
|
|
|
PixelShader = compile ps_2_0 ps_main();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full brightness mode
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p1
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_main();
|
|
|
|
PixelShader = compile ps_2_0 ps_fullbright();
|
|
|
|
}
|
2012-04-17 19:13:47 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Normal with highlight
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p2
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_main();
|
|
|
|
PixelShader = compile ps_2_0 ps_main_highlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full brightness mode with highlight
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p3
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_main();
|
|
|
|
PixelShader = compile ps_2_0 ps_fullbright_highlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
//mxd. same as p0-p3, but using vertexColor variable
|
|
|
|
// Normal
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p4
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_customvertexcolor();
|
|
|
|
PixelShader = compile ps_2_0 ps_main();
|
|
|
|
}
|
|
|
|
|
|
|
|
pass p5 {} //mxd. need this only to maintain offset
|
2012-04-17 19:13:47 +00:00
|
|
|
|
|
|
|
// Normal with highlight
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p6
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_customvertexcolor();
|
|
|
|
PixelShader = compile ps_2_0 ps_main_highlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
pass p7 {} //mxd. need this only to maintain offset
|
|
|
|
|
|
|
|
//mxd. same as p0-p3, but with fog calculation
|
|
|
|
// Normal
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p8
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_lightpass();
|
|
|
|
PixelShader = compile ps_2_0 ps_main_fog();
|
|
|
|
}
|
|
|
|
|
|
|
|
pass p9 {} //mxd. need this only to maintain offset
|
2012-05-20 00:56:59 +00:00
|
|
|
|
|
|
|
// Normal with highlight
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p10
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_lightpass();
|
|
|
|
PixelShader = compile ps_2_0 ps_main_highlight_fog();
|
|
|
|
}
|
|
|
|
|
|
|
|
pass p11 {} //mxd. need this only to maintain offset
|
|
|
|
|
|
|
|
//mxd. same as p4-p7, but with fog calculation
|
|
|
|
// Normal
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p12
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_customvertexcolor_fog();
|
|
|
|
PixelShader = compile ps_2_0 ps_main_fog();
|
|
|
|
}
|
|
|
|
|
|
|
|
pass p13 {} //mxd. need this only to maintain offset
|
2012-05-20 00:56:59 +00:00
|
|
|
|
|
|
|
// Normal with highlight
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p14
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_customvertexcolor_fog();
|
|
|
|
PixelShader = compile ps_2_0 ps_main_highlight_fog();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pass p15 {} //mxd. need this only to maintain offset
|
|
|
|
|
|
|
|
//mxd. Just fills everything with vertexColor. Used in ThingCage rendering.
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p16
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_customvertexcolor();
|
|
|
|
PixelShader = compile ps_2_0 ps_constant_color();
|
|
|
|
}
|
|
|
|
|
|
|
|
//mxd. Light pass
|
2014-12-03 23:15:26 +00:00
|
|
|
pass p17
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
VertexShader = compile vs_2_0 vs_lightpass();
|
|
|
|
PixelShader = compile ps_2_0 ps_lightpass();
|
|
|
|
AlphaBlendEnable = true;
|
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|