我创建了一组代表房间的球形模型。
Model[] roomModel = new Model[21];
在我的 LoadContent 方法中:
for (int x = 0; x < 21; x++)
{
roomModel[x] = Content.Load<Model>("Models\\sphere_model");
}
在我的 Draw 方法中:
for (int x = 0; x < 21; x++)
{
//copy any parent transforms
Matrix[] transforms = new Matrix[roomModel[x].Bones.Count];
roomModel[x].CopyAbsoluteBoneTransformsTo(transforms);
//draw the model; a model can have multiple meshes, so loop.
foreach (ModelMesh mesh in roomModel[x].Meshes)
{
//this is where the mesh orientation is set, as well as our camera and projection
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(modelPosition[x]);
effect.View = Matrix.CreateLookAt(cameraPosition, new Vector3(0.0f,-100.0f,0.0f), Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(42.0f), aspectRatio, 1.0f, 10000.0f);
}
mesh.Draw();
}
}
我只想更改我的一个球形模型的纹理,比如 roomModel[5]。当我使用此代码时,它会更改所有房间模型。
foreach (ModelMesh mesh in roomModel[5].Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.Texture = textureToSet;
}
}
我如何让它改变其中一个房间模型而不是所有房间模型的纹理?我所能想到的就是为每个房间创建一个单独的模型,但这听起来很昂贵。