XNA Billboard(公告板技术)
公告板技術是3D游戲中用的非常多的一種技術,主要是用于控制場景中的Texture的方向,讓他始終以一定的角度對著我們的鏡頭(一般是垂直于鏡頭)。
如我們在3D游戲中看到的怪物的藍、紅和怪物名字、一些花草樹木等,無論我們在哪個方向看它總是對著我們。
如下圖所示:
GraphicsDeviceManager graphics; Texture2D texRedPanda;//鏡頭信息參數 Vector3 pos, lookat, up; //World,View,Project矩陣 Matrix world,view, project; BasicEffect basicEffect; //頂點結構 VertexPositionTexture[] vpt; VertexDeclaration vertexDec;public GameMain() {graphics = new GraphicsDeviceManager(this);Content.RootDirectory = "Content"; }protected override void Initialize() {//初始化鏡頭信息pos = new Vector3(0, 0,200);lookat = Vector3.Zero;up = Vector3.Up;//初始化變換矩陣world = Matrix.Identity;bbWorld = Matrix.Identity;view = Matrix.CreateLookAt(pos, lookat, up);project = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 800f/600f, 1, 1000);vpt = new VertexPositionTexture[6];GraphicsDevice.RenderState.CullMode = CullMode.None;base.Initialize(); }protected override void LoadContent() {texRedPanda = Content.Load<Texture2D>("RedPanda");basicEffect = new BasicEffect(GraphicsDevice, null);vertexDec = new VertexDeclaration(GraphicsDevice, VertexPositionTexture.VertexElements);//定義三角形的各頂點坐標和紋理坐標vpt[0] = new VertexPositionTexture(new Vector3(-25,-25, 0), new Vector2(0, 1));vpt[1] = new VertexPositionTexture(new Vector3(-25, 25, 0), new Vector2(0, 0));vpt[2] = new VertexPositionTexture(new Vector3(25,-25, 0), new Vector2(1, 1));vpt[3] = new VertexPositionTexture(new Vector3(-25, 25, 0), new Vector2(0, 0));vpt[4] = new VertexPositionTexture(new Vector3(25, 25, 0), new Vector2(1, 0));vpt[5] = new VertexPositionTexture(new Vector3(25,-25, 0), new Vector2(1, 1)); }protected override void Update(GameTime gameTime) {// Allows the game to exitif (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit();base.Update(gameTime); }protected override void Draw(GameTime gameTime) {GraphicsDevice.Clear(Color.Black);//設置變換矩陣參數basicEffect.World = world;basicEffect.View = view;basicEffect.Projection = project;//設置繪制紋理basicEffect.TextureEnabled = true;basicEffect.Texture = texRedPanda;basicEffect.Begin();foreach (var pass in basicEffect.CurrentTechnique.Passes){pass.Begin();GraphicsDevice.VertexDeclaration = vertexDec;GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vpt,0, 2);pass.End();}basicEffect.End();base.Draw(gameTime); }
?????? texRedPanda的位置大概是在坐標原點,為了等會便于觀察,將在Update加些內容。讓鏡頭的位置通過鍵盤控制繞原點旋轉(這里不是用World變換)。這里是在網上找的公式:
x1 = x0 * cosB + y0 * sinB
y1 = -x0 * sinB + y0 * cosB
?
?????? x0,y0表示鏡頭現在的位置,y1,y2表示繞原點旋轉B弧度后的坐標。
?????? 現在我們就呆以看到沒有使用公告板技術時的效果了。把下面的代碼加到Update方法的里:
?????? 下面我們加一個用了公告板的World變換。主要代碼如下,
bbWorld = Matrix.CreateBillboard(Vector3.Zero, -pos, Vector3.Up, null);
?????? 現在再來對比下 效果,發現無論我們控制鏡頭在哪個位置,小熊貓圖片的下面始終對著我們。
這里已經實現了一個初級的公告板。當然也可以不用XNA的現有方法,我們可以把代碼放到HLSL里面去,這里有個實現,但我幫他是數學原理沒有推算過。所以只有套著用了。
float4x4 World; float4x4 View; float4x4 Projection; texture Texture;sampler textureSampler=sampler_state {texture=<Texture>;magfilter = LINEAR; minfilter = LINEAR;mipfilter=LINEAR; AddressU = CLAMP; AddressV = CLAMP; };struct VertexShaderInput {float3 Pos:POSITION0;float2 TexCoord:TEXCOORD0; };struct VertexShaderOutput {float4 Position : POSITION0;float2 TexCoord:TEXCOORD0; };VertexShaderOutput VertexShaderFunction(VertexShaderInput input) {VertexShaderOutput output;float4x4 worldViewMatrix = mul(World, View); float3 positionVS = input.Pos + float3(worldViewMatrix._41, worldViewMatrix._42, worldViewMatrix._43);output.Position = mul(float4(positionVS, 1.0f), Projection);output.TexCoord=input.TexCoord;return output; }float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 {return tex2D(textureSampler,input.TexCoord); }technique BillBoard {pass BillBoard{VertexShader = compile vs_1_0 VertexShaderFunction();PixelShader = compile ps_1_0 PixelShaderFunction();} }????? 這里就實現了比較常用的公告板技術。到底是用XNA現有方法,還是HLSL,自己視情況面定吧。
轉載于:https://www.cnblogs.com/lm3515/archive/2010/09/18/1829995.html
總結
以上是生活随笔為你收集整理的XNA Billboard(公告板技术)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DropDownList的常用属性和事件
- 下一篇: IIS5.1错误,启动时WEB服务提示: