blue
blue

Reputation: 7385

Unity - how to change the material of a torus mesh by percentage?

Ok, so basically I have a torus shaped mesh and I need it in Unity, depending on a value, to change its material around in a clockwise direction. Like this where the white is increasing in percentage:

enter image description here

I have no idea how to go about this as I have limited knowledge of dynamic shaders. Would I need to gradually show a separate object? Or how could I go about this?

Upvotes: 0

Views: 253

Answers (1)

Kalle Halvarsson
Kalle Halvarsson

Reputation: 1268

Although you could technically make a shader which calculates the angle to each pixel given an object center, your best bet would be to unwrap the torus in such a way that the travel direction of your transition is mapped to the X axis in UV space (see image). Then, you can expose a property in the range of 0 to 1 which represents the cutoff point in percent. Finally, in your fragment or surface function, you can do this:

if (i.uv.x - _Cutoff > 0) {
    // Material A
    col = tex2D(_TextureA, i.uv);
    roughness = _RoughnessA;
    // etc.
} else { 
    // Material B
    col = tex2D(_TextureB, i.uv);
    roughness = _RoughnessB;
}

You can then animate the _Cutoff property through C# script. What's even cooler is that you can use a mask texture to describe the transition, and offset its uv.x coord by your offset variable, and then use the color value to lerp between two material properties.

torus unwrap

Upvotes: 0

Related Questions