'Project'에 해당되는 글 27건

  1. 2019.10.09 10/8 joint 안정화 작업
  2. 2019.10.08 10/6 procedural grid, physics and soft body(nvidia flux)
  3. 2019.10.05 10/5 procedural Mesh
  4. 2019.10.04 04. 10/3 softBody effect

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

10/6 procedural grid, physics and soft body(nvidia flux)  (0) 2019.10.08
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)
,
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)
,

1. Quad 그리기

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
[RequireComponent(typeof(MeshFilter))]
public class proceduralMesh : MonoBehaviour
{
    Mesh mesh;
    Material material;
 
    Vector3[] vertices;
    int[] triangles;
 
 
    private void Awake()
    {
        mesh = GetComponent<MeshFilter>().mesh;
        material = GetComponent<Renderer>().material;
    }
    // Start is called before the first frame update
    void Start()
    {
        meshData();
        createObj();
    }
 
    void meshData()
    {
        //vertices, triangles베열에 값 넣기
        //vertices에서는 점의 위치를 설정
        vertices = new Vector3[] { new Vector3(000), new Vector3(001), new Vector3(100), new Vector3(1,0,1) };
        //triangles에서는 vertices를 어떻게 연결할지 정하기 
        triangles = new int[] { 012213 };
    }
 
    //obj 그리기 
    void createObj()
    {
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        material.color = Color.green;
 
        //법선벡터 계산(라이팅)
        mesh.RecalculateNormals();
    }
 
    }
 
cs


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

10/8 joint 안정화 작업  (0) 2019.10.09
10/6 procedural grid, physics and soft body(nvidia flux)  (0) 2019.10.08
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)
,


젤리 효과를 넣을 gameObject에 적용할 스크립트

Collision chk를 하기 때문에 rigidBody 필요함.


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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class jellyFier : MonoBehaviour
{
    public float bounceSpeed;
    public float fallForce;
    public float stiffness;
 
    private MeshFilter meshFilter;
    private Mesh mesh;
 
    jellyVertex[] jellyVertices;
    Vector3[] currentMeshVertices;
 
    private void Start()
    {
        meshFilter = GetComponent<MeshFilter>();
        mesh = meshFilter.mesh;
 
        GetVertices();
    }
 
    private void GetVertices()
    {
        jellyVertices = new jellyVertex[mesh.vertices.Length];
        currentMeshVertices = new Vector3[mesh.vertices.Length];
        for(int i=0; i< mesh.vertices.Length; i++)
        {
            jellyVertices[i] = new jellyVertex(i, mesh.vertices[i], mesh.vertices[i], Vector3.zero);
            currentMeshVertices[i] = mesh.vertices[i];
        }
    }
 
    private void Update()
    {
        UpdateVertices();
    }
 
    private void UpdateVertices()
    {
        for(int i=0; i<jellyVertices.Length; i++)
        {
            jellyVertices[i].UpdateVelocity(bounceSpeed);
            jellyVertices[i].Settle(stiffness);
 
            jellyVertices[i].currentVertexPosition += jellyVertices[i].currentVelocity * Time.deltaTime;
            currentMeshVertices[i] = jellyVertices[i].currentVertexPosition;
        }
 
        mesh.vertices = currentMeshVertices;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        mesh.RecalculateTangents();
    }
 
    public void OnCollisionEnter(Collision col)
    {
        ContactPoint[] collisionPoints = col.contacts;
        for(int i = 0; i< collisionPoints.Length; i++)
        {
            Vector3 inputPoint = collisionPoints[i].point + (collisionPoints[i].point * .1f);
            ApplyPressureToPoint(inputPoint, fallForce);
        }
    }
 
    public void ApplyPressureToPoint(Vector3 _point, float _pressure)
    {
        for(int i=0; i< jellyVertices.Length; i++)
        {
            jellyVertices[i].ApplyPressureToVertex(transform, _point, _pressure);
        }
    }
}
 
cs


추가적으로 필요한 스크립트

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class jellyVertex : MonoBehaviour
{
    public int verticeIndex;
    public Vector3 initialVertexPosition;
    public Vector3 currentVertexPosition;
 
    public Vector3 currentVelocity;
 
    public jellyVertex(int _verticeIndex, Vector3 _initialVertexPosition, Vector3 _currentVertexPosition, Vector3 _currentVelocity)
    {
        verticeIndex = _verticeIndex;
        initialVertexPosition = _initialVertexPosition;
        currentVertexPosition = _currentVertexPosition;
        currentVelocity = _currentVelocity;
    }
 
    public Vector3 GetCurrentDisplacement()
    {
        return currentVertexPosition - initialVertexPosition;
    }
 
    public void UpdateVelocity(float _bounceSpeed)
    {
        currentVelocity = currentVelocity - GetCurrentDisplacement() * _bounceSpeed * Time.deltaTime;
    }
 
    public void Settle(float _stiffness)
    {
        currentVelocity *= 1f - _stiffness * Time.deltaTime;
    }
 
    public void ApplyPressureToVertex(Transform _transform, Vector3 _position, float _pressure)
    {
        Vector3 distanceVerticePoint = currentVertexPosition - _transform.InverseTransformPoint(_position);
        float adaptedPressure = _pressure / (1f + distanceVerticePoint.sqrMagnitude);
        float velocity = adaptedPressure * Time.deltaTime;
        currentVelocity += distanceVerticePoint.normalized * velocity;
    }
}
 
cs


튜토리얼

https://www.youtube.com/watch?v=UxLJ6XewTVs&t=31s

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

10/6 procedural grid, physics and soft body(nvidia flux)  (0) 2019.10.08
10/5 procedural Mesh  (0) 2019.10.05
03. 10/2 physics & leapmotion SDK  (0) 2019.10.02
02. 기술 기획  (0) 2019.10.02
01. 프로젝트 기획  (0) 2019.10.02
Posted by 도이(doi)
,