- Home /
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?
Statement is pointing you in the right direction, but I don't see why you can't just model the vert colors.
That's right. One overlooks the easiest solutions easily. :)
Answer by Statement · Mar 09, 2011 at 01:24 PM
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:
// Unityscript converted to C#
void Start ()
{
var mesh = GetComponent<MeshFilter>().mesh;
var uv = mesh.uv;
var colors = 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.
Answer by Clonus · Nov 15, 2017 at 12:31 PM
void Start () {
var mesh = GetComponent<MeshFilter>().mesh;
Vector2[] uv = mesh.uv;
Color[] colors = new Color[uv.Length];
for (var i = 0; i< uv.Length; i++)
colors[i] = Color.Lerp(Color.red, Color.green, uv[i].x);
mesh.colors = colors;
}
Your answer
Follow this Question
Related Questions
procedurally generated gradient 2 Answers
How to avoid Texture.Apply() 1 Answer
Cutout Material is not showing texture transparency? 0 Answers
2 materials/shaders 1 texture? 2 Answers
Textures not showing up 0 Answers