wangguan
2020-11-27 4bfaf190a4e16ecfc945bc26525b21dcaacc0417
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using Core.Extensions;
using UnityEngine;
using Random = UnityEngine.Random;
 
namespace TowerDefense.MeshCreator
{
    /// <summary>
    /// Creates a Mesh that represents an area
    /// </summary>
    [Serializable]
    public class AreaMeshCreator : MonoBehaviour
    {
        [HideInInspector]
        public MeshObject meshObject;
 
        public Transform outSidePointsParent;
 
        /// <summary>
        /// The parent transform of points in the mesh
        /// </summary>
        public Transform pointsCenter
        {
            get
            {
                if (outSidePointsParent == null)
                {
                    var points = new GameObject("Points");
                    outSidePointsParent = points.transform;
                    outSidePointsParent.SetParent(transform, false);
                    outSidePointsParent.eulerAngles = new Vector3(90, 0, 0);
                }
#if UNITY_EDITOR
                outSidePointsParent.hideFlags = HideFlags.HideInHierarchy;
#endif
                return outSidePointsParent;
            }
        }
 
#if UNITY_EDITOR
        /// <summary>
        /// Gets an array of the Transforms of points in this mesh - only used by editor script
        /// </summary>
        public Transform[] pointsTransforms
        {
            get
            {
                Transform[] childern = new Transform[pointsCenter.childCount];
                int length = pointsCenter.childCount;
                for (int i = 0; i < length; i++)
                {
                    childern[i] = pointsCenter.GetChild(i);
                }
                return childern;
            }
        }
#endif
 
        /// <summary>
        /// Get a list of Vector3s that correspond to the positions of the points in this mesh
        /// </summary>
        /// <returns>List of Points</returns>
        public List<Vector3> GetPoints()
        {
            return GetChildrenPositions(pointsCenter);
        }
 
        /// <summary>
        /// Gets a random Vector3 that lies inside the mesh object
        /// </summary>
        /// <returns>Random point</returns>
        public Vector3 GetRandomPointInside()
        {
            return transform.TransformPoint(meshObject.RandomPointInMesh());
        }
 
        /// <summary>
        /// Forces all points to have a local "y" position of 0
        /// Makes them coplanar
        /// </summary>
        public void ForcePointsFlat()
        {
            int length = pointsCenter.childCount;
            for (int i = 0; i < length; i++)
            {
                Transform t = pointsCenter.GetChild(i);
                Vector3 position = t.localPosition;
                position.z = 0;
                t.localPosition = position;
            }
        }
 
        List<Vector3> GetChildrenPositions(Transform parent)
        {
            int length = parent.childCount;
            List<Vector3> points = new List<Vector3>();
            for (int i = 0; i < length; i++)
            {
                points.Add(parent.GetChild(i).position);
            }
            return points;
        }
 
#if UNITY_EDITOR
        void OnDrawGizmos()
        {
            int count = pointsCenter.childCount;
            for (int i = 0; i < count - 1; i++)
            {
                Vector3 from = pointsCenter.GetChild(i).position;
                Vector3 to = pointsCenter.GetChild(i + 1).position;
                Gizmos.DrawLine(from, to);
            }
            // last to first
            Vector3 last = pointsCenter.GetChild(count - 1).position;
            Vector3 first = pointsCenter.GetChild(0).position;
            Gizmos.DrawLine(last, first);
        }
#endif
    }
 
    [Serializable]
    public class Triangle
    {
        public Vector3 v0;
 
        public Vector3 v1;
        
        public Vector3 v2;
 
        public float area;
 
        /// <summary>
        /// Represents a Triangle in a mesh
        /// </summary>
        /// <param name="v0">First Point</param>
        /// <param name="v1">Second Point</param>
        /// <param name="v2">Third Point</param>
        public Triangle(Vector3 v0, Vector3 v1, Vector3 v2)
        {
            this.v0 = v0;
            this.v1 = v1;
            this.v2 = v2;
 
            // Precalculate area
            float a = Vector3.Distance(v0, v1), b = Vector3.Distance(v1, v2), c = Vector3.Distance(v2, v0);
            float s = (a + b + c) / 2;
            area = Mathf.Sqrt(s * (s - a) * (s - b) * (s - c));
        }
    }
 
    /// <summary>
    /// Contains the triangles of the mesh
    /// Calculates the mesh area
    /// Can get a random point within the mesh area
    /// </summary>
    [Serializable]
    public class MeshObject
    {
        public List<Triangle> triangles;
 
        public float completeArea;
 
        protected Vector3 cacheVec;
        protected bool first = true;
 
        public MeshObject(List<Triangle> triangles)
        {
            this.triangles = triangles;
            completeArea = this.triangles.Sum(x => x.area);
        }
 
 
        /// <summary>
        /// Gets a random point in the mesh
        /// </summary>
        /// <returns>Random point</returns>
        public Vector3 RandomPointInMesh()
        {
            Triangle randomTriangle = triangles.WeightedSelection(completeArea, t => t.area);
            float x = Random.value, y = Random.value;
            if (x + y >= 1)
            {
                x = 1 - x;
                y = 1 - y;
            }
            float z = 1 - x - y;
            var randomBaryCentricPoint = new Vector3(x, y, z);
            
            
            // River: 直接返回数据.ATTENTION TO OPP:其实最后可以直接设置一个顶点当做是Node则可。
            if (first)
            {
                this.cacheVec.x = x;
                this.cacheVec.y = y;
                this.cacheVec.z = z;
                first = false;
            }
            return cacheVec;
 
            /** 如果要返回随机点,则打开下面的代码。
            Vector3 cartesianPoint = (randomBaryCentricPoint.x * randomTriangle.v0) + (randomBaryCentricPoint.y *
                                                                                       randomTriangle.v1) +
                                     (randomBaryCentricPoint.z * randomTriangle.v2);
            return cartesianPoint;*/
        }
    }
}