forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecorationVisibilitySystem.cs
More file actions
69 lines (64 loc) · 2.42 KB
/
Copy pathDecorationVisibilitySystem.cs
File metadata and controls
69 lines (64 loc) · 2.42 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
using Unity.Burst;
using Unity.Entities;
using Unity.Entities.Content;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
namespace Streaming.RuntimeContentManager
{
// Creates jobs that compute visibility of the entities
[WorldSystemFilter(WorldSystemFilterFlags.Default | WorldSystemFilterFlags.Editor)]
[UpdateInGroup(typeof(SimulationSystemGroup))]
[RequireMatchingQueriesForUpdate]
public partial struct DecorationVisibilitySystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
new DecorationVisibilityJob
{
CamPos = Camera.main.transform.position,
LoadRadius = Camera.main.farClipPlane,
CamForward = Camera.main.transform.forward
}.ScheduleParallel();
}
}
// Job to compute the visibility of an entity and trigger loading and unloading
[BurstCompile]
partial struct DecorationVisibilityJob : IJobEntity
{
public float LoadRadius;
public float3 CamPos;
public float3 CamForward;
void Execute(ref DecorationVisualComponentData dec, in LocalToWorld transform)
{
// "in view" just means within distance in this sample.
var distToCamera = math.distance(transform.Position, CamPos);
var newWithinLoadRange = distToCamera < LoadRadius;
if (dec.withinLoadRange && !newWithinLoadRange)
{
dec.withinLoadRange = false;
dec.shouldRender = false;
dec.loaded = false;
dec.mesh.Release();
dec.material.Release();
}
else if (!dec.withinLoadRange && newWithinLoadRange)
{
dec.withinLoadRange = true;
dec.mesh.LoadAsync();
dec.material.LoadAsync();
}
dec.withinLoadRange = newWithinLoadRange;
if (newWithinLoadRange)
{
if (!dec.loaded)
{
dec.loaded = dec.material.LoadingStatus >= ObjectLoadingStatus.Completed &&
dec.mesh.LoadingStatus >= ObjectLoadingStatus.Completed;
}
dec.shouldRender = distToCamera < LoadRadius * .25f ||
math.distance(transform.Position, CamPos + CamForward * LoadRadius) < LoadRadius;
}
}
}
}