1. Grid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
[RequireComponent (typeof(MeshFilter), typeof(MeshRenderer))]
public class ProceduralGrid : MonoBehaviour
{
    Mesh mesh;
    Vector3[] vertices;
    int[] triangles;
 
    public float cellSize = 1.0f;
    public Vector3 gridOffset;
    public int gridSize;
 
    private void Awake()
    {
        //get mesh
        mesh = GetComponent<MeshFilter>().mesh;
    }
 
    void Start()
    {
        ContinousGridMesh();
        UpdateMesh();
    }
 
    void DiscreteGridMesh()
    {
        vertices = new Vector3[4*gridSize*gridSize];
        triangles = new int[6*gridSize*gridSize];
 
        float vertexOffset = cellSize * 0.5f;
 
        int v = 0;
        int t = 0;
 
        //create vertex grid and triangle 
        //cell을 하나하나 씩 그려준다고 생각하면 된다. 
        for(int x=0; x<gridSize; x++)
        {
            for(int y=0; y<gridSize; y++)
            {
                Vector3 cellOffset = new Vector3(cellSize * x, 0, cellSize * y);
 
                vertices[v] = new Vector3(-vertexOffset, x+y, -vertexOffset) + cellOffset + gridOffset;
                vertices[v+1= new Vector3(-vertexOffset, x+y, vertexOffset) + cellOffset + gridOffset;
                vertices[v+2= new Vector3(vertexOffset, x+y, -vertexOffset) + cellOffset + gridOffset;
                vertices[v+3= new Vector3(vertexOffset, x+y, vertexOffset) + cellOffset + gridOffset;
 
                triangles[t] = v;
                triangles[t + 1= triangles[t+4= v + 1;
                triangles[t + 2= triangles[t+3= v + 2;
                triangles[t + 5= v + 3;
 
                v += 4;
                t += 6;
            }
        }
    }
 
    void ContinousGridMesh()
    {
        vertices = new Vector3[(gridSize + 1* (gridSize + 1)];
        triangles = new int[6 * gridSize * gridSize];
 
        float vertexOffset = cellSize * 0.5f;
 
        int v = 0;
        int t = 0;
 
        //create vertex grid
        //연결된 vertices를 구하기 때문에 한 번에 구함 
        for (int x = 0; x <= gridSize; x++)
        {
            for (int y = 0; y <= gridSize; y++)
            {
                vertices[v] = new Vector3(((x * cellSize) - vertexOffset), (x+y)*0.2f, ((y*cellSize)-vertexOffset));
                v++;
            }
        }
 
        //reset v
        v = 0;
 
        //setting each cell's triangle
        //vertices의 순서가 전체적으로 순차적으로 진행되기 때문에 
        //cell의 윗 부분은 gridSize+1을 더해준 순서로 구해진.  
        for(int x=0; x<gridSize; x++)
        {
            for(int y=0; y< gridSize; y++)
            {
                triangles[t] = v;
                triangles[t + 1= triangles[t + 4= v + 1;
                triangles[t + 2= triangles[t + 3= v + (gridSize+1);
                triangles[t + 5= v + (gridSize+1)+1;
 
                v++;
                t += 6;
            }
            v++;
        }
    }
 
    void UpdateMesh()
    {
        mesh.Clear();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }
 
 
}
 
cs

2. soft Body

https://www.youtube.com/watch?v=TNAKv1dkYyQ


'Project > TouchMe' 카테고리의 다른 글

10/8 joint 안정화 작업  (0) 2019.10.09
10/5 procedural Mesh  (0) 2019.10.05
04. 10/3 softBody effect  (0) 2019.10.04
03. 10/2 physics & leapmotion SDK  (0) 2019.10.02
02. 기술 기획  (0) 2019.10.02
Posted by 도이(doi)
,