XNA Billboard(公告板技术)
公告板技術(shù)是3D游戲中用的非常多的一種技術(shù),主要是用于控制場(chǎng)景中的Texture的方向,讓他始終以一定的角度對(duì)著我們的鏡頭(一般是垂直于鏡頭)。
如我們?cè)?D游戲中看到的怪物的藍(lán)、紅和怪物名字、一些花草樹木等,無論我們?cè)谀膫€(gè)方向看它總是對(duì)著我們。
如下圖所示:
GraphicsDeviceManager graphics; Texture2D texRedPanda;//鏡頭信息參數(shù) Vector3 pos, lookat, up; //World,View,Project矩陣 Matrix world,view, project; BasicEffect basicEffect; //頂點(diǎn)結(jié)構(gòu) 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);//定義三角形的各頂點(diǎn)坐標(biāo)和紋理坐標(biāo)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);//設(shè)置變換矩陣參數(shù)basicEffect.World = world;basicEffect.View = view;basicEffect.Projection = project;//設(shè)置繪制紋理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的位置大概是在坐標(biāo)原點(diǎn),為了等會(huì)便于觀察,將在Update加些內(nèi)容。讓鏡頭的位置通過鍵盤控制繞原點(diǎn)旋轉(zhuǎn)(這里不是用World變換)。這里是在網(wǎng)上找的公式:
x1 = x0 * cosB + y0 * sinB
y1 = -x0 * sinB + y0 * cosB
?
?????? x0,y0表示鏡頭現(xiàn)在的位置,y1,y2表示繞原點(diǎn)旋轉(zhuǎn)B弧度后的坐標(biāo)。
?????? 現(xiàn)在我們就呆以看到?jīng)]有使用公告板技術(shù)時(shí)的效果了。把下面的代碼加到Update方法的里:
?????? 下面我們加一個(gè)用了公告板的World變換。主要代碼如下,
bbWorld = Matrix.CreateBillboard(Vector3.Zero, -pos, Vector3.Up, null);
?????? 現(xiàn)在再來對(duì)比下 效果,發(fā)現(xiàn)無論我們控制鏡頭在哪個(gè)位置,小熊貓圖片的下面始終對(duì)著我們。
這里已經(jīng)實(shí)現(xiàn)了一個(gè)初級(jí)的公告板。當(dāng)然也可以不用XNA的現(xiàn)有方法,我們可以把代碼放到HLSL里面去,這里有個(gè)實(shí)現(xiàn),但我?guī)退菙?shù)學(xué)原理沒有推算過。所以只有套著用了。
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();} }????? 這里就實(shí)現(xiàn)了比較常用的公告板技術(shù)。到底是用XNA現(xiàn)有方法,還是HLSL,自己視情況面定吧。
轉(zhuǎn)載于:https://www.cnblogs.com/lm3515/archive/2010/09/18/1829995.html
總結(jié)
以上是生活随笔為你收集整理的XNA Billboard(公告板技术)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DropDownList的常用属性和事件
- 下一篇: IIS5.1错误,启动时WEB服务提示: