Access all materials associated with a mesh renderer

Hiya,

I am working with some highlight code provided from some sample I recieved from someone up here and am running into a problem where multiple materials are attached to an object, for example a door.

This door has two materials, a glass material and a door material. The glass material is in the 1st slot and the door material is in the 2nd slot. When I make the call to highlight the mesh only the glass material is executing the highlight.

So, I was looking at a way to essentially iterate through all the materials associated with the mesh, store the base color of each and change the color but only for the particular instance of the material that is associated with this mesh. I read and was thinking about sharedMaterials but that seemed to not be the way to go.

Does anyone have some ideas or better ways to do highlighting?

Here is the code I am using:

private var overObject : GameObject;
private var baseColor : Color;
private var highlightFactor : float = .4;


function Update () {

	// Get RayCast Hit
	var r : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	var hit : RaycastHit;
	
	// Check Hit  Make Sure No Current Object
	if (Physics.Raycast(r, hit)) {
		if (overObject == null) { 
			
			// Set Over Object  Highlight
			overObject = hit.transform.gameObject;
			this.highlightCurrentObject();
	
		// Left Orig Over Object  Over New One
		} else if (hit.transform != overObject.transform){
			
			// Remove Highlight Of Old Object
			this.restoreCurrentObject();
			
			// Store New Object  Highlight
			overObject = hit.transform.gameObject;
			this.highlightCurrentObject();

		}	
	} else {
	
		// Not Over Any Interact Object Restore Highlight (if previously stored)
		this.restoreCurrentObject();
	}
}

function highlightCurrentObject() {
	
	// Make Sure Object Is Interact
	if (overObject.tag == "Interact"){
		
		// Get Highest Level Renderer  Base Color
		var rend : Renderer = overObject.GetComponentInChildren(Renderer);
		baseColor = rend.material.GetColor("_Color");
		
		// Generate New Color  Apply
		var newColor : Color = Color(baseColor.r + highlightFactor, baseColor.g +highlightFactor, baseColor.b + highlightFactor, baseColor.a);
		rend.material.color = newColor;
	
	}
}

function restoreCurrentObject() {
						
	// Ensure Object Is Had  Able To Ineract
	if (overObject != null) {
		if (overObject.tag == "Interact"){
			
			// Get Highest Level Renderer  Reset To Base Color
			var rend : Renderer= overObject.GetComponentInChildren(Renderer);
			rend.material.color = baseColor;
			
			// Clear Object
			overObject = null;
		}
			
	}
}

Thanks for the help!

Regards,

– Clint

I’m having the same problem.
I have a mesh composed of different parts, each subpart has its own meshrenderer, and material.
When I try to highlight the mesh, only the last material has a change in its “main color” attribute.
I guess there must be a way to either combine the mesh before import (so there is only one mesh) or iterate through the subcomponents…

You can do it via chnaging out this line…

Yours:

var rend : Renderer = overObject.GetComponentInChildren(Renderer);

New:

var rends : Renderer[] = overObject.GetComponentsInChildren(Renderer);

This will give you an array of all the renderers attached from the parent on down.

Then, you could use the Renderer.materials property to get to each material of each renderer.

Hope this is what you’re looking for,
Nathan

So basically, you could make that part of your code look like this:

var rends : Renderer[] = overObject.GetComponentsInChildren(Renderer);

for ( r in rends )
    for ( m in r.materials )
        m.color = newColor; // or baseColor, depending