= 0.8;
/************* TWEAKABLES **************/
float4x4 WorldIT : WorldInverseTranspose < string UIWidget="None"; >;
float4x4 WorldViewProj : WorldViewProjection < string UIWidget="None"; >;
float4x4 World : World < string UIWidget="None"; >;
float4x4 ViewI : ViewInverse < string UIWidget="None"; >;
float Timer : Time < string UIWidget="None"; >;
float TimeScale <
string UIWidget = "slider";
string UIName = "Speed";
float UIMin = 0.1;
float UIMax = 10;
float UIStep = .1;
> = 4.0f;
float Horizontal <
string UIWidget = "slider";
float UIMin = 0.001;
float UIMax = 10;
float UIStep = 0.01;
> = 0.5f;
float Vertical <
string UIWidget = "slider";
float UIMin = 0.001;
float UIMax = 10.0;
float UIStep = 0.1;
> = 0.5;
float3 LightPos : Position <
string Object = "PointLight";
string Space = "World";
> = {-10.0f, 10.0f, -10.0f};
float3 LightColor <
string UIName = "Lamp";
string UIWidget = "Color";
> = {1.0f, 1.0f, 1.0f};
float3 AmbiColor : Ambient <
string UIName = "Ambient Light";
string UIWidget = "Color";
> = {0.2f, 0.2f, 0.2f};
float3 SurfColor : DIFFUSE <
string UIName = "Surface";
string UIWidget = "Color";
> = {0.9f, 0.9f, 0.9f};
float SpecExpon : SpecularPower <
string UIWidget = "slider";
float UIMin = 1.0;
float UIMax = 128.0;
float UIStep = 1.0;
string UIName = "Specular Power";
> = 5.0;
texture colorTexture : DIFFUSE <
string ResourceName = "default_color.dds";
string UIName = "Color Texture (if used)";
string ResourceType = "2D";
>;
sampler2D colorSampler = sampler_state {
Texture = ;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = None;
};
/************* DATA STRUCTS **************/
/* data from application vertex buffer */
struct appdata {
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float4 Normal : NORMAL;
};
/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 HPosition : POSITION;
float4 TexCoord0 : TEXCOORD0;
float4 diffCol : COLOR0;
float4 specCol : COLOR1;
};
/*********** vertex shader ******/
vertexOutput MrWiggleVS(appdata IN) {
vertexOutput OUT;
float3 Nn = normalize(mul(IN.Normal, WorldIT).xyz);
float timeNow = Timer*TimeScale;
float4 Po = float4(IN.Position.xyz,1);
float iny = Po.y * Vertical + timeNow;
float wiggleX = sin(iny) * Horizontal;
float wiggleY = cos(iny) * Horizontal; // deriv
Nn.y = Nn.y + wiggleY;
Nn = normalize(Nn);
Po.x = Po.x + wiggleX;
OUT.HPosition = mul(Po, WorldViewProj);
float3 Pw = mul(Po, World).xyz;
float3 Ln = normalize(LightPos - Pw);
float ldn = dot(Ln,Nn);
float diffComp = max(0,ldn);
float3 diffContrib = SurfColor * ( diffComp * LightColor + AmbiColor);
OUT.diffCol = float4(diffContrib,1);
OUT.TexCoord0 = IN.UV;
float3 Vn = normalize(ViewI[3].xyz - Pw);
float3 Hn = normalize(Vn + Ln);
float hdn = pow(max(0,dot(Hn,Nn)),SpecExpon);
OUT.specCol = float4(hdn * LightColor,1);
return OUT;
}
/********* pixel shader ********/
float4 MrWigglePS_t(vertexOutput IN) : COLOR {
float4 result = IN.diffCol * tex2D(colorSampler, IN.TexCoord0) + IN.specCol;
return result;
}
/*************/
technique Untextured <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexShader = compile vs_1_1 MrWiggleVS();
ZEnable = true;
ZWriteEnable = true;
CullMode = None;
// Don't need a pixel shader for a super-simple surface
SpecularEnable = true;
ColorArg1[ 0 ] = Diffuse;
ColorOp[ 0 ] = SelectArg1;
ColorArg2[ 0 ] = Specular;
AlphaArg1[ 0 ] = Diffuse;
AlphaOp[ 0 ] = SelectArg1;
}
}
technique Textured <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexShader = compile vs_1_1 MrWiggleVS();
ZEnable = true;
ZWriteEnable = true;
CullMode = None;
PixelShader = compile ps_1_1 MrWigglePS_t();
}
}
/***************************** eof ***/
]]> = 0.8; // version #
#include "include/Quad.fxh"
#define DOTS_PER_BIT 8.0
#define IMG_DIVS 8.0
///////////////////////////////////////////////////////////////
/// TWEAKABLES ////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
float4 ClearColor : DIFFUSE = {0,0,0,1.0};
float ClearDepth < string UIWidget = "none"; > = 1.0;
////////////////////////////////////////////////////////
/// TEXTURES ///////////////////////////////////////////
////////////////////////////////////////////////////////
float4 make_tones(float3 Pos : POSITION, float3 Size : PSIZE) : COLOR {
float2 delta = Pos.xy - float2(0.5,0.5);
float d = dot(delta,delta);
float rSquared = (Pos.z*Pos.z)/2.0;
float n2 = (d;
sampler ToneSampler = sampler_state
{
texture = ;
AddressU = WRAP;
AddressV = WRAP;
AddressW = CLAMP;
MIPFILTER = NONE;
MINFILTER = ANISOTROPIC;
MAGFILTER = LINEAR;
};
////////////
#define DOWNSIZE (1.0/IMG_DIVS)
DECLARE_SIZED_QUAD_TEX(SceneMap,SceneSampler,"A8R8G8B8",DOWNSIZE)
texture SceneDepth : RENDERDEPTHSTENCILTARGET <
float2 ViewportRatio = {DOWNSIZE,DOWNSIZE};
string Format = "D24S8";
string UIWidget = "None";
>;
/*********************************************************/
/*********** pixel shader ********************************/
/*********************************************************/
float4 tonePS(QuadVertexOutput IN) : COLOR {
QUAD_REAL4 scnC = tex2D(SceneSampler,IN.UV);
float lum = dot(float3(.2,.7,.1),scnC.xyz);
float3 lx = float3((DOTS_PER_BIT*IMG_DIVS*IN.UV.xy),lum);
QUAD_REAL4 dotC = tex3D(ToneSampler,lx);
return float4(dotC.xyz,1.0);
}
////////////////////////////////////////////////////////////////////
/// TECHNIQUES /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
technique Toned <
string Script =
"ClearSetColor=ClearColor;"
"ClearSetDepth=ClearDepth;"
"Clear=Color;"
"Clear=Depth;"
"RenderColorTarget0=SceneMap;"
"RenderDepthStencilTarget=SceneDepth;"
"ClearSetColor=ClearColor;"
"ClearSetDepth=ClearDepth;"
"Clear=Color;"
"Clear=Depth;"
"ScriptExternal=color;"
"Pass=p0;";
>
{
pass p0 <
string Script =
"RenderColorTarget0=;"
"RenderDepthStencilTarget=;"
"Draw=Buffer;";
> {
VertexShader = compile vs_2_0 ScreenQuadVS();
ZEnable = true;
ZWriteEnable = true;
CullMode = None;
PixelShader = compile ps_2_0 tonePS();
}
}
/***************************** eof ***/
]]>; \
sampler SampName = sampler_state { \
texture = ; \
AddressU = AddrMode; \
AddressV = AddrMode; \
MipFilter = LINEAR; \
MinFilter = LINEAR; \
MagFilter = LINEAR; \
};
//
// Simple 2D File Textures
//
// example usage: FILE_TEXTURE_2D(GlowMap,GlowSampler,"myfile.dds")
//
#define FILE_TEXTURE_2D(TextureName,SamplerName,Diskfile) FILE_TEXTURE_2D_MODAL(TextureName,SamplerName,(Diskfile),WRAP)
//
// Use this variation of DECLARE_QUAD_TEX() if you want a *scaled* render target
//
// example usage: DECLARE_SIZED_QUAD_TEX(GlowMap,GlowSampler,"A8R8G8B8",1.0)
#define DECLARE_SIZED_QUAD_TEX(TexName,SampName,PixFmt,Multiple) texture TexName : RENDERCOLORTARGET < \
float2 ViewPortRatio = {Multiple,Multiple}; \
int MipLevels = 1; \
string Format = PixFmt ; \
string UIWidget = "None"; \
>; \
sampler SampName = sampler_state { \
texture = ; \
AddressU = CLAMP; \
AddressV = CLAMP; \
MipFilter = POINT; \
MinFilter = LINEAR; \
MagFilter = LINEAR; \
};
//
// Use this macro to easily declare typical color render targets
//
// example usage: DECLARE_QUAD_TEX(ObjMap,ObjSampler,"A8R8G8B8")
#define DECLARE_QUAD_TEX(TextureName,SamplerName,PixelFormat) DECLARE_SIZED_QUAD_TEX(TextureName,SamplerName,(PixelFormat),1.0)
//
// Use this macro to easily declare variable-sized depth render targets
//
// example usage: DECLARE_SIZED_QUAD_DEPTH_BUFFER(DepthMap,"D24S8",0.5)
#define DECLARE_SIZED_QUAD_DEPTH_BUFFER(TextureName,PixelFormat,Multiple) texture TextureName : RENDERDEPTHSTENCILTARGET < \
float2 ViewPortRatio = {Multiple,Multiple}; \
string Format = (PixelFormat); \
string UIWidget = "None"; \
>;
//
// Use this macro to easily declare typical depth render targets
//
// example usage: DECLARE_QUAD_DEPTH_BUFFER(DepthMap,"D24S8")
#define DECLARE_QUAD_DEPTH_BUFFER(TexName,PixFmt) DECLARE_SIZED_QUAD_DEPTH_BUFFER(TexName,PixFmt,1.0)
//
// declare exact-sized arbitrary texture
//
// example usage: DECLARE_SIZED_TEX(BlahMap,BlahSampler,"R32F",128,1)
#define DECLARE_SIZED_TEX(Tex,Samp,Fmt,Wd,Ht) texture Tex : RENDERCOLORTARGET < \
float2 Dimensions = { Wd, Ht }; \
string Format = Fmt ; \
string UIWidget = "None"; \
int miplevels=1;\
>; \
sampler Samp = sampler_state { \
texture = ; \
AddressU = CLAMP; \
AddressV = CLAMP; \
MipFilter = NONE; \
MinFilter = LINEAR; \
MagFilter = LINEAR; \
};
//
// declare exact-sized square texture, as for shadow maps
//
// example usage: DECLARE_SQUARE_QUAD_TEX(ShadMap,ShadObjSampler,"A16R16G16B16F",512)
#define DECLARE_SQUARE_QUAD_TEX(TexName,SampName,PixFmt,Size) DECLARE_SIZED_TEX(TexName,SampName,(PixFmt),Size,Size)
//
// likewise for shadow depth targets
//
// example usage: DECLARE_SQUARE_QUAD_DEPTH_BUFFER(ShadDepth,"D24S8",512)
#define DECLARE_SQUARE_QUAD_DEPTH_BUFFER(TextureName,PixelFormat,Size) texture TextureName : RENDERDEPTHSTENCILTARGET < \
float2 Dimensions = { Size, Size }; \
string Format = (PixelFormat) ; \
string UIWidget = "None"; \
>;
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// Utility Functions ////////
////////////////////////////////////////////////////////////////////////////
//
// Scale inputs for use with texture-based lookup tables. A value ranging from zero to one needs
// a slight scaling and offset to be sure to point at the centers of the first and last pixels
// of that lookup texture. Pass the integer size of the table in TableSize
// For now we'll assume that all tables are 1D, square, or cube-shaped -- all axes of equal size
//
// Cost of this operation for pixel shaders: two const-register
// entries and a MAD (one cycle)
QUAD_REAL scale_lookup(QUAD_REAL Value,const QUAD_REAL TableSize)
{
QUAD_REAL scale = ((TableSize - 1.0)/TableSize);
QUAD_REAL shift = (0.5 / TableSize);
return (scale*Value + shift);
}
QUAD_REAL2 scale_lookup(QUAD_REAL2 Value,const QUAD_REAL TableSize)
{
QUAD_REAL scale = ((TableSize - 1.0)/TableSize);
QUAD_REAL shift = (0.5 / TableSize);
return (scale.xx*Value + shift.xx);
}
QUAD_REAL3 scale_lookup(QUAD_REAL3 Value,const QUAD_REAL TableSize)
{
QUAD_REAL scale = ((TableSize - 1.0)/TableSize);
QUAD_REAL shift = (0.5 / TableSize);
return (scale.xxx*Value + shift.xxx);
}
// pre-multiply and un-pre-mutliply functions. The precision
// of thse operatoions is often limited to 8-bit so don't
// always count on them!
// The macro value of NV_ALPHA_EPSILON, if defined, is used to
// avoid IEEE "NaN" values that may occur when erroneously
// dividing by a zero alpha (thanks to Pete Warden @ Apple
// Computer for the suggestion in GPU GEMS II)
// multiply color by alpha to turn an un-premultipied
// pixel value into a premultiplied one
QUAD_REAL4 premultiply(QUAD_REAL4 C)
{
return QUAD_REAL4((C.w*C.xyz),C.w);
}
#define NV_ALPHA_EPSILON 0.0001
// given a premultiplied pixel color, try to undo the premultiplication.
// beware of precision errors
QUAD_REAL4 unpremultiply(QUAD_REAL4 C)
{
#ifdef NV_ALPHA_EPSILON
QUAD_REAL a = C.w + NV_ALPHA_EPSILON;
return QUAD_REAL4((C.xyz / a),C.w);
#else /* ! NV_ALPHA_EPSILON */
return QUAD_REAL4((C.xyz / C.w),C.w);
#endif /* ! NV_ALPHA_EPSILON */
}
/////////////////////////////////////////////////////////////////////////////////////
// Structure Declaration ////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
struct QuadVertexOutput {
QUAD_REAL4 Position : POSITION;
QUAD_REAL2 UV : TEXCOORD0;
};
/////////////////////////////////////////////////////////////////////////////////////
// Hidden tweakables declared by this .fxh file /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
#ifndef NO_TEXEL_OFFSET
#ifdef TWEAKABLE_TEXEL_OFFSET
QUAD_REAL QuadTexOffset = 0.5;
#else /* !TWEAKABLE_TEXEL_OFFSET */
QUAD_REAL QuadTexOffset < string UIWidget="None"; > = 0.5;
#endif /* !TWEAKABLE_TEXEL_OFFSET */
QUAD_REAL2 QuadScreenSize : VIEWPORTPIXELSIZE < string UIWidget="None"; >;
#endif /* NO_TEXEL_OFFSET */
////////////////////////////////////////////////////////////
////////////////////////////////// vertex shaders //////////
////////////////////////////////////////////////////////////
QuadVertexOutput ScreenQuadVS(
QUAD_REAL3 Position : POSITION,
QUAD_REAL3 TexCoord : TEXCOORD0
) {
QuadVertexOutput OUT;
OUT.Position = QUAD_REAL4(Position, 1);
#ifdef NO_TEXEL_OFFSET
OUT.UV = TexCoord.xy;
#else /* NO_TEXEL_OFFSET */
QUAD_REAL2 off = QUAD_REAL2(QuadTexOffset/(QuadScreenSize.x),QuadTexOffset/(QuadScreenSize.y));
OUT.UV = QUAD_REAL2(TexCoord.xy+off);
#endif /* NO_TEXEL_OFFSET */
return OUT;
}
//////////////////////////////////////////////////////
////////////////////////////////// pixel shaders /////
//////////////////////////////////////////////////////
// add glow on top of model
QUAD_REAL4 TexQuadPS(QuadVertexOutput IN,uniform sampler2D InputSampler) : COLOR
{
QUAD_REAL4 texCol = tex2D(InputSampler, IN.UV);
return texCol;
}
QUAD_REAL4 TexQuadBiasPS(QuadVertexOutput IN,uniform sampler2D InputSampler,QUAD_REAL TBias) : COLOR
{
QUAD_REAL4 texCol = tex2Dbias(InputSampler, QUAD_REAL4(IN.UV,0,TBias));
return texCol;
}
//////////////////////////////////////////////////////////////////
/// Macros to define passes within Techniques ////////////////////
//////////////////////////////////////////////////////////////////
// older HLSL syntax
#define TEX_TECH(TechName,SamplerName) technique TechName { \
pass TexturePass { \
VertexShader = compile vs_2_0 ScreenQuadVS(); \
AlphaBlendEnable = false; ZEnable = false; \
PixelShader = compile ps_2_a TexQuadPS(SamplerName); } }
#define TEX_BLEND_TECH(TechName,SamplerName) technique TechName { \
pass TexturePass { \
VertexShader = compile vs_2_0 ScreenQuadVS(); \
ZEnable = false; AlphaBlendEnable = true; \
SrcBlend = SrcAlpha; DestBlend = InvSrcAlpha; \
PixelShader = compile ps_2_a TexQuadPS(SamplerName); } }
// newer HLSL syntax
#define TEX_TECH2(TechName,SamplerName,TargName) technique TechName { \
pass TexturePass < \
string ScriptFunction = "RenderColorTarget0=" (TargName) ";" \
"DrawInternal=Buffer;"; \
> { \
VertexShader = compile vs_2_0 ScreenQuadVS(); \
AlphaBlendEnable = false; ZEnable = false; \
PixelShader = compile ps_2_a TexQuadPS(SamplerName); } }
#define TEX_BLEND_TECH2(TechName,SamplerName) technique TechName { \
pass TexturePass < \
string ScriptFunction = "RenderColorTarget0=" (TargName) ";" \
"DrawInternal=Buffer;"; \
> { \
VertexShader = compile vs_2_0 ScreenQuadVS(); \
ZEnable = false; AlphaBlendEnable = true; \
SrcBlend = SrcAlpha; DestBlend = InvSrcAlpha; \
PixelShader = compile ps_2_a TexQuadPS(SamplerName); } }
#endif /* _QUAD_FXH */
////////////// eof ///
]]>