本文原创版权归 博客园 Meta.Grfx 所有,转载请详细标明原创作者及出处,以示尊重!

作者:Meta.Grfx
原文:http://www.cnblogs.com/Baesky/archive/2011/02/01/1948789.html

xna3.1与4.0的区别不小,但也不是很大,在转换一些项目时候下面的tip能给些帮助。原文地址是:http://blogs.msdn.com/b/shawnhar/archive/2011/01/04/xna-3-1-to-4-0-cheat-sheet.aspx?utm_source=twitterfeed&utm_medium=twitter

遇到如下问题,该备忘录将解决的问题:

The name ‘SpriteBlendMode‘ does not exist in the current context
The name ‘SaveStateMode‘ does not exist in the current context

‘Microsoft.Xna.Framework.Graphics.GraphicsDevice‘ does not contain a definition for ‘RenderState‘…

‘Microsoft.Xna.Framework.Graphics.Effect‘ does not contain a definition for ‘Begin‘ …
‘Microsoft.Xna.Framework.Graphics.Effect‘ does not contain a definition for ‘End‘..
‘Microsoft.Xna.Framework.Graphics.Effect‘ does not contain a definition for ‘CommitChanges‘ …
‘Microsoft.Xna.Framework.Graphics.EffectPass‘ does not contain a definition for ‘Begin‘ …
‘Microsoft.Xna.Framework.Graphics.EffectPass‘ does not contain a definition for ‘End‘ ….
No overload for method ‘Clone‘ takes 1 arguments

The name ‘ShaderProfile‘ does not exist in the current context
‘Microsoft.Xna.Framework.GameTime‘ does not contain a definition for ‘TotalRealTime‘ …
‘Microsoft.Xna.Framework.Color‘ does not contain a definition for ‘TransparentBlack‘ …

The type or namespace name ‘ResolveTexture2D‘ could not be found …
‘Microsoft.Xna.Framework.Graphics.GraphicsDevice‘ does not contain a definition for ‘ResolveBackBuffer‘…
The type or namespace name ‘DepthStencilBuffer‘ could not be found …

‘Microsoft.Xna.Framework.Graphics.RenderTarget2D‘ does not contain a constructor that takes 5 arguments
‘Microsoft.Xna.Framework.Graphics.RenderTarget2D‘ does not contain a definition for ‘GetTexture‘ …

‘Microsoft.Xna.Framework.Graphics.PresentationParameters‘ does not contain a definition for ‘MultiSampleType‘ …
‘Microsoft.Xna.Framework.Graphics.PresentationParameters‘ does not contain a definition for ‘MultiSampleQuality‘ …

The best overloaded method match for ‘Microsoft.Xna.Framework.Graphics.GraphicsDevice.SetRenderTarget

‘Microsoft.Xna.Framework.Graphics.GraphicsDevice‘ does not contain a definition for ‘VertexDeclaration
‘Microsoft.Xna.Framework.Graphics.GraphicsDevice‘ does not contain a definition for ‘Vertices

‘Microsoft.Xna.Framework.Graphics.VertexPositionTexture‘ does not contain a definition for ‘SizeInBytes
‘Microsoft.Xna.Framework.Graphics.VertexPositionTexture‘ does not contain a definition for ‘VertexElements

‘Microsoft.Xna.Framework.Graphics.ModelMesh‘ does not contain a definition for ‘IndexBuffer
‘Microsoft.Xna.Framework.Graphics.ModelMesh‘ does not contain a definition for ‘VertexBuffer

‘Microsoft.Xna.Framework.Graphics.ModelMeshPart‘ does not contain a definition for ‘BaseVertex
‘Microsoft.Xna.Framework.Graphics.ModelMeshPart‘ does not contain a definition for ‘StreamOffset
‘Microsoft.Xna.Framework.Graphics.ModelMeshPart‘ does not contain a definition for ‘VertexStride

‘Microsoft.Xna.Framework.Storage.StorageContainer‘ does not contain a definition for ‘TitleLocation
‘Microsoft.Xna.Framework.Storage.StorageContainer‘ does not contain a definition for ‘Path
‘Microsoft.Xna.Framework.Storage.StorageDevice‘ does not contain a definition for ‘OpenContainer
‘Microsoft.Xna.Framework.GamerServices.Guide‘ does not contain a definition for ‘BeginShowStorageDeviceSelector
‘Microsoft.Xna.Framework.GamerServices.Guide‘ does not contain a definition for ‘EndShowStorageDeviceSelector

syntax error: unexpected token ‘VertexShader
syntax error: unexpected token ‘PixelShader
error X3539: ps_1_x is no longer supported

PS:其他问题,如xna模型画出边界,看起来透明或者丢失顶点以及看起来不对劲。

----------------------------------------------------------------------------------------

XNA 3.1 转为 XNA4.0 的例子:

SpriteBlendMode, SaveStateMode

?
1
2
3
4
5
6
7
// XNA 3.1
sprite.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
// XNA 4.0
sprite.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
  
// XNA 3.1
// 通过深度排列场景对象

?
1
2
3
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);
// XNA 4.0
// 通过深度排列场景对象

?
1
2
3
4
5
6
7
8
9
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
  
// XNA 3.1
sprite.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
// XNA 4.0
sprite.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
  
// XNA 3.1
// 画背景图

?
1
2
3
4
5
6
spriteBatch.Begin(SpriteBlendMode.None);
Viewport viewport = GraphicsDevice.Viewport;
spriteBatch.Draw(background, new Rectangle(0, 0, viewport.Width, viewport.Height), Color.White);
spriteBatch.End();
// XNA 4.0
// 画背景图

?
1
2
3
spriteBatch.Begin(0, BlendState.Opaque);
spriteBatch.Draw(background, GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.End();

RenderState

?
1
2
// XNA 3.1
// 启用alpha混合和深度写

?
1
2
3
4
5
6
7
8
9
10
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = true;
GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
GraphicsDevice.RenderState.AlphaSourceBlend = Blend.One;
GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.One;
GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
// XNA 4.0
// 启用alpha混合和深度写

?
1
2
3
4
5
GraphicsDevice.BlendState = BlendState.AlphaBlend;
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
  
// XNA 3.1
// 重置混合和深度写

?
1
2
3
4
5
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;
GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
// XNA 4.0
// 重置混合和深度写

?
1
2
3
4
5
GraphicsDevice.BlendState = BlendState.Additive;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  
// XNA 3.1
// 启用深度缓冲

?
1
2
3
GraphicsDevice.RenderState.DepthBufferEnable = true;
// XNA 4.0
// 启用深度缓冲

?
1
2
3
4
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  
// XNA 3.1
// 禁用深度缓冲

?
1
2
3
4
GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
GraphicsDevice.RenderState.DepthBufferEnable = false;
// XNA 4.0
// 禁用深度缓冲

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GraphicsDevice.DepthStencilState = DepthStencilState.None;
  
// XNA 3.1
// 累加混合(zero on alpha)
GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.SourceBlend = Blend.One;
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
// XNA 4.0
// 累加混合(zero on alpha)
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
  
// XNA 3.1
// 恢复混合模式

?
1
2
3
4
5
GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;
// XNA 4.0
// 恢复混合模式

?
1
2
3
4
5
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  
// XNA 3.1
// 设置alpha混合,无深度测试和深度写

?
1
2
3
4
5
6
7
GraphicsDevice.RenderState.DepthBufferEnable = false;
GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
// XNA 4.0
// 设置alpha混合,无深度测试和深度写

?
1
2
3
4
5
GraphicsDevice.DepthStencilState = DepthStencilState.None;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
  
// XNA 3.1
// 绘制3D模型时设置合适渲染模式

?
1
2
3
4
5
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.AlphaTestEnable = false;
GraphicsDevice.RenderState.DepthBufferEnable = true;
// XNA 4.0
// 绘制3D模型时设置合适渲染模式

?
1
2
3
4
5
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  
// XNA 3.1
// 加性混合

?
1
2
3
4
5
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.SourceBlend = Blend.One;
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
// XNA 4.0
// 加性混合

?
1
2
3
4
GraphicsDevice.BlendState = BlendState.Additive;
  
// XNA 3.1
// 设置加性混合

?
1
2
3
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
// XNA 4.0
// 设置加性混合

?
1
2
3
4
GraphicsDevice.BlendState = BlendState.Additive;
  
// XNA 3.1
// 设置alpha混合

?
1
2
3
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
// XNA 4.0
// 设置alpha混合

?
1
2
3
4
5
6
7
8
9
GraphicsDevice.BlendState = BlendState.AlphaBlend;
  
// XNA 3.1
GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
// XNA 4.0
 GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  
// XNA 3.1
// 设置渲染状态

?
1
2
3
4
5
6
7
8
9
10
11
12
13
GraphicsDevice.RenderState.DepthBufferEnable = false;
GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.SourceBlend = Blend.One;
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = true;
GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.Zero;
GraphicsDevice.RenderState.AlphaSourceBlend = Blend.Zero;
   
      // drawing code here..
   
// 恢复状态

?
1
2
3
4
5
6
GraphicsDevice.RenderState.DepthBufferEnable = true;
GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;
// XNA 4.0
//存储状态

?
1
2
3
4
DepthStencilState ds = GraphicsDevice.DepthStencilState;
BlendState bs = GraphicsDevice.BlendState;
   
//设置渲染状态

?
1
2
3
4
5
6
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
   
       // drawing code here..
   
// 恢复状态

?
1
2
GraphicsDevice.DepthStencilState = ds;
GraphicsDevice.BlendState = bs;

Effect, EffectPass, Begin(), End(), CommitChanges(), Clone()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
// 应用效果 XNA 3.1
blurEffect.CommitChanges();
blurEffect.Begin(SaveStateMode.SaveState);
blurEffect.CurrentTechnique.Passes[0].Begin();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
blurEffect.CurrentTechnique.Passes[0].End();
blurEffect.End();
// 应用效果 XNA 4.0
blurEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
  
// XNA 3.1
// 提交效果改变

?
1
2
3
effect.CommitChanges();
// XNA 4.0
//  当任何效果属性改变需要调用EffectPass.Apply()

?
1
// 否则就删除它
?
1
2
// XNA 3.1
// 开始一个效果

?
1
2
3
4
effect.Begin(SaveStateMode.SaveState);
effect.CurrentTechnique.Passes[0].Begin();
// XNA 4.0
// 开始一个效果

?
1
2
3
4
effect.CurrentTechnique.Passes[0].Apply();
  
// XNA 3.1
// 结束一个效果

?
1
2
3
4
effect.CurrentTechnique.Passes[0].End();
effect.End();
// XNA 4.0
// 如果不需要可直接删除

?
1
2
// XNA 3.1
// 产生一个效果的克隆体

?
1
2
3
Effect newEffect = replacementEffect.Clone(replacementEffect.GraphicsDevice);
// XNA 4.0
// 产生一个效果的克隆体

?
1
2
3
4
Effect newEffect = replacementEffect.Clone();
  
// XNA 3.1
// 启用效果中的tech

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
postprocessEffect.CurrentTechnique = postprocessEffect.Techniques[effectTechniqueName];     
   
// 用预处理效果画一个全屏的精灵.
spriteBatch.Begin(SpriteBlendMode.None,SpriteSortMode.Immediate, SaveStateMode.None);
   
postprocessEffect.Begin();
postprocessEffect.CurrentTechnique.Passes[0].Begin();
   
spriteBatch.Draw(sceneRenderTarget.GetTexture(), Vector2.Zero, Color.White);
spriteBatch.End();
   
postprocessEffect.CurrentTechnique.Passes[0].End();
postprocessEffect.End();
// XNA 4.0
// 启用效果中的tech

?
1
2
3
postprocessEffect.CurrentTechnique = postprocessEffect.Techniques[effectTechniqueName];
   
// 用预处理效果画一个全屏的精灵.

?
1
2
3
spriteBatch.Begin(0, BlendState.Opaque, null, null, null, postprocessEffect);
spriteBatch.Draw(sceneRenderTarget, Vector2.Zero, Color.White);
spriteBatch.End();

ShaderProfile, TotalRealTime, TransparentBlack

?
1
2
3
4
5
// XNA 3.1
graphics.MinimumPixelShaderProfile = ShaderProfile.PS_3_0; //any PS number...
graphics.MinimumVertexShaderProfile = ShaderProfile.VS_3_0;//any VS number...
// XNA 4.0
// 不再需要该语句

?
1
2
3
4
5
6
7
8
9
// XNA 3.1
float myTime = (float)gameTime.TotalRealTime.TotalSeconds * 0.2f;
// XNA 4.0
float myTime = (float)gameTime.TotalGameTime.TotalSeconds * 0.2f;
  
// XNA 3.1
GraphicsDevice.Clear(Color.TransparentBlack);
// XNA 4.0
GraphicsDevice.Clear(Color.Transparent);

ResolveTexture2D, ResolveBackBuffer, RenderTarget2D, GetTexture, DepthStencilBuffer, PresentationParameters, MultiSampleType, MultiSampleQuality, SetRenderTarget

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// XNA 3.1
ResolveTexture2D sceneMap;
// XNA 4.0
RenderTarget2D sceneMap;
  
// XNA 3.1
// 查询主缓冲的分辨率和格式.
PresentationParameters pp = GraphicsDevice.PresentationParameters;
   
// 从缓冲读取一个材质数据.
sceneMap = new ResolveTexture2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1, pp.BackBufferFormat);
// XNA 4.0
// 查询主缓冲的分辨率和格式.
PresentationParameters pp = GraphicsDevice.PresentationParameters;
   
