XNA 3.1 转换到 XNA4.0 的备忘录
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... |
posted on 2011-02-01 22:57 Meta.Grfx 阅读(6070) 评论(3) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?