void Thingy::set_multiply_color()

in roboschool/cpp-household/assets-mesh.cpp [233:269]


void Thingy::set_multiply_color(const std::string& tex, uint32_t* color, std::string* replace_texture)  // not urdf at all, but does very similar thing to the function above
{
	shared_ptr<Material> replace_me, modified;
	shared_ptr<ThingyClass> alt_class(new ThingyClass);
	*alt_class = *klass; // copy by value
	alt_class->shapedet_visual.reset(new ShapeDetailLevels);
	alt_class->shapedet_visual->materials.reset(new MaterialNamespace);
	for (auto pair: klass->shapedet_visual->materials->name2mtl) {
		shared_ptr<Material> mat = pair.second;
		if (pair.first==tex) {
			replace_me = mat;
			modified.reset(new Material(replace_me->name + ":colorized"));
			*modified = *replace_me; // copy by value
			if (color) modified->multiply_color = *color;
			if (replace_texture) modified->diffuse_texture_image_fn = *replace_texture;
			alt_class->shapedet_visual->materials->name2mtl[pair.first] = modified;
		} else {
			alt_class->shapedet_visual->materials->name2mtl[pair.first] = mat;
		}
	}
	for (int lev=0; lev<DETAIL_LEVELS; lev++) {
		const std::vector<shared_ptr<Shape>>& shapes = klass->shapedet_visual->detail_levels[lev];
		std::vector<shared_ptr<Shape>>& alt_shapes = alt_class->shapedet_visual->detail_levels[lev];
		for (int c=0; c<(int)shapes.size(); ++c) {
			if (shapes[c]->material==replace_me) {
				shared_ptr<Shape> alt_shape(new Shape);
				*alt_shape = *shapes[c];
				alt_shape->material = modified;
				alt_shapes.push_back(alt_shape);
			} else {
				alt_shapes.push_back(shapes[c]);
			}
		}
	}
	alt_class->modified_from_class = klass;
	klass = alt_class;
}