// 从缓冲读取一个材质数据.
sceneMap = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false, pp.BackBufferFormat,
                                pp.DepthStencilFormat);
//or 
   
sceneMap = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false, pp.BackBufferFormat,
                                pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
  
// XNA 3.1
GraphicsDevice.ResolveBackBuffer(sceneMap);
// XNA 4.0
GraphicsDevice.SetRenderTarget(sceneMap);
  
// XNA 3.1
int width = GraphicsDevice.Viewport.Width;
int height = GraphicsDevice.Viewport.Height;
   
// 创建渲染对象

?
1
2
3
4
5
6
myRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, 1, SurfaceFormat.Color);
// XNA 4.0
int width = GraphicsDevice.Viewport.Width;
int height = GraphicsDevice.Viewport.Height;
   
// 创建渲染对象

?
1
2
3
4
5
6
myRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, true, SurfaceFormat.Color, DepthFormat.Depth24);
  
// XNA 3.1
PresentationParameters pp = GraphicsDevice.PresentationParameters;
   
// 创建自动以渲染对象

?
1
2
3
4
5
6
sceneRenderTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1,
                                   pp.BackBufferFormat, pp.MultiSampleType, pp.MultiSampleQuality);
// XNA 4.0
PresentationParameters pp = GraphicsDevice.PresentationParameters;
   
