HLSL高级应用实例
废话少说,直接入题。
先给出整个应用程序构架:
HLSL代码如下:
这是默认的效果,效果图如下:
1
float4 PSfun(PSInput input) : COLOR0
2
{
3
//负片效果
4
return(1- float4(color*AmbientColor) );
5
}
float4 PSfun(PSInput input) : COLOR02
{3
//负片效果4
return(1- float4(color*AmbientColor) );5
}模糊效果:
1
float4 PSfun(PSInput input) : COLOR0
2
{
3
//模糊效果
4
float4 color = float4( tex2D( TextureSampler,float2(input.texcoord.x+0.0025, input.texcoord.y+0.025))*AmbientColor);
5
color += tex2D( TextureSampler,float2(input.texcoord.x-0.025, input.texcoord.y-0.025));
6
color += tex2D( TextureSampler,float2(input.texcoord.x+0.025, input.texcoord.y-0.025));
7
color += tex2D( TextureSampler,float2(input.texcoord.x-0.025, input.texcoord.y+0.025));
8
color = color / 4;
9
return( color );
10
}
float4 PSfun(PSInput input) : COLOR02
{3
//模糊效果4
float4 color = float4( tex2D( TextureSampler,float2(input.texcoord.x+0.0025, input.texcoord.y+0.025))*AmbientColor);5
color += tex2D( TextureSampler,float2(input.texcoord.x-0.025, input.texcoord.y-0.025));6
color += tex2D( TextureSampler,float2(input.texcoord.x+0.025, input.texcoord.y-0.025));7
color += tex2D( TextureSampler,float2(input.texcoord.x-0.025, input.texcoord.y+0.025));8
color = color / 4;9
return( color );10
}
浮雕效果:
1
float4 PSfun(PSInput input) : COLOR0
2
{
3
//浮雕
4
float sharpAmount = 15.0f;
5
float4 color;
6
color.rgb = 0.5f;
7
color.a = 1.0f;
8
color -= float4( tex2D( TextureSampler, input.texcoord - 0.0001) * sharpAmount*AmbientColor);
9
color += float4( tex2D( TextureSampler, input.texcoord + 0.0001) * sharpAmount*AmbientColor);
10
color = (color.r+color.g+color.b) / 3.0f;
11
return( color );
12
}
float4 PSfun(PSInput input) : COLOR02
{3
//浮雕4
float sharpAmount = 15.0f;5
float4 color;6
color.rgb = 0.5f;7
color.a = 1.0f;8
color -= float4( tex2D( TextureSampler, input.texcoord - 0.0001) * sharpAmount*AmbientColor);9
color += float4( tex2D( TextureSampler, input.texcoord + 0.0001) * sharpAmount*AmbientColor);10
color = (color.r+color.g+color.b) / 3.0f;11
return( color );12
}Grayscale:
1
float4 PSfun(PSInput input) : COLOR0
2
{
3
//Grayscale
4
float4 color =float4(tex2D(TextureSampler, input.texcoord)*AmbientColor);
5
float4 gs = dot(color.rgb, float3(0.3, 0.59, 0.11));
6
if (input.texcoord.x > 0.5f)
7
color = lerp(gs, color, (1 - input.texcoord.x) * 2);
8
else
9
color = lerp(gs, color, input.texcoord.x * 2);
10
return( color );
11
}
float4 PSfun(PSInput input) : COLOR02
{3
//Grayscale4
float4 color =float4(tex2D(TextureSampler, input.texcoord)*AmbientColor);5
float4 gs = dot(color.rgb, float3(0.3, 0.59, 0.11));6
if (input.texcoord.x > 0.5f)7
color = lerp(gs, color, (1 - input.texcoord.x) * 2);8
else9
color = lerp(gs, color, input.texcoord.x * 2);10
return( color );11
}
粉笔效果:
1
float4 PSfun(PSInput input) : COLOR0
2
{
3
//粉笔
4
float sharpAmount = 100.0f;
5
float4 color = float4(tex2D(TextureSampler, input.texcoord)*AmbientColor);
6
color += tex2D( TextureSampler, input.texcoord - 0.001) * sharpAmount;
7
color -= tex2D( TextureSampler, input.texcoord + 0.001) * sharpAmount;
8
return( color );
9
}
float4 PSfun(PSInput input) : COLOR02
{3
//粉笔4
float sharpAmount = 100.0f;5
float4 color = float4(tex2D(TextureSampler, input.texcoord)*AmbientColor);6
color += tex2D( TextureSampler, input.texcoord - 0.001) * sharpAmount;7
color -= tex2D( TextureSampler, input.texcoord + 0.001) * sharpAmount;8
return( color );9
}波动效果:
1
float4 PSfun(PSInput input) : COLOR0
2
{
3
//动的
4
float y = input.texcoord.y;
5
float x = input.texcoord.x;
6
y = y + (sin(x*100)*0.01);
7
float4 color = float4(tex2D(TextureSampler, float2(x,y))*AmbientColor);
8
return( color );
9
}
float4 PSfun(PSInput input) : COLOR02
{3
//动的4
float y = input.texcoord.y;5
float x = input.texcoord.x;6
y = y + (sin(x*100)*0.01);7
float4 color = float4(tex2D(TextureSampler, float2(x,y))*AmbientColor);8
return( color );9
}




浙公网安备 33010602011771号