Advertisement
MrKubic

Untitled

Apr 1st, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Plate : MonoBehaviour {
  6.     Mesh mesh;
  7.     Vector3[] verticles;
  8.  
  9.     public float size_x = 0.05f;
  10.     public float size_y = 0.05f;
  11.     public float height_metall = 0.005f;
  12.     public float width_incision = 10.0f;
  13.  
  14.     public int count = 4;
  15.  
  16.     //ФИЗИКА
  17.     float[,] h;
  18.     float[,]Q;
  19.  
  20.     // Use this for initialization
  21.     void Start () {
  22.         mesh = GetComponent<MeshFilter>().mesh;
  23.         h = new float[count,count];
  24.         Q = new float[count, count];
  25.  
  26.  
  27.         //region Отрисовка пластины сварного соединения
  28.         //создаем вершины
  29.         verticles = new Vector3[count*count];
  30.         int n = 0;
  31.         for (int i = 0; i < count; i++) {
  32.             for (int j = 0; j < count; j++) {
  33.                 verticles [n] = new Vector3 (i*size_x/(count-1),0,j*size_y/(count-1));
  34.                 n++;
  35.             }
  36.         }
  37.         //создаем полигоны (треугольники)
  38.         int[] tri = new int[(count-1)*(count-1)*6];
  39.         n = 0;
  40.         for (int i = 0; i < count-2; i++) {
  41.             for (int j = 0; j < count-2; j++) {
  42.                 tri[n] = (i*count)+j;
  43.                 tri[n+1] = (i*count)+j+1;
  44.                 tri[n+2] = (i*count)+j+count+1;
  45.                 tri[n+3] = (i*count)+j;
  46.                 tri[n+4] = (i*count)+j+count+1;
  47.                 tri[n+5] = (i*count)+j+count;
  48.                 n+=6;
  49.             }
  50.         }
  51.         //Устанавливаем нормали
  52.         Vector3[] normals = new Vector3[count*count];
  53.         for (int i = 0; i < count; i++) {
  54.             normals [i] = -Vector3.up;
  55.         }
  56.         //endregion
  57.  
  58.         //Делаем разрез стыка металлов
  59.             for (int i = (count - 1) / 2 * count; i < (count - 1) / 2 * count + count - 1; i++)
  60.             {
  61.                 verticles[i] -= new Vector3(0, width_incision * height_metall, 0);
  62.             }
  63.  
  64.        
  65.         mesh.vertices = verticles;
  66.         mesh.triangles = tri;
  67.         mesh.normals = normals;
  68.         mesh.RecalculateNormals();
  69.         mesh.RecalculateBounds();
  70.  
  71.         //Добавляем коллайдер для пластины
  72.         transform.gameObject.AddComponent<MeshCollider>(); //Добавим компонент MeshCollider
  73.         transform.GetComponent<MeshCollider> ().convex = true; //Установим convex в истину
  74.         transform.GetComponent<MeshCollider> ().sharedMesh = mesh; //Применим на mesh наш сгенерированный меш.
  75.     }
  76.    
  77.     // Update is called once per frame
  78.     void Update () {
  79.  
  80.         mesh.vertices = verticles;
  81.         mesh.RecalculateNormals();
  82.         mesh.RecalculateBounds();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement