-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMainPlayerPipeline.cs
More file actions
90 lines (72 loc) · 3.18 KB
/
MainPlayerPipeline.cs
File metadata and controls
90 lines (72 loc) · 3.18 KB
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
using System;
using DCL.AvatarRendering.AvatarShape.ComputeShader;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Jobs;
namespace DCL.AvatarRendering.AvatarShape.Components
{
/// <summary>
/// Dedicated pipeline for the main player avatar. Scheduled and completed immediately
/// each frame so its TransformAccessArray locks are released before InterpolateCharacterSystem.
/// </summary>
internal class MainPlayerPipeline : IDisposable
{
private readonly int bonesArrayLength;
private readonly Transform[] boneArray;
private bool registered;
private TransformAccessArray bonesTA;
private TransformAccessArray rootTA;
private NativeArray<float4x4> bonesCombined;
private NativeArray<float4x4> avatarMatrix;
private NativeArray<bool> updateFlag;
public BoneMatrixCalculationJob Job;
internal MainPlayerPipeline(int bonesArrayLength)
{
this.bonesArrayLength = bonesArrayLength;
boneArray = new Transform[bonesArrayLength];
bonesCombined = new NativeArray<float4x4>(bonesArrayLength, Allocator.Persistent);
avatarMatrix = new NativeArray<float4x4>(1, Allocator.Persistent);
updateFlag = new NativeArray<bool>(1, Allocator.Persistent);
Job = new BoneMatrixCalculationJob(bonesArrayLength, bonesArrayLength, bonesCombined);
}
public void Register(Transform rootTransform, BoneArray bones, Transform dummyTransform)
{
updateFlag[0] = true;
int actualCount = Mathf.Min(bones.Count, bonesArrayLength);
for (int i = 0; i < actualCount; i++)
boneArray[i] = bones[i];
for (int i = actualCount; i < bonesArrayLength; i++)
boneArray[i] = dummyTransform;
if (bonesTA.isCreated) bonesTA.Dispose();
if (rootTA.isCreated) rootTA.Dispose();
bonesTA = new TransformAccessArray(boneArray);
rootTA = new TransformAccessArray(new[] { rootTransform });
registered = true;
}
public void ScheduleAndComplete()
{
if (!registered)
return;
var boneGather = new BoneGatherJob { BonesCombined = bonesCombined };
var boneGatherHandle = boneGather.Schedule(bonesTA);
var rootGather = new AvatarRootGatherJob { MatrixFromAllAvatars = avatarMatrix };
var rootGatherHandle = rootGather.Schedule(rootTA);
var gatherHandle = JobHandle.CombineDependencies(boneGatherHandle, rootGatherHandle);
Job.AvatarTransform = avatarMatrix;
Job.UpdateAvatar = updateFlag;
var calcHandle = Job.Schedule(1, 1, gatherHandle);
calcHandle.Complete(); // Fast — 1 avatar, 62 bones. Unlocks main player transforms.
}
public void Dispose()
{
bonesCombined.Dispose();
avatarMatrix.Dispose();
updateFlag.Dispose();
Job.Dispose();
if (bonesTA.isCreated) bonesTA.Dispose();
if (rootTA.isCreated) rootTA.Dispose();
}
}
}