Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
  6.  
  7. public class Gridd : MonoBehaviour
  8. {
  9. public Mesh mesh;
  10. public float axisCounter;
  11. public Vector3[] vertices;
  12. public int xSize, ySize;
  13. point nokta;
  14.  
  15. int sayac = 0;
  16.  
  17. private void Awake ()
  18. {
  19. mesh = new Mesh();
  20. Generate();
  21. }
  22.  
  23. private void Generate ()
  24. {
  25. _generate();
  26. }
  27.  
  28. private void OnDrawGizmos ()
  29. {
  30. draw();
  31. }
  32.  
  33. void Start()
  34. {
  35. nokta = new point(1,1,xSize,ySize,vertices);
  36. }
  37.  
  38. void Update()
  39. {
  40. createMesh();
  41. StartCoroutine(destroy());
  42. }
  43.  
  44. IEnumerator destroy()
  45. {
  46. float zAxis = 0;
  47. while(true)
  48. {
  49. while(zAxis>=-axisCounter)
  50. {
  51. if(zAxis<=-axisCounter)
  52. {
  53. break;
  54. }
  55. zAxis-=0.01f;
  56. nokta.func(xSize/2,ySize/2,zAxis);
  57.  
  58. yield return new WaitForSeconds(0.01f);
  59. }
  60.  
  61. while(zAxis<=axisCounter)
  62. {
  63. if(zAxis>=axisCounter)
  64. {
  65. break;
  66. }
  67. zAxis+=0.01f;
  68. nokta.func(xSize/2,ySize/2,zAxis);
  69.  
  70. yield return new WaitForSeconds(0.01f);
  71. }
  72. }
  73. }
  74.  
  75. /*************************************************************************/
  76.  
  77. void _generate()
  78. {
  79. mesh = new Mesh();
  80. GetComponent<MeshFilter>().mesh = mesh;
  81. vertices = new Vector3[(xSize + 1) * (ySize + 1)];
  82. for (int y = 0; y <= ySize; y++)
  83. {
  84. for (int x = 0; x <= xSize; x++)
  85. {
  86. vertices[sayac] = new Vector3(x, y);
  87. sayac++;
  88. }
  89. }
  90. sayac=0;
  91. }
  92.  
  93. void createMesh()
  94. {
  95. mesh.vertices = vertices;
  96. int[] triangles = new int[xSize*xSize*6];
  97. for(int y=0;y<ySize;y++)
  98. {
  99. for(int x=0;x<xSize;x++)
  100. {
  101. triangles[sayac] = (xSize+1)*y+x;
  102. sayac++;
  103. triangles[sayac] = (xSize+1)*(y+1)+x;
  104. sayac++;
  105. triangles[sayac] = (xSize+1)*y+x+1;
  106. sayac++;
  107. triangles[sayac] = (xSize+1)*y+x+1;
  108. sayac++;
  109. triangles[sayac] = (xSize+1)*(y+1)+x;
  110. sayac++;
  111. triangles[sayac] = (xSize+1)*(y+1)+x+1;
  112. sayac++;
  113. }
  114. }
  115. sayac=0;
  116. mesh.triangles = triangles;
  117. }
  118.  
  119. void draw()
  120. {
  121. if (vertices == null)
  122. {
  123. return;
  124. }
  125. Gizmos.color = Color.black;
  126. for (int i = 0; i < vertices.Length; i++)
  127. {
  128. Gizmos.DrawSphere(vertices[i], 0.01f);
  129. }
  130. }
  131. }
  132.  
  133. /*****************************************************************************/
  134.  
  135. public class point
  136. {
  137. public int x;
  138. public int y;
  139. public int xSize,ySize;
  140. public int[,] _nokta;
  141. public int[,] dalga;
  142. int atlayici;
  143. int kolaynokta;
  144. int kolaynoktax,kolaynoktay;
  145. public Vector3[] vertices;
  146. bool buldumu = false;
  147.  
  148. public point(int x,int y,int xSize,int ySize,Vector3[] vertices)
  149. {
  150. this.x = x;
  151. this.y = atlayici*y;
  152. this.xSize = xSize;
  153. this.ySize = ySize;
  154. this.vertices = vertices;
  155. this.atlayici = xSize+1;
  156. this._nokta = new int[xSize,ySize];
  157. this.kolaynokta = (xSize+1)*y+x;
  158. this.dalga = new int[ySize/2+1,xSize*ySize];
  159. }
  160.  
  161. public void func(int x,int y,float z)
  162. {
  163. vertices[pointFinder(x,y)] = new Vector3(x,y,-z/2f);
  164. subWaves(x,y,z);
  165. }
  166.  
  167. public void subWaves(int x,int y,float z)
  168. {
  169. if(!buldumu)
  170. {
  171. find(x,y,z);
  172. }
  173. int counter = 8;
  174. for(int a=0;a<ySize/2;a++)
  175. {
  176. for(int b=0;b<counter;b++)
  177. {
  178. vertices[dalga[a,b]] = new Vector3(vertices[dalga[a,b]].x,vertices[dalga[a,b]].y,_math(a,z));
  179. }
  180. counter+=8;
  181. }
  182. }
  183.  
  184. /*************************************************************************/
  185.  
  186. void find(int x,int y,float z)
  187. {
  188. int katman = 2;
  189. int wavex = x;
  190. int wavey = y;
  191. int _x=0;
  192. for(int a=0;a<ySize/2;a++)
  193. {
  194. wavex--;
  195. wavey++;
  196. int xarray=0;
  197. int yarray=0;
  198. for(int c=0;c<katman;c++)
  199. {
  200. dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
  201. xarray++;
  202. _x++;
  203. }
  204. for(int c=0;c<katman;c++)
  205. {
  206. dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
  207. yarray--;
  208. _x++;
  209. }
  210. for(int c=0;c<katman;c++)
  211. {
  212. dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
  213. xarray--;
  214. _x++;
  215. }
  216. for(int c=0;c<katman;c++)
  217. {
  218. dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
  219. yarray++;
  220. _x++;
  221. }
  222. katman+=2;
  223. _x=0;
  224. }
  225. buldumu = true;
  226. }
  227.  
  228. int pointFinder(int x,int y)
  229. {
  230. int nokta = (xSize+1)*y+x;
  231. return nokta;
  232. }
  233.  
  234. float _math(int a,float z)
  235. {
  236. z*=-1;
  237. float deger;
  238. deger=z/(1.25f+(a+1));
  239. return deger;
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement