2019-11-18 19:31:44 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(MeshFilter))]
|
2019-11-21 09:44:56 +00:00
|
|
|
|
|
2019-11-18 19:31:44 +00:00
|
|
|
|
public class MeshGen : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
Mesh mesh;
|
|
|
|
|
|
|
|
|
|
Vector3[] vertices;
|
|
|
|
|
int[] triangles;
|
|
|
|
|
|
2019-11-21 09:44:56 +00:00
|
|
|
|
public float xSize = 1;
|
|
|
|
|
public float zSize = 1;
|
|
|
|
|
public int xRes = 100;
|
|
|
|
|
public int zRes = 100;
|
|
|
|
|
|
|
|
|
|
Vector2[] uvs;
|
|
|
|
|
Vector4[] tangents;
|
2019-11-18 19:31:44 +00:00
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
mesh = new Mesh();
|
|
|
|
|
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
|
|
|
|
|
CreateShape();
|
|
|
|
|
UpdateMesh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateShape()
|
|
|
|
|
{
|
2019-11-21 09:44:56 +00:00
|
|
|
|
//vertices being calculated
|
|
|
|
|
vertices = new Vector3[(xRes + 1) * (zRes + 1)];
|
|
|
|
|
uvs = new Vector2[vertices.Length];
|
|
|
|
|
tangents = new Vector4[vertices.Length];
|
2019-11-18 19:31:44 +00:00
|
|
|
|
|
2019-11-21 09:44:56 +00:00
|
|
|
|
for (int i = 0, z = 0; z <= zRes; z++)
|
2019-11-18 19:31:44 +00:00
|
|
|
|
{
|
2019-11-21 09:44:56 +00:00
|
|
|
|
for (int x = 0; x <= xRes; x++)
|
2019-11-18 19:31:44 +00:00
|
|
|
|
{
|
2019-11-21 09:44:56 +00:00
|
|
|
|
//convert int to float, then map range 0-1, then move by half size (origin at center), then scale by size
|
|
|
|
|
vertices[i] = new Vector3(((x * 1.0f / xRes - 0.5f) * xSize), 0,(z * 1.0f / zRes - 0.5f) * zSize);
|
|
|
|
|
//map uvs 0-1 depending onf Resolution of mesh
|
|
|
|
|
uvs[i] = new Vector2(x * 1.0f / xRes, z * 1.0f / zRes);
|
|
|
|
|
//set all tangents to plane
|
|
|
|
|
tangents[i] = new Vector4(1f, 0f, 0f, -1f);
|
2019-11-18 19:31:44 +00:00
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 09:44:56 +00:00
|
|
|
|
//triangles being calculated
|
|
|
|
|
triangles = new int[xRes * zRes * 6];
|
2019-11-18 19:31:44 +00:00
|
|
|
|
|
|
|
|
|
int vert = 0;
|
|
|
|
|
int tris = 0;
|
|
|
|
|
|
2019-11-21 09:44:56 +00:00
|
|
|
|
for (int z = 0; z < zRes; z++)
|
2019-11-18 19:31:44 +00:00
|
|
|
|
{
|
2019-11-21 09:44:56 +00:00
|
|
|
|
for (int x = 0; x < xRes; x++)
|
2019-11-18 19:31:44 +00:00
|
|
|
|
{
|
|
|
|
|
triangles[tris + 0] = vert + 0;
|
2019-11-21 09:44:56 +00:00
|
|
|
|
triangles[tris + 1] = vert + xRes + 1;
|
2019-11-18 19:31:44 +00:00
|
|
|
|
triangles[tris + 2] = vert + 1;
|
|
|
|
|
triangles[tris + 3] = vert + 1;
|
2019-11-21 09:44:56 +00:00
|
|
|
|
triangles[tris + 4] = vert + xRes + 1;
|
|
|
|
|
triangles[tris + 5] = vert + xRes + 2;
|
2019-11-18 19:31:44 +00:00
|
|
|
|
|
|
|
|
|
vert++;
|
|
|
|
|
tris += 6;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
vert++;
|
2019-11-21 09:44:56 +00:00
|
|
|
|
}
|
2019-11-18 19:31:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateMesh()
|
|
|
|
|
{
|
|
|
|
|
mesh.Clear();
|
|
|
|
|
|
|
|
|
|
mesh.vertices = vertices;
|
|
|
|
|
mesh.triangles = triangles;
|
|
|
|
|
|
2019-11-21 09:44:56 +00:00
|
|
|
|
mesh.uv = uvs;
|
|
|
|
|
mesh.tangents = tangents;
|
2019-11-18 19:31:44 +00:00
|
|
|
|
|
2019-11-21 09:44:56 +00:00
|
|
|
|
mesh.RecalculateNormals();
|
|
|
|
|
GetComponent<MeshFilter>().mesh = mesh;
|
2019-11-18 19:31:44 +00:00
|
|
|
|
|
|
|
|
|
}
|
2019-11-21 09:44:56 +00:00
|
|
|
|
/*
|
|
|
|
|
private void OnDrawGizmos()
|
|
|
|
|
{
|
|
|
|
|
if (vertices == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < vertices.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Gizmos.DrawSphere(vertices[i], .1f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2019-11-18 19:31:44 +00:00
|
|
|
|
|
|
|
|
|
}
|
2019-11-21 09:44:56 +00:00
|
|
|
|
|
|
|
|
|
|