// we will draw into buffer "paint"
// then displace pic into buffer "pool"
// then display
float Script : STANDARDSGLOBAL
<
string UIWidget = "none";
string ScriptClass = "scene";
string ScriptOrder = "standard";
string ScriptOutput = "color";
string Script = "Technique=liquid;";
> = 0.8;
///////////////////////////////////////////////////////////////
/// UNTWEAKABLES //////////////////////////////////////////////
///////////////////////////////////////////////////////////////
float passnumber ; // loop counter, hidden
///////////////////////////////////////////////////////////////
/// TWEAKABLES ////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
float npasses <
float UIStep = 1.0;
string UIName = "Blur passes";
> = 8.0f;
bool bReset : FXCOMPOSER_RESETPULSE
<
string UIName="Clear Canvas";
>;
bool Painting
<
string UIName="Paint with Mouse?";
> = true;
float4 ClearColor <
string UIWidget = "None";
string UIName = "background";
> = {0.5,0.5,0.5,0.0};
float ClearDepth = 1.0;
float4 MouseL : LEFTMOUSEDOWN < string UIWidget="None"; >;
float3 MousePos : MOUSEPOSITION < string UIWidget="None"; >;
float Timer : TIME < string UIWidget = "None"; >;
float Opacity <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.01;
> = 0.45f;
float BrushSizeStart <
string UIWidget = "slider";
float UIMin = 0.001;
float UIMax = 0.15;
float UIStep = 0.001;
string UIName = "Brush Start Size";
> = 0.045f;
float BrushSizeEnd <
string UIWidget = "slider";
float UIMin = 0.001;
float UIMax = 0.15;
float UIStep = 0.001;
string UIName = "Brush End Size";
> = 0.01f;
float FadeTime <
string UIWidget = "slider";
float UIMin = 0.1;
float UIMax = 10.0;
float UIStep = 0.1;
string UIName = "Brush Decay Time";
> = 1.10f;
float FadeInTime <
string UIWidget = "slider";
float UIMin = 0.001;
float UIMax = 0.3;
float UIStep = 0.001;
string UIName = "Brush Attack Time";
> = 0.15f;
float Push <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.001;
string UIName = "Brush Pressure";
> = 0.1f;
float Effect <
string UIWidget = "slider";
string UIName = "Effect Strength";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.001;
> = 1.0f;
float Bright
<
string UIWidget = "slider";
float UIMin = 0.01;
float UIMax = 2.0;
float UIStep = 0.01;
string UIName = "Overall Brightness";
> = 1.0;
////////////////////////////////////////////////////
/// Textures ///////////////////////////////////////
////////////////////////////////////////////////////
//DECLARE_QUAD_TEX(PaintTex,PaintSamp,"A8R8G8B8")
DECLARE_QUAD_TEX(PaintTex,PaintSamp,"A16B16G16R16F")
texture SourceTexture <
string ResourceName = "default_color.dds";
string TextureType = "2D";
string UIName = "Original Image";
>;
sampler2D SourceSampler = sampler_state
{
Texture = ;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = CLAMP;
AddressV = CLAMP;
};
texture AccumBuffer : RENDERCOLORTARGET <
float2 ViewPortRatio = {1,1};
int MipLevels = 1;
string Format = "A16B16G16R16F" ;
string UIWidget = "None";
>;
sampler AccumSampler = sampler_state {
texture = ;
AddressU = CLAMP;
AddressV = CLAMP;
MipFilter = POINT;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
/****************************************/
/*** The dead-simple shaders: ***********/
/****************************************/
// VM functions
QUAD_REAL2 dir_color()
{
QUAD_REAL2 dirVec = MousePos.xy - MouseL.xy;
QUAD_REAL l = length(dirVec);
if (l > 0.0) {
dirVec = normalize(dirVec);
dirVec = Push*dirVec;
} else {
dirVec = 0;
}
return dirVec;
}
float fadeout()
{
float f = 1 - min(1,max(0,(Timer - MouseL.w))/FadeTime);
return f;
}
float lerpsize()
{
float f = fadeout();
float ds = lerp(BrushSizeEnd,BrushSizeStart,f);
return ds;
}
float fadein()
{
float f = min(1,max(0,(Timer - MouseL.w))/FadeInTime);
return f;
}
///
QUAD_REAL4 strokePS(QuadVertexOutput IN,
uniform QUAD_REAL2 BrushColor,
uniform QUAD_REAL Fadeout,
uniform QUAD_REAL Fadein,
uniform QUAD_REAL BrushSize) : COLOR
{
QUAD_REAL2 delta = IN.UV.xy-MousePos.xy;
QUAD_REAL dl = dot(delta,delta);
QUAD_REAL dd = MouseL.z*(1-min(dl/BrushSize,1));
dd *= Painting * Opacity * Fadeout * Fadein;
return QUAD_REAL4(0.5+(BrushColor),0.5,dd);
//return QUAD_REAL4(q.xxx,dd);
}
QUAD_REAL4 liquidPS(QuadVertexOutput IN) : COLOR
{
QUAD_REAL2 disp = Effect*2.0*(tex2D(PaintSamp, IN.UV).xy - QUAD_REAL2(0.5,0.5));
QUAD_REAL4 texCol = tex2D(SourceSampler, IN.UV-disp);
return texCol;
}
/*********************************************************/
/*********** pixel shader ********************************/
/*********************************************************/
float4 accumPS(QuadVertexOutput IN,uniform float delta) : COLOR
{
float2 off = delta*Effect*(tex2D(PaintSamp, IN.UV).xy-float2(.5,.5));
float4 texCol = tex2D(SourceSampler, (IN.UV-off));
return texCol;
}
///////////////////////////////////////////////////////////
/// Final Pass ////////////////////////////////////////////
///////////////////////////////////////////////////////////
float4 finalPS(QuadVertexOutput IN) : COLOR
{
float4 texCol = Bright * tex2D(AccumSampler, IN.UV) / npasses;
return texCol;
}
////////////////// Technique ////////
technique liquid <
string Script =
// Clear Accum Buffer
"RenderColorTarget0=AccumBuffer;"
"ClearSetColor=ClearColor;"
"Clear=Color;"
// paint into blur-dir buffer...
"Pass=paint;"
// accumulate
"LoopByCount=npasses;"
"LoopGetIndex=passnumber;"
"Pass=Accumulate;"
"LoopEnd;"
// draw accum buffer to framebuffer
"Pass=FinalPass;";
> {
pass paint <
string Script =
"RenderColorTarget0=PaintTex;"
"RenderDepthStencilTarget=;"
"LoopByCount=bReset;"
"ClearSetColor=ClearColor;"
"Clear=Color0;"
"LoopEnd=;"
"Draw=Buffer;";
> {
VertexShader = compile vs_1_1 ScreenQuadVS();
PixelShader = compile ps_2_a strokePS(dir_color(),
fadeout(),
fadein(),
lerpsize());
AlphaBlendEnable = true;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
ZEnable = false;
}
pass Accumulate <
string Script =
"RenderColorTarget0=AccumBuffer;"
"RenderDepthStencilTarget=;"
"Draw=Buffer;";
> {
VertexShader = compile vs_3_0 ScreenQuadVS();
ZEnable = false;
AlphaBlendEnable = true;
SrcBlend = ONE;
DestBlend = ONE;
PixelShader = compile ps_3_0 accumPS((passnumber/(npasses-1.0)));
}
pass FinalPass <
string Script =
"RenderColorTarget0=;"
"RenderDepthStencilTarget=;"
"Draw=Buffer;";
> {
VertexShader = compile vs_3_0 ScreenQuadVS();
AlphaBlendEnable = false;
ZEnable = false;
PixelShader = compile ps_3_0 finalPS();
}
/* pass display <
string Script =
"RenderColorTarget0=;"
"RenderDepthStencilTarget=;"
"Draw=Buffer;";
> {
VertexShader = compile vs_1_1 ScreenQuadVS();
PixelShader = compile ps_2_a liquidPS();
//PixelShader = compile ps_2_a TexQuadPS(PaintSamp);
AlphaBlendEnable = false;
ZEnable = false;
} */
}
/////////////////////////////////////////////
///////////////////////////////////// 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 ///
]]>