载入中...
搜索中...
未找到
CubeTypeRegistry.h
浏览该文件的文档.
1#pragma once
2
3// 方块类型注册表:定义有哪些 cube、各面材质、方向性与组合声明。
4// 注册表在创建 world 时传入;网格化阶段用 resolveFaceTex 把“类型 id”解析为
5// 各面“纹理 id”输出给渲染器。空注册表下行为退化为“类型 id == 纹理 id”,
6// 与旧的体素语义完全一致。
7
8#include "voxel/CubeType.h"
9#include "voxel/FaceDir.h"
10
11#include <cstdint>
12#include <string>
13#include <unordered_map>
14#include <vector>
15
16namespace eve::voxel {
17
19public:
20 CubeTypeRegistry() = default;
21
27 uint8_t add(const CubeType &type);
28
37 int loadFromJson(const std::string &json, std::string *error = nullptr);
38
40 const CubeType *find(const std::string &name) const;
42 const CubeType *find(uint8_t id) const;
43
45 uint8_t variantId(const std::string &name, int orientation) const;
46
48 int count() const { return int(byName_.size()); }
50 int variantCount() const { return int(types_.size()) - 1; }
51
52 void clear();
53
55 static const CubeTypeRegistry &empty();
56
57private:
58 std::unordered_map<std::string, uint8_t> byName_; // name -> 0 度变体 id
59 std::vector<CubeType> types_{CubeType{}}; // index 0 = 空气占位
60};
61
66inline uint8_t resolveFaceTex(const CubeTypeRegistry &types, uint8_t id, FaceDir dir) {
67 const CubeType *t = types.find(id);
68 if (!t) return id;
69 return t->faceTex[int(dir)];
70}
71
72} // namespace eve::voxel
std::string type
std::string id
std::string error
const char * name
Definition RockMesh.cpp:21
V3 dir
Definition TreeMesh.cpp:121
int variantCount() const
类型总数(含方向变体,不含空气占位)。
int count() const
命名的方块类型数量(不含方向变体)。
const CubeType * find(const std::string &name) const
按名字返回 0 度变体;未找到返回 nullptr。
static const CubeTypeRegistry & empty()
进程级空注册表(默认参数与向后兼容回退用)。
uint8_t add(const CubeType &type)
注册一个方块类型,返回其基础类型 id(0 保留给空气)。 方向性方块会按 orientation ∈ {0,1,2,3} 绕 Y 轴旋转 faceTex, 展开成 4 个连续的具体类型变体;基础 id...
int loadFromJson(const std::string &json, std::string *error=nullptr)
从 JSON 数组或单对象批量注册,返回成功数量。 元素形如: { "name": "furnace", "faceTex": [4, 4, 4, 4, 5, 4],...
uint8_t variantId(const std::string &name, int orientation) const
名字 + orientation(0..3) 对应的具体类型 id;非方向性类型忽略 orientation。
方块类型定义:名字、各面图集纹理、方向性、组合声明。 方向性方块在注册时按 orientation 绕 Y 轴展开成多个"具体类型"变体, 每个变体持有旋转后的各面纹理;渲染端只消费纹理 id,不接触 ...
Definition Chunk.h:12
uint8_t resolveFaceTex(const CubeTypeRegistry &types, uint8_t id, FaceDir dir)
求某个体素在某面方向上的实际图集纹理 id。 未注册的 id 退化为“所有面 = 原 id”(向后兼容:体素值即纹理 id)。
FaceDir
Six axis-aligned face directions. Each chunk keeps a separate instance buffer per direction so camera...
Definition FaceDir.h:17
uint8_t faceTex[6]
各面图集纹理索引,按 FaceDir 顺序 PosX..NegZ。
Definition CubeType.h:19