1
\$\begingroup\$

I'm trying to achieve a simple effect: I have a cube mesh and I want some of the vertices to change color. In this case, I want it to form some sort of a gradient where the unchanged vertices are set to the default color (with alpha = 1) and the changed vertices to slowly fade (default color with alpha decreasing towards 0).

I'm able to modify these vertices' color with this piece of code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Mesh mesh;
    Vector3[] vertices;
    Color[] colors;
    List<int> verticesToChange;

    void Start()
    {
        mesh = GetComponent<MeshFilter>().mesh;
        vertices = mesh.vertices;
        colors = new Color[vertices.Length];
        verticesToChange = new List<int>();

        for(int i = 0; i < vertices.Length; i++) {
            colors[i] = Color.white; //Set the default vertex color
            if(Mathf.Approximately(vertices[i].y, 0.5f)) //Change only the vertices on the plane y=0.5
                verticesToChange.Add(i);
        }

    }

    void Update()
    {
        foreach(int i in verticesToChange) {
            colors[i].a = Mathf.Max(colors[i].a - Time.deltaTime * 0.5f, 0f);
        }

        mesh.colors = colors;
        //Not needed here, but I will be changing the vertices' position too later
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        mesh.RecalculateTangents();
    }
}

My problem is selecting a shader that actually uses this color and this color only. I've tried Unlit/Color, Unlit/Transparent and Particles/Standard Unlit with multiple combinations of Rendering Mode, Color Mode and Albedo color.

Is there a built-in shader that does this and I'm not getting the right combination of its parameters? Or do I have to build my own shader? I've never wrote a shader with Unity, so I would prefer a built-in option or an open source / free shader.

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

After continuing testing different combinations of Shader parameters I got it working like this:

Material Shader

I feel silly because this actually makes sense, so I don't know why I didn't try that...

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .