summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/NGS.MeshFusionPro/JobsMeshMoverSTD.cs
blob: 535b8affb599445ddbf8718cac547a910dacb056 (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
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
using System;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;

namespace NGS.MeshFusionPro;

public class JobsMeshMoverSTD : IAsyncCombinedMeshMover, ICombinedMeshMover, IDisposable
{
	[BurstCompile]
	private struct MovePartsJob : IJobParallelFor
	{
		[NativeDisableParallelForRestriction]
		public NativeArray<Vector3> vertices;

		[NativeDisableParallelForRestriction]
		public NativeArray<Vector3> normals;

		[NativeDisableParallelForRestriction]
		public NativeArray<Vector4> tangents;

		[WriteOnly]
		[NativeDisableParallelForRestriction]
		public NativeArray<Bounds> bounds;

		[ReadOnly]
		[NativeDisableParallelForRestriction]
		public NativeArray<Bounds> localBounds;

		[ReadOnly]
		public NativeList<PartMoveInfo> moveInfos;

		public void Execute(int idx)
		{
			PartMoveInfo partMoveInfo = moveInfos[idx];
			int partIndex = partMoveInfo.partIndex;
			int vertexStart = partMoveInfo.vertexStart;
			int num = vertexStart + partMoveInfo.vertexCount;
			Matrix4x4 targetTransform = partMoveInfo.targetTransform;
			Matrix4x4 inverse = partMoveInfo.currentTransform.inverse;
			for (int i = vertexStart; i < num; i++)
			{
				Vector3 point = vertices[i];
				Vector3 vector = normals[i];
				Vector4 vector2 = tangents[i];
				float w = vector2.w;
				point = inverse.MultiplyPoint3x4(point);
				point = targetTransform.MultiplyPoint3x4(point);
				vector = inverse.MultiplyVector(vector);
				vector = targetTransform.MultiplyVector(vector);
				vector2 = inverse.MultiplyVector(vector2);
				vector2 = targetTransform.MultiplyVector(vector2);
				vector2.w = w;
				vertices[i] = point;
				normals[i] = vector;
				tangents[i] = vector2;
			}
			bounds[partIndex] = localBounds[partIndex].Transform(targetTransform);
		}
	}

	[BurstCompile]
	private struct RecalculateBoundsJob : IJob
	{
		public NativeArray<Bounds> bounds;

		public NativeArray<Bounds> boundingBox;

		public void Execute()
		{
			Bounds value = boundingBox[0];
			for (int i = 0; i < bounds.Length; i++)
			{
				value.Encapsulate(bounds[i]);
			}
			boundingBox[0] = value;
		}
	}

	private MeshDataNativeArraysSTD _meshData;

	private NativeList<PartMoveInfo> _moveInfos;

	private JobHandle _handle;

	public JobsMeshMoverSTD(MeshDataNativeArraysSTD meshData)
	{
		_meshData = meshData;
		_moveInfos = new NativeList<PartMoveInfo>(Allocator.Persistent);
	}

	public void MoveParts(IList<PartMoveInfo> moveInfos)
	{
		MovePartsAsync(moveInfos);
		FinishAsyncMoving();
	}

	public void MovePartsAsync(IList<PartMoveInfo> moveInfos)
	{
		NativeArray<Bounds> bounds = _meshData.Bounds;
		bounds[0] = new Bounds(_meshData.GetBounds().center, Vector3.zero);
		_moveInfos.Clear();
		for (int i = 0; i < moveInfos.Count; i++)
		{
			ref NativeList<PartMoveInfo> moveInfos2 = ref _moveInfos;
			PartMoveInfo value = moveInfos[i];
			moveInfos2.Add(in value);
		}
		MovePartsJob movePartsJob = default(MovePartsJob);
		movePartsJob.vertices = _meshData.Vertices;
		movePartsJob.normals = _meshData.Normals;
		movePartsJob.tangents = _meshData.Tangents;
		movePartsJob.bounds = _meshData.PartsBounds;
		movePartsJob.localBounds = _meshData.PartsBoundsLocal;
		movePartsJob.moveInfos = _moveInfos;
		MovePartsJob jobData = movePartsJob;
		RecalculateBoundsJob recalculateBoundsJob = default(RecalculateBoundsJob);
		recalculateBoundsJob.bounds = _meshData.PartsBounds;
		recalculateBoundsJob.boundingBox = bounds;
		RecalculateBoundsJob jobData2 = recalculateBoundsJob;
		_handle = jobData2.Schedule(IJobParallelForExtensions.Schedule(jobData, _moveInfos.Length, 4));
	}

	public void FinishAsyncMoving()
	{
		_handle.Complete();
	}

	public void ApplyData()
	{
		if (!_handle.IsCompleted)
		{
			FinishAsyncMoving();
		}
		_meshData.ApplyDataToMesh();
	}

	public void Dispose()
	{
		_moveInfos.Dispose();
	}
}