using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Exam0915SphereTool : MonoBehaviour
{
public int num = 20;
public float r = 5;
// Start is called before the first frame update
void Start()
{
VertexHelper vh = new VertexHelper();
Mesh mesh = new Mesh();
float angle = 2 * Mathf.PI / num;
for (int i = 0; i < num/2+1; i++)
{
float y = Mathf.Cos(i * angle) * r;
float xr = Mathf.Sin(i * angle) * r;
float uvy = (float)i / (num / 2);
for (int j = 0; j < num; j++)
{
float x = Mathf.Sin(j * angle) * xr;
float z = Mathf.Cos(j * angle) * xr;
float uvx = (float)j / num;
vh.AddVert(new Vector3(x, y, z), Color.white, new Vector2(uvx, uvy));
if (j==num-1)
{
float x0 = Mathf.Sin(0) * xr;
float z0 = Mathf.Cos(0) * xr;
vh.AddVert(new Vector3(x0, y, z0), Color.white, new Vector2(1, uvy));
}
if (i<num/2)
{
vh.AddTriangle(i * (num + 1) + j, (i + 1) * (num + 1) + j, i* (num + 1) + j + 1);
vh.AddTriangle(i * (num + 1) + j+1, (i + 1) * (num + 1) + j, (i + 1) * (num + 1) + j + 1);
}
}
}
vh.FillMesh(mesh);
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<MeshCollider>().sharedMesh = mesh;
}
// Update is called once per frame
void Update()
{
}
}
柏林地图UV Plane
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Exam0915PlaneTool : MonoBehaviour
{
public int num = 20;
// Start is called before the first frame update
void Start()
{
VertexHelper vh = new VertexHelper();
Mesh mesh = new Mesh();
for (int x = 0; x < num; x++)
{
for (int z = 0; z < num; z++)
{
float y = Mathf.PerlinNoise(x * 0.02f, z * 0.02f);
Vector2 pos = new Vector2(((float)x / (num - 1)), ((float)z / (num - 1)));
vh.AddVert(new Vector3(x, y*5, z), Color.white, pos);
if (x<num-1&&z<num-1)
{
vh.AddTriangle(x * num + z, x * num + z + 1, (x + 1) * num + z + 1);
vh.AddTriangle(x * num + z, (x + 1) * num + z + 1, (x + 1) * num + z);
}
}
}
vh.FillMesh(mesh);
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<MeshCollider>().sharedMesh = mesh;
}
// Update is called once per frame
void Update()
{
}
}