anton.stetz Posted April 11, 2013 Share Posted April 11, 2013 Hi all I need to be able to replace materials on the objects through code. Example: I have material "sample_01", which is assigned to many objects and surfaces in the current scene. Now I want all of these objects to assign material "sample_02", then sample_03 etc. These materials are in the same library, and have a clear structure by name. My idea: temp = sample_01.getObjects (); temp.setMaterial (sample_02); But the functions of type material :: getObjects in documentation is not found. Now I look in the direction of Material :: saveState and Material :: restoreState. Maybe you have any other solutions to this issue? thanks in advance Link to comment
ulf.schroeter Posted April 11, 2013 Share Posted April 11, 2013 Anton, what effect do you want to achive ? In general modification of the assigned material instance (in your case "sample_01") instead of trying to replace it on all surfaces should be the way to follow, therefore save()/restore() sounds promissing Link to comment
anton.stetz Posted April 11, 2013 Author Share Posted April 11, 2013 Thanks for reply! I need a library of materials in my application, to be able to change the color and type of roof, wall, floor Link to comment
ulf.schroeter Posted April 11, 2013 Share Posted April 11, 2013 For each material sub-type create a reference material instance e.g. roof_type, wall_type in addition to your _1, _2, _3... material variants Asssign these reference materials to your geometry Swap materials on-the-fly by: serialization of selected variant _1. _2, .. material to Buffer stream (in-memory) via Material::saveState( buffer ) Call Material::restoreState( buffer ) on reference material instance This should swap your scene materials without much effort Link to comment
anton.stetz Posted April 12, 2013 Author Share Posted April 12, 2013 Thanks Ulf, i will test it today Link to comment
anton.stetz Posted April 12, 2013 Author Share Posted April 12, 2013 void select(Material curMat, Material varMat) { Buffer buffer = new Buffer(); // new memory buffer curMat = engine.materials.findMaterial(curMat); // current material on objects Material temp_mat = engine.materials.findMaterial(varMat); // temp material to store variants temp_mat.saveState(buffer); // store material in temp curMat.restoreState(buffer); // restore material settings to current material //buffer.clear(); log.message("select_yes\n"); } and then select("roof_type","roof_type_2"); but don't work Link to comment
frustum Posted April 12, 2013 Share Posted April 12, 2013 Please add a seekSet() call before restoreState(). buffer.seekSet(0); curMat.restoreState(buffer); Link to comment
anton.stetz Posted April 12, 2013 Author Share Posted April 12, 2013 Ok, thanks, it works Link to comment
ulf.schroeter Posted April 12, 2013 Share Posted April 12, 2013 BTW, just as a hint: you define select(Material curMat, Material varMat), but actually string parameters are provided. Then curMat string parameter will be used to find material and curMat then will be assigned a Material instance (or NULL). Though UNIGINE as untyped script language supports this, I wouldn't recommend it. Link to comment
anton.stetz Posted April 13, 2013 Author Share Posted April 13, 2013 Thanks Ulf, i think about another solution Link to comment
Recommended Posts