joseph.howell Posted February 11, 2016 Posted February 11, 2016 Hi, I'm trying to create a material inheriting from one defined in my mat file, and then set the albedo color on that material. It doesn't seem to work... Several objects in this screen capture are supposed to be different colors here, but instead appear as white. Here's my code Materials *materials = Materials::get(); materials->inheritMaterial(iceMaterial->baseMatId, MATERIAL_LIBRARY, iceMaterial->matId); MaterialPtr material = materials->findMaterial(iceMaterial->matId); if (color != NULL) { int parameter = material->findParameter("albedo_color"); material->setParameter(parameter, vec4(color[0], color[1], color[2], color[3])); } The parameters I'm passing into inheritMaterial are "ice_matte", "ice_material_library" and "ice_matte_X" (Where x is a uniquie id). The values passed into the vector in setParameter vary, but in a particular example are set to: 0.59, 0.52, 0.43, 1. And this is our ice_matt from our iceMaterials.mat file: <material name="ice_matte" parent="mesh_base"> <parameter name="albedo_color">1 1 1 1</parameter> <parameter name="ior">0</parameter> </material> We have certain objects in our library that have no textures but rely on material colors. In the attached screenshot I demonstrate on the left what the objects are supposed to look like, and on the right, what they look like in unigine. The textured objects show up fine, the material-colored objects just show as white. Thanks
silent Posted February 12, 2016 Posted February 12, 2016 Hi Joseph, Just created a new C++ project from SDK Browser. In *.cpp file I have following code that is working fine: #include <UnigineEngine.h> #include <UnigineMaterials.h> #include <UnigineMaterial.h> #include <UnigineObjectMeshStatic.h> using namespace Unigine; /* */ int main(int argc,char *argv[]) { // init engine Unigine::EnginePtr engine(UNIGINE_VERSION,argc,argv); Materials *materials = Materials::get(); materials->create("new_library"); materials->inheritMaterial("material_ball", "new_library", "new_material_ball"); MaterialPtr material = materials->findMaterial("new_material_ball"); int parameter = material->findParameter("albedo_color"); material->setParameter(parameter, vec4(1.0f, 0.0f, 0.0f, 1.0f)); ObjectMeshStaticPtr object = ObjectMeshStatic::create("core/meshes/material_ball.mesh"); for (int i = 0; i < object->getNumSurfaces(); i++) { object->setMaterial("new_material_ball", i); object->setProperty("surface_base", i); } object->setWorldTransform(UNIGINE_MAT4(translate(vec3(1.0f, 1.0f, 0.0f)))); object->release(); // main loop while(!engine->isDone()) { engine->update(); engine->render(); engine->swap(); } return 0; } Are you sure that you assigned correct material to object's surfaces after you changed the material parameters?Thanks! How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN
joseph.howell Posted February 12, 2016 Author Posted February 12, 2016 Yes, pretty sure. Immediately after that code, we're doing this: materialsMap[matLookupId] = iceMaterial; //iceMaterial is a structure, one of the members is the material we created. We store all our code created materials in a map iceMaterial->material = material; //this is where we set it's internal material pointer to the material we created in the code I posted above. //and then immediately afterward on the following line: mesh->setMaterial(materialsMap[matLookupId], "*"); I'll look through your code to see if I can get any more clues as to what I might be doing wrong.
joseph.howell Posted February 12, 2016 Author Posted February 12, 2016 It looks like we're doing pretty much the same stuff. One thing I forgot to mention is we're also doing this after setting the material: mesh->setProperty("surface_base", "*"); What does the xml for your "material_ball" material look like? The only real difference I see is the for loop you're doing to set the material and "surface_base" property. Would that have a different result than setting it with the "*" parameter? I don't think so, but I'll try it. Thanks for the help
silent Posted February 12, 2016 Posted February 12, 2016 Hi Joseph, What does the xml for your "material_ball" material look like? it's default material from the newly created project. Just create a new default project with SDK Browser (don't forget to specify C++ API). Material library (*.mat), *.world file and test content will be available in <project_dir>/data directory. The only real difference I see is the for loop you're doing to set the material and "surface_base" property. Would that have a different result than setting it with the "*" parameter? I don't think so, but I'll try it. The result should be the same. Will test it also in my scene. How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN
joseph.howell Posted February 12, 2016 Author Posted February 12, 2016 So the tinting using the albedo_color does seem to work if I use the material_ball material. So now I just need to figure out what's the difference between the materials and what it is that makes the albedo_color start working.
joseph.howell Posted February 12, 2016 Author Posted February 12, 2016 All right, I figured out the problem, and it was our code :) . I mean, I had kind of always figured I was making a mistake somewhere, just couldn't pinpoint it. The problem was the map we were using to store the materials in. The first pass through on some other object that used ice_matte would create the inherited material, ice_matte_X (where X was a unique identifier for the texture, since we needed the same material but with different textures). The first iteration would create a material with the color 1,1,1,1. Subsequent passes would see that ice_matte_X had already been created and would just use the same material, never even setting the color. So I fixed it by adding the color to the key. In other words: ice_matte_X_1,1,1,1 or ice_matte_X_1,0,0,1 for a red material. Thanks, sorry for the runaround! Anyways, now that I have the key right, the original ice_matte material works with the albedo color.
silent Posted February 12, 2016 Posted February 12, 2016 Good to hear that :)I will mark topic as solved. How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN
Recommended Posts