Skip to content

Commit d403893

Browse files
author
xwc2022
committed
準備頂點的相鄰三角形資訊
1 parent e1c143d commit d403893

File tree

8 files changed

+147
-20
lines changed

8 files changed

+147
-20
lines changed

Assembly-CSharp.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<Compile Include="Assets\Pack\DiyTerrain\Script\Brush.cs" />
124124
<Compile Include="Assets\Pack\DiyTerrain\Script\ShowMeshBound.cs" />
125125
<Compile Include="Assets\scripts\Level\ManyBike.cs" />
126+
<Compile Include="Assets\scripts\GravityGenerator\GravitySensor.cs" />
126127
<None Include="Assets\Pack\DiyTerrain\shaders\ToSphere.shader" />
127128
<None Include="Assets\Pack\DiyTerrain\shaders\ToSpherePBR.shader" />
128129
<None Include="Assets\Pack\DiyTerrain\shaders\brush\brush_fill.shader" />

Assets/Prefab/GravitySensor.prefab

1.15 KB
Binary file not shown.

Assets/Scenes/knot.unity

16 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
public class GravitySensor : MonoBehaviour
4+
{
5+
public List<int> neighborTriangleIndex;
6+
public void init() { neighborTriangleIndex = new List<int>(); }
7+
public void addNeighborTriangleIndex(int index)
8+
{
9+
neighborTriangleIndex.Add(index);
10+
}
11+
public int triangelIndex;
12+
public string info()
13+
{
14+
var len = neighborTriangleIndex.Count;
15+
var msg = "";
16+
for (var i = 0; i < len; ++i)
17+
msg += neighborTriangleIndex[i] + ",";
18+
19+
return msg;
20+
}
21+
}

Assets/scripts/GravityGenerator/GravitySensor.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/scripts/GravityGenerator/MeshGravityGenerator.cs

+68-11
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,87 @@
1-
using UnityEngine;
1+
using System.Collections.Generic;
2+
using UnityEngine;
23
//非球圓形星球:像是Donuts、Knot
34
public class MeshGravityGenerator : MonoBehaviour, GroundGravityGenerator
45
{
5-
public float findingGravitySensorR = 4;
6-
public Transform gs;
76

7+
8+
public float findingGravitySensorR = 4;
89
Collider[] colliderList = new Collider[100];//大小看需求自己設定
910
Color purple = new Color(159.0f / 255, 90.0f / 255, 253.0f / 255);
1011

1112
void Awake()
1213
{
13-
createGS();
14+
Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
15+
triangles = mesh.triangles;
16+
17+
vertices = mesh.vertices;
18+
normals = mesh.normals;
19+
createGravitySensor();
1420
}
1521

16-
public void createGS()
22+
int[] triangles;
23+
Vector3[] vertices;
24+
Vector3[] normals;
25+
GravitySensor[] gsList;
26+
public GravitySensor gravitySensorPrefab; // Prefab
27+
void createGravitySensor()
1728
{
18-
Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
19-
2029
var modelPos = transform.position;
21-
int vCount = mesh.vertices.Length;
22-
Vector3[] vertices = mesh.vertices;
23-
Vector3[] normals = mesh.normals;
30+
int vCount = vertices.Length;
31+
32+
// 跑1次所有vertex
33+
gsList = new GravitySensor[vCount];
2434
for (int i = 0; i < vCount; i++)
2535
{
2636
var v0 = vertices[i];
2737
var n0 = normals[i];
2838
Quaternion rot0 = Quaternion.LookRotation(n0);
29-
Instantiate(gs, v0 + modelPos, rot0, this.transform);
39+
var gs = Instantiate(gravitySensorPrefab, v0 + modelPos, rot0, this.transform);
40+
gs.init();
41+
gs.triangelIndex = i;
42+
gs.name = "gs" + i;
43+
gsList[i] = gs;
44+
}
45+
46+
// 跑1次所有三角形
47+
int triCount = triangles.Length / 3;
48+
for (int i = 0; i < triCount; ++i)
49+
{
50+
int v0Index = triangles[3 * i];
51+
int v1Index = triangles[3 * i + 1];
52+
int v2Index = triangles[3 * i + 2];
53+
gsList[v0Index].addNeighborTriangleIndex(i);
54+
gsList[v1Index].addNeighborTriangleIndex(i);
55+
gsList[v2Index].addNeighborTriangleIndex(i);
56+
}
57+
}
58+
59+
void createGravityDir(GravitySensor gs)
60+
{
61+
// 找出位在那個三角形上
62+
var triIndexList = gs.neighborTriangleIndex;
63+
// print(triIndexList.Count + "->" + gs.info());
64+
65+
var triCount = triIndexList.Count;
66+
for (var i = 0; i < triCount; ++i)
67+
{
68+
var triIndex = triIndexList[i];
69+
int v0Index = triangles[3 * triIndex];
70+
int v1Index = triangles[3 * triIndex + 1];
71+
int v2Index = triangles[3 * triIndex + 2];
72+
print(v0Index + "," + v1Index + "," + v2Index);
73+
74+
var v0 = vertices[v0Index];
75+
var v1 = vertices[v1Index];
76+
var v2 = vertices[v2Index];
77+
78+
var parentPos = transform.position;
79+
v0 += parentPos;
80+
v1 += parentPos;
81+
v2 += parentPos;
82+
Debug.DrawLine(v0, v1, Color.red);
83+
Debug.DrawLine(v1, v2, Color.green);
84+
Debug.DrawLine(v2, v0, Color.blue);
3085
}
3186
}
3287

@@ -53,6 +108,8 @@ public Vector3 findGravityDir(Vector3 headUp, Vector3 targetPos, bool isHitFloor
53108
nearestDistance = nowDistance;
54109
}
55110
}
111+
var gs = nearestC.GetComponent<GravitySensor>();
112+
createGravityDir(gs);
56113

57114
Debug.DrawRay(nearestC.transform.position, nearestC.transform.forward * 5, purple);
58115
return -nearestC.transform.forward;

Assets/scripts/Tool/GeometryTool.cs

+37
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,41 @@ public static void GetShootingRay(Vector3 mousePos, out Vector3 from, out Vector
9797
else
9898
GeometryTool.GetShootingRayPerspective(mousePos, out from, out dir);
9999
}
100+
101+
// 這些點z都是0
102+
static void CalculateBarycentricCoordinates(Vector3 s0, Vector3 s1, Vector3 s2, Vector3 P, ref bool success, out float α, out float β, out float γ)
103+
{
104+
var diff = P - s0;
105+
106+
// https://1.bp.blogspot.com/-csct6vAS1vs/YajSLKgW1WI/AAAAAAABDd8/shSLs78p274fnRa3R5fv3wN9dNS5WCN3wCPcBGAsYHg/s4618/screen_sapce.png
107+
// 求ray(P,S0-S2)和ray(S0,S1-S2)的交點
108+
// 等同於求ray(P,S0-S2)和平面的交點
109+
var dir01 = s1 - s0;
110+
var dir02 = s2 - s0;
111+
112+
var help = Vector3.Cross(dir01, dir02);
113+
var n = Vector3.Cross(help, dir01); // 不需要正規化
114+
115+
Vector3 hitPos;
116+
var result = RayHitPlane(P, -dir02, s0, n, out hitPos);
117+
if (!result)
118+
{
119+
// 退化成直線的三角形才有也可能
120+
// console.log('平行', s0, s1, s2, P);
121+
success = false;
122+
}
123+
124+
var p_on_dir01 = hitPos;
125+
var vector_α = p_on_dir01 - s0;
126+
var vector_β = diff - vector_α;
127+
128+
// 擋掉dir01、dir02是y軸平行的情況
129+
// 浮點數請用 number_equal,不然會GG
130+
// 見圖:bug/float_point_compaire_error(fixed)/bug_when_clipping_2.jpg
131+
// 其實當初直接用長度比算α、β不是更簡單嗎?
132+
α = vector_α.magnitude / dir01.magnitude;
133+
β = vector_β.magnitude / dir02.magnitude;
134+
135+
γ = 1 - α - β;
136+
}
100137
}

UserSettings/EditorUserSettings.asset

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ EditorUserSettings:
66
serializedVersion: 4
77
m_ConfigSettings:
88
RecentlyUsedScenePath-0:
9-
value: 22424703114646680e0b0227036c7a1e121b1d393f0b29223f20193cf0ae2136ebf32f
9+
value: 22424703114646680e0b0227036c7b1f18020c39193b2f3e2a1d1221f7f3132ae3f13ffdfe742a323016f6
1010
flags: 0
1111
RecentlyUsedScenePath-1:
12-
value: 22424703114646680e0b0227036c7b1f18020c39193b2f3e2a1d1221f7f3132ae3f13ffdfe742a323016f6
12+
value: 22424703114646680e0b0227036c4f1c17191d3e0a21283e633c133af6f9
1313
flags: 0
1414
RecentlyUsedScenePath-2:
15-
value: 22424703114646680e0b0227036c4f1c17191d3e0a21283e633c133af6f9
15+
value: 22424703114646680e0b0227036c7b1f18020c39623d28393930
1616
flags: 0
1717
RecentlyUsedScenePath-3:
18-
value: 22424703114646680e0b0227036c7b1f18020c39623d28393930
18+
value: 22424703114646680e0b0227036c731918122d3a623d28393930
1919
flags: 0
2020
RecentlyUsedScenePath-4:
21-
value: 22424703114646680e0b0227036c731918122d3a623d28393930
21+
value: 22424703114646680e0b0227036c4f1c17191d3e623d28393930
2222
flags: 0
2323
RecentlyUsedScenePath-5:
24-
value: 22424703114646680e0b0227036c0b4512121f38292d68252320092a
24+
value: 224247031146466b0c0d076d342a462413050a2b252669233d211821e7df203df0f537e0e9742a323016f6
2525
flags: 0
2626
RecentlyUsedScenePath-6:
27-
value: 22424703114646680e0b0227036c4f1c17191d3e623d28393930
27+
value: 22424703114646680e0b0227036c0d1419190d3e3f66333e243d04
2828
flags: 0
2929
RecentlyUsedScenePath-7:
30-
value: 22424703114646680e0b0227036c0d1419190d3e3f66333e243d04
30+
value: 224247031146466b0c0d076d27224b15045835233e3a29221a280936f0ae2136ebf32f
3131
flags: 0
3232
RecentlyUsedScenePath-8:
33-
value: 224247031146466b0c0d076d342a462413050a2b252669233d211821e7df203df0f537e0e9742a323016f6
33+
value: 22424703114646680e0b0227036c0b4512121f38292d68252320092a
3434
flags: 0
3535
RecentlyUsedScenePath-9:
3636
value: 22424703114646680e0b0227036c541e1903563f22213229

0 commit comments

Comments
 (0)