젤리 효과를 넣을 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)
,