使用 XNA/3D 開發/3D 引擎建立遊戲
3D 遊戲引擎的示例,可以簡化 3D 遊戲的建立。 應該包含引擎的簡短介紹、功能、支援,以及使用它的示例專案。 3D 引擎的示例可以在這裡找到:http://forums.create.msdn.com/forums/t/12882.aspx

快速入門引擎是一個面向 XNA 的 3D 遊戲引擎,它允許開發者儘快開始 3D 遊戲程式設計。[1]
當前版本為 0.262。 它仍然處於測試階段。 引擎在 Ms-PL 下發布,這意味著它是免費使用的。
包括物理引擎、遊戲內 GUI 框架、陰影貼圖、法線貼圖、用於地形的多紋理混合等等。
- Visual Studio 2010
- XNA 遊戲工作室 4.0

快速入門引擎的概念很容易理解。
一個遊戲是由場景組成的。 這些場景儲存在場景管理器中,它是遊戲類 "QSGame" 的一部分。 場景管理器負責載入和儲存當前場景。
一個場景是由實體組成的。 場景中的每個物件都是一個實體! 它可以是相機、地形、燈光、玩家以及其他任何東西。
一個實體只包含基本資訊,例如位置和旋轉。 要賦予它模型或相機等功能,您必須新增元件。
元件繫結到實體,負責諸如儲存模型、處理輸入或發出燈光等操作。
另一個重要的事實是,每個實體都必須手動新增到遊戲場景管理器中!
引擎還具有一個訊息系統,它允許遊戲的每個部分發送和接收請求。 這些請求可以包括更改當前相機、輸入或移動等操作。
如何在您的專案中實現引擎
- 訪問 http://quickstartengine.codeplex.com/ 並下載最新版本(本教程基於 V0.26)。
- 由於您可能希望對引擎進行更改(請注意許可證!),因此您應該將資料夾 "QuickStart Engine v0.26\framework\code" 複製到您的專案資料夾中。
- 為了更好地理解,將 "code" 重新命名為 "QS" 或 "QuickStart" 等名稱。
- 現在使用 VS2010 開啟您的專案解決方案檔案。
- 開啟解決方案資源管理器,右鍵單擊您的解決方案,然後單擊 "新增現有專案"。
- 根據您計劃程式設計的平臺,從 "QS" 中的每個資料夾中選擇 csproj/vsproj 檔案(如果沒有特定的 Windows/XBox 專案檔案,則選擇通用檔案)。
- 現在在您的 XNA 專案中執行以下操作
- 轉到 '<您的 XnaGame> -> 引用' 並新增對解決方案中所有專案的引用。
- 在 "內容" 中的引用中新增 "QuickStart_>Windows/XBox>" 和 "QuickStart.ContentPipeline"
- 在您的 Game.cs 中...
- 新增 "using QuickStart;"
- 將您的遊戲設定為 QSGame 的子類。
就是這樣! 您的遊戲現在基於快速入門引擎!
如何建立一個場景
在遊戲初始化期間,所有場景都必須建立並新增到場景管理器中。 您必須在遊戲的 LoadContent 方法中執行此操作。
protected override void LoadContent()
{
base.LoadContent();
//create and load all scenes here
Scene newScene = new PlayScene(this);
Scene anotherScene = new AnotherScene(this);
this.SceneManager.LoadScene(newScene, true);
this.SceneManager.LoadScene(anotherScene, true);
//choose the currently active Scene
this.SceneManager.ActiveScene = newScene;
}
當場景初始化時,它可以載入以後可能需要的任何東西,包括模型、紋理、影像等等。 儘管如此,資產可以在您需要時隨時載入。
更新: 應該注意的是,自該評論最初發布以來,將實體載入到您的場景中已變得非常簡單。 以下是有關如何載入實體和元件而無需使用任何 C# 程式碼的文件,您現在可以完全使用 XML 定義您的實體。http://quickstartengine.codeplex.com/wikipage?title=Creating%20Entities%20and%20Components%20with%20XML
如何建立一個地形
這個有點棘手。
確保您的 Content 資料夾中包含 "Material\Terrain.qsm"。 最好是從引擎附帶的測試專案中獲取該檔案。 您可以在以下路徑中找到它:"QuickStart Engine v0.22\framework\test\QuickStartSampleGame\Content\Material"。
將檔案新增到專案後,您可能需要在屬性中將匯入器和處理器更改為 "QuickStart 材料匯入器/處理器"。
我們稍後會再次檢視此檔案。
要建立一個地形,您需要一個灰度 高度圖。 它必須是正方形的,並且一側的長度必須是 2 的冪(2、4、8、16、32、64、...)。
現在在場景中建立地形非常容易
//create Terrain
Terrain terrain = new Terrain(game, QuickStart.Graphics.LOD.High);
terrain.Name = "MyTerrain";
//set the elevation strength and load the heigtmap
terrain.ElevationStrength = 75;
terrain.Initialize("./Images/heightmap", 1, 4);
//add physic to terrain
PhysicsComponent tf = new PhysicsComponent(terrain, terrain.heightData, terrain.ScaleFactor);
//add the terrain (which is derived from BaseEntity) to the SceneManager of your game
game.SceneManager.AddEntity(terrain);
}
現在只剩下一個東西了! 您必須為您的地形新增紋理貼圖。 此貼圖必須與高度圖大小相同,並使用三種顏色來定義地面的紋理。
red (255,0,0) = rocks green (0,255,0) = grass blue (0,0,255) = water
仔細檢視 "Terrain.qsm"。 您會找到 "TEXTURE_MAP" 的路徑。 將其更改為您放置影像的位置。 要建立貼圖,您可以使用任何 影像處理 軟體。 以無損格式(例如 "png")儲存它。
如何建立一個實體
場景中的所有內容都源自 BaseEntity。 每當您要建立一個物件時,您都必須從建立一個 BaseEntity 物件開始。
實體本身是不可見的。 您必須新增 RenderComponent,在其中設定模型和材質。 如果您希望為您的實體進行碰撞檢測和/或物理運算,您可以新增 PhysicsComponent。 這是一個簡單的球體示例。
//BaseEntity(your game,position, rotation, scale)
BaseEntity sphere = new BaseEntity(this.game, new Vector3(500, 100, 510), Matrix.Identity, 5f);
sphere.Name = "sphere";
//RenderComponent(parent object, path to model, path to material)
RenderComponent r = new RenderComponent(sphere, "Models/unit_sphere", "Material/SimpleColored");
r.modelColor = Color.Orange;
(((Physicscomponent(parent object, type of collider, density, reacts to forces)
PhysicsComponent p = new PhysicsComponent(sphere, ShapeType.Sphere, 5,true);
//add the entity to your games SceneManager
game.SceneManager.AddEntity(sphere);
如何新增一個第三人稱相機
每個物件都可以用作相機,這對於第一人稱視角或監控攝像頭等情況很有用。
對於第三人稱視角,我們需要一個新的 BaseEntity,並將 CameraComponent 新增到其中。 我們還向其添加了 ArcBallCameraInputComponent,這使我們以後可以旋轉相機。 完成此操作後,您必須將相機新增到場景管理器中,因為我們必須傳送一些訊息,這些訊息只有在相機已為遊戲所知的情況下才會起作用。
BaseEntity cam = new BaseEntity(game,new Vector3(20,0,20),Matrix.Identity,1);
//CameraComponent(object, Field of View,screen width, screen height, near plane, far plane)
CameraComponent camComp = new CameraComponent(cam, 60f, game.Settings.Resolution.X, game.Settings.Resolution.Y, 0.5f, 1000);
//add the input component
ArcBallCameraInputComponent thirdPersonCam = new ArcBallCameraInputComponent(cam);
game.SceneManager.AddEntity(cam);
- 第一條訊息是,可以說從現在開始這是主相機
MsgSetRenderEntity RndMsg = ObjectPool.Aquire<MsgSetRenderEntity>();
RndMsg.Entity = cam;
this.game.SendInterfaceMessage(RndMsg, InterfaceType.Camera);
- 第二條訊息將玩家物件設定為父物件
MsgSetParent msg = ObjectPool.Aquire<MsgSetParent>();
msg.ParentEntity = playerEntity;
msg.UniqueTarget = cam.UniqueID;
game.SendMessage(msg);
- 第三條訊息是,可以說玩家與相機一起旋轉
MsgLockCharacterRotationToCamera msg = ObjectPool.Aquire<MsgLockCharacterRotationToCamera>();
msg.UniqueTarget = player.UniqueID;
msg.LockRotation = true;
game.SendMessage(msg);
最後,您的相機始終處於玩家身後的第三人稱視角!
如何建立一個角色物件
在沒有玩家的情況下,第三人稱相機毫無用處。 您將從之前的示例中識別出大部分指令碼。
BaseEntity player = new BaseEntity(this.game, new Vector3(500, 100, 500), Matrix.Identity, 1);
player.Name = "player";
RenderComponent comp = new RenderComponent(player, "Models/unit_sphere", "Material/SimpleColored");
//you can set whether an object receives and creates shadows
comp.SetShadowingProperties(true, true);
comp.modelColor = Color.Blue;
//The engine already has special physics and an input component for a player
CharacterPhysicsComponent newPhysComponent = new CharacterPhysicsComponent(player, ShapeType.Sphere, 5.0f);
CharacterInputComponent input = new CharacterInputComponent(player);
game.SceneManager.AddEntity(player);
//Tell the game, that this is the controlled object
MsgSetControlledEntity msgSetControlled = ObjectPool.Aquire<MsgSetControlledEntity>();
msgSetControlled.ControlledEntityID = player.UniqueID;
this.game.SendInterfaceMessage(msgSetControlled, InterfaceType.SceneManager);
如何建立一個燈光
我們基本場景的最後一步是新增燈光。 每個實體都可以發出燈光。 因此,您只需建立燈光設定、LightEmitterComponent,並在將其新增到場景管理器後初始化它。 這可能看起來像這樣
BaseEntity light = new BaseEntity(game, new Vector3(0, 500, 0), Matrix.CreateRotationX((float)Math.PI/2f), 1);
light.Name = "light";
LightSettings s = new QuickStart.EnvironmentalSettings.LightSettings();
s.LightDirection = Vector3.Down;
s.AmbientColor = new Vector4(1f, 0f, 0f, 0f);
s.DiffuseColor = new Vector4(0f, 1f, 0f, 0f);
s.SpecularColor = Vector4.Zero;
s.MinimumAmbient = 50f;
s.SpecularPower = 10f;
LightEmitterComponent lc = new LightEmitterComponent(light, s);
game.SceneManager.AddEntity(light);
lc.InitializeLightDirection();
快速入門引擎很有潛力,但需要一些時間才能理解其運作方式。 在某些方面,它有點過於靜態,需要改進。
引擎已更新以支援 XNA 4.0,並且實體和元件的載入現在也簡單得多,可以使用 XML 完全完成。 自該評論釋出以來,還添加了其他功能,例如模型的紋理貼圖和遊戲內 GUI。
juliusse