Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Change vertices color on URP ?

Discussion in 'Universal Render Pipeline' started by fomafomitch, Jun 22, 2022.

  1. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    86
    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/c...9/manual/writing-shaders-urp-unlit-color.html

    And I have this code to change vertix color :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    5. public class HexMesh : MonoBehaviour
    6. {
    7.  
    8.     Mesh hexMesh;
    9.     [SerializeField] List<Vector3> vertices;
    10.     [SerializeField] List<Color> colors;
    11.     [SerializeField] List<int> triangles;
    12.  
    13.     [SerializeField] Renderer hexRend;
    14.  
    15.     MeshCollider meshCollider;
    16.  
    17.     void Awake()
    18.     {
    19.         GetComponent<MeshFilter>().mesh = hexMesh = new Mesh();
    20.         hexRend = GetComponent<Renderer>();
    21.         meshCollider = gameObject.AddComponent<MeshCollider>();
    22.         hexMesh.name = "Hex Mesh";
    23.         vertices = new List<Vector3>();
    24.         colors = new List<Color>();
    25.         triangles = new List<int>();
    26.     }
    27.  
    28.     public void Triangulate(HexCell[] cells)
    29.     {
    30.         hexMesh.Clear();
    31.         vertices.Clear();
    32.         colors.Clear();
    33.         triangles.Clear();
    34.         for (int i = 0; i < cells.Length; i++)
    35.         {
    36.             Triangulate(cells[i]);
    37.             hexRend.material.SetColor("_BaseColor", Color.green);
    38.         }
    39.         hexMesh.vertices = vertices.ToArray();
    40.         hexMesh.colors = colors.ToArray();
    41.         hexMesh.triangles = triangles.ToArray();
    42.         hexMesh.RecalculateNormals();
    43.         meshCollider.sharedMesh = hexMesh;
    44.     }
    45.  
    46.     void Triangulate(HexCell cell)
    47.     {
    48.         Vector3 center = cell.transform.localPosition;
    49.         for (int i = 0; i < 6; i++)
    50.         {
    51.             AddTriangle(
    52.                 center,
    53.                 center + HexMetrics.corners[i],
    54.                 center + HexMetrics.corners[i + 1]
    55.             );
    56.             AddTriangleColor(cell.color);
    57.         }
    58.     }
    59.  
    60.     void AddTriangle(Vector3 v1, Vector3 v2, Vector3 v3)
    61.     {
    62.         int vertexIndex = vertices.Count;
    63.         vertices.Add(v1);
    64.         vertices.Add(v2);
    65.         vertices.Add(v3);
    66.         triangles.Add(vertexIndex);
    67.         triangles.Add(vertexIndex + 1);
    68.         triangles.Add(vertexIndex + 2);
    69.     }
    70.  
    71.     void AddTriangleColor(Color color)
    72.     {
    73.         colors.Add(color);
    74.         colors.Add(color);
    75.         colors.Add(color);
    76.     }
    77. }

    here the custom shader thas is NOT working anymore :
    https://imgur.com/IYXE8NB

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

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    628
    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)
     
    TinyMic likes this.
  3. Jesper-Nielsen

    Jesper-Nielsen

    Joined:
    Nov 14, 2013
    Posts:
    95
    Surely if it can be done in shader graph it can also be done in custom shader code?
     
  4. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    628
    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)
     
    TinyMic and goncalo-vasconcelos like this.
  5. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    86
    Thanks you it's working !
     
  6. Jesper-Nielsen

    Jesper-Nielsen

    Joined:
    Nov 14, 2013
    Posts:
    95
    You mean it's easier like that than writing from scratch?
     
  7. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    679
    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.