Jump to content

[SOLVED] Swtich.prop - problem with materials


photo

Recommended Posts

Hello,

I have a problem with objects with switch.prop when performing material switching on them (in VR).
I have modified the code of the material laser switcher from this post by fox:

so that i can perform material switching on a given node when clicking a button in gui ( it turns out to be more intuitive for the users) and also so that it is performed to all the children in this node´s hierarchy.
Firstly i create a vector of materials, and then there is the callback function of my button:

 

void GuiSample::mat_switch2()
{
	insel3 = Editor::get()->getNodeByName("Kitchen_schrank_3");
	int children = insel3->getNumChildren();
	for (int i = 0; i < children; i++)
	{
		ObjectPtr object5 = Object::cast(insel3->getChild(i));
		object5->setMaterial(my_materials3[mat_index3], "*");
	}

	if ((mat_index3 + 1) < my_materials3.size())
		mat_index3++;
	else
		mat_index3 = 0;
}



Materials switching works fine as long as I don´t use one of the switch objects ( it only has "change rotation" enabled) which also are in the given node´s hierarchy. After that if i click the material switching button once again the application crashes, so I assume there is some conflict with this property.  

Would be great if you could have a look at it!

Link to comment

Hi Piotr,

You could try something like this (I've changed your code a bit and added a recursive function switchMaterials() for hierarchy processing):

// method performing material switching for all child nodes of the corresponding types in the specified node's hierarchy recursively
int GuiSample::switchMaterials(NodePtr n)
{
	int res = 0; // no changes (initial value)
  	if (!n) return 0; // nothing to change here
  
	// getting node's type to process it (along with the hierarchy) properly
	int type = n->getType();
  
	// we shall change material only for mesh static, dynamic and clutter/cluster objects to avoid setting mesh_base material to inappropriate object types (e.g. ObjectText, etc.)
	if ((type == Node::OBJECT_MESH_STATIC) || (type == Node::OBJECT_MESH_DYNAMIC) || (type == Node::OBJECT_MESH_CLUTTER) || (type == Node::OBJECT_MESH_CLUSTER)){
		
		ObjectPtr object5 = Object::cast(n);
		object5->setMaterial(my_materials3[mat_index3], "*");
		// setting the flag, as we've changed the material 
		res = 1;
	}
	else // checking whether we've got a node reference to process its hierarchy properly
	if (type == Node::NODE_REFERENCE){
		res = switchMaterials(NodeReference::cast(n)->getReference());
	}
	// trying to change material for all node's children
	int children = n->getNumChildren();
	for (int i = 0; i < children; i++)
	{
		res += switchMaterials(n->getChild(i));
	}

	// returning the result: whether we've changed material of the node (or any node in its hierarchy) or not
	return res;
}

// your mat_switch2 method (onClick handler for your button)
void GuiSample::mat_switch2()
{
	// let it be a button (a switch object used in the default VR Template world) <- you can replace it with your Kitchen_schrank_3 or anything else
	insel3 = Editor::get()->getNodeByName("button");
	// if a material was switched, proceed to the next one in the list
	if (switchMaterials(insel3)) {
		if ((mat_index3 + 1) < my_materials3.size())
			mat_index3++;
		else
			mat_index3 = 0;
	}
}

This should work fine for all objects!

Hope this helps!

Thanks!

Link to comment
  • 2 weeks later...
  • morbid changed the title to [SOLVED] Swtich.prop - problem with materials
×
×
  • Create New...