XNA 3.1 轉(zhuǎn)換到 XNA4.0 的備忘錄 xna3.1與4.0的區(qū)別不小,但也不是很大,在轉(zhuǎn)換一些項目時候下面的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 轉(zhuǎn)為 XNA4.0 的例子:
SpriteBlendMode, SaveStateMode
view sourceprint?
2 sprite.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
4 sprite.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
view sourceprint?
1 spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);
view sourceprint?
1 spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
4 sprite.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
6 sprite.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
view sourceprint?
1 spriteBatch.Begin(SpriteBlendMode.None);
2 Viewport viewport = GraphicsDevice.Viewport;
3 spriteBatch.Draw(background, new Rectangle(0, 0, viewport.Width, viewport.Height), Color.White);
view sourceprint?
1 spriteBatch.Begin(0, BlendState.Opaque);
2 spriteBatch.Draw(background, GraphicsDevice.Viewport.Bounds, Color.White);
RenderState
view sourceprint?
view sourceprint?
01 GraphicsDevice.RenderState.AlphaBlendEnable = true ;
02 GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
03 GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
04 GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = true ;
05 GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
06 GraphicsDevice.RenderState.AlphaSourceBlend = Blend.One;
07 GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.One;
08 GraphicsDevice.RenderState.DepthBufferWriteEnable = false ;
view sourceprint?
1 GraphicsDevice.BlendState = BlendState.AlphaBlend;
2 GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
view sourceprint?
1 GraphicsDevice.RenderState.AlphaBlendEnable = false ;
2 GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false ;
3 GraphicsDevice.RenderState.DepthBufferWriteEnable = true ;
view sourceprint?
1 GraphicsDevice.BlendState = BlendState.Additive;
2 GraphicsDevice.DepthStencilState = DepthStencilState.Default;
view sourceprint?
1 GraphicsDevice.RenderState.DepthBufferEnable = true ;
view sourceprint?
1 GraphicsDevice.DepthStencilState = DepthStencilState.Default;
view sourceprint?
1 GraphicsDevice.RenderState.DepthBufferWriteEnable = false ;
2 GraphicsDevice.RenderState.DepthBufferEnable = false ;
view sourceprint?
01 GraphicsDevice.DepthStencilState = DepthStencilState.None;
05 GraphicsDevice.RenderState.DepthBufferWriteEnable = false ;
06 GraphicsDevice.RenderState.AlphaBlendEnable = true ;
07 GraphicsDevice.RenderState.SourceBlend = Blend.One;
08 GraphicsDevice.RenderState.DestinationBlend = Blend.One;
11 GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
12 GraphicsDevice.BlendState = BlendState.AlphaBlend;
view sourceprint?
1 GraphicsDevice.RenderState.DepthBufferWriteEnable = true ;
2 GraphicsDevice.RenderState.AlphaBlendEnable = false ;
3 GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false ;
view sourceprint?
1 GraphicsDevice.BlendState = BlendState.Opaque;
2 GraphicsDevice.DepthStencilState = DepthStencilState.Default;
5 // 設(shè)置alpha混合,無深度測試和深度寫
view sourceprint?
1 GraphicsDevice.RenderState.DepthBufferEnable = false ;
2 GraphicsDevice.RenderState.DepthBufferWriteEnable = false ;
3 GraphicsDevice.RenderState.AlphaBlendEnable = true ;
4 GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
5 GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
7 // 設(shè)置alpha混合,無深度測試和深度寫
view sourceprint?
1 GraphicsDevice.DepthStencilState = DepthStencilState.None;
2 GraphicsDevice.BlendState = BlendState.AlphaBlend;
view sourceprint?
1 GraphicsDevice.RenderState.AlphaBlendEnable = false ;
2 GraphicsDevice.RenderState.AlphaTestEnable = false ;
3 GraphicsDevice.RenderState.DepthBufferEnable = true ;
view sourceprint?
1 GraphicsDevice.BlendState = BlendState.Opaque;
2 GraphicsDevice.DepthStencilState = DepthStencilState.Default;
view sourceprint?
1 GraphicsDevice.RenderState.AlphaBlendEnable = true ;
2 GraphicsDevice.RenderState.SourceBlend = Blend.One;
3 GraphicsDevice.RenderState.DestinationBlend = Blend.One;
view sourceprint?
1 GraphicsDevice.BlendState = BlendState.Additive;
view sourceprint?
1 GraphicsDevice.RenderState.DestinationBlend = Blend.One;
view sourceprint?
1 GraphicsDevice.BlendState = BlendState.Additive;
view sourceprint?
1 GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
view sourceprint?
1 GraphicsDevice.BlendState = BlendState.AlphaBlend;
4 GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
6 GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
view sourceprint?
01 GraphicsDevice.RenderState.DepthBufferEnable = false ;
02 GraphicsDevice.RenderState.DepthBufferWriteEnable = false ;
03 GraphicsDevice.RenderState.AlphaBlendEnable = true ;
04 GraphicsDevice.RenderState.SourceBlend = Blend.One;
05 GraphicsDevice.RenderState.DestinationBlend = Blend.One;
06 GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = true ;
07 GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
08 GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.Zero;
09 GraphicsDevice.RenderState.AlphaSourceBlend = Blend.Zero;
11 ????? // drawing code here..
view sourceprint?
1 GraphicsDevice.RenderState.DepthBufferEnable = true ;
2 GraphicsDevice.RenderState.DepthBufferWriteEnable = true ;
3 GraphicsDevice.RenderState.AlphaBlendEnable = false ;
4 GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false ;
view sourceprint?
1 DepthStencilState ds = GraphicsDevice.DepthStencilState;
2 BlendState bs = GraphicsDevice.BlendState;
view sourceprint?
1 GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
2 GraphicsDevice.BlendState = BlendState.AlphaBlend;
4 ?????? // drawing code here..
view sourceprint?
1 GraphicsDevice.DepthStencilState = ds;
2 GraphicsDevice.BlendState = bs;
Effect, EffectPass, Begin(), End(), CommitChanges(), Clone()
view sourceprint?
02 blurEffect.CommitChanges();
03 blurEffect.Begin(SaveStateMode.SaveState);
04 blurEffect.CurrentTechnique.Passes[0].Begin();
05 GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
06 blurEffect.CurrentTechnique.Passes[0].End();
09 blurEffect.CurrentTechnique.Passes[0].Apply();
10 GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
view sourceprint?
3 //? 當(dāng)任何效果屬性改變需要調(diào)用EffectPass.Apply()
view sourceprint?
view sourceprint?
view sourceprint?
1 effect.Begin(SaveStateMode.SaveState);
2 effect.CurrentTechnique.Passes[0].Begin();
view sourceprint?
1 effect.CurrentTechnique.Passes[0].Apply();
view sourceprint?
1 effect.CurrentTechnique.Passes[0].End();
view sourceprint?
view sourceprint?
1 Effect newEffect = replacementEffect.Clone(replacementEffect.GraphicsDevice);
view sourceprint?
1 Effect newEffect = replacementEffect.Clone();
view sourceprint?
01 postprocessEffect.CurrentTechnique = postprocessEffect.Techniques[effectTechniqueName];????
04 spriteBatch.Begin(SpriteBlendMode.None,SpriteSortMode.Immediate, SaveStateMode.None);
06 postprocessEffect.Begin();
07 postprocessEffect.CurrentTechnique.Passes[0].Begin();
09 spriteBatch.Draw(sceneRenderTarget.GetTexture(), Vector2.Zero, Color.White);
12 postprocessEffect.CurrentTechnique.Passes[0].End();
13 postprocessEffect.End();
view sourceprint?
1 postprocessEffect.CurrentTechnique = postprocessEffect.Techniques[effectTechniqueName];
view sourceprint?
1 spriteBatch.Begin(0, BlendState.Opaque, null , null , null , postprocessEffect);
2 spriteBatch.Draw(sceneRenderTarget, Vector2.Zero, Color.White);
ShaderProfile, TotalRealTime, TransparentBlack
view sourceprint?
2 graphics.MinimumPixelShaderProfile = ShaderProfile.PS_3_0; //any PS number...
3 graphics.MinimumVertexShaderProfile = ShaderProfile.VS_3_0;//any VS number...
view sourceprint?
2 float myTime = (float )gameTime.TotalRealTime.TotalSeconds * 0.2f;
4 float myTime = (float )gameTime.TotalGameTime.TotalSeconds * 0.2f;
7 GraphicsDevice.Clear(Color.TransparentBlack);
9 GraphicsDevice.Clear(Color.Transparent);
ResolveTexture2D, ResolveBackBuffer, RenderTarget2D, GetTexture, DepthStencilBuffer, PresentationParameters, MultiSampleType, MultiSampleQuality, SetRenderTarget
view sourceprint?
02 ResolveTexture2D sceneMap;
04 RenderTarget2D sceneMap;
08 PresentationParameters pp = GraphicsDevice.PresentationParameters;
10 // 從緩沖讀取一個材質(zhì)數(shù)據(jù).
11 sceneMap = new ResolveTexture2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1, pp.BackBufferFormat);
14 PresentationParameters pp = GraphicsDevice.PresentationParameters;
16 // 從緩沖讀取一個材質(zhì)數(shù)據(jù).
17 sceneMap = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false , pp.BackBufferFormat,
18 ??????????????????????????????? pp.DepthStencilFormat);
21 sceneMap = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false , pp.BackBufferFormat,
22 ??????????????????????????????? pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
25 GraphicsDevice.ResolveBackBuffer(sceneMap);
27 GraphicsDevice.SetRenderTarget(sceneMap);
30 int width = GraphicsDevice.Viewport.Width;
31 int height = GraphicsDevice.Viewport.Height;
view sourceprint?
1 myRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, 1, SurfaceFormat.Color);
3 int width = GraphicsDevice.Viewport.Width;
4 int height = GraphicsDevice.Viewport.Height;
view sourceprint?
1 myRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, true , SurfaceFormat.Color, DepthFormat.Depth24);
4 PresentationParameters pp = GraphicsDevice.PresentationParameters;
view sourceprint?
1 sceneRenderTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1,
2 ?????????????????????????????????? pp.BackBufferFormat, pp.MultiSampleType, pp.MultiSampleQuality);
4 PresentationParameters pp = GraphicsDevice.PresentationParameters;
view sourceprint?
1 sceneRenderTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false ,
2 ?????????????????????????????????? pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
3 ?????????????????????????????????? RenderTargetUsage.DiscardContents);
6 PresentationParameters pp = GraphicsDevice.PresentationParameters;
view sourceprint?
1 drawBuffer = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1,
2 ??????????????????????????????? SurfaceFormat.Color, pp.MultiSampleType, pp.MultiSampleQuality);
4 drawDepthBuffer = new DepthStencilBuffer(GraphicsDevice, pp.AutoDepthStencilFormat,
5 ??????????????????????????????????????????? pp.MultiSampleType, pp.MultiSampleQuality);
7 PresentationParameters pp = GraphicsDevice.PresentationParameters;
view sourceprint?
1 drawBuffer = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, true ,
2 ??????????????????????????????? SurfaceFormat.Color,DepthFormat.Depth24Stencil8,
3 ??????????????????????????????? pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
4 //NOTE:? DepthStencilBuffer 類不再存在
view sourceprint?
2 spriteBatch.Draw(myRenderTarget.GetTexture(), Vector2.Zero, Color.White);
4 spriteBatch.Draw(myRenderTarget, Vector2.Zero, Color.White); // NOTE: ".GetTexure()"? 不再需要
view sourceprint?
02 Texture2D myTexture = myRenderTarget.GetTexture();
04 Texture2D myTexture = myRenderTarget; // NOTE: ".GetTexure()"? No longer needed
07 GraphicsDevice.SetRenderTarget(0, myRenderTarget);
09 GraphicsDevice.SetRenderTarget(myRenderTarget);
view sourceprint?
1 GraphicsDevice.SetRenderTarget(0, colorRT);
2 GraphicsDevice.SetRenderTarget(1, depthRT);
view sourceprint?
1 GraphicsDevice.SetRenderTargets(colorRT, depthRT);
4 GraphicsDevice.SetRenderTarget(0, null );
6 GraphicsDevice.SetRenderTarget(null );
view sourceprint?
1 GraphicsDevice.ResolveBackBuffer(depthMap);
view sourceprint?
1 GraphicsDevice.Textures[1] = depthMap;
2 Viewport viewport = GraphicsDevice.Viewport;
3 dofEffect.CurrentTechnique = depthBlurTechnique;
4 DrawFullscreenQuad(sceneMap, viewport.Width, viewport.Height, dofEffect);
view sourceprint?
1 GraphicsDevice.SetRenderTarget(null );
view sourceprint?
01 GraphicsDevice.Textures[1] = depthMap;
02 GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
03 Viewport viewport = GraphicsDevice.Viewport;
04 dofEffect.CurrentTechnique = depthBlurTechnique;
05 DrawFullscreenQuad(sceneMap, viewport.Width, viewport.Height, dofEffect);
08 ResolveTexture2D resolveTarget;
09 RenderTarget2D renderTarget1;
10 RenderTarget2D renderTarget2;
13 PresentationParameters pp = GraphicsDevice.PresentationParameters;
15 int width = pp.BackBufferWidth;
16 int height = pp.BackBufferHeight;
17 SurfaceFormat format = pp.BackBufferFormat;
19 // 創(chuàng)建一個材質(zhì)讀取緩沖中的內(nèi)容.
20 resolveTarget = new ResolveTexture2D(GraphicsDevice, width, height, 1, format);
22 // Create two rendertargets half size for the bloom processing.
26 renderTarget1 = new RenderTarget2D(GraphicsDevice, width, height, 1,format);
27 renderTarget2 = new RenderTarget2D(GraphicsDevice, width, height, 1,format);
29 // ... In the Draw Method...
30 GraphicsDevice.ResolveBackBuffer(resolveTarget);
31 // ...apply effect and draw pass 1...
33 RenderTarget2D sceneRenderTarget;
34 RenderTarget2D renderTarget1;
35 RenderTarget2D renderTarget2;
38 PresentationParameters pp = GraphicsDevice.PresentationParameters;
40 int width = pp.BackBufferWidth;
41 int height = pp.BackBufferHeight;
42 SurfaceFormat format = pp.BackBufferFormat;
44 // Create a texture for rendering the main scene, prior to applying bloom.
45 sceneRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, false ,
46 ?????????????????????????????????????? format, pp.DepthStencilFormat, pp.MultiSampleCount,
47 ?????????????????????????????????????? RenderTargetUsage.DiscardContents);
49 // Create two rendertargets half size for the bloom processing.
53 renderTarget1 = new RenderTarget2D(GraphicsDevice, width, height, false , format, DepthFormat.None);
54 renderTarget2 = new RenderTarget2D(GraphicsDevice, width, height, false , format, DepthFormat.None);
56 // ...In the Draw Method...
57 GraphicsDevice.SetRenderTarget(sceneRenderTarget);
58 GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;
59 // ...apply effect and draw pass 1....
VertexDeclaration, Vertices, VertexElements, SizeInBytes
view sourceprint?
02 // Vertex declaration for rendering our 3D model.
03 GraphicsDevice.VertexDeclaration = new VertexDeclaration(VertexPositionTexture.VertexElements);
05 // Delete it. No longer needed.
08 // set vertex buffer and declaration
09 GraphicsDevice.VertexDeclaration = vertexDeclaration;
10 GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionTexture.SizeInBytes);
12 // set vertex buffer and declaration
13 GraphicsDevice.SetVertexBuffer(vertexBuffer);
16 // create vertex declaration
17 vertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionTexture.VertexElements);
19 // create vertex declaration
20 vertexDeclaration = new VertexDeclaration(VertexPositionTexture.VertexDeclaration.GetVertexElements());
23 // reset vertex buffer declaration
24 GraphicsDevice.VertexDeclaration = null ;
25 GraphicsDevice.Vertices[0].SetSource(null , 0, 0);
27 // reset vertex buffer declaration
28 GraphicsDevice.SetVertexBuffer(null );
32 VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[100];
34 // set new particles to vertex buffer
35 vertexBuffer.SetData<VertexPositionNormalTexture>(VertexPositionNormalTexture.SizeInBytes * vertexCount,
36 ??????????????????? vertices,vertexCount,count,VertexPositionNormalTexture.SizeInBytes);
39 VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[100];
41 // set new particles to vertex buffer
42 vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
view sourceprint?
view sourceprint?
1 VertexBuffer, StreamOffset, VertexStride, IndexBuffer, BaseVertex
view sourceprint?
03 foreach (ModelMeshPart meshPart in mesh.MeshParts)
05 ??? // if primitives to render
06 ??? if (meshPart.PrimitiveCount > 0)
08 ??????? // setup vertices and indices
09 ??????? GraphicsDevice.VertexDeclaration = meshPart.VertexDeclaration;
10 ??????? GraphicsDevice.Vertices[0].SetSource(mesh.VertexBuffer, meshPart.StreamOffset, meshPart.VertexStride);
11 ??????? GraphicsDevice.Indices = mesh.IndexBuffer;
14 GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; // may be needed in some cases...
17 foreach (ModelMeshPart meshPart in mesh.MeshParts)
19 ??? // if primitives to render
20 ??? if (meshPart.PrimitiveCount > 0)
22 ??????? // setup vertices and indices
23 ??????? GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
24 ??????? GraphicsDevice.Indices = meshPart.IndexBuffer;
29 GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
30 ??? meshPart.BaseVertex, 0, meshPart.NumVertices,
31 ??? meshPart.StartIndex, meshPart.PrimitiveCount);
34 GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
35 ??? meshPart.VertexOffset, 0, meshPart.NumVertices,
36 ??? meshPart.StartIndex, meshPart.PrimitiveCount);
Points, PointSpriteEnable, PointSizeMax, PointList
view sourceprint?
02 // create the vertex buffer
03 vertexBuffer = new VertexBuffer(GraphicsDevice, typeof (VertexPositionNormalTexture),
04 ??????????????????????????????? 250, BufferUsage.WriteOnly | BufferUsage.Points);
06 // create the vertex buffer
07 vertexBuffer = new VertexBuffer(GraphicsDevice, typeof (VertexPositionNormalTexture),
08 ??????????????????????????????? 250, BufferUsage.WriteOnly | BufferUsage.None);
11 // enable point sprite 3.1
12 GraphicsDevice.RenderState.PointSpriteEnable = true ;
13 GraphicsDevice.RenderState.PointSizeMax = 128;
15 // Delete it. No longer available.
18 // draw the point sprites
19 GraphicsDevice.DrawPrimitives(PrimitiveType.PointList, vertexPosition, numberVertices);
21 // draw the point sprites
22 GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, vertexPosition, numberVertices);
OpenContainer, BeginShowStorageDeviceSelector, EndShowStorageDeviceSelector, Path, TitleLocation, FileStream
view sourceprint?
003 StorageContainer storageContainer = storageDevice.OpenContainer("YourGameName" );
005 //To make life easier simply create a method to replace the storageDevice.OpenContainer...
008 /// Synchronously opens storage container
010 private static StorageContainer OpenContainer(StorageDevice storageDevice, string saveGameName)
012 ??? IAsyncResult result = storageDevice.BeginOpenContainer(saveGameName, null , null );
014 ??? // Wait for the WaitHandle to become signaled.
015 ??? result.AsyncWaitHandle.WaitOne();
017 ??? StorageContainer container = storageDevice.EndOpenContainer(result);
019 ??? // Close the wait handle.
020 ??? result.AsyncWaitHandle.Close();
026 StorageContainer storageContainer = OpenContainer(storageDevice, "YourGameName" );
029 // retrieve the storage device
030 Guide.BeginShowStorageDeviceSelector(GetStorageDeviceResult, retrievalDelegate);
032 // retrieve the storage device
035 ??? StorageDevice.BeginShowSelector(GetStorageDeviceResult, retrievalDelegate);
039 // retrieve and store the storage device
040 storageDevice = Guide.EndShowStorageDeviceSelector(result);
042 // retrieve and store the storage device
043 storageDevice = StorageDevice.EndShowSelector(result);
046 // get the level setup files
047 string [] filenames = Directory.GetFiles(storageContainer.Path, "LevelSetup*.xml" );
049 // get the level setup files
050 string [] filenames = storageContainer.GetFileNames("LevelSetup*.xml" ");
053 // save game level data
054 using (FileStream stream = new FileStream(Path.Combine(storageContainer.Path, levelFilename), FileMode.Create))
056 ??? new XmlSerializer(typeof (SaveGameLevel)).Serialize(stream, levelData);
059 // save game level data
060 using (Stream stream = storageContainer.OpenFile(levelFilename, FileMode.Create))
062 ??? new XmlSerializer(typeof (SaveGameLevel)).Serialize(stream, levelData);
066 // Delete the saved game level data.
067 using (StorageContainer storageContainer = storageDevice.OpenContainer("saveGameName" ))
069 ??? File.Delete(Path.Combine(storageContainer.Path, saveGameLevel.FileName));
071 ??? File.Delete(Path.Combine(storageContainer.Path, "SaveGameLevel" +
072 ??????? Path.GetFileNameWithoutExtension(saveGameLevel.FileName).Substring(8) +".xml" ));
075 // Delete the saved game level data. NOTE: using OpenContainer method created in previous example
076 using (StorageContainer storageContainer = OpenContainer(storageDevice, "saveGameName" ))
078 ??? storageContainer.DeleteFile(saveGameLevel.FileName);
080 ??? storageContainer.DeleteFile("SaveGameLevel" +
081 ??????? Path.GetFileNameWithoutExtension(saveGameLevel.FileName).Substring(8) + ".xml" );
085 //Load the Next Level...
087 // Find the path of the next level.
090 // Loop here so we can try again when we can't find a level.
093 ??? // Try to find the next level. They are sequentially numbered txt files.
094 ??? levelPath = String.Format("Levels/{0}.txt" , ++levelIndex);
095 ??? levelPath = Path.Combine(StorageContainer.TitleLocation, "Content/" + levelPath);
096 ??? if (File.Exists(levelPath))
099 ??? // If there isn't even a level 0, something has gone wrong.
100 ??? if (levelIndex == 0)
101 ??????? throw new Exception("No levels found." );
103 ??? // Whenever we can't find a level, start over again at 0.
107 // Unloads the content for the current level before loading the next one.
112 level = new Level(Services, levelPath);
114 // Load the Next Level...
116 // move to the next level
117 levelIndex = (levelIndex + 1) % numberOfLevels;
119 // Unloads the content for the current level before loading the next one.
124 string levelPath = string .Format("Content/Levels/{0}.txt" , levelIndex);
125 using (Stream fileStream = TitleContainer.OpenStream(levelPath))
126 ??? level = new Level(Services, fileStream, levelIndex);
130 //Save the current state of the session, with the given storage device.
132 // check the parameter
133 if ((storageDevice == null ) || !storageDevice.IsConnected)
139 using (StorageContainer storageContainer = storageDevice.OpenContainer(Session.SaveGameContainerName))
142 ??? string descriptionFilename;
144 ??? // get the filenames
145 ??? if (overwriteDescription == null )
147 ??????? int saveGameIndex = 0;
148 ??????? string testFilename;
151 ??????????? saveGameIndex++;
152 ??????????? testFilename = Path.Combine(storageContainer.Path, "SaveGame" + saveGameIndex.ToString() + ".xml" );
154 ??????? while (File.Exists(testFilename));
155 ??????? filename = testFilename;
156 ??????? descriptionFilename = "SaveGameDescription" + saveGameIndex.ToString() + ".xml" ;
160 ??????? filename = Path.Combine(storageContainer.Path, overwriteDescription.FileName);
161 ??????? descriptionFilename = "SaveGameDescription" +
162 ??????????? Path.GetFileNameWithoutExtension(overwriteDescription.FileName).Substring(8) + ".xml" ;
165 ??? using (FileStream stream = new FileStream(filename, FileMode.Create))
167 ??????? using (XmlWriter xmlWriter = XmlWriter.Create(stream))
169 ??????????? //create and write xml data...
173 ?? // create the save game description
174 ??? SaveGameDescription description = new SaveGameDescription();
175 ??? description.FileName = Path.GetFileName(filename);
176 ??? description.ChapterName = IsQuestLineComplete ? "Quest Line Complete" : Quest.Name;
177 ??? description.Description = DateTime.Now.ToString();
179 ??? using (FileStream stream = new FileStream(Path.Combine(storageContainer.Path, descriptionFilename), FileMode.Create))
181 ??????? new XmlSerializer(typeof (SaveGameDescription)).Serialize(stream, description);
185 //Save the current state of the session, with the given storage device.
187 // check the parameter
188 if ((storageDevice == null ) || !storageDevice.IsConnected)
193 // open the container Note: using OpenContainer method from previous examples
194 using (StorageContainer storageContainer = OpenContainer(storageDevice, Session.SaveGameContainerName))
197 ??? string descriptionFilename;
198 ??? // get the filenames
199 ??? if (overwriteDescription == null )
201 ??????? int saveGameIndex = 0;
202 ??????? string testFilename;
205 ??????????? saveGameIndex++;
206 ??????????? testFilename = "SaveGame" + saveGameIndex.ToString() + ".xml" ;
208 ??????? while (storageContainer.FileExists(testFilename));
209 ??????? filename = testFilename;
210 ??????? descriptionFilename = "SaveGameDescription" + saveGameIndex.ToString() + ".xml" ;
214 ??????? filename = overwriteDescription.FileName;
215 ??????? descriptionFilename = "SaveGameDescription" +
216 ??????????? Path.GetFileNameWithoutExtension(overwriteDescription.FileName).Substring(8) + ".xml" ;
219 ??? // Note: using Stream instead of FileStream...
220 ??? using (Stream stream = storageContainer.OpenFile(filename, FileMode.Create))
222 ??????? using (XmlWriter xmlWriter = XmlWriter.Create(stream))
224 ??????????? //create and write xml data...
228 ??? // create the save game description
229 ??? SaveGameDescription description = new SaveGameDescription();
230 ??? description.FileName = Path.GetFileName(filename);
231 ??? description.ChapterName = IsQuestLineComplete ? "Quest Line Complete" : Quest.Name;
232 ??? description.Description = DateTime.Now.ToString();
234 ??? // Note: using Stream instead of FileStream...
235 ??? using (Stream stream = storageContainer.OpenFile(descriptionFilename, FileMode.Create))
237 ??????? new XmlSerializer(typeof (SaveGameDescription)).Serialize(stream, description);
view sourceprint?
view sourceprint?
1 VertexShader, PixelShader, ps_1_x
view sourceprint?
02 VertexShaderOutput VertexShader(...)
06 float4 PixelShader(...)
11 // VertexShader can not be used
12 VertexShaderOutput? VertexShaderFunction(...)
16 // PixelShader can not be used
17 float4 PixelShaderFunction(...)
27 ??????? VertexShader = compile vs_1_1 VertexShader();
28 ??????? PixelShader? = compile ps_1_1 PixelShader();
36 ??????? VertexShader = compile vs_2_0 VertexShaderFunction(); //VertexShader can not be used & set vs higher than 1_1
37 ??????? 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
view sourceprint?
1 // Set suitable renderstates for drawing a 3D model
2 GraphicsDevice.BlendState = BlendState.Opaque;
3 GraphicsDevice.DepthStencilState = DepthStencilState.Default;
5 // your model draw code starts here...
總結(jié)
以上是生活随笔 為你收集整理的[转]XNA 3.1 转换到 XNA4.0 的备忘录 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。