forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAnimationBakingSystem.cs
More file actions
176 lines (154 loc) · 6.36 KB
/
Copy pathSimpleAnimationBakingSystem.cs
File metadata and controls
176 lines (154 loc) · 6.36 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Hybrid.Baking;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
[TemporaryBakingType]
internal struct DeformationSampleData : IComponentData
{
public UnityObjectRef<DeformationsSampleAuthoring> DeformationSample;
public UnityObjectRef<SkinnedMeshRenderer> SkinnedMeshRenderer;
}
internal class DeformationSampleBaker : Baker<DeformationsSampleAuthoring>
{
public override void Bake(DeformationsSampleAuthoring authoring)
{
var skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>(authoring);
if (skinnedMeshRenderer == null)
return;
var rootTransform = skinnedMeshRenderer.rootBone ? skinnedMeshRenderer.rootBone : skinnedMeshRenderer.transform;
DependsOn(rootTransform);
foreach (var bone in skinnedMeshRenderer.bones)
DependsOn(bone);
DependsOn(skinnedMeshRenderer.sharedMesh);
AddComponent( new DeformationSampleData()
{
DeformationSample = authoring,
SkinnedMeshRenderer = skinnedMeshRenderer
});
}
}
[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
public partial class ComputeSkinMatricesBakingSystem : SystemBase
{
protected override void OnUpdate()
{
var bakingSystem = World.GetExistingSystemManaged<BakingSystem>();
var ecb = new EntityCommandBuffer(Allocator.TempJob);
Entities.ForEach((Entity entity, in DeformationSampleData deformMesh) =>
{
// Only execute this if we have a valid skinning setup
var renderer = deformMesh.SkinnedMeshRenderer.Value;
var bones = renderer.bones;
var hasSkinning = bones.Length > 0 && renderer.sharedMesh.bindposes.Length > 0;
if (hasSkinning)
{
// Setup Reference to root
var rootBone = renderer.rootBone ? renderer.rootBone : renderer.transform;
var rootEntity = bakingSystem.GetEntity(rootBone);
ecb.AddComponent(entity, new RootEntity { Value = rootEntity });
// World to local is required for root space conversion of the SkinMatrices
ecb.AddComponent<WorldToLocal>(rootEntity);
ecb.AddComponent<RootTag>(rootEntity);
var boneEntityArray = ecb.AddBuffer<BoneEntity>(entity);
boneEntityArray.ResizeUninitialized(bones.Length);
for (int boneIndex = 0; boneIndex < bones.Length; ++boneIndex)
{
var bone = renderer.bones[boneIndex];
var boneEntity = bakingSystem.GetEntity(bone);
boneEntityArray[boneIndex] = new BoneEntity { Value = boneEntity };
}
// Store the bindpose for each bone
var bindPoseArray = ecb.AddBuffer<BindPose>(entity);
bindPoseArray.ResizeUninitialized(bones.Length);
for (int boneIndex = 0; boneIndex != bones.Length; ++boneIndex)
{
var bindPose = renderer.sharedMesh.bindposes[boneIndex];
bindPoseArray[boneIndex] = new BindPose { Value = bindPose };
}
// Add tags to the bones so we can find them later
// when computing the SkinMatrices
for (int boneIndex = 0; boneIndex < bones.Length; ++boneIndex)
{
var bone = renderer.bones[boneIndex];
var boneEntity = bakingSystem.GetEntity(bone);
ecb.AddComponent(boneEntity, new BoneTag());
}
}
// Override the material color of the deformation materials
var c = deformMesh.DeformationSample.Value.Color.linear;
var color = new float4(c.r, c.g, c.b, c.a);
var additionalEntities = EntityManager.GetBuffer<AdditionalEntitiesBakingData>(entity);
foreach (var rendererEntity in additionalEntities.AsNativeArray())
{
if (EntityManager.HasComponent<RenderMesh>(rendererEntity.Value))
{
ecb.AddComponent(rendererEntity.Value, new HDRPMaterialPropertyBaseColor { Value = color });
}
}
}).WithEntityQueryOptions(EntityQueryOptions.IncludeDisabledEntities).WithoutBurst().WithStructuralChanges().Run();
ecb.Playback(EntityManager);
ecb.Dispose();
}
}
class AnimatePositionBaker : Baker<AnimatePositionAuthoring>
{
public override void Bake(AnimatePositionAuthoring authoring)
{
var positionAnimation = new AnimatePosition
{
From = authoring.FromPosition,
To = authoring.ToPosition,
Frequency = 1f / authoring.Phase,
PhaseShift = authoring.Offset * authoring.Phase,
};
AddComponent(positionAnimation);
AddComponent<Translation>();
}
}
class AnimateRotationBaker : Baker<AnimateRotationAuthoring>
{
public override void Bake(AnimateRotationAuthoring authoring)
{
var rotationAnimation = new AnimateRotation
{
From = quaternion.Euler(math.radians(authoring.FromRotation)),
To = quaternion.Euler(math.radians(authoring.ToRotation)),
Frequency = 1f / authoring.Phase,
PhaseShift = authoring.Offset * authoring.Phase,
};
AddComponent(rotationAnimation);
AddComponent<Rotation>();
}
}
class AnimateScaleBaker : Baker<AnimateScaleAuthoring>
{
public override void Bake(AnimateScaleAuthoring authoring)
{
var scaleAnimation = new AnimateScale
{
From = authoring.FromScale,
To = authoring.ToScale,
Frequency = 1f / authoring.Phase,
PhaseShift = authoring.Offset * authoring.Phase,
};
AddComponent(scaleAnimation);
AddComponent<NonUniformScale>();
}
}
class AnimateBlendShapeBaker : Baker<AnimateBlendShapeAuthoring>
{
public override void Bake(AnimateBlendShapeAuthoring authoring)
{
var blendshapeAnimation = new AnimateBlendShape
{
From = authoring.FromWeight,
To = authoring.ToWeight,
Frequency = 1f / authoring.Phase,
PhaseShift = authoring.Offset * authoring.Phase,
};
AddComponent(blendshapeAnimation);
}
}