Search Unity

Resolved shader graph on generated mesh

Discussion in 'Shader Graph' started by herr_mittens, Jun 28, 2019.

  1. herr_mittens

    herr_mittens

    Joined:
    Jun 7, 2018
    Posts:
    2
    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<MeshFilter>().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:
    https://imgur.com/a/wEKZaqN
     

    Attached Files:

  2. herr_mittens

    herr_mittens

    Joined:
    Jun 7, 2018
    Posts:
    2
    I found a solution. the problem I had was that I hadn't applied UV's.
    Here is the new code.




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(MeshFilter))]
    6. public class MeshGenerator : MonoBehaviour
    7. {
    8.     Mesh mesh;
    9.     public Transform cam;
    10.  
    11.     Vector3[] vertices;
    12.     int[] triangles;
    13.     Vector2[] uvs;
    14.     public int xSize = 1000;
    15.     public int zSize = 1000;
    16.     public int previousXPos = -999, previousZPos = -999;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         mesh = new Mesh();
    22.         GetComponent<MeshFilter>().mesh = mesh;
    23.  
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.  
    29.         int xPos = (int)Mathf.Floor(cam.position.x);
    30.         int zPos = (int)Mathf.Floor(cam.position.z);
    31.         if (xPos != previousXPos || zPos != previousZPos)
    32.         {
    33.             CreateShape((int)cam.position.z, (int)cam.position.x);
    34.             UpdateMesh();
    35.             //Debug.Log("update");
    36.             previousXPos = xPos;
    37.             previousZPos = zPos;
    38.         }
    39.  
    40.    
    41.  
    42.     }
    43.      
    44.        
    45.  
    46.    
    47.     void CreateShape(int zPos, int xPos)
    48.     {
    49.         vertices = new Vector3[(xSize + 1) * (zSize + 1)] ;
    50.         uvs = new Vector2[(xSize + 1) * (zSize + 1)];
    51.         int jump = 500;
    52.         for (int i = 0, z = zPos  - (zSize/2); z <= zPos  + (zSize/2); z += jump)
    53.         {
    54.  
    55.             for (int x = xPos  - (xSize/2); x <= xPos  + (xSize/2); x += jump )
    56.             {
    57.                 //float y = Mathf.Sin((x * 0.3f) + (z * 0.3f)) * 2f;
    58.                 vertices[i] = new Vector3(x, 0, z);
    59.  
    60.                 uvs[i] = new Vector2(((float)(x + (xSize / 2)) / xSize), ((float)(z + (zSize / 2)) / zSize));
    61.  
    62.                 i++;
    63.             }
    64.         }
    65.        
    66.  
    67.        
    68.  
    69.         triangles = new int[(xSize * zSize * 6)/jump];
    70.  
    71.         int vert = 0;
    72.         int tris = 0;
    73.  
    74.         for (int z = 0; z < zSize; z+= jump)
    75.         {
    76.             for (int x = 0; x < xSize; x+= jump)
    77.             {
    78.                 triangles[tris + 0] = vert + 0;
    79.                 triangles[tris + 1] = vert + (xSize/jump) + 1;
    80.                 triangles[tris + 2] = vert + 1;
    81.                 triangles[tris + 3] = vert + 1;
    82.                 triangles[tris + 4] = vert + (xSize / jump) + 1;
    83.                 triangles[tris + 5] = vert + (xSize / jump) + 2;
    84.  
    85.                 vert++;
    86.                 tris += 6;
    87.  
    88.             }
    89.             vert++;
    90.         }
    91.  
    92.        
    93.  
    94.      
    95.     }
    96.  
    97.     void UpdateMesh()
    98.     {
    99.         mesh.Clear();
    100.  
    101.         mesh.vertices = vertices;
    102.         mesh.triangles = triangles;
    103.         mesh.uv = uvs;
    104.         mesh.RecalculateNormals();
    105.  
    106.  
    107.        
    108.  
    109.  
    110.     }
    111.  
    112.  
    113. }
    114.  
    this way the shader will scroll with the terrain as its generated, just have to figure out how to optimize it.
     
    DREBOTgamestudio likes this.
  3. Bicsi

    Bicsi

    Joined:
    Apr 14, 2017
    Posts:
    6
    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!