-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprefab_manager.zig
175 lines (141 loc) · 6.25 KB
/
prefab_manager.zig
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
const std = @import("std");
const ecs = @import("zflecs");
const ecsu = @import("flecs_util/flecs_util.zig");
const zm = @import("zmath");
const zmesh = @import("zmesh");
const ztracy = @import("ztracy");
const zwin32 = @import("zwin32");
const fd = @import("config/flecs_data.zig");
const renderer = @import("renderer/renderer.zig");
const util = @import("util.zig");
const IdLocal = @import("core/core.zig").IdLocal;
const assert = std.debug.assert;
const PrefabHashMap = std.AutoHashMap(IdLocal, ecsu.Entity);
const MaterialHashmap = std.AutoHashMap(IdLocal, fd.UberShader);
pub const PrefabManager = struct {
prefab_hash_map: PrefabHashMap,
material_hash_map: MaterialHashmap,
is_a: ecsu.Entity,
rctx: *renderer.Renderer,
pub fn init(rctx: *renderer.Renderer, world: ecsu.World, allocator: std.mem.Allocator) PrefabManager {
return PrefabManager{
.prefab_hash_map = PrefabHashMap.init(allocator),
.material_hash_map = MaterialHashmap.init(allocator),
.is_a = ecsu.Entity.init(world.world, ecs.IsA),
.rctx = rctx,
};
}
pub fn deinit(self: *@This()) void {
self.prefab_hash_map.deinit();
self.material_hash_map.deinit();
}
pub fn createHierarchicalStaticMeshPrefab(self: *@This(), path: [:0]const u8, id: IdLocal, vertex_layout_id: IdLocal, world: ecsu.World) ecsu.Entity {
const existing_prefab = self.prefab_hash_map.get(id);
if (existing_prefab) |prefab| {
return prefab;
}
var entity = world.newPrefab(path);
entity.setOverride(fd.Forward{});
// Set position, rotation and scale
var position = fd.Position.init(0, 0, 0);
var rotation = fd.Rotation{};
var scale = fd.Scale.createScalar(1);
entity.setOverride(position);
entity.setOverride(rotation);
entity.setOverride(scale);
// Set transform
var transform = fd.Transform.initWithQuaternion(rotation.elems().*);
transform.setPos(position.elems().*);
transform.setScale(scale.elems().*);
entity.setOverride(transform);
const hierarchical_static_mesh = self.loadHierarchicalMesh(path, vertex_layout_id);
entity.setOverride(hierarchical_static_mesh);
self.prefab_hash_map.put(id, entity) catch unreachable;
return entity;
}
pub fn loadPrefabFromBinary(self: *@This(), path: [:0]const u8, id: IdLocal, vertex_layout_id: IdLocal, world: ecsu.World) ecsu.Entity {
const existing_prefab = self.prefab_hash_map.get(id);
if (existing_prefab) |prefab| {
return prefab;
}
const mesh_handle = self.rctx.loadMesh(path, vertex_layout_id) catch unreachable;
var entity = world.newPrefab(path);
entity.setOverride(fd.Forward{});
// Set position, rotation and scale
var position = fd.Position.init(0, 0, 0);
var rotation = fd.Rotation{};
var scale = fd.Scale.createScalar(1);
entity.setOverride(position);
entity.setOverride(rotation);
entity.setOverride(scale);
// Set transform
var transform = fd.Transform.initWithQuaternion(rotation.elems().*);
transform.setPos(position.elems().*);
transform.setScale(scale.elems().*);
entity.setOverride(transform);
var static_mesh_component: fd.StaticMesh = undefined;
static_mesh_component.mesh_handle = mesh_handle;
entity.setOverride(static_mesh_component);
self.prefab_hash_map.put(id, entity) catch unreachable;
return entity;
}
pub fn instantiatePrefab(self: @This(), world: ecsu.World, prefab: ecsu.Entity) ecsu.Entity {
const entity = world.newEntity();
entity.addPair(self.is_a, prefab);
return entity;
}
pub fn getPrefab(self: *@This(), id: IdLocal) ?ecsu.Entity {
const existing_prefab = self.prefab_hash_map.get(id);
if (existing_prefab) |prefab| {
return prefab;
}
return null;
}
pub fn storeMaterial(self: *@This(), id: IdLocal, material: fd.UberShader) void {
self.material_hash_map.put(id, material) catch unreachable;
}
pub fn getMaterial(self: *@This(), id: IdLocal) ?fd.UberShader {
const trazy_zone = ztracy.ZoneNC(@src(), "Get Material", 0x00_ff_ff_00);
defer trazy_zone.End();
const existing_material = self.material_hash_map.get(id);
if (existing_material) |material| {
return material;
}
return null;
}
fn loadHierarchicalMesh(self: *@This(), path: [:0]const u8, vertex_layout_id: IdLocal) fd.HierarchicalStaticMesh {
var hierarchical_mesh = std.mem.zeroes(fd.HierarchicalStaticMesh);
// Try to load LODs
for (0..renderer.mesh_lod_max_count) |lod| {
var content_lod_path_buffer: [256]u8 = undefined;
const content_lod_path = std.fmt.bufPrintZ(
content_lod_path_buffer[0..content_lod_path_buffer.len],
"content/{s}_LOD{d}.bin",
.{path, lod},
) catch unreachable;
_ = std.fs.cwd().statFile(content_lod_path) catch |err| switch (err) {
else => continue
};
var lod_path_buffer: [256]u8 = undefined;
const lod_path = std.fmt.bufPrintZ(
lod_path_buffer[0..lod_path_buffer.len],
"{s}_LOD{d}.bin",
.{path, lod},
) catch unreachable;
hierarchical_mesh.static_meshes[hierarchical_mesh.static_mesh_count].mesh_handle = self.rctx.loadMesh(lod_path, vertex_layout_id) catch unreachable;
hierarchical_mesh.static_mesh_count = 1;
}
// Load non-lodded mesh
if (hierarchical_mesh.static_mesh_count == 0) {
var lod_path_buffer: [256]u8 = undefined;
const lod_path = std.fmt.bufPrintZ(
lod_path_buffer[0..lod_path_buffer.len],
"{s}.bin",
.{path},
) catch unreachable;
hierarchical_mesh.static_meshes[hierarchical_mesh.static_mesh_count].mesh_handle = self.rctx.loadMesh(lod_path, vertex_layout_id) catch unreachable;
hierarchical_mesh.static_mesh_count = 1;
}
return hierarchical_mesh;
}
};