// 创建自动以渲染对象

?
1
2
3
4
5
6
7
8
sceneRenderTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false,
                                   pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
                                   RenderTargetUsage.DiscardContents);
  
// XNA 3.1
PresentationParameters pp = GraphicsDevice.PresentationParameters;
   
// 配置一个深度缓冲

?
1
2
3
4
5
6
7
8
9
drawBuffer = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1,
                                SurfaceFormat.Color, pp.MultiSampleType, pp.MultiSampleQuality);
   
drawDepthBuffer = new DepthStencilBuffer(GraphicsDevice, pp.AutoDepthStencilFormat,
                                            pp.MultiSampleType, pp.MultiSampleQuality);
// XNA 4.0
PresentationParameters pp = GraphicsDevice.PresentationParameters;
   
// 配置一个深度缓冲

?
1
2
3
4
drawBuffer = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, true,
                                SurfaceFormat.Color,DepthFormat.Depth24Stencil8,
                                pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
//NOTE:  DepthStencilBuffer 类不再存在

?
1
2
3
4
// XNA 3.1
spriteBatch.Draw(myRenderTarget.GetTexture(), Vector2.Zero, Color.White);
// XNA 4.0
spriteBatch.Draw(myRenderTarget, Vector2.Zero, Color.White); // NOTE: ".GetTexure()"  不再需要

?
1
2
3
4
5
6
7
8
9
10
11
12
// XNA 3.1
Texture2D myTexture = myRenderTarget.GetTexture();
// XNA 4.0
Texture2D myTexture = myRenderTarget; // NOTE: ".GetTexure()"  No longer needed
  
// XNA 3.1
GraphicsDevice.SetRenderTarget(0, myRenderTarget);
// XNA 4.0
GraphicsDevice.SetRenderTarget(myRenderTarget);
  
// XNA 3.1
// 设置两个渲染目标

?
1
2
3
4
GraphicsDevice.SetRenderTarget(0, colorRT);
GraphicsDevice.SetRenderTarget(1, depthRT);
// XNA 4.0
// 设置两个渲染目标

?
1
2
3
4
5
6
7
8
9
GraphicsDevice.SetRenderTargets(colorRT, depthRT);
  
// XNA 3.1
GraphicsDevice.SetRenderTarget(0, null);
// XNA 4.0
GraphicsDevice.SetRenderTarget(null);
  
// XNA 3.1
// 将深度缓冲解析为深度图

?
1
2
3
GraphicsDevice.ResolveBackBuffer(depthMap);
   
// 绘制场景图, 用深度图模糊它

?
1
2
3
4
5
6
GraphicsDevice.Textures[1] = depthMap;
Viewport viewport = GraphicsDevice.Viewport;
dofEffect.CurrentTechnique = depthBlurTechnique;
DrawFullscreenQuad(sceneMap, viewport.Width, viewport.Height, dofEffect);
// XNA 4.0
// 将深度缓冲解析为深度图

?
1
2
3
GraphicsDevice.SetRenderTarget(null);
   
// 绘制场景图, 用深度图模糊它

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
GraphicsDevice.Textures[1] = depthMap;
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
Viewport viewport = GraphicsDevice.Viewport;
dofEffect.CurrentTechnique = depthBlurTechnique;
DrawFullscreenQuad(sceneMap, viewport.Width, viewport.Height, dofEffect);
  
// XNA 3.1
ResolveTexture2D resolveTarget;
RenderTarget2D renderTarget1;
RenderTarget2D renderTarget2;
   
// 查找主缓冲的分辨率和格式.
PresentationParameters pp = GraphicsDevice.PresentationParameters;
   
int width = pp.BackBufferWidth;
int height = pp.BackBufferHeight;
SurfaceFormat format = pp.BackBufferFormat;
   
// 创建一个材质读取缓冲中的内容.
resolveTarget = new ResolveTexture2D(GraphicsDevice, width, height, 1, format);
   
