forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeRenderingSystem.cs
More file actions
118 lines (101 loc) · 4.68 KB
/
Copy pathVolumeRenderingSystem.cs
File metadata and controls
118 lines (101 loc) · 4.68 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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEditor;
using UnityEngine;
namespace Streaming.SceneManagement.StreamingVolume
{
[WorldSystemFilter(WorldSystemFilterFlags.Default | WorldSystemFilterFlags.Editor)]
public partial struct VolumeRenderingSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Volume>();
}
public void OnUpdate(ref SystemState state)
{
NativeHashSet<int> selectedGameObjectsIds = new NativeHashSet<int>(100, Allocator.TempJob);
#if UNITY_EDITOR
var selectedObjs = Selection.gameObjects;
foreach (var selected in selectedObjs)
{
selectedGameObjectsIds.Add(selected.GetInstanceID());
}
#endif
state.Dependency = new DrawBBJob
{
selectedGOIds = selectedGameObjectsIds,
checkGO = true,
color = Color.yellow
}.ScheduleParallel(state.Dependency);
state.Dependency = new DrawBBSceneJob
{
selectedGOIds = selectedGameObjectsIds,
checkGO = true,
color = Color.green,
localToWorldLookUp = SystemAPI.GetComponentLookup<LocalToWorld>(true),
streamingVolumeLookUp = SystemAPI.GetComponentLookup<Volume>(true),
}.ScheduleParallel(state.Dependency);
selectedGameObjectsIds.Dispose(state.Dependency);
}
public static void DrawAABB(in float3 pos, in float3 min, in float3 max, in Color color)
{
// Draw a bounding box
Debug.DrawLine(pos + new float3(min.x, min.y, min.z), pos + new float3(max.x, min.y, min.z), color);
Debug.DrawLine(pos + new float3(min.x, max.y, min.z), pos + new float3(max.x, max.y, min.z), color);
Debug.DrawLine(pos + new float3(min.x, min.y, min.z), pos + new float3(min.x, max.y, min.z), color);
Debug.DrawLine(pos + new float3(max.x, min.y, min.z), pos + new float3(max.x, max.y, min.z), color);
Debug.DrawLine(pos + new float3(min.x, min.y, max.z), pos + new float3(max.x, min.y, max.z), color);
Debug.DrawLine(pos + new float3(min.x, max.y, max.z), pos + new float3(max.x, max.y, max.z), color);
Debug.DrawLine(pos + new float3(min.x, min.y, max.z), pos + new float3(min.x, max.y, max.z), color);
Debug.DrawLine(pos + new float3(max.x, min.y, max.z), pos + new float3(max.x, max.y, max.z), color);
Debug.DrawLine(pos + new float3(min.x, min.y, min.z), pos + new float3(min.x, min.y, max.z), color);
Debug.DrawLine(pos + new float3(max.x, min.y, min.z), pos + new float3(max.x, min.y, max.z), color);
Debug.DrawLine(pos + new float3(min.x, max.y, min.z), pos + new float3(min.x, max.y, max.z), color);
Debug.DrawLine(pos + new float3(max.x, max.y, min.z), pos + new float3(max.x, max.y, max.z), color);
}
[BurstCompile]
public partial struct DrawBBJob : IJobEntity
{
[ReadOnly] public NativeHashSet<int> selectedGOIds;
public bool checkGO;
public Color color;
public void Execute(in Volume meshBB, in LocalToWorld t, in StreamingGO go)
{
if (checkGO && selectedGOIds.IsCreated && !selectedGOIds.Contains(go.InstanceID))
{
return;
}
var max = meshBB.Scale / 2f;
var min = -max;
DrawAABB(t.Position, min, max, color);
}
}
[BurstCompile]
public partial struct DrawBBSceneJob : IJobEntity
{
[ReadOnly] public NativeHashSet<int> selectedGOIds;
[ReadOnly] public ComponentLookup<Volume> streamingVolumeLookUp;
[ReadOnly] public ComponentLookup<LocalToWorld> localToWorldLookUp;
public bool checkGO;
public Color color;
public void Execute(in DynamicBuffer<VolumeBuffer> volumes, in StreamingGO go)
{
if (checkGO && selectedGOIds.IsCreated && !selectedGOIds.Contains(go.InstanceID))
{
return;
}
foreach (var volume in volumes)
{
var volumeEntity = volume.volumeEntity;
var max = streamingVolumeLookUp[volumeEntity].Scale / 2f;
var min = -max;
DrawAABB(localToWorldLookUp[volumeEntity].Position, min, max, color);
}
}
}
}
}