HLSL高级应用实例

   废话少说,直接入题。

先给出整个应用程序构架:

Code
  1using System;
  2using System.Collections.Generic;
  3using Microsoft.Xna.Framework;
  4using Microsoft.Xna.Framework.Audio;
  5using Microsoft.Xna.Framework.Content;
  6using Microsoft.Xna.Framework.GamerServices;
  7using Microsoft.Xna.Framework.Graphics;
  8using Microsoft.Xna.Framework.Input;
  9using Microsoft.Xna.Framework.Media;
 10using Microsoft.Xna.Framework.Net;
 11using Microsoft.Xna.Framework.Storage;
 12
 13namespace HLSLDemo1
 14{
 15    /**//// <summary>
 16    /// This is the main type for your game
 17    /// </summary>

 18    public class Game1 : Microsoft.Xna.Framework.Game
 19    {
 20        GraphicsDeviceManager graphics;
 21        SpriteBatch spriteBatch;
 22
 23        Effect effe;
 24        Model mod;
 25        Texture2D texture;
 26
 27        Matrix world;
 28        Matrix view;
 29        Matrix projection;
 30
 31        float time = 0f;
 32
 33        public Game1()
 34        {
 35            graphics = new GraphicsDeviceManager(this);
 36            Content.RootDirectory = "Content";
 37        }

 38
 39        protected override void Initialize()
 40        {
 41            world = Matrix.Identity;
 42            view = Matrix.CreateLookAt(new Vector3(00100), Vector3.Forward, Vector3.Up);
 43            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 11000);
 44
 45            base.Initialize();
 46        }

 47
 48        protected override void LoadContent()
 49        {
 50            // Create a new SpriteBatch, which can be used to draw textures.
 51            spriteBatch = new SpriteBatch(GraphicsDevice);
 52            mod = Content.Load<Model>("cub");
 53            texture = Content.Load<Texture2D>("des");
 54            effe = Content.Load<Effect>("effect1");
 55
 56        }

 57
 58        protected override void Update(GameTime gameTime)
 59        {
 60            KeyboardState ks = Keyboard.GetState();
 61
 62            相机平移#region 相机平移
 63
 64            if (ks.IsKeyDown(Keys.W))
 65            {
 66                view *= Matrix.CreateTranslation(000.5f);
 67            }

 68            if (ks.IsKeyDown(Keys.S))
 69            {
 70                view *= Matrix.CreateTranslation(00-0.5f);
 71            }

 72
 73            if (ks.IsKeyDown(Keys.A))
 74            {
 75                view *= Matrix.CreateTranslation(-0.5f00);
 76            }

 77            if (ks.IsKeyDown(Keys.D))
 78            {
 79                view *= Matrix.CreateTranslation(0.5f00);
 80            }

 81
 82            if (ks.IsKeyDown(Keys.Q))
 83            {
 84                view *= Matrix.CreateTranslation(00.5f0);
 85            }

 86            if (ks.IsKeyDown(Keys.E))
 87            {
 88                view *= Matrix.CreateTranslation(0-0.5f0);
 89            }

 90
 91            #endregion

 92
 93            相机旋转#region 相机旋转
 94
 95            if (ks.IsKeyDown(Keys.Left))
 96            {
 97                view *= Matrix.CreateRotationY(0.01f);
 98            }

 99            if (ks.IsKeyDown(Keys.Right))
100            {
101                view *= Matrix.CreateRotationY(-0.01f);
102            }

103            if (ks.IsKeyDown(Keys.Up))
104            {
105                view *= Matrix.CreateRotationX(0.01f);
106            }

107            if (ks.IsKeyDown(Keys.Down))
108            {
109                view *= Matrix.CreateRotationX(-0.01f);
110            }

111
112            if (ks.IsKeyDown(Keys.PageUp))
113            {
114                view *= Matrix.CreateRotationZ(0.01f);
115            }

116            if (ks.IsKeyDown(Keys.PageDown))
117            {
118                view *= Matrix.CreateRotationZ(-0.01f);
119            }

120
121            #endregion

122
123            物体旋转#region 物体旋转
124
125            if (ks.IsKeyDown(Keys.NumPad4))
126            {
127                world *= Matrix.CreateRotationY(0.01f);
128            }

129            if (ks.IsKeyDown(Keys.NumPad6))
130            {
131                world *= Matrix.CreateRotationY(-0.01f);
132            }

133            if (ks.IsKeyDown(Keys.NumPad8))
134            {
135                world *= Matrix.CreateRotationX(0.01f);
136            }

137            if (ks.IsKeyDown(Keys.NumPad5))
138            {
139                world *= Matrix.CreateRotationX(-0.01f);
140            }

141
142            if (ks.IsKeyDown(Keys.NumPad7))
143            {
144                world *= Matrix.CreateRotationZ(0.01f);
145            }

146            if (ks.IsKeyDown(Keys.NumPad9))
147            {
148                world *= Matrix.CreateRotationZ(-0.01f);
149            }

150
151            #endregion

152
153            base.Update(gameTime);
154        }

155
156        /**//// <summary>
157        /// This is called when the game should draw itself.
158        /// </summary>
159        /// <param name="gameTime">Provides a snapshot of timing values.</param>

160        protected override void Draw(GameTime gameTime)
161        {
162            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
163
164            time += (float)gameTime.ElapsedGameTime.TotalSeconds;
165
166            effe.Parameters["World"].SetValue(world);
167            effe.Parameters["View"].SetValue(view);
168            effe.Parameters["Projection"].SetValue(projection);
169            effe.Parameters["Texture"].SetValue(texture);
170            effe.Parameters["AmbientColor"].SetValue(Color.YellowGreen.ToVector4());
171            effe.Parameters["time"].SetValue(time);
172
173            foreach(ModelMesh mesh in mod.Meshes)
174            {
175                foreach (ModelMeshPart part in mesh.MeshParts)
176                {
177                    part.Effect = effe;
178                }

179                mesh.Draw();
180            }

181
182            base.Draw(gameTime);
183        }

184    }

185}

186

HLSL代码如下:

HLSL
 1float4x4 World;
 2float4x4 View;
 3float4x4 Projection;
 4
 5float4 AmbientColor:COLOR0;
 6texture Texture;
 7float time:TIME;
 8float Offset=100;
 9
10sampler TextureSampler=sampler_state
11{
12    texture=<Texture>;
13    minfilter=LINEAR;
14    magfilter=LINEAR;
15}
;
16
17struct VPInput
18{
19    float4 position:POSITION;
20    float2 texcoord:TEXCOORD0;
21}
;
22
23struct VPOutput
24{
25    float4 position:POSITION;
26    float2 texcoord:TEXCOORD0;
27}
;
28
29struct PSInput
30{
31    float2 texcoord:TEXCOORD0;
32}
;
33
34VPOutput VSfun(VPInput input)
35{
36    VPOutput output;
37    output.position=mul(input.position,mul(mul(World,View),Projection));
38    output.texcoord=input.texcoord;
39    return output;
40}

41
42float4 PSfun(PSInput input) : COLOR0
43{
44    return float4(tex2D(TextureSampler,input.texcoord)*AmbientColor);
45}

46
47technique Technique1
48{
49    pass Pass1
50    {
51        VertexShader = compile vs_2_0 VSfun();
52        PixelShader = compile ps_2_0 PSfun();
53    }

54}
       
55   
56

这是默认的效果,效果图如下:

  HLSL高级应用实例_第1张图片

负片效果:

1 float4 PSfun(PSInput input) : COLOR0
2 {
3    //负片效果
4    return(1- float4(color*AmbientColor) );
5}

HLSL高级应用实例_第2张图片

模糊效果:

 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}

 

HLSL高级应用实例_第3张图片

浮雕效果:

 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}

HLSL高级应用实例_第4张图片

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.30.590.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}

 

HLSL高级应用实例_第5张图片

粉笔效果:

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}

HLSL高级应用实例_第6张图片

波动效果:

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}

HLSL高级应用实例_第7张图片

你可能感兴趣的:(HLSL高级应用实例)