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
67 lines (62 loc) · 2.32 KB
/
Copy pathDecorationVisibilitySystem.cs
File metadata and controls
67 lines (62 loc) · 2.32 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
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,
camFwd = 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 camFwd;
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.shouldRender = false;
dec.loaded = false;
dec.mesh.Release();
dec.material.Release();
}
else if (!dec.withinLoadRange && newWithinLoadRange)
{
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 + camFwd * loadRadius) < loadRadius;
}
}
}
}