මෙවර අප 3D model එකක් ක්රිඩාවට ගන්නා ආකාරය බලමු.
පලමුව 3D model එකක් 3D MAX මගින් නිර්මානය කරන්න.
ඉන් පසු එය File->Export මගින් Export කරන්න.
නව ක්රමලේඛය ioyd 3d model tllA wka;ra.; lrkak.
ඉන් පසුව Game 1.cs වල class එකක් හදන්න.
Model car;
ඉන් පසුව LoadContent() වල Car model tllA wka;ra.; lrkak.
car = Content.Load<Model>("Car");
දැන් Draw() methord එකේ Car Model එක render කරන්න.
//create a new matrix and copy that tranfomation to 3d model
Matrix[] transforms = new Matrix[car.Bones.Count];
//copy the absolute transforms to the car model.
car.CopyAbsoluteBoneTransformsTo(transforms);
//use two foreach loops to loop through the mesh and mesh effect
foreach (ModelMesh mesh in car.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
//enable default lighting provides by XNA.
effect.EnableDefaultLighting();
//define the world matrix to locate the position
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(30.0f, -40.0f, -400.0f);
//define view matrix to set up the camara location
effect.View = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward, Vector3.Up);
//define projection matrix to model projection ,aspect ratio,
and near and far plane distance.
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(80.0f), 800.0f / 600.0f, 10.0f, 10000.0f);
}
mesh.Draw(); //Draw the 3d mesh
අවසානයට F5 ඹබා කීඩාව ආරමීබ කරන්න.
Final code of Game1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Draw_3d_model
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
/// 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.
protected override void Initialize()
base.Initialize();
/// LoadContent will be called once per game and is the place to load
/// all of your content.
protected override void LoadContent()
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
/// UnloadContent will be called once per game and is the place to unload
/// all content.
protected override void UnloadContent()
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// <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();
base.Update(gameTime);
/// This is called when the game should draw itself.
protected override void Draw(GameTime gameTime)
GraphicsDevice.Clear(Color.Black);
mesh.Draw();
base.Draw(gameTime);