// Create two rendertargets half size for the bloom processing.
width /= 2;
height /= 2;
   
renderTarget1 = new RenderTarget2D(GraphicsDevice, width, height, 1,format);
renderTarget2 = new RenderTarget2D(GraphicsDevice, width, height, 1,format);
   
// ... In the Draw Method...
 GraphicsDevice.ResolveBackBuffer(resolveTarget);
 // ...apply effect and draw pass 1...
// XNA 4.0
RenderTarget2D sceneRenderTarget;
RenderTarget2D renderTarget1;
RenderTarget2D renderTarget2;
   
// 查找主缓冲的分辨率和格式. .
PresentationParameters pp = GraphicsDevice.PresentationParameters;
   
int width = pp.BackBufferWidth;
int height = pp.BackBufferHeight;
SurfaceFormat format = pp.BackBufferFormat;
   
// Create a texture for rendering the main scene, prior to applying bloom.
sceneRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, false,
                                       format, pp.DepthStencilFormat, pp.MultiSampleCount,
                                       RenderTargetUsage.DiscardContents);
   
// Create two rendertargets half size for the bloom processing.
width /= 2;
height /= 2;
   
renderTarget1 = new RenderTarget2D(GraphicsDevice, width, height, false, format, DepthFormat.None);
renderTarget2 = new RenderTarget2D(GraphicsDevice, width, height, false, format, DepthFormat.None);
   
// ...In the Draw Method...
GraphicsDevice.SetRenderTarget(sceneRenderTarget);
GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;
// ...apply effect and draw pass 1....

VertexDeclaration, Vertices, VertexElements, SizeInBytes

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// XNA 3.1
// Vertex declaration for rendering our 3D model.
 GraphicsDevice.VertexDeclaration = new VertexDeclaration(VertexPositionTexture.VertexElements);
// XNA 4.0
// Delete it. No longer needed.
  
// XNA 3.1
// set vertex buffer and declaration
GraphicsDevice.VertexDeclaration = vertexDeclaration;
GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionTexture.SizeInBytes);
// XNA 4.0
// set vertex buffer and declaration
GraphicsDevice.SetVertexBuffer(vertexBuffer);
  
// XNA 3.1
// create vertex declaration
vertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionTexture.VertexElements);
// XNA 4.0
// create vertex declaration
vertexDeclaration = new VertexDeclaration(VertexPositionTexture.VertexDeclaration.GetVertexElements());
  
// XNA 3.1
// reset vertex buffer declaration
GraphicsDevice.VertexDeclaration = null;
GraphicsDevice.Vertices[0].SetSource(null, 0, 0);
// XNA 4.0
// reset vertex buffer declaration
GraphicsDevice.SetVertexBuffer(null);
  
// XNA 3.1
// the vertices array
VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[100];
   
 // set new particles to vertex buffer
vertexBuffer.SetData<VertexPositionNormalTexture>(VertexPositionNormalTexture.SizeInBytes * vertexCount,
                    vertices,vertexCount,count,VertexPositionNormalTexture.SizeInBytes);
// XNA 4.0
// the vertices array
VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[100];
   
// set new particles to vertex buffer
vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);

?
1
  
?
1
VertexBuffer, StreamOffset, VertexStride, IndexBuffer, BaseVertex
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// XNA 3.1
// for each mesh part
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
    // if primitives to render
    if (meshPart.PrimitiveCount > 0)
    {
        // setup vertices and indices
        GraphicsDevice.VertexDeclaration = meshPart.VertexDeclaration;
        GraphicsDevice.Vertices[0].SetSource(mesh.VertexBuffer, meshPart.StreamOffset, meshPart.VertexStride);
        GraphicsDevice.Indices = mesh.IndexBuffer;
        ...
// XNA 4.0
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; // may be needed in some cases...
   
// for each mesh part
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
    // if primitives to render
    if (meshPart.PrimitiveCount > 0)
    {
        // setup vertices and indices
        GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
        GraphicsDevice.Indices = meshPart.IndexBuffer;
        ...
  
// XNA 3.1
// draw primitives
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
    meshPart.BaseVertex, 0, meshPart.NumVertices,
    meshPart.StartIndex, meshPart.PrimitiveCount);
// XNA 4.0
// draw primitives
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
    meshPart.VertexOffset, 0, meshPart.NumVertices,
    meshPart.StartIndex, meshPart.PrimitiveCount);

Points, PointSpriteEnable, PointSizeMax, PointList

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// XNA 3.1
 // create the vertex buffer
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture),
                                250, BufferUsage.WriteOnly | BufferUsage.Points);
// XNA 4.0
// create the vertex buffer
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture),
                                250, BufferUsage.WriteOnly | BufferUsage.None);
  
// XNA 3.1
// enable point sprite 3.1
GraphicsDevice.RenderState.PointSpriteEnable = true;
GraphicsDevice.RenderState.PointSizeMax = 128;
// XNA 4.0
// Delete it. No longer available.
  
// XNA 3.1
// draw the point sprites
GraphicsDevice.DrawPrimitives(PrimitiveType.PointList, vertexPosition, numberVertices);
// XNA 4.0
// draw the point sprites
GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, vertexPosition, numberVertices);

OpenContainer, BeginShowStorageDeviceSelector, EndShowStorageDeviceSelector, Path, TitleLocation, FileStream

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// XNA 3.1
// open the container
StorageContainer storageContainer = storageDevice.OpenContainer("YourGameName");
// XNA 4.0
//To make life easier simply create a method to replace the storageDevice.OpenContainer... 
   
/// <summary>
/// Synchronously opens storage container
/// </summary>
private static StorageContainer OpenContainer(StorageDevice storageDevice, string saveGameName)
{
    IAsyncResult result = storageDevice.BeginOpenContainer(saveGameName, null, null);
   
    // Wait for the WaitHandle to become signaled.
    result.AsyncWaitHandle.WaitOne();
   
    StorageContainer container = storageDevice.EndOpenContainer(result);
   
    // Close the wait handle.
    result.AsyncWaitHandle.Close();
   
    return container;
}
   
// open the container
StorageContainer storageContainer = OpenContainer(storageDevice, "YourGameName");
  
// XNA 3.1
// retrieve the storage device
Guide.BeginShowStorageDeviceSelector(GetStorageDeviceResult, retrievalDelegate);
// XNA 4.0
// retrieve the storage device
if (!Guide.IsVisible)
{
    StorageDevice.BeginShowSelector(GetStorageDeviceResult, retrievalDelegate);
}
  
// XNA 3.1
 // retrieve and store the storage device
 storageDevice = Guide.EndShowStorageDeviceSelector(result);
// XNA 4.0
// retrieve and store the storage device
 storageDevice = StorageDevice.EndShowSelector(result);
  
// XNA 3.1
// get the level setup files
 string[] filenames = Directory.GetFiles(storageContainer.Path, "LevelSetup*.xml");
// XNA 4.0
// get the level setup files
string[] filenames = storageContainer.GetFileNames("LevelSetup*.xml"");
  
// XNA 3.1
// save game level data
using (FileStream stream = new FileStream(Path.Combine(storageContainer.Path, levelFilename), FileMode.Create))
{
    new XmlSerializer(typeof(SaveGameLevel)).Serialize(stream, levelData);
}
// XNA 4.0
// save game level data
using (Stream stream = storageContainer.OpenFile(levelFilename, FileMode.Create))
{
    new XmlSerializer(typeof(SaveGameLevel)).Serialize(stream, levelData);
}
  
// XNA 3.1
// Delete the saved game level data.
using (StorageContainer storageContainer = storageDevice.OpenContainer("saveGameName"))
{
    File.Delete(Path.Combine(storageContainer.Path, saveGameLevel.FileName));
   
    File.Delete(Path.Combine(storageContainer.Path, "SaveGameLevel" +
        Path.GetFileNameWithoutExtension(saveGameLevel.FileName).Substring(8) +".xml"));
}
// XNA 4.0
// Delete the saved game level data. NOTE: using OpenContainer method created in previous example
using (StorageContainer storageContainer = OpenContainer(storageDevice, "saveGameName"))
{
    storageContainer.DeleteFile(saveGameLevel.FileName);
   
    storageContainer.DeleteFile("SaveGameLevel" +
        Path.GetFileNameWithoutExtension(saveGameLevel.FileName).Substring(8) + ".xml");
}
  
// XNA 3.1
//Load the Next Level...
   
// Find the path of the next level.
string levelPath;
   
// Loop here so we can try again when we can't find a level.
while (true)
{
    // Try to find the next level. They are sequentially numbered txt files.
    levelPath = String.Format("Levels/{0}.txt", ++levelIndex);
    levelPath = Path.Combine(StorageContainer.TitleLocation, "Content/" + levelPath);
    if (File.Exists(levelPath))
        break;
   
    // If there isn't even a level 0, something has gone wrong.
    if (levelIndex == 0)
        throw new Exception("No levels found.");
   
    // Whenever we can't find a level, start over again at 0.
    levelIndex = -1;
}
   
// Unloads the content for the current level before loading the next one.
if (level != null)
    level.Dispose();
   
// Load the level.
level = new Level(Services, levelPath);
// XNA 4.0
// Load the Next Level...
   
// move to the next level
levelIndex = (levelIndex + 1) % numberOfLevels;
   
// Unloads the content for the current level before loading the next one.
if (level != null)
    level.Dispose();
   
// Load the level.
string levelPath = string.Format("Content/Levels/{0}.txt", levelIndex);
using (Stream fileStream = TitleContainer.OpenStream(levelPath))
    level = new Level(Services, fileStream, levelIndex);
  
  
// XNA 3.1
//Save the current state of the session, with the given storage device. 
   
// check the parameter
if ((storageDevice == null) || !storageDevice.IsConnected)
{
    return;
}
   
// open the container
using (StorageContainer storageContainer = storageDevice.OpenContainer(Session.SaveGameContainerName))
{
    string filename;
    string descriptionFilename;
   
    // get the filenames
    if (overwriteDescription == null)
    {
        int saveGameIndex = 0;
        string testFilename;
        do
        {
            saveGameIndex++;
            testFilename = Path.Combine(storageContainer.Path, "SaveGame" + saveGameIndex.ToString() + ".xml");
        }
        while (File.Exists(testFilename));
        filename = testFilename;
        descriptionFilename = "SaveGameDescription" + saveGameIndex.ToString() + ".xml";
    }
    else
    {
        filename = Path.Combine(storageContainer.Path, overwriteDescription.FileName);
        descriptionFilename = "SaveGameDescription" +
            Path.GetFileNameWithoutExtension(overwriteDescription.FileName).Substring(8) + ".xml";
    }
   
    using (FileStream stream = new FileStream(filename, FileMode.Create))
    {
        using (XmlWriter xmlWriter = XmlWriter.Create(stream))
        {
            //create and write xml data...
        }
    }
   
   // create the save game description
    SaveGameDescription description = new SaveGameDescription();
    description.FileName = Path.GetFileName(filename);
    description.ChapterName = IsQuestLineComplete ? "Quest Line Complete" : Quest.Name;
    description.Description = DateTime.Now.ToString();
   
    using (FileStream stream = new FileStream(Path.Combine(storageContainer.Path, descriptionFilename), FileMode.Create))
    {
        new XmlSerializer(typeof(SaveGameDescription)).Serialize(stream, description);
    }
}
// XNA 4.0
//Save the current state of the session, with the given storage device. 
   
// check the parameter
if ((storageDevice == null) || !storageDevice.IsConnected)
{
    return;
}
   
// open the container Note: using OpenContainer method from previous examples
using (StorageContainer storageContainer = OpenContainer(storageDevice, Session.SaveGameContainerName))
{
    string filename;
    string descriptionFilename;
    // get the filenames
    if (overwriteDescription == null)
    {
        int saveGameIndex = 0;
        string testFilename;
        do
        {
            saveGameIndex++;
            testFilename = "SaveGame" + saveGameIndex.ToString() + ".xml";
        }
        while (storageContainer.FileExists(testFilename));
        filename = testFilename;
        descriptionFilename = "SaveGameDescription" + saveGameIndex.ToString() + ".xml";
    }
    else
    {
        filename = overwriteDescription.FileName;
        descriptionFilename = "SaveGameDescription" +
            Path.GetFileNameWithoutExtension(overwriteDescription.FileName).Substring(8) + ".xml";
    }
   
    // Note: using Stream instead of FileStream...
    using (Stream stream = storageContainer.OpenFile(filename, FileMode.Create))
    {
        using (XmlWriter xmlWriter = XmlWriter.Create(stream))
        {
            //create and write xml data...
        }
    }
   
    // create the save game description
    SaveGameDescription description = new SaveGameDescription();
    description.FileName = Path.GetFileName(filename);
    description.ChapterName = IsQuestLineComplete ? "Quest Line Complete" : Quest.Name;
    description.Description = DateTime.Now.ToString();
   
    // Note: using Stream instead of FileStream...
    using (Stream stream = storageContainer.OpenFile(descriptionFilename, FileMode.Create))
    {
        new XmlSerializer(typeof(SaveGameDescription)).Serialize(stream, description);
    }
}

?
1
  
?
1
VertexShader, PixelShader, ps_1_x
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// XNA 3.1
VertexShaderOutput VertexShader(...)
{
    //some code
}
float4 PixelShader(...)
{
   // some code
}
// XNA 4.0
// VertexShader can not be used
VertexShaderOutput  VertexShaderFunction(...)
{
   // some code
}
// PixelShader can not be used
float4 PixelShaderFunction(...)
{
   // some code
}
  
// XNA 3.1
technique
{
    pass
    {
        VertexShader = compile vs_1_1 VertexShader();
        PixelShader  = compile ps_1_1 PixelShader();
    }
}
// XNA 4.0
technique
{
    pass
    {
        VertexShader = compile vs_2_0 VertexShaderFunction(); //VertexShader can not be used & set vs higher than 1_1
        PixelShader  = compile ps_2_0 PixelShaderFunction(); //PixelShader can not be used & set ps higher than 1_1
    }
}

XNA Model drawn inside out, slightly transparent, missing parts or just looks wrong

?
1
2
3
4
5
// Set suitable renderstates for drawing a 3D model
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
   
// your model draw code starts here...

XNA 3.1 转换到 XNA4.0 的备忘录相关推荐

  1. [转]XNA 3.1 转换到 XNA4.0 的备忘录

    XNA 3.1 转换到 XNA4.0 的备忘录 xna3.1与4.0的区别不小,但也不是很大,在转换一些项目时候下面的tip能给些帮助.原文地址是:http://blogs.msdn.com/b/sh ...

  2. QQ超市模拟排配2D版 1.08 (XNA4.0)

    XNA4.0开发的.学XNA后的练手作品. 功能: 1.模拟配置货架    添加.删除.移动.旋转功能齐全    可设置双面宝石和4面宝石.    自动寻路 2.地图齐全.可保存. 3.实时自动计算当 ...

  3. QQ超市模拟排配3D版 1.1 (XNA4.0)

    XNA4.0学习过程中,练手方便,就把2D的改为3D的了.功能基本没变化. 加了一些学习过程中学到的东西而已.山水.草地~ 超市货架地板等都是自己用3ds max2011建模,材质球改了下颜色,随便加 ...

  4. 如何转换高度:0; 达到高度:自动; 使用CSS?

    我正在尝试使用CSS过渡使<ul>滑落. <ul>从height: 0; . 悬停时,将高度设置为height:auto; . 但是,这导致它只是显示而不是过渡, 如果我从he ...

  5. [代码发布]中文文字转换组件 1.0,支持VB/ASP编程

    中文文字转换组件 1.0 *************************************************************** 版本历史: ***************** ...

  6. qt QString数值转换格式化不足补0 QString十进制转换不足补0

    int num = 1; QString("%1").arg(num,2, 10, QChar('0')); 这样输出的就是01. 其中2代表要输出几位,10代表10进制转换,QC ...

  7. 智能实验室-专用链转换 1.5.0.150

    专用链转换(SpecialLinkio)是用以双向转换专用链的工具. 特点: 1.支持格式:支持市面上的各种格式,包括网际快车(FlashGet).迅雷(Thunder).QQ旋风..FS2You等: ...

  8. QQ超市模拟排配2D版1.13 (XNA4.0) (修正双格货架移动的一个bug和3-5地图)

    抱歉,更新了一个地图-- 下载地址:(版本过期了,请下新版) 1.13:更新日期:2012-3-22 更新3店5口地图错误问题.启动程序前请手动删除旧版地图数据. 地址:C:\(我的文档路径)\Sav ...

  9. 【XNA4.0】动画

    2D XNA游戏中的动画的制作过程很像卡通手翻书.动画制作包含着大量的独立图像,通过在一个周期内图像间的快速切换来使它们显示为动画.通常精灵动画存放在图片文件中,您需要用某种顺序把图片上独立的图像提取 ...

最新文章

  1. 2021年大数据Spark(十七):Spark Core的RDD持久化
  2. CentOS7安装Composer
  3. 戴尔PowerEdge-C服务器新成员
  4. 通过崩溃地址找错误行数之Delphi版
  5. fiddler filters 使用(fiddler只显示指定请求,fiddler不显示指定请求,即filter请求过滤)转自:http://blog.csdn.net/notejs/article/
  6. One or more breakpoints cannot be set and have been disabled
  7. Mongodb基本操作说明
  8. Spark学习笔记(7)---Spark SQL学习笔记
  9. WPF 实现控件间拖拽内容
  10. 简易语音助手—python
  11. word模板中替换文本中自定义字符串生成月报
  12. 计算机技术工种技师,技师10个职业(工种)国家职业标准要求申报条件
  13. 人工智能重新定义管理
  14. 永久域名注册流程知识
  15. MySQL数据库视图:视图定义、创建视图、修改视图
  16. pytorch系列(八):猫狗大战3-单机多卡无脑训练
  17. Zerg 族历史简介
  18. JS如何获取屏幕、浏览器及网页高度宽度?
  19. U盘容量变小?这儿有解决方法!
  20. SSM框架将数据库数据导出为Excel文件

热门文章

  1. react redux 简化_Redux 源码解析
  2. 经典Java-SpringCloud面试题
  3. 洛谷——P1548 [NOIP1997 普及组] 棋盘问题
  4. Angular 在项目中使用fullcalendar 日程表
  5. 西农 生成树配置_配置STP功能
  6. Confluence 6 针对合并完全失败的内容重新运行合并
  7. 开源大数据周刊-第22期
  8. 郭宏志的android无线点餐系统,Android无线点餐系统--含代码.doc
  9. mysql配合memcache部署_Docker多容器配合实现开发环境(nginx、php、memcached、mysql)...
  10. 飘逸的python - yield简明教程