Change vertices color on URP ?

Hello,

I’m following a tutorial for hex map. They are composed with 6 triangles. And I want to change the color of 1 triangle. Problem, in the tutorial (2015) they use a Standard Surface Shader, and I’m using URP so I don’t have this shader working.

I made a custom shader from this unity documentation :
https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@10.9/manual/writing-shaders-urp-unlit-color.html

And I have this code to change vertix color :

using UnityEngine;
using System.Collections.Generic;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class HexMesh : MonoBehaviour
{

    Mesh hexMesh;
    [SerializeField] List<Vector3> vertices;
    [SerializeField] List<Color> colors;
    [SerializeField] List<int> triangles;

    [SerializeField] Renderer hexRend;

    MeshCollider meshCollider;

    void Awake()
    {
        GetComponent<MeshFilter>().mesh = hexMesh = new Mesh();
        hexRend = GetComponent<Renderer>();
        meshCollider = gameObject.AddComponent<MeshCollider>();
        hexMesh.name = "Hex Mesh";
        vertices = new List<Vector3>();
        colors = new List<Color>();
        triangles = new List<int>();
    }

    public void Triangulate(HexCell[] cells)
    {
        hexMesh.Clear();
        vertices.Clear();
        colors.Clear();
        triangles.Clear();
        for (int i = 0; i < cells.Length; i++)
        {
            Triangulate(cells[i]);
            hexRend.material.SetColor("_BaseColor", Color.green);
        }
        hexMesh.vertices = vertices.ToArray();
        hexMesh.colors = colors.ToArray();
        hexMesh.triangles = triangles.ToArray();
        hexMesh.RecalculateNormals();
        meshCollider.sharedMesh = hexMesh;
    }

    void Triangulate(HexCell cell)
    {
        Vector3 center = cell.transform.localPosition;
        for (int i = 0; i < 6; i++)
        {
            AddTriangle(
                center,
                center + HexMetrics.corners[i],
                center + HexMetrics.corners[i + 1]
            );
            AddTriangleColor(cell.color);
        }
    }

    void AddTriangle(Vector3 v1, Vector3 v2, Vector3 v3)
    {
        int vertexIndex = vertices.Count;
        vertices.Add(v1);
        vertices.Add(v2);
        vertices.Add(v3);
        triangles.Add(vertexIndex);
        triangles.Add(vertexIndex + 1);
        triangles.Add(vertexIndex + 2);
    }

    void AddTriangleColor(Color color)
    {
        colors.Add(color);
        colors.Add(color);
        colors.Add(color);
    }
}

here the custom shader thas is NOT working anymore :
Imgur

How to update this shader to work with URP and my code ?

There’s no surface shader support with URP/HDRP.

You’ll have to use Shader Graph, create a new URP Lit Shader Graph, and add (amongst other things) a Vertex Colour node to get the colour.

(Alternatively, it’s possible to duplicate the default URP lit shader and modify it, but it’s easier said than done, as it’s spread over several files, and you need to understand what the different passes do, and how to ensure that shader works in both forward and deferred modes)

1 Like

Surely if it can be done in shader graph it can also be done in custom shader code?

Yes. But only by duplicating+modifying a large shader that is part of the URP package.

Surface shaders were an abstraction layer that don’t currently exist for scriptable pipelines. Shader graph was supposed to replace them. Many people preferred the code-based approach though. To some extent, you can use a custom function node in ShaderGraph if you prefer code to visual spaghetti

(If you’re dealing with unlit shaders, e.g. for 2D/UI, the shaders are much less complex and it’s still easy enough to work with code-based shaders)

2 Likes

Thanks you it’s working !

You mean it’s easier like that than writing from scratch?

Personally, I’ve just been reading through the URP source and writing URP shaders from scratch when possible. It’s really not that bad. For making asset store stuff tho, anything that needs to “just work” for everyone, shadergraph is the way to go unfortunately.