File size: 4,379 Bytes
41791c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using BezierMesh;
using System.Collections.Generic;
using System.Linq;
using Unity.Burst.Intrinsics;
using UnityEngine;

[CreateAssetMenu(fileName = "TissueMeshGenerator", menuName = "Learning/Blood Suction/Tissue Mesh Generator", order = 1)]
public class TissueMeshGenerator : ScriptableObject
{
    public int NumControlPoints
    {
        get { return m_numControlPts; }
        set { m_numControlPts = value; }
    }

    public Mesh GenerateMesh()
    {
        Mesh copiedMesh = new Mesh()
        {
            name = m_referenceMesh.name + " Copy",
            vertices = m_referenceMesh.vertices,
            triangles = m_referenceMesh.triangles,
            normals = m_referenceMesh.normals,
            tangents = m_referenceMesh.tangents,
            bounds = m_referenceMesh.bounds,
            uv = m_referenceMesh.uv
        };

        // Generate Bezier surface mesh
        BezierMeshGeneratorWithCurvatureLimit bezierMeshGenerator = new BezierMeshGeneratorWithCurvatureLimit(
            u_cells: 25,
            w_cells: 25,
            x_start: copiedMesh.bounds.min.x,
            x_end: copiedMesh.bounds.max.x,
            y_start: copiedMesh.bounds.min.z,
            y_end: copiedMesh.bounds.max.z,
            u_ctrl_pts: m_numControlPts,
            w_ctrl_pts: m_numControlPts,
            max_deformation: 0.5f,
            random_ctrl_pts: true,
            curv_treshold: 5
        );

        float[,] X, Y, Z;
        (X, Y, Z) = bezierMeshGenerator.GenerateGrid();

        int[] surfaceIndices = FindMeshTopSurfaceIndices(copiedMesh);
        int[] surfaceInnerIndices = FindMeshTopSurfaceInnerIndices(copiedMesh, surfaceIndices);

        // Modify the copied mesh surface
        float minX = X.Cast<float>().Min();
        float maxX = X.Cast<float>().Max();
        float minY = Y.Cast<float>().Min();
        float maxY = Y.Cast<float>().Max();

        float rangeX = maxX - minX;
        float rangeY = maxY - minY;

        int sizeX = X.GetLength(0);
        int sizeY = Y.GetLength(1);

        Vector3[] vertices = copiedMesh.vertices;
        foreach (int idx in surfaceInnerIndices)
        {
            int indexX = (int)((vertices[idx].x - minX) / rangeX * (sizeX - 1));
            int indexY = (int)((vertices[idx].z - minY) / rangeY * (sizeY - 1));

            // safety guarantee
            indexX = Mathf.Max(0, Mathf.Min(indexX, sizeX - 1));
            indexY = Mathf.Max(0, Mathf.Min(indexY, sizeY - 1));

            vertices[idx].y += Z[indexX, indexY];
        }
        copiedMesh.SetVertices(vertices);
        copiedMesh.RecalculateBounds();
        copiedMesh.RecalculateNormals();
        copiedMesh.RecalculateTangents();

        return copiedMesh;
    }

    static private int[] FindMeshTopSurfaceIndices(Mesh mesh)
    {
        List<int> indices = new List<int>();
        Vector3[] vertices = mesh.vertices;
        float MaxHeightY = -float.MaxValue;
        float eps = 1e-6f;
        for (int i = 0; i < vertices.Length; i++)
        {
            if (vertices[i].y > MaxHeightY + eps)
            {
                MaxHeightY = vertices[i].y;
                indices.Clear();
                indices.Add(i);
            }
            else if (Mathf.Abs(vertices[i].y - MaxHeightY) < eps)
            {
                indices.Add(i);
            }
        }
        return indices.ToArray();
    }


    static private int[] FindMeshTopSurfaceInnerIndices(Mesh mesh, int[] surfaceIndices)
    {
        List<int> indices = new List<int>();
        Vector3[] vertices = mesh.vertices;
        float eps = 1e-6f;
        Bounds newBounds = new Bounds();
        Vector3 newMinBound = new Vector3()
        {
            x = mesh.bounds.min.x + eps,
            y = mesh.bounds.max.y,
            z = mesh.bounds.min.z + eps,
        };
        Vector3 newMaxBound = new Vector3()
        {
            x = mesh.bounds.max.x - eps,
            y = mesh.bounds.max.y,
            z = mesh.bounds.max.z - eps,
        };
        newBounds.SetMinMax(newMinBound, newMaxBound);

        foreach (int i in surfaceIndices)
        {
            if (newBounds.Contains(vertices[i]))
            {
                indices.Add(i);
            }
        }
        return indices.ToArray();
    }

    [SerializeField]
    private Mesh m_referenceMesh;
    [SerializeField]
    private int m_numControlPts;
}