summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/NGS.MeshFusionPro/CombineSource.cs
blob: f010062ea4a7ea3b01daed4c065ddc08f17e3df4 (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
using System;
using UnityEngine;

namespace NGS.MeshFusionPro;

public class CombineSource : ICombineSource<CombinedObject, CombinedObjectPart>, ICombineSource
{
	public Vector3 Position { get; private set; }

	public Bounds Bounds { get; private set; }

	public MeshCombineInfo CombineInfo { get; private set; }

	public RendererSettings RendererSettings { get; private set; }

	public event Action<ICombinedObject, ICombinedObjectPart> onCombined;

	public event Action<ICombinedObject, string> onCombineError;

	public event Action<ICombinedObject> onCombineFailed;

	public event Action<CombinedObject, CombinedObjectPart> onCombinedTyped;

	public event Action<CombinedObject, string> onCombineErrorTyped;

	public event Action<CombinedObject> onCombineFailedTyped;

	public CombineSource(GameObject go, int submeshIndex = 0)
		: this(go.GetComponent<MeshFilter>().mesh, go.GetComponent<MeshRenderer>(), submeshIndex)
	{
	}

	public CombineSource(Mesh mesh, MeshRenderer renderer, int submeshIndex = 0)
	{
		if (mesh == null)
		{
			throw new ArgumentException("Mesh is null");
		}
		if (renderer == null)
		{
			throw new ArgumentException("Mesh Renderer is null");
		}
		if (submeshIndex >= mesh.subMeshCount)
		{
			throw new ArgumentException("Submesh index is greater the submeshCount");
		}
		MeshCombineInfo info = new MeshCombineInfo(mesh, renderer.localToWorldMatrix, renderer.lightmapScaleOffset, renderer.realtimeLightmapScaleOffset, submeshIndex);
		RendererSettings settings = new RendererSettings(renderer, submeshIndex);
		Construct(info, settings, renderer.bounds);
	}

	public CombineSource(MeshCombineInfo info, RendererSettings settings)
		: this(info, settings, info.mesh.bounds.Transform(info.transformMatrix))
	{
	}

	public CombineSource(MeshCombineInfo info, RendererSettings settings, Bounds bounds)
	{
		Construct(info, settings, bounds);
	}

	private void Construct(MeshCombineInfo info, RendererSettings settings, Bounds bounds)
	{
		CombineInfo = info;
		RendererSettings = settings;
		Position = info.transformMatrix.GetTranslation();
		Bounds = bounds;
	}

	public void Combined(CombinedObject root, CombinedObjectPart part)
	{
		this.onCombined?.Invoke(root, part);
		this.onCombinedTyped?.Invoke(root, part);
	}

	public void CombineError(CombinedObject root, string errorMessage)
	{
		if (this.onCombineError == null && this.onCombinedTyped == null)
		{
			Debug.Log("Error during combine " + root.name + ", reason :" + errorMessage);
			return;
		}
		this.onCombineError?.Invoke(root, errorMessage);
		this.onCombineErrorTyped?.Invoke(root, errorMessage);
	}

	public void CombineFailed(CombinedObject root)
	{
		this.onCombineFailed?.Invoke(root);
		this.onCombineFailedTyped?.Invoke(root);
	}
}