Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
- public class Gridd : MonoBehaviour
- {
- public Mesh mesh;
- public float axisCounter;
- public Vector3[] vertices;
- public int xSize, ySize;
- point nokta;
- int sayac = 0;
- private void Awake ()
- {
- mesh = new Mesh();
- Generate();
- }
- private void Generate ()
- {
- _generate();
- }
- private void OnDrawGizmos ()
- {
- draw();
- }
- void Start()
- {
- nokta = new point(1,1,xSize,ySize,vertices);
- }
- void Update()
- {
- createMesh();
- StartCoroutine(destroy());
- }
- IEnumerator destroy()
- {
- float zAxis = 0;
- while(true)
- {
- while(zAxis>=-axisCounter)
- {
- if(zAxis<=-axisCounter)
- {
- break;
- }
- zAxis-=0.01f;
- nokta.func(xSize/2,ySize/2,zAxis);
- yield return new WaitForSeconds(0.01f);
- }
- while(zAxis<=axisCounter)
- {
- if(zAxis>=axisCounter)
- {
- break;
- }
- zAxis+=0.01f;
- nokta.func(xSize/2,ySize/2,zAxis);
- yield return new WaitForSeconds(0.01f);
- }
- }
- }
- /*************************************************************************/
- void _generate()
- {
- mesh = new Mesh();
- GetComponent<MeshFilter>().mesh = mesh;
- vertices = new Vector3[(xSize + 1) * (ySize + 1)];
- for (int y = 0; y <= ySize; y++)
- {
- for (int x = 0; x <= xSize; x++)
- {
- vertices[sayac] = new Vector3(x, y);
- sayac++;
- }
- }
- sayac=0;
- }
- void createMesh()
- {
- mesh.vertices = vertices;
- int[] triangles = new int[xSize*xSize*6];
- for(int y=0;y<ySize;y++)
- {
- for(int x=0;x<xSize;x++)
- {
- triangles[sayac] = (xSize+1)*y+x;
- sayac++;
- triangles[sayac] = (xSize+1)*(y+1)+x;
- sayac++;
- triangles[sayac] = (xSize+1)*y+x+1;
- sayac++;
- triangles[sayac] = (xSize+1)*y+x+1;
- sayac++;
- triangles[sayac] = (xSize+1)*(y+1)+x;
- sayac++;
- triangles[sayac] = (xSize+1)*(y+1)+x+1;
- sayac++;
- }
- }
- sayac=0;
- mesh.triangles = triangles;
- }
- void draw()
- {
- if (vertices == null)
- {
- return;
- }
- Gizmos.color = Color.black;
- for (int i = 0; i < vertices.Length; i++)
- {
- Gizmos.DrawSphere(vertices[i], 0.01f);
- }
- }
- }
- /*****************************************************************************/
- public class point
- {
- public int x;
- public int y;
- public int xSize,ySize;
- public int[,] _nokta;
- public int[,] dalga;
- int atlayici;
- int kolaynokta;
- int kolaynoktax,kolaynoktay;
- public Vector3[] vertices;
- bool buldumu = false;
- public point(int x,int y,int xSize,int ySize,Vector3[] vertices)
- {
- this.x = x;
- this.y = atlayici*y;
- this.xSize = xSize;
- this.ySize = ySize;
- this.vertices = vertices;
- this.atlayici = xSize+1;
- this._nokta = new int[xSize,ySize];
- this.kolaynokta = (xSize+1)*y+x;
- this.dalga = new int[ySize/2+1,xSize*ySize];
- }
- public void func(int x,int y,float z)
- {
- vertices[pointFinder(x,y)] = new Vector3(x,y,-z/2f);
- subWaves(x,y,z);
- }
- public void subWaves(int x,int y,float z)
- {
- if(!buldumu)
- {
- find(x,y,z);
- }
- int counter = 8;
- for(int a=0;a<ySize/2;a++)
- {
- for(int b=0;b<counter;b++)
- {
- vertices[dalga[a,b]] = new Vector3(vertices[dalga[a,b]].x,vertices[dalga[a,b]].y,_math(a,z));
- }
- counter+=8;
- }
- }
- /*************************************************************************/
- void find(int x,int y,float z)
- {
- int katman = 2;
- int wavex = x;
- int wavey = y;
- int _x=0;
- for(int a=0;a<ySize/2;a++)
- {
- wavex--;
- wavey++;
- int xarray=0;
- int yarray=0;
- for(int c=0;c<katman;c++)
- {
- dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
- xarray++;
- _x++;
- }
- for(int c=0;c<katman;c++)
- {
- dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
- yarray--;
- _x++;
- }
- for(int c=0;c<katman;c++)
- {
- dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
- xarray--;
- _x++;
- }
- for(int c=0;c<katman;c++)
- {
- dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
- yarray++;
- _x++;
- }
- katman+=2;
- _x=0;
- }
- buldumu = true;
- }
- int pointFinder(int x,int y)
- {
- int nokta = (xSize+1)*y+x;
- return nokta;
- }
- float _math(int a,float z)
- {
- z*=-1;
- float deger;
- deger=z/(1.25f+(a+1));
- return deger;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement