在三維的系統中,有時會挂接攝像頭的資訊,模拟現實攝像頭的視野,模拟攝像頭在三維中的上下左右移動,模拟拉近攝像頭的視野。
以下是C#的代碼實作:
//滑鼠點選擷取位置,設定高度為相對
IWorldPointInfo6 Iworldinfo = sgworld.Window.PixelToWorld(X, Y, WorldPointType.WPT_DEFAULT);
Iworldinfo.Position.AltitudeType = AltitudeTypeCode.ATC_TERRAIN_RELATIVE;
Iworldinfo.Position.Altitude = 10;
//建立一個攝像頭的模型
ITemodel = sgworld.Creator.CreateModel(Iworldinfo.Position, 模型檔案路 徑, 1, ModelTypeCode.MT_NORMAL, 0, "shipin");
//設定攝像頭的向上視角
Iworldinfo.Position.Pitch = -90;
rtapos = Iworldinfo.Position.Copy();
//建立顔色
IColor6 crtcolor = sgworld.Creator.CreateColor(Color.LightBlue.R, Color.LightBlue.G, Color.LightBlue.B, 120);
//建立椎體效果
ITeRegBase = sgworld.Creator.CreateCone(Iworldinfo.Position, 2, 5, crtcolor, crtcolor, -1, 0, "shizhui");
//将椎體綁定到模型上,并飛到模型
ITeRegBase.Attachment.AttachTo(ITemodel.ID, 0, 5, 0);
sgworld.Navigate.FlyTo(ITemodel);
//攝像頭和椎體的左移
ITemodel.Position.Yaw += 10;
ITeRegBase.Position.Pitch = rtapos.Pitch;
ITeRegBase.Position.Yaw += 10;
//攝像頭和椎體的收縮
ITeRegBase.Height += 1;
ITeRegBase.Attachment.AttachTo(ITemodel.ID, 0, ITeRegBase.Height,
0);
ITeRegBase.Position.Pitch = rtapos.Pitch;
ITeRegBase.Radius -= 0.3;
//攝像頭和椎體的上俯視
if (rtapos.Pitch > -130.0 && rtapos.Pitch < -35.0)
{
rtapos.Pitch -= 9;
ITeRegBase.Position.Pitch = rtapos.Pitch;
ITeRegBase.Attachment.AttachTo(ITemodel.ID, 0,
ITeRegBase.Height, 0);
ITemodel.Position.Pitch -= 9;
}