|
我是一名美工,最近正在学习shader,如下代码是一个可以正常在MAX里编译的发光shader的源代码,关于technique和Pass的Script的注解我一直没弄明白,请哪位朋友给指点一二吧。
----------------------------------------------------------------------------------------------------------
string ParamID = "0x000001"; // this string tells 3ds Max to use the DXSAS compiler
float3 EmissiveColor
<
string UIName = "Color";
string UIWidget = "Color";
> = {1.0f, 0.0196078f, 0.0196078f };
float3 GlowColor : Emissive
<
string UIName = "Color";
string UIWidget = "Color";
> = {1.0f, 0.25098f, 0.25098f };
//----------------------------------
float4x4 wvp : WorldViewProjection;
//float4x4 worldI : WorldInverse;
// Render-to-Texture (RTT) glow example. Copyright NVIDIA (provided 'as is'). Adapted from HammerTime.fx in 3dsmax
float4 ClearColor : DIFFUSE = {0,0,0,1.0};
float ClearDepth
<
string UIWidget = "none";
> = 1.0;
float Script : STANDARDSGLOBAL
<
string UIWidget = "none";
string ScriptClass = "object";
string ScriptOrder = "standard";
string ScriptOutput = "color";
// Call a script in the main technique.
string Script = "Technique=Technique?Complete;";
> = 0.8;
#define RTT_SIZE 128
float TexelIncrement <
string UIName = "Texel Stride for Blur";
string UIWidget = "None";
> = 1.0f / RTT_SIZE;
texture GlowMap1 : RENDERCOLORTARGET
<
float2 Dimensions = { RTT_SIZE, RTT_SIZE };
int MIPLEVELS = 1;
string format = "X8R8G8B8";
string UIWidget = "None";
>;
texture GlowMap2 : RENDERCOLORTARGET
<
float2 Dimensions = { RTT_SIZE, RTT_SIZE };
int MIPLEVELS = 1;
string format = "X8R8G8B8";
string UIWidget = "None";
>;
sampler GlowSamp1 = sampler_state
{
texture = <GlowMap1>;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = NONE;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};
sampler GlowSamp2 = sampler_state
{
texture = <GlowMap2>;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = NONE;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};
texture DepthBuffer : RENDERDEPTHSTENCILTARGET
<
float2 Dimensions = { RTT_SIZE, RTT_SIZE };
string format = "D24S8";
string UIWidget = "None";
>;
// input from application
struct a2v {
float4 position : POSITION;
float2 texCoord : TEXCOORD0;
};
// output to fragment program
struct v2f {
float4 position : POSITION;
float2 texCoord : TEXCOORD0;
};
struct VS_OUTPUT_BLUR
{
float4 Position : POSITION;
float4 TexCoord0 : TEXCOORD0;
float4 TexCoord1 : TEXCOORD1;
float4 TexCoord2 : TEXCOORD2;
float4 TexCoord3 : TEXCOORD3;
float4 TexCoord4 : TEXCOORD4;
float4 TexCoord5 : TEXCOORD5;
float4 TexCoord6 : TEXCOORD6;
float4 TexCoord7 : TEXCOORD7;
float4 TexCoord8 : COLOR;
};
struct VS_OUTPUT
{
float4 Position : POSITION;
float4 Diffuse : COLOR0;
float4 TexCoord0 : TEXCOORD0;
};
v2f VS(a2v In)
{
v2f Out = (v2f)0;
Out.position = mul(In.position, wvp); //transform vert position to homogeneous clip space
return Out;
}
v2f VS_Quad(a2v In, float3 DEFTexCoord : TEXCOORD0)
{
v2f Out = (v2f)0;
Out.position = In.position;
Out.texCoord = DEFTexCoord;
return Out;
}
VS_OUTPUT_BLUR VS_Quad_Vertical_9tap(float3 Position : POSITION,
float3 TexCoord : TEXCOORD0)
{
VS_OUTPUT_BLUR OUT = (VS_OUTPUT_BLUR)0;
OUT.Position = float4(Position, 1);
float3 Coord = float3(TexCoord.x + TexelIncrement, TexCoord.y + TexelIncrement, 1);
OUT.TexCoord0 = float4(Coord.x, Coord.y + TexelIncrement, TexCoord.z, 1);
OUT.TexCoord1 = float4(Coord.x, Coord.y + TexelIncrement * 2, TexCoord.z, 1);
OUT.TexCoord2 = float4(Coord.x, Coord.y + TexelIncrement * 3, TexCoord.z, 1);
OUT.TexCoord3 = float4(Coord.x, Coord.y + TexelIncrement * 4, TexCoord.z, 1);
OUT.TexCoord4 = float4(Coord.x, Coord.y, TexCoord.z, 1);
OUT.TexCoord5 = float4(Coord.x, Coord.y - TexelIncrement, TexCoord.z, 1);
OUT.TexCoord6 = float4(Coord.x, Coord.y - TexelIncrement * 2, TexCoord.z, 1);
OUT.TexCoord7 = float4(Coord.x, Coord.y - TexelIncrement * 3, TexCoord.z, 1);
OUT.TexCoord8 = float4(Coord.x, Coord.y - TexelIncrement * 4, TexCoord.z, 1);
return OUT;
}
VS_OUTPUT_BLUR VS_Quad_Horizontal_9tap(float3 Position : POSITION,
float3 TexCoord : TEXCOORD0)
{
VS_OUTPUT_BLUR OUT = (VS_OUTPUT_BLUR)0;
OUT.Position = float4(Position, 1);
float3 Coord = float3(TexCoord.x + TexelIncrement, TexCoord.y + TexelIncrement, 1);
OUT.TexCoord0 = float4(Coord.x + TexelIncrement, Coord.y, TexCoord.z, 1);
OUT.TexCoord1 = float4(Coord.x + TexelIncrement * 2, Coord.y, TexCoord.z, 1);
OUT.TexCoord2 = float4(Coord.x + TexelIncrement * 3, Coord.y, TexCoord.z, 1);
OUT.TexCoord3 = float4(Coord.x + TexelIncrement * 4, Coord.y, TexCoord.z, 1);
OUT.TexCoord4 = float4(Coord.x, Coord.y, TexCoord.z, 1);
OUT.TexCoord5 = float4(Coord.x - TexelIncrement, Coord.y, TexCoord.z, 1);
OUT.TexCoord6 = float4(Coord.x - TexelIncrement * 2, Coord.y, TexCoord.z, 1);
OUT.TexCoord7 = float4(Coord.x - TexelIncrement * 3, Coord.y, TexCoord.z, 1);
OUT.TexCoord8 = float4(Coord.x - TexelIncrement * 4, Coord.y, TexCoord.z, 1);
return OUT;
}
// map the glow-mask texture to the screen - no lighting
// this shader will draw to a texture
float4 PS_BlurBuffer(v2f In) : COLOR
{
float4 Col = float4(1,1,1,1);
float3 input2 = GlowColor.rgb;
Col = float4(input2.rgb, 1);
Col *= float4(1,1,1,1);
return Col;
}
// For two-pass blur, we have chosen to do the horizontal blur FIRST. The
// vertical pass includes a post-blur scale factor.
// Relative filter weights indexed by distance from 'home' texel
// This set for 9-texel sampling
#define WT9_0 1.0
#define WT9_1 0.8
#define WT9_2 0.6
#define WT9_3 0.4
#define WT9_4 0.2
#define WT9_NORMALIZE (WT9_0+2.0*(WT9_1+WT9_2+WT9_3+WT9_4))
float4 PS_Blur_Horizontal_9tap(VS_OUTPUT_BLUR IN) : COLOR
{
float4 OutCol = (float4)0;
OutCol += tex2D(GlowSamp1, IN.TexCoord0.xy) * (WT9_1/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp1, IN.TexCoord1.xy) * (WT9_2/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp1, IN.TexCoord2.xy) * (WT9_3/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp1, IN.TexCoord3.xy) * (WT9_4/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp1, IN.TexCoord4.xy) * (WT9_0/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp1, IN.TexCoord5.xy) * (WT9_1/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp1, IN.TexCoord6.xy) * (WT9_2/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp1, IN.TexCoord7.xy) * (WT9_3/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp1, IN.TexCoord8.xy) * (WT9_3/WT9_NORMALIZE);
return OutCol;
}
float4 PS_Blur_Vertical_9tap(VS_OUTPUT_BLUR IN, v2f In) : COLOR
{
float4 OutCol = (float4)0;
OutCol += tex2D(GlowSamp2, IN.TexCoord0.xy) * (WT9_1/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp2, IN.TexCoord1.xy) * (WT9_2/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp2, IN.TexCoord2.xy) * (WT9_3/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp2, IN.TexCoord3.xy) * (WT9_4/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp2, IN.TexCoord4.xy) * (WT9_0/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp2, IN.TexCoord5.xy) * (WT9_1/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp2, IN.TexCoord6.xy) * (WT9_2/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp2, IN.TexCoord7.xy) * (WT9_3/WT9_NORMALIZE);
OutCol += tex2D(GlowSamp2, IN.TexCoord8.xy) * (WT9_3/WT9_NORMALIZE);
return OutCol;
}
// just drawn model itself
float4 PS_MODEL(v2f In) : COLOR
{
float4 Col = float4(0,0,0,1);
float3 input1 = EmissiveColor.rgb;
Col = float4(input1, 1);
return Col;
}
// add glow on top of model
float4 PS_GlowPass(VS_OUTPUT IN) : COLOR
{
float4 tex = tex2D(GlowSamp1, float2(IN.TexCoord0.x, IN.TexCoord0.y));
return tex;
}
technique Complete
<
string Script =
"ClearSetColor=ClearColor;"
"ClearSetDepth=ClearDepth;"
" ass=BlurPass;"
"Pass=BlurGlowBuffer_Horz;"
"Pass=BlurGlowBuffer_Vert;"
"Pass=ModelPass1;"
"Pass=GlowPass;";
>
{
pass BlurPass
<
string Script = "RenderColorTarget0=GlowMap1;"
"RenderDepthStencilTarget=DepthBuffer;"
"Clear=Color;"
"Clear=Depth;"
"Draw=Geometry;";
>
{
CullMode = none;
ZEnable = true;
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS_BlurBuffer();
}
pass BlurGlowBuffer_Horz
<
string Script ="RenderColorTarget0=GlowMap2;"
"RenderDepthStencilTarget=DepthBuffer;"
"Clear=Color;"
"Clear=Depth;"
"Draw=Buffer;";
>
{
CullMode = none;
ZEnable = false;
VertexShader = compile vs_3_0 VS_Quad_Horizontal_9tap();
PixelShader = compile ps_3_0 PS_Blur_Horizontal_9tap();
}
pass BlurGlowBuffer_Vert
<
string Script = "RenderColorTarget0=GlowMap1;"
"RenderDepthStencilTarget=DepthBuffer;"
"Clear=Color;"
"Clear=Depth;"
"Draw=Buffer;";
>
{
CullMode = none;
ZEnable = false;
VertexShader = compile vs_3_0 VS_Quad_Vertical_9tap();
PixelShader = compile ps_3_0 PS_Blur_Vertical_9tap();
}
pass ModelPass1
<
string Script= "RenderColorTarget0=;"
"Draw=Geometry;";
>
{
AlphaBlendEnable = false;
AlphaTestEnable = FALSE;
CullMode = cw;
ZEnable = true;
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS_MODEL();
}
pass GlowPass
<
string Script= "RenderColorTarget0=;"
"Draw=Buffer;";
>
{
CullMode = none;
ZEnable = false;
ZWriteEnable = false;
AlphaBlendEnable = true;
SrcBlend = One;
DestBlend = One;
AlphaTestEnable = FALSE;
VertexShader = compile vs_3_0 VS_Quad();
PixelShader = compile ps_3_0 PS_GlowPass();
}
}
------------------------------------------------------------------------------------------------
我弄不明白的是,这些代码是什么意思,如何发生作用的,我没有在DX的SDK帮助文件中找到关于Script这种注解类型的说明。:
string Script= "RenderColorTarget0=;"
"Draw=Buffer;";
string Script= "RenderColorTarget0=;"
"Draw=Geometry;";
string Script = "RenderColorTarget0=GlowMap1;"
"RenderDepthStencilTarget=DepthBuffer;"
"Clear=Color;"
"Clear=Depth;"
"Draw=Buffer;";
string Script =
"ClearSetColor=ClearColor;"
"ClearSetDepth=ClearDepth;"
"Pass=BlurPass;"
"Pass=BlurGlowBuffer_Horz;"
"Pass=BlurGlowBuffer_Vert;"
"Pass=ModelPass1;"
"Pass=GlowPass;";
还有这些贴图,它们后面的语义代表的是什么意思,是如何在technique和pass中产生作用的:
texture DepthBuffer : RENDERDEPTHSTENCILTARGET
<
float2 Dimensions = { RTT_SIZE, RTT_SIZE };
string format = "D24S8";
string UIWidget = "None";
>;
texture GlowMap1 : RENDERCOLORTARGET
<
float2 Dimensions = { RTT_SIZE, RTT_SIZE };
int MIPLEVELS = 1;
string format = "X8R8G8B8";
string UIWidget = "None";
>;
float Script : STANDARDSGLOBAL
<
string UIWidget = "none";
string ScriptClass = "object";
string ScriptOrder = "standard";
string ScriptOutput = "color";
// Call a script in the main technique.
string Script = "Technique=Technique?Complete;";
> = 0.8;
|
|