summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Thronefall/NGS.MeshFusionPro/CombineTree.cs
blob: 2fd39af05ba7ad282997d8fc165440831526e98c (plain)
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
using System;
using UnityEngine;

namespace NGS.MeshFusionPro;

public class CombineTree : BinaryTree<CombineTreeNode, ICombineSource>
{
	private ICombinedMeshFactory _factory;

	private float _leafSize;

	private int _vertexLimit;

	public float LeafSize => _leafSize;

	public int VertexLimit => _vertexLimit;

	public ICombinedMeshFactory CombinedMeshFactory => _factory;

	public event Action<CombinedObject> onStaticCombinedObjectCreated;

	public event Action<DynamicCombinedObject> onDynamicCombinedObjectCreated;

	public event Action<CombinedLODGroup> onCombinedLODGroupCreated;

	public CombineTree(ICombinedMeshFactory factory, float leafSize, int vertexLimit)
	{
		_factory = factory;
		_leafSize = leafSize;
		_vertexLimit = vertexLimit;
	}

	public void Combine()
	{
		TreeTraversal(delegate(CombineTreeNode node, int depth)
		{
			node.Combine();
			return true;
		});
	}

	protected override CombineTreeNode CreateRoot(ICombineSource source)
	{
		return CreateNode(source.Position, Vector3.one * _leafSize, isLeaf: true);
	}

	protected override CombineTreeNode CreateNode(Vector3 center, Vector3 size, bool isLeaf)
	{
		CombineTreeNode combineTreeNode = new CombineTreeNode(this, center, size, isLeaf);
		combineTreeNode.onStaticCombinedObjectCreated += this.onStaticCombinedObjectCreated;
		combineTreeNode.onDynamicCombinedObjectCreated += this.onDynamicCombinedObjectCreated;
		combineTreeNode.onCombinedLODGroupCreated += this.onCombinedLODGroupCreated;
		return combineTreeNode;
	}

	protected override CombineTreeNode ExpandRoot(CombineTreeNode root, ICombineSource target)
	{
		Bounds bounds = root.Bounds;
		Bounds bounds2 = target.Bounds;
		Vector3 center = Vector3.zero;
		Vector3 size = Vector3.zero;
		Vector3 center2 = Vector3.zero;
		bool flag = false;
		for (int i = 0; i < 3; i++)
		{
			if (bounds2.min[i] < bounds.min[i])
			{
				size = bounds.size;
				size[i] *= 2f;
				center = bounds.center;
				center[i] -= bounds.size[i] / 2f;
				center2 = bounds.center;
				center2[i] -= bounds.size[i];
				break;
			}
			if (bounds2.max[i] > bounds.max[i])
			{
				size = bounds.size;
				size[i] *= 2f;
				center = bounds.center;
				center[i] += bounds.size[i] / 2f;
				center2 = bounds.center;
				center2[i] += bounds.size[i];
				flag = true;
				break;
			}
		}
		CombineTreeNode combineTreeNode = CreateNode(center, size, isLeaf: false);
		CombineTreeNode combineTreeNode2 = CreateNode(center2, bounds.size, root.IsLeaf);
		if (flag)
		{
			combineTreeNode.SetChilds(base.RootInternal, combineTreeNode2);
		}
		else
		{
			combineTreeNode.SetChilds(combineTreeNode2, base.RootInternal);
		}
		return combineTreeNode;
	}

	protected override bool Includes(CombineTreeNode node, ICombineSource source)
	{
		Bounds bounds = node.Bounds;
		Bounds bounds2 = source.Bounds;
		return bounds.Contains(bounds2);
	}

	protected override bool Intersects(CombineTreeNode node, ICombineSource source)
	{
		return node.Bounds.Contains(source.Position);
	}
}