shader graph on generated mesh

I created a simple water shader graph and I was wondering if it was possible to apply it to a generated mesh. I can’t find much information on it, am I missing something?

This is the mesh generator:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
public class MeshGenerator : MonoBehaviour
{
Mesh mesh;
public Transform cam;

Vector3[ ] vertices;
int[ ] triangles;
public int xSize = 1000;
public int zSize = 1000;
public int previousXPos = -999, previousZPos = -999;

// Start is called before the first frame update
void Start()
{
mesh = new Mesh();
GetComponent().mesh = mesh;

}

private void Update()
{
int xPos = (int)Mathf.Floor(cam.position.x / 10);
int zPos = (int)Mathf.Floor(cam.position.z / 10);
if (xPos != previousXPos || zPos != previousZPos)
{
CreateShape((int)cam.position.z, (int)cam.position.x);
UpdateMesh();
//Debug.Log(“update”);
previousXPos = xPos;
previousZPos = zPos;
}

}
void CreateShape(int zPos, int xPos)
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)] ;

int jump = 50;
for (int i = 0, z = zPos - (zSize/2); z <= zPos + (zSize/2); z += jump)
{

for (int x = xPos - (xSize/2); x <= xPos + (xSize/2); x += jump )
{
vertices = new Vector3(x, 0, z);
i++;
}
}
triangles = new int[(xSize * zSize * 6)/jump];
int vert = 0;
int tris = 0;
for (int z = 0; z < zSize; z+= jump)
{
for (int x = 0; x < xSize; x+= jump)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + (xSize/jump) + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + (xSize / jump) + 1;
triangles[tris + 5] = vert + (xSize / jump) + 2;
vert++;
tris += 6;
}
vert++;
}

}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}
Here is the Shader Graph:
Imgur: The magic of the Internet

I found a solution. the problem I had was that I hadn’t applied UV’s.
Here is the new code.

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

[RequireComponent(typeof(MeshFilter))]
public class MeshGenerator : MonoBehaviour
{
    Mesh mesh;
    public Transform cam;

    Vector3[] vertices;
    int[] triangles;
    Vector2[] uvs;
    public int xSize = 1000;
    public int zSize = 1000;
    public int previousXPos = -999, previousZPos = -999;

    // Start is called before the first frame update
    void Start()
    {
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;

    }

    private void Update()
    {

        int xPos = (int)Mathf.Floor(cam.position.x);
        int zPos = (int)Mathf.Floor(cam.position.z);
        if (xPos != previousXPos || zPos != previousZPos)
        {
            CreateShape((int)cam.position.z, (int)cam.position.x);
            UpdateMesh();
            //Debug.Log("update");
            previousXPos = xPos;
            previousZPos = zPos;
        }

   

    }
      
       

   
    void CreateShape(int zPos, int xPos)
    {
        vertices = new Vector3[(xSize + 1) * (zSize + 1)] ;
        uvs = new Vector2[(xSize + 1) * (zSize + 1)];
        int jump = 500;
        for (int i = 0, z = zPos  - (zSize/2); z <= zPos  + (zSize/2); z += jump)
        {

            for (int x = xPos  - (xSize/2); x <= xPos  + (xSize/2); x += jump )
            {
                //float y = Mathf.Sin((x * 0.3f) + (z * 0.3f)) * 2f;
                vertices[i] = new Vector3(x, 0, z);

                uvs[i] = new Vector2(((float)(x + (xSize / 2)) / xSize), ((float)(z + (zSize / 2)) / zSize));

                i++;
            }
        }
       

       

        triangles = new int[(xSize * zSize * 6)/jump];

        int vert = 0;
        int tris = 0;

        for (int z = 0; z < zSize; z+= jump)
        {
            for (int x = 0; x < xSize; x+= jump)
            {
                triangles[tris + 0] = vert + 0;
                triangles[tris + 1] = vert + (xSize/jump) + 1;
                triangles[tris + 2] = vert + 1;
                triangles[tris + 3] = vert + 1;
                triangles[tris + 4] = vert + (xSize / jump) + 1;
                triangles[tris + 5] = vert + (xSize / jump) + 2;

                vert++;
                tris += 6;

            }
            vert++;
        }

       

      
    }

    void UpdateMesh()
    {
        mesh.Clear();

        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;
        mesh.RecalculateNormals();


       


    }


}

this way the shader will scroll with the terrain as its generated, just have to figure out how to optimize it.

1 Like

Hey man,

I know this is old, i just wanted to say thank you for going back and answering it yourself! I used a long time on trying to figure out why my shader didnt work. This worked smoothly!