69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Generate XZ plane
|
|
/// </summary>
|
|
public class PlaneGenerator : MeshProcessor
|
|
{
|
|
public float Width = 1;
|
|
public float Height = 1;
|
|
|
|
public int WidthSlices = 0;
|
|
public int HeightSlices = 0;
|
|
|
|
|
|
public override void Execute(ProceduralMeshData m)
|
|
{
|
|
int widthVertexCount = 2 + WidthSlices;
|
|
int heightVertexCount = 2 + HeightSlices;
|
|
int vertexCount = widthVertexCount*heightVertexCount;
|
|
|
|
//generate vertices && normals
|
|
Vector3 offset = new Vector3(-Width / 2, 0, Height / 2);
|
|
m.Vertices = new Vector3[vertexCount];
|
|
m.Normals = new Vector3[vertexCount];
|
|
|
|
for (int x = 0; x < widthVertexCount; ++x)
|
|
{
|
|
for (int z = 0; z < heightVertexCount; ++z)
|
|
{
|
|
int id = z*widthVertexCount + x;
|
|
m.Vertices[id]= new Vector3((Width / (widthVertexCount - 1)) * x, 0, (-Height / (heightVertexCount - 1)) * z) + offset;
|
|
m.Normals[id] = Vector3.up;
|
|
}
|
|
}
|
|
|
|
|
|
//generate indices
|
|
int[] indices = new int[(widthVertexCount - 1) * (heightVertexCount - 1) * 6];
|
|
int indexOffset = 0;
|
|
for (int x = 0; x < widthVertexCount - 1; ++x){
|
|
for (int z = 0; z < heightVertexCount - 1; ++z) {
|
|
indices[indexOffset + 0] = z * widthVertexCount + x;
|
|
indices[indexOffset + 1] = z * widthVertexCount + x + 1;
|
|
indices[indexOffset + 2] = (z + 1) * widthVertexCount + x;
|
|
indices[indexOffset + 3] = indices[indexOffset + 2];
|
|
indices[indexOffset + 4] = indices[indexOffset + 1];
|
|
indices[indexOffset + 5] = (z + 1) * widthVertexCount + x + 1;
|
|
indexOffset += 6;
|
|
}
|
|
}
|
|
|
|
m.Indices.Add(indices);
|
|
|
|
//generate uvs
|
|
List<Vector4> uvs = new List<Vector4>(vertexCount);
|
|
for (int z = 0; z < heightVertexCount; ++z)
|
|
{
|
|
for (int x = 0; x < widthVertexCount; ++x)
|
|
{
|
|
|
|
uvs.Add(new Vector4((float)x / (widthVertexCount - 1), (float)z / (heightVertexCount - 1), 0, 0));
|
|
}
|
|
}
|
|
m.Uvs.Add(uvs);
|
|
}
|
|
}
|