在 MSRS 模擬中,AddSky 與 AddGround 方法負責為 Boe-Bot 機器人構建周圍世界。以下為核心實作細節。
建立 SkyDomeEntity 並插入模擬引擎,同時加入一個代表太陽的方向光源:
void AddSky()
{
// 使用靜態貼圖建立天空穹頂,供逐像素光照使用
SkyDomeEntity sky = new SkyDomeEntity("skydome.dds", "sky_diff.dds");
SimulationEngine.GlobalInstancePort.Insert(sky);
// 新增方向光模擬陽光
LightSourceEntity sun = new LightSourceEntity();
sun.State.Name = "Sun";
sun.Type = LightSourceEntityType.Directional;
sun.Color = new Vector4(0.8f, 0.8f, 0.8f, 1);
sun.Direction = new Vector3(0.5f, -0.75f, 0.5f);
SimulationEngine.GlobalInstancePort.Insert(sun);
}

圖 10:添加天空與光源
使用 HeightFieldEntity 在零海拔處建立廣大地面,套用隨 MSRS 提供的紋理:
void AddGround()
{
HeightFieldEntity ground = new HeightFieldEntity(
"simple ground", // 名稱
"03RamIESc.dds", // 紋理圖像
new MaterialProperties(
"ground",
0.2f, // 反彈係數
0.5f, // 動態摩擦
0.5f // 靜態摩擦
)
);
SimulationEngine.GlobalInstancePort.Insert(ground);
}
接受 Vector3 位置參數,建立 Boe-Bot 實體並啟動其模擬驅動服務:
void AddBoeBot(Vector3 pos)
{
BoeBot boeBot = new BoeBot(pos);
boeBot.State.Name = "SimulatedBoeBot";
// 啟動模擬驅動服務作為實體夥伴
CreateService(
drive.Contract.Identifier,
Microsoft.Robotics.Simulation.Partners.CreateEntityPartner(
"https://localhost/" + boeBot.State.Name)
);
SimulationEngine.GlobalInstancePort.Insert(boeBot);
}