Jump to content
gamecreator

Model Copy

Recommended Posts

Just trying to get copies (not the default instances) of models I can individually apply textures to. Neither of the following work and I can't find any helpful examples online.

 

model=Model::Load("Models/brick2.mdl",Asset::CreateNew);

 

model=(Model *)Model::Load("Models/brick2.mdl")->Copy();

 

I was trying to work off of this example with no luck and whatever I could find online.

http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/asset/assetcopy-r40

Share this post


Link to post

I thought that same model file can never have different materials. They are always instances.

No, sorry. Now I see they are always instanced only in the editor.

Share this post


Link to post

 

Well, thats what he is showing in his second code example. It works in lua so it should work in c++. The only suggestion would be to try to create the copy from the original loaded model reference instead to see if it makes a difference.

 

model1 = Model:Load("Models/brick.mdl")
model2 = model1:Copy()
model2:SetPosition(1,0,1)

Share this post


Link to post

Good thought but no luck. I'll make a simpler example and see if I can submit a bug report with example. This is part of the two for loops which go through the array and create bricks:

 

level[j][i].model=(Model *)preloads_models[2]->Copy(true);
// level[j][i].model=Model::Load("Models/brick2.mdl", Asset::CreateNew);
level[j][i].model->SetPosition(i, 0, j, true);
level[j][i].model->SetRotation(90, 270, 0, true);

ran=(int)Math::Random(0, 4);
if(ran==0) level[j][i].model->GetSurface(0)->GetMaterial()->SetTexture(Texture::Load("Models/blue.tex"));
else if(ran==1) level[j][i].model->GetSurface(0)->GetMaterial()->SetTexture(Texture::Load("Models/green.tex"));
else if(ran==2) level[j][i].model->GetSurface(0)->GetMaterial()->SetTexture(Texture::Load("Models/red.tex"));
else level[j][i].model->GetSurface(0)->GetMaterial()->SetTexture(Texture::Load("Models/yellow.tex"));

 

... results in the same texture for all of the copies.

Share this post


Link to post

hmmm the problem there is probably due to the fact that the material itself is not an individual copy... you have the same material being applied to all the copied bricks, so just changing the texture would just apply the last texture change to the same material thats being used on everything. This should work if you apply different materials to each model copy. Or create your own material via code and then make an individual copy of it to be applied to each individual brick that you want to have a certain texture.

Share this post


Link to post

this seems to work ok:

 

mymodel = Model:Load("Models/crates/crate_large.mdl")
model = {}
mat = {}
mat[0] = Material:Load("materials/developer/bluegrid.mat")
mat[1] = Material:Load("materials/developer/greengrid.mat")
mat[2] = Material:Load("materials/developer/greygrid.mat")
for i = 0,2 do
 model[i] = mymodel:Copy()
 model[i]:SetPosition(1+i,0,0)
 model[i]:SetMaterial(mat[i])
end

 

and this way as well:

 

mymodel = Model:Load("Models/crates/crate_large.mdl")
material = mymodel:GetSurface(0):GetMaterial()
model = {}
mat = {}
tex = {}
tex[0] = Texture:Load("materials/developer/bluegrid.tex")
tex[1] = Texture:Load("materials/developer/greengrid.tex")
tex[2] = Texture:Load("materials/developer/greygrid.tex")
for i = 0,2 do
 model[i] = mymodel:Copy()
 model[i]:SetPosition(1+i,0,0)
 mat[i] = tolua.cast(material:Copy(), "Material")
 mat[i]:SetTexture(tex[i])
 model[i]:SetMaterial(mat[i])
end

Share this post


Link to post

At this point I'm not exactly sure what's going on but based on what you suggested, I decided to start experimenting again. The following ended up working for me.

 

ran=(int)Math::Random(0, 4);
if(ran==0) level[j][i].model->SetMaterial(Material::Load("Models/blue.mat"));
else if(ran==1) level[j][i].model->SetMaterial(Material::Load("Models/green.mat"));
else if(ran==2) level[j][i].model->SetMaterial(Material::Load("Models/red.mat"));
else level[j][i].model->SetMaterial(Material::Load("Models/yellow.mat"));

 

I'm in your debt macklebee. Thank you for all your help!!

Share this post


Link to post

I came across this again as I was animating explosions and wanted to add a note here that using the following (adding the true argument) is also needed.

explosion[i].model->SetMaterial(Material::Load(material_filename), true);

 

Share this post


Link to post

It's not supposed to be true, it is Asset::Unmanaged for the flags value. But that is probably equal to 1 and true resolves to that.

Share this post


Link to post

I think you're talking about Material::Load.  I was talking about SetMaterial, the bool argument to set it to recursive since I guess it's false by default.

  • Haha 1

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...