103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class dasdas : MonoBehaviour
|
|
{
|
|
Mesh myMesh;
|
|
MeshFilter meshFilter;
|
|
MeshCollider meshCollider;
|
|
[SerializeField] Vector2 planeSize = new Vector2(1, 1);
|
|
[SerializeField] int planeResolution = 1;
|
|
List<Vector3> vertices = new List<Vector3>();
|
|
List<int> triangles = new List<int>();
|
|
// Start is called before the first frame update
|
|
public int a = 0;
|
|
void Awake(){
|
|
meshFilter = GetComponent<MeshFilter>();
|
|
myMesh = meshFilter.mesh;
|
|
meshCollider = GetComponent<MeshCollider>();
|
|
}
|
|
void Start()
|
|
{
|
|
//log the number of vertices
|
|
Debug.Log(myMesh.vertices.Length);
|
|
GeneratePlane(planeSize, planeResolution);
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
planeResolution = Mathf.Clamp(planeResolution, 1, 50);
|
|
//GeneratePlane(planeSize, planeResolution);
|
|
Debug.Log(vertices.Count);
|
|
stretchToRight();
|
|
AssignMesh();
|
|
a++;
|
|
}
|
|
|
|
void GeneratePlane(Vector2 size, int resolution){
|
|
var vertices_old = myMesh.vertices;
|
|
vertices = new List<Vector3>();
|
|
float xPerStep = size.x / resolution;
|
|
float yPerStep = size.y / resolution;
|
|
for(int i = 0; i < resolution+1; i++){
|
|
for(int j = 0; j < resolution+1; j++){
|
|
vertices.Add(new Vector3(j * xPerStep , 0, i * yPerStep));
|
|
}
|
|
}
|
|
triangles = new List<int>();
|
|
for(int i = 0; i < resolution - 1; i++){
|
|
for(int j = 0; j < resolution - 1; j++){
|
|
var p = i*resolution + j + i;
|
|
triangles.Add(p);
|
|
triangles.Add(p+ resolution + 1);
|
|
triangles.Add(p+ resolution + 2);
|
|
triangles.Add(p);
|
|
triangles.Add(p+ resolution + 2);
|
|
triangles.Add(p+ 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void AssignMesh(){
|
|
myMesh.Clear();
|
|
myMesh.vertices = vertices.ToArray();
|
|
myMesh.triangles = triangles.ToArray();
|
|
meshFilter.mesh = myMesh;
|
|
meshCollider.sharedMesh = myMesh;
|
|
myMesh.RecalculateNormals();
|
|
|
|
}
|
|
int dist=0;
|
|
void stretchToRight(){
|
|
for(int i = 0; i < vertices.Count; i++){
|
|
vertices[i] = new Vector3(vertices[i].x, vertices[i].y, vertices[i].z + (i/planeResolution)*1f);
|
|
}
|
|
dist+=1;
|
|
triangles = new List<int>();
|
|
for(int i = 0; i < planeResolution - 1; i++){
|
|
for(int j = 0; j < planeResolution - 1; j++){
|
|
var p = i*(planeResolution+dist) + j + i;
|
|
//do not let the triangles go out of the plane
|
|
if(p+ planeResolution+dist + 2 > vertices.Count){
|
|
//Debug.Log("out of range");
|
|
/// Debug.Log(vertices.Count);
|
|
break;
|
|
}
|
|
triangles.Add(p);
|
|
triangles.Add(p+ planeResolution+dist + 1);
|
|
triangles.Add(p+ planeResolution+dist + 2);
|
|
triangles.Add(p);
|
|
triangles.Add(p+ planeResolution+dist + 2);
|
|
triangles.Add(p+ 1);
|
|
Debug.Log(p);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|