x


is there a way to make a gradient material, white to black, in Unity3D?

Assuming a model has UV's already setup, is there a way to "create" procedurally or otherwise, in Unity3d, a gradient material without using a texture?

Here's an example of a gradient ramp: http://www.secondpicture.com/tutorials/3d/gradient_ramp_parameters.png

Or do I need to create a texture, import it and apply it to the model?

more ▼

asked Mar 09 '11 at 01:13 PM

dissidently gravatar image

dissidently
450 50 55 60

Statement is pointing you in the right direction, but I don't see why you can't just model the vert colors.

Mar 09 '11 at 01:51 PM Jessy

That's right. One overlooks the easiest solutions easily. :)

Mar 09 '11 at 02:03 PM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can create a material procedurally without textures by writing your own shaders. That way, you'll be working primarily with vertex positions, normals, uvs, colors and calling different arithmetic functions that are interpolated across the surface.

For example, you could do the ramp along your uv.x or uv.y. Just interpolate a color using the value from the uv map.

Or even more simple, depending on your needs, just set the Mesh.colors to the colors instead and use a shader that makes use of colors (I think the built in ones do, but I am not 100% sure - try it out!). Otherwise you can check out this shader.

  • Edit: The link I provided actually says:

    (Note that most builtin shaders don't display vertex colors, you can use eg. a particle shader to see vertex colors)

Here, each color represents a vertex in your mesh. This color is interpolated across the surface of the neighboring triangles automatically, just like texture coordinates or normals.

Editing the example from Mesh.colors could yield:

function Start () 
{
    var mesh : Mesh = GetComponent(MeshFilter).mesh;
    var uv : Vector2[] = mesh.uv;
    var colors : Color[] = new Color[uv.Length];

    // Instead if vertex.y we use uv.x
    for (var i = 0; i < uv.Length;i++)
        colors[i] = Color.Lerp(Color.red, Color.green, uv[i].x); 

    mesh.colors = colors;
}

I tested this with a particle material and it seems to work fine.

more ▼

answered Mar 09 '11 at 01:24 PM

Statement gravatar image

Statement ♦♦
20.2k 35 71 176

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2276
x1740
x841
x308
x22

asked: Mar 09 '11 at 01:13 PM

Seen: 3300 times

Last Updated: Mar 09 '11 at 01:13 PM