Hi,
I’ve created a script that creates a mesh plane based on a specified size that is dictated in runtime. This is a path/road generator. My problem is that I can’t seem to find a way to get each of the vertices to align on the Y-Axis to the terrain height. I’ve tried with terrain.SampleHeight but it doesn’t work.
I’ve tried pretty much everything I can think of. I just need the Mesh to curve around the terrain and lie flat against it. Can anyone help?
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class HeightMapping : MonoBehaviour
{
Mesh mesh;
public Vector3[] vertices;
int[] triangles;
public int xSize = 2;
public int zSize = 1;
public Terrain terrain;
private Vector3 startPos;
private Vector3 endPos;
private Vector2[] newUV;
// Start is called before the first frame update
void Start()
{
terrain = Terrain.activeTerrain;
startPos = GameObject.Find("XXX").GetComponent<PathGenerator>().start.transform.position;
endPos = GameObject.Find("XXX").GetComponent<PathGenerator>().end.transform.position;
Vector3 startPosEdit = new Vector3(startPos.x, startPos.y + 0.01f, startPos.z);
Vector3 endPosEdit = new Vector3(endPos.x, endPos.y +0.01f, endPos.z);
transform.position = startPosEdit;
transform.LookAt(endPosEdit);
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
float distancePre = GameObject.Find("XXX").GetComponent<PathGenerator>().distance;
zSize = Mathf.RoundToInt(distancePre);
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
vertices[i] = new Vector3(x, 0, z);
i++;
}
}
triangles = new int[xSize * zSize * 6];
int vert = 0;
int tris = 0;
for (int z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + xSize + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + xSize + 1;
triangles[tris + 5] = vert + xSize + 2;
vert++;
tris += 6;
}
vert++;
}
newUV = new Vector2 [vertices.Length];
float scaleFactor = 0.1f;
for (int i = 0; i < triangles.Length; i += 3)
{
// Get the three vertices bounding this triangle.
Vector3 v1 = vertices[triangles[i]];
Vector3 v2 = vertices[triangles[i + 1]];
Vector3 v3 = vertices[triangles[i + 2]];
// Compute a vector perpendicular to the face.
Vector3 normal = Vector3.Cross(v3 - v1, v2 - v1);
// Form a rotation that points the z+ axis in this perpendicular direction.
// Multiplying by the inverse will flatten the triangle into an xy plane.
Quaternion rotation = Quaternion.Inverse(Quaternion.LookRotation(normal));
// Assign the uvs, applying a scale factor to control the texture tiling.
newUV[triangles[i]] = (Vector2)(rotation * v1) * scaleFactor;
newUV[triangles[i + 1]] = (Vector2)(rotation * v2) * scaleFactor;
newUV[triangles[i + 2]] = (Vector2)(rotation * v3) * scaleFactor;
}
UpdateMesh();
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = newUV;
mesh.RecalculateNormals();
this.GetComponent<Renderer>().material = Resources.Load("Materials/Shared/DirtRoad", typeof(Material)) as Material;
}
}