summaryrefslogtreecommitdiff
path: root/Thronefall/NGS.MeshFusionPro/DynamicCombinedObjectPartInternal.cs
blob: d7a0c1ca92d76a955ad77fd6dcc2e5d1930956d8 (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
using UnityEngine;

namespace NGS.MeshFusionPro;

public class DynamicCombinedObjectPartInternal : DynamicCombinedObjectPart
{
	private Matrix4x4 _localTransform;

	private Matrix4x4 _targetLocalTransform;

	private Matrix4x4 _worldToLocalMatrix;

	private bool _inMove;

	public DynamicCombinedObjectPartInternal(DynamicCombinedObject root, CombinedObjectPart basePart, Matrix4x4 transformMatrix)
		: base(root, basePart, transformMatrix)
	{
		Vector3 pos = transformMatrix.GetTranslation() - root.transform.position;
		_localTransform = transformMatrix.SetTranslation(pos);
		_worldToLocalMatrix = base.Root.transform.worldToLocalMatrix;
	}

	public override void Move(Vector3 position, Quaternion rotation, Vector3 scale)
	{
		Move(Matrix4x4.TRS(position, rotation, scale));
	}

	public override void Move(Matrix4x4 transform)
	{
		Matrix4x4 localTransform = _worldToLocalMatrix * transform;
		MoveLocal(localTransform);
	}

	public override void MoveLocal(Matrix4x4 localTransform)
	{
		if (!_inMove)
		{
			_targetLocalTransform = localTransform;
			_root.UpdatePart(this);
			_inMove = true;
		}
	}

	public PartMoveInfo CreateMoveInfo()
	{
		CombinedMeshPart meshPart = _basePart.MeshPart;
		PartMoveInfo result = default(PartMoveInfo);
		result.partIndex = meshPart.Index;
		result.vertexStart = meshPart.VertexStart;
		result.vertexCount = meshPart.VertexCount;
		result.currentTransform = _localTransform;
		result.targetTransform = _targetLocalTransform;
		return result;
	}

	public void PositionUpdated()
	{
		_localTransform = _targetLocalTransform;
		_inMove = false;
	}
}