XNa 3D text

Source: Internet
Author: User
Tags vector font

For almost two years, I lamented the subtlety of the verse "a hundred years in a twinkling of an eye". I wasted too much time in two years, so I decided to set sail again, I believe this time we can go further and wider .....

I have been studying the secondary development of xNa for two years. It took a long time to complete the process from the excitement of the beginning to the space ry in the middle, to the inability to work at the end, however, I still need to study a lot of knowledge points by myself, and it is still a little powerless to deal with HLSL. This will be one of the main knowledge points I will use to make up for the free time this year, in order to spur myself to think that all HLSL learning insights are recorded here, and they will study and learn with like-minded friends.

After feeling done, go to the question ........

3D text in xNa has always been quite tangled. drawstring in spritebatch can only draw screen text. The following uses simple code to draw a 3D text. The original address is https://devel.nucleus x.org/framework/wiki/vectorfonts.

      private GraphicsDeviceManager graphics;      private  SpriteBatch spriteBatch;        /// <summary>        /// load the font        /// </summary>        private VectorFont vectorFont;        /// <summary>        /// text string        /// </summary>        private Text text;        /// <summary>        /// draw the string        /// </summary>        private TextBatch textBatch;        /// <summary>        /// camera viewMatrix        /// </summary>        private Matrix viewMatrix = Matrix.Identity;        /// <summary>        /// camera projectionMatrix        /// </summary>        private Matrix projectionMatrix = Matrix.Identity;        /// <summary>        /// record the update number        /// </summary>        private int Index = 0;        /// <summary>        /// text rotate matrix        /// </summary>        private Matrix RotateMatrix = Matrix.Identity;        public MyText()        {            graphics = new GraphicsDeviceManager(this);            Content.RootDirectory = "Content";        }        /// <summary>        /// Allows the game to perform any initialization it needs to before starting to run.        /// This is where it can query for any required services and load any non-graphic        /// related content.  Calling base.Initialize will enumerate through any components        /// and initialize them as well.        /// </summary>        protected override void Initialize()        {            base.Initialize();        }        /// <summary>        /// LoadContent will be called once per game and is the place to load        /// all of your content.        /// </summary>        protected override void LoadContent()        {            // Create a new SpriteBatch, which can be used to draw textures.            spriteBatch = new SpriteBatch(GraphicsDevice);            this.textBatch = new TextBatch(this.graphics.GraphicsDevice);            viewMatrix = Matrix.CreateLookAt(new Vector3(0 , 0 , 50) , new Vector3(0 , 0 , -100) , Vector3.Up);            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4 , GraphicsDevice.Viewport.AspectRatio , 1.0f , 1000);            this.vectorFont = this.Content.Load<VectorFont>("Fonts/defaultFont");            this.text = this.vectorFont.Extrude("H0");        }        /// <summary>        /// UnloadContent will be called once per game and is the place to unload        /// all content.        /// </summary>        protected override void UnloadContent()        {            // TODO: Unload any non ContentManager content here        }        /// <summary>        /// Allows the game to run logic such as updating the world,        /// checking for collisions, gathering input, and playing audio.        /// </summary>        /// <param name="gameTime">Provides a snapshot of timing values.</param>        protected override void Update(GameTime gameTime)        {            // Allows the game to exit            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.Exit();            Index++;            if (Index % 60 == 0)            {                string str = "H" + (Index / 60).ToString();                this.text = this.vectorFont.Extrude(str);            }            // TODO: Add your update logic here            RotateMatrix *= Matrix.CreateRotationY(MathHelper.Pi / 180);                        base.Update(gameTime);        }        /// <summary>        /// This is called when the game should draw itself.        /// </summary>        /// <param name="gameTime">Provides a snapshot of timing values.</param>        protected override void Draw(GameTime gameTime)        {            GraphicsDevice.Clear(Color.CornflowerBlue);            this.textBatch.ViewProjection = viewMatrix * projectionMatrix;            this.textBatch.Begin();            this.textBatch.DrawText(this.text , Matrix.CreateScale(0.1f) * RotateMatrix * Matrix.CreateTranslation(0 , 0 , 30) , Color.Red);            this.textBatch.End();            base.Draw(gameTime);        }

 

Pay attention to the following points: first, put the font file named defaultfont under the project's content directory, and then for defaultfont. the spritefont file needs to modify its attributes: modify its content porcessor to vector font, and then compile it (test version vs2010 + xna4.0 )......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.