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 :
How to update this shader to work with URP and my code ?