2013-11-03 50 views
0

我创建了代表房间的球形模型阵列。在3D模型的一个实例上更改纹理

Model[] roomModel = new Model[21]; 

在我的LoadContent方法:

for (int x = 0; x < 21; x++) 
{ 
    roomModel[x] = Content.Load<Model>("Models\\sphere_model"); 
} 

在我的绘制方法:

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;      
    } 
} 

我该如何改变其中一个房间模型的纹理而不是全部?我能想到的是为每个房间创建一个单独的模型,但这听起来很昂贵。

回答

1

我认为这是由于内容管理器的行为:

ContentManager的每个实例将只加载任何给定的资源 一次。第二次请求资源时,它将返回上次返回的相同 实例。

所以,当你设置

roomModel[x] = Content.Load<Model>("Models\\sphere_model"); 

你给同一个参考每一个模型。

我认为这就是为什么你的最后一个代码改变你所有的模型。