| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using UnityEngine;
- public class PlaneGenerator : MonoBehaviour
- {
- private const int pqsResolution = 16;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- var vertices = new Vector3[(pqsResolution + 1) * (pqsResolution + 1)];
- Vector2[] uv = new Vector2[(pqsResolution + 1) * (pqsResolution + 1)];
- var triangles = new int[(pqsResolution) * (pqsResolution) * 6];
- for (int x = 0, vertex = 0; x < pqsResolution + 1; x++)
- {
- for (int z = 0; z < pqsResolution + 1; z++, vertex++)
- {
- vertices[vertex] = new Vector3(x, 0, z);
- uv[vertex] = new Vector2((float)x / pqsResolution, (float)z / pqsResolution);
- }
- }
- int triangle = 0;
- for (int x = 1, vertex = 0; x < pqsResolution + 1; x++, vertex++)
- {
- for (int z = 1; z < pqsResolution + 1; z++, vertex++)
- {
- triangles[triangle] = vertex;
- triangles[triangle + 1] = vertex + pqsResolution + 2;
- triangles[triangle + 2] = vertex + pqsResolution + 1;
- triangles[triangle + 3] = vertex;
- triangles[triangle + 4] = vertex + 1;
- triangles[triangle + 5] = vertex + pqsResolution + 2;
- triangle += 6;
- }
- }
- var mesh = GetComponent<MeshFilter>();
- mesh.mesh.vertices = vertices;
- mesh.mesh.triangles = triangles;
- mesh.mesh.uv = uv;
- GetComponent<MeshCollider>().sharedMesh = mesh.mesh;
- mesh.mesh.RecalculateBounds();
- mesh.mesh.RecalculateNormals();
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- }
|