Search Unity

Question Probuilder perlin noise

Discussion in 'World Building' started by stychu, Jan 25, 2021.

  1. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
    Hey there,

    I'm trying to play with Probuilder and Perlin noise to generate some terrain. I have a problem with the textures after I displace the Y coordinate for vertices with Perlin noise. Is this correct behaviour that textures are getting so off or I'm doing something wrong??

    @kaarrrllll Would you be able to advise? Unity_XqAuv9JmtL.png

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine.ProBuilder;
    6. using UnityEngine.ProBuilder.MeshOperations;
    7.  
    8. class test : MonoBehaviour {
    9.   ProBuilderMesh  m_Mesh;
    10.   Face            m_LastExtrudedFace = null;
    11.   public Material material;
    12.  
    13.   public int size       = 2;
    14.   public int widthCuts  = 0;
    15.   public int heightCuts = 0;
    16.  
    17.   public int   perlinScale  = 2;
    18.   public int   perlinOffset = 2;
    19.   public float offsetX      = 100f;
    20.   public float offsetY      = 100f;
    21.   void         Start() { BuildMesh(); }
    22.  
    23.   private void OnGUI() {
    24.     if (GUI.Button(new Rect(10, 70, 50, 30), "Click")) {
    25.       BuildMesh();
    26.       Debug.Log("VAR");
    27.     }
    28.   }
    29.  
    30.   void BuildMesh() {
    31.     if (m_Mesh != null)
    32.       Destroy(m_Mesh.gameObject);
    33.  
    34.     List<ProBuilderMesh> meshes = new List<ProBuilderMesh>();
    35.  
    36.     for (int i = 0; i < size; i++) {
    37.       for (int j = 0; j < size; j++) {
    38.         ProBuilderMesh plane = ShapeGenerator.GeneratePlane(PivotLocation.FirstVertex, 1, 1, widthCuts, heightCuts, Axis.Up);
    39.         plane.transform.position = new Vector3(i, 0, j);
    40.         meshes.Add(plane);
    41.       }
    42.     }
    43.  
    44.     m_Mesh = CombineMeshes.Combine(meshes, meshes.First()).First();
    45.     meshes.Skip(1).ToList().ForEach(mesh => GameObject.Destroy(mesh.gameObject));
    46.  
    47.     m_Mesh.GetComponent<MeshRenderer>().sharedMaterial = material;
    48.  
    49.     ExtrudeEdge();
    50.   }
    51.  
    52.   void ExtrudeEdge() {
    53.     // Move vertex positions up
    54.     Vertex[] vertices = m_Mesh.GetVertices();
    55.    
    56.     for (int i = 0; i < m_Mesh.vertexCount; i++) {
    57.       Vector3 vertex = vertices[i].position;
    58.  
    59.       float xCoord = (vertex.x / m_Mesh.vertexCount) * perlinScale + offsetX;
    60.       float yCoord = (vertex.z / m_Mesh.vertexCount) * perlinScale + offsetY;
    61.  
    62.       float y = Mathf.PerlinNoise(xCoord, yCoord) * perlinOffset;
    63.  
    64.       Debug.Log(y);
    65.       vertices[i].position += new Vector3(0, y, 0);
    66.     }
    67.  
    68.     m_Mesh.SetVertices(vertices);
    69.     m_Mesh.faces.ToList().ForEach(face => face.uv = AutoUnwrapSettings.stretch);
    70.     m_Mesh.ToMesh();
    71.     m_Mesh.Refresh();
    72.   }
    73. }
    74.  
     
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    Try setting each face to use the same texture group. Something like this:

    Code (CSharp):
    1. int group = m_Mesh.GetUnusedTextureGroup();
    2. m_Mesh.faces.ForEach(x => x.textureGroup = group);
    3. m_Mesh.Refresh(RefreshMask.UV);
    To clarify what is happening, texture groups tell ProBuilder to project the UVs for these faces at the same time. It is equivalent to selecting all the faces in your plane and in the UV Editor selecting Planar Project.
     
  3. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
    Awesome that worked!! Thank you
     
    kaarrrllll likes this.