2013-11-03 44 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"); 

你給同一個參考每一個模型。

我認爲這就是爲什麼你的最後一個代碼改變你所有的模型。