Added vox scene importer. No options at the moments.
This commit is contained in:
parent
059f490669
commit
9a74362aca
1
SCsub
1
SCsub
@ -59,6 +59,7 @@ if env["tools"]:
|
||||
"editor/terrain/*.cpp",
|
||||
"editor/fast_noise_lite/*.cpp",
|
||||
"editor/instance_library/*.cpp",
|
||||
"editor/vox/*.cpp",
|
||||
]
|
||||
voxel_files += voxel_editor_files
|
||||
|
||||
|
9
editor/vox/vox_editor_plugin.cpp
Normal file
9
editor/vox/vox_editor_plugin.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "vox_editor_plugin.h"
|
||||
#include "vox_importer.h"
|
||||
|
||||
VoxEditorPlugin::VoxEditorPlugin(EditorNode *p_node) {
|
||||
Ref<VoxelVoxImporter> vox_importer;
|
||||
vox_importer.instance();
|
||||
//add_import_plugin(vox_importer);
|
||||
ResourceFormatImporter::get_singleton()->add_importer(vox_importer);
|
||||
}
|
12
editor/vox/vox_editor_plugin.h
Normal file
12
editor/vox/vox_editor_plugin.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef VOX_EDITOR_PLUGIN_H
|
||||
#define VOX_EDITOR_PLUGIN_H
|
||||
|
||||
#include <editor/editor_plugin.h>
|
||||
|
||||
class VoxEditorPlugin : public EditorPlugin {
|
||||
GDCLASS(VoxEditorPlugin, EditorPlugin)
|
||||
public:
|
||||
VoxEditorPlugin(EditorNode *p_node);
|
||||
};
|
||||
|
||||
#endif // VOX_EDITOR_PLUGIN_H
|
209
editor/vox/vox_importer.cpp
Normal file
209
editor/vox/vox_importer.cpp
Normal file
@ -0,0 +1,209 @@
|
||||
#include "vox_importer.h"
|
||||
#include "../../meshers/cubes/voxel_mesher_cubes.h"
|
||||
#include "../../storage/voxel_buffer.h"
|
||||
#include "../../streams/vox_data.h"
|
||||
|
||||
#include <scene/3d/mesh_instance.h>
|
||||
#include <scene/3d/spatial.h>
|
||||
#include <scene/resources/mesh.h>
|
||||
#include <scene/resources/packed_scene.h>
|
||||
|
||||
String VoxelVoxImporter::get_importer_name() const {
|
||||
return "VoxelVoxImporter";
|
||||
}
|
||||
|
||||
String VoxelVoxImporter::get_visible_name() const {
|
||||
return "VoxelVoxImporter";
|
||||
}
|
||||
|
||||
void VoxelVoxImporter::get_recognized_extensions(List<String> *p_extensions) const {
|
||||
p_extensions->push_back("vox");
|
||||
}
|
||||
|
||||
String VoxelVoxImporter::get_preset_name(int p_idx) const {
|
||||
return "Default";
|
||||
}
|
||||
|
||||
int VoxelVoxImporter::get_preset_count() const {
|
||||
return 1;
|
||||
}
|
||||
|
||||
String VoxelVoxImporter::get_save_extension() const {
|
||||
return "tscn";
|
||||
}
|
||||
|
||||
String VoxelVoxImporter::get_resource_type() const {
|
||||
return "PackedScene";
|
||||
}
|
||||
|
||||
// float VoxelVoxImporter::get_priority() const {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// int VoxelVoxImporter::get_import_order() const {
|
||||
// }
|
||||
|
||||
void VoxelVoxImporter::get_import_options(List<ImportOption> *r_options, int p_preset) const {
|
||||
}
|
||||
|
||||
bool VoxelVoxImporter::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
static void add_mesh_instance(Ref<Mesh> mesh, Node *parent, Node *owner, Vector3 offset) {
|
||||
MeshInstance *mesh_instance = memnew(MeshInstance);
|
||||
mesh_instance->set_mesh(mesh);
|
||||
parent->add_child(mesh_instance);
|
||||
mesh_instance->set_owner(owner);
|
||||
mesh_instance->set_translation(offset);
|
||||
// TODO Colliders? Needs conventions or attributes probably
|
||||
}
|
||||
|
||||
struct VoxMesh {
|
||||
Ref<Mesh> mesh;
|
||||
Vector3 pivot;
|
||||
};
|
||||
|
||||
static Error process_scene_node_recursively(const vox::Data &data, int node_id, Spatial *parent_node,
|
||||
Spatial *&root_node, int depth, const Vector<VoxMesh> &meshes) {
|
||||
//
|
||||
ERR_FAIL_COND_V(depth > 10, ERR_INVALID_DATA);
|
||||
const vox::Node *vox_node = data.get_node(node_id);
|
||||
|
||||
switch (vox_node->type) {
|
||||
case vox::Node::TYPE_TRANSFORM: {
|
||||
Spatial *node = memnew(Spatial);
|
||||
if (root_node == nullptr) {
|
||||
root_node = node;
|
||||
} else {
|
||||
ERR_FAIL_COND_V(parent_node == nullptr, ERR_BUG);
|
||||
parent_node->add_child(node);
|
||||
node->set_owner(root_node);
|
||||
}
|
||||
const vox::TransformNode *vox_transform_node = reinterpret_cast<const vox::TransformNode *>(vox_node);
|
||||
node->set_transform(Transform(vox_transform_node->rotation.basis, vox_transform_node->position.to_vec3()));
|
||||
process_scene_node_recursively(data, vox_transform_node->child_node_id, node, root_node, depth + 1, meshes);
|
||||
} break;
|
||||
|
||||
case vox::Node::TYPE_GROUP: {
|
||||
const vox::GroupNode *vox_group_node = reinterpret_cast<const vox::GroupNode *>(vox_node);
|
||||
for (unsigned int i = 0; i < vox_group_node->child_node_ids.size(); ++i) {
|
||||
const int child_node_id = vox_group_node->child_node_ids[i];
|
||||
process_scene_node_recursively(data, child_node_id, parent_node, root_node, depth + 1, meshes);
|
||||
}
|
||||
} break;
|
||||
|
||||
case vox::Node::TYPE_SHAPE: {
|
||||
ERR_FAIL_COND_V(parent_node == nullptr, ERR_BUG);
|
||||
ERR_FAIL_COND_V(root_node == nullptr, ERR_BUG);
|
||||
const vox::ShapeNode *vox_shape_node = reinterpret_cast<const vox::ShapeNode *>(vox_node);
|
||||
const VoxMesh &mesh_data = meshes[vox_shape_node->model_id];
|
||||
ERR_FAIL_COND_V(mesh_data.mesh.is_null(), ERR_BUG);
|
||||
const Vector3 offset = -mesh_data.pivot;
|
||||
add_mesh_instance(mesh_data.mesh, parent_node, root_node, offset);
|
||||
} break;
|
||||
|
||||
default:
|
||||
ERR_FAIL_V(ERR_INVALID_DATA);
|
||||
break;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error VoxelVoxImporter::import(const String &p_source_file, const String &p_save_path,
|
||||
const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files,
|
||||
Variant *r_metadata) {
|
||||
//
|
||||
vox::Data data;
|
||||
const Error load_err = data.load_from_file(p_source_file);
|
||||
ERR_FAIL_COND_V(load_err != OK, load_err);
|
||||
|
||||
Vector<VoxMesh> meshes;
|
||||
meshes.resize(data.get_model_count());
|
||||
|
||||
// Get color palette
|
||||
Ref<VoxelColorPalette> palette;
|
||||
palette.instance();
|
||||
for (unsigned int i = 0; i < data.get_palette().size(); ++i) {
|
||||
Color8 color = data.get_palette()[i];
|
||||
palette->set_color8(i, color);
|
||||
}
|
||||
|
||||
Ref<VoxelMesherCubes> mesher;
|
||||
mesher.instance();
|
||||
mesher->set_color_mode(VoxelMesherCubes::COLOR_MESHER_PALETTE);
|
||||
mesher->set_palette(palette);
|
||||
mesher->set_greedy_meshing_enabled(true);
|
||||
|
||||
Ref<SpatialMaterial> material;
|
||||
material.instance();
|
||||
material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
|
||||
material->set_roughness(1.f);
|
||||
|
||||
Ref<SpatialMaterial> material2;
|
||||
material2.instance();
|
||||
material2->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
|
||||
material2->set_roughness(1.f);
|
||||
|
||||
Array materials_array;
|
||||
materials_array.push_back(material);
|
||||
materials_array.push_back(material2);
|
||||
|
||||
// Build meshes from voxel models
|
||||
for (unsigned int i = 0; i < data.get_model_count(); ++i) {
|
||||
const vox::Model &model = data.get_model(i);
|
||||
|
||||
Ref<VoxelBuffer> voxels;
|
||||
voxels.instance();
|
||||
voxels->create(model.size + Vector3i(VoxelMesherCubes::PADDING * 2));
|
||||
voxels->decompress_channel(VoxelBuffer::CHANNEL_COLOR);
|
||||
|
||||
Span<uint8_t> dst_color_indices;
|
||||
ERR_FAIL_COND_V(!voxels->get_channel_raw(VoxelBuffer::CHANNEL_COLOR, dst_color_indices), ERR_BUG);
|
||||
Span<const uint8_t> src_color_indices = to_span_const(model.color_indexes);
|
||||
copy_3d_region_zxy(dst_color_indices, voxels->get_size(), Vector3i(VoxelMesherCubes::PADDING),
|
||||
src_color_indices, model.size, Vector3i(), model.size);
|
||||
|
||||
Ref<Mesh> mesh = mesher->build_mesh(voxels, materials_array);
|
||||
VoxMesh mesh_info;
|
||||
mesh_info.mesh = mesh;
|
||||
// In MagicaVoxel scene graph, pivots are at the center of models, not at the lower corner.
|
||||
// TODO I don't know if this is correct, but I could not find a reference saying how that pivot should be calculated
|
||||
mesh_info.pivot = (voxels->get_size() / 2 - Vector3i(1)).to_vec3();
|
||||
meshes.write[i] = mesh_info;
|
||||
}
|
||||
|
||||
Spatial *root_node = nullptr;
|
||||
if (data.get_root_node_id() != -1) {
|
||||
// Convert scene graph into a node tree
|
||||
process_scene_node_recursively(data, data.get_root_node_id(), nullptr, root_node, 0, meshes);
|
||||
ERR_FAIL_COND_V(root_node == nullptr, ERR_INVALID_DATA);
|
||||
|
||||
} else if (meshes.size() > 0) {
|
||||
// Some vox files don't have a scene graph
|
||||
root_node = memnew(Spatial);
|
||||
const VoxMesh &mesh0 = meshes[0];
|
||||
add_mesh_instance(mesh0.mesh, root_node, root_node, Vector3());
|
||||
}
|
||||
|
||||
// Save meshes
|
||||
for (int i = 0; i < meshes.size(); ++i) {
|
||||
Ref<Mesh> mesh = meshes[i].mesh;
|
||||
String res_save_path = String("{0}.model{1}.mesh").format(varray(p_save_path, i));
|
||||
ResourceSaver::save(res_save_path, mesh);
|
||||
}
|
||||
|
||||
root_node->set_name(p_save_path.get_file().get_basename());
|
||||
|
||||
// Save scene
|
||||
Ref<PackedScene> scene;
|
||||
scene.instance();
|
||||
scene->pack(root_node);
|
||||
String scene_save_path = p_save_path + ".tscn";
|
||||
Error save_err = ResourceSaver::save(scene_save_path, scene);
|
||||
memdelete(root_node);
|
||||
ERR_FAIL_COND_V_MSG(save_err != OK, save_err, "Cannot save scene to file '" + scene_save_path);
|
||||
|
||||
return OK;
|
||||
}
|
25
editor/vox/vox_importer.h
Normal file
25
editor/vox/vox_importer.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef VOX_IMPORTER_H
|
||||
#define VOX_IMPORTER_H
|
||||
|
||||
#include <editor/import/editor_import_plugin.h>
|
||||
|
||||
class VoxelVoxImporter : public ResourceImporter {
|
||||
GDCLASS(VoxelVoxImporter, ResourceImporter)
|
||||
public:
|
||||
String get_importer_name() const override;
|
||||
String get_visible_name() const override;
|
||||
void get_recognized_extensions(List<String> *p_extensions) const override;
|
||||
String get_preset_name(int p_idx) const override;
|
||||
int get_preset_count() const override;
|
||||
String get_save_extension() const override;
|
||||
String get_resource_type() const override;
|
||||
//float get_priority() const override;
|
||||
//int get_import_order() const override;
|
||||
void get_import_options(List<ImportOption> *r_options, int p_preset = 0) const override;
|
||||
bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const override;
|
||||
|
||||
Error import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options,
|
||||
List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata = nullptr) override;
|
||||
};
|
||||
|
||||
#endif // VOX_IMPORTER_H
|
@ -3,11 +3,6 @@
|
||||
#include "edition/voxel_tool_buffer.h"
|
||||
#include "edition/voxel_tool_lod_terrain.h"
|
||||
#include "edition/voxel_tool_terrain.h"
|
||||
#include "editor/editor_plugin.h"
|
||||
#include "editor/fast_noise_lite/fast_noise_lite_editor_plugin.h"
|
||||
#include "editor/graph/voxel_graph_editor_plugin.h"
|
||||
#include "editor/instance_library/voxel_instance_library_editor_plugin.h"
|
||||
#include "editor/terrain/voxel_terrain_editor_plugin.h"
|
||||
#include "generators/graph/voxel_generator_graph.h"
|
||||
#include "generators/graph/voxel_graph_node_db.h"
|
||||
#include "generators/simple/voxel_generator_flat.h"
|
||||
@ -48,6 +43,12 @@
|
||||
#include <core/engine.h>
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
#include "editor/editor_plugin.h"
|
||||
#include "editor/fast_noise_lite/fast_noise_lite_editor_plugin.h"
|
||||
#include "editor/graph/voxel_graph_editor_plugin.h"
|
||||
#include "editor/instance_library/voxel_instance_library_editor_plugin.h"
|
||||
#include "editor/terrain/voxel_terrain_editor_plugin.h"
|
||||
#include "editor/vox/vox_editor_plugin.h"
|
||||
#include "editor/voxel_debug.h"
|
||||
#endif
|
||||
|
||||
@ -147,6 +148,7 @@ void register_voxel_types() {
|
||||
EditorPlugins::add_by_type<VoxelTerrainEditorPlugin>();
|
||||
EditorPlugins::add_by_type<VoxelInstanceLibraryEditorPlugin>();
|
||||
EditorPlugins::add_by_type<FastNoiseLiteEditorPlugin>();
|
||||
EditorPlugins::add_by_type<VoxEditorPlugin>();
|
||||
#endif
|
||||
|
||||
#ifdef VOXEL_RUN_TESTS
|
||||
|
583
streams/vox_data.cpp
Normal file
583
streams/vox_data.cpp
Normal file
@ -0,0 +1,583 @@
|
||||
#include "vox_data.h"
|
||||
#include "../util/macros.h"
|
||||
|
||||
#include <core/os/file_access.h>
|
||||
#include <core/variant.h>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace vox {
|
||||
|
||||
const uint32_t PALETTE_SIZE = 256;
|
||||
|
||||
uint32_t g_default_palette[PALETTE_SIZE] = {
|
||||
0x00000000, 0xffffffff, 0xffccffff, 0xff99ffff, 0xff66ffff, 0xff33ffff, 0xff00ffff, 0xffffccff,
|
||||
0xffccccff, 0xff99ccff, 0xff66ccff, 0xff33ccff, 0xff00ccff, 0xffff99ff, 0xffcc99ff, 0xff9999ff,
|
||||
0xff6699ff, 0xff3399ff, 0xff0099ff, 0xffff66ff, 0xffcc66ff, 0xff9966ff, 0xff6666ff, 0xff3366ff,
|
||||
0xff0066ff, 0xffff33ff, 0xffcc33ff, 0xff9933ff, 0xff6633ff, 0xff3333ff, 0xff0033ff, 0xffff00ff,
|
||||
0xffcc00ff, 0xff9900ff, 0xff6600ff, 0xff3300ff, 0xff0000ff, 0xffffffcc, 0xffccffcc, 0xff99ffcc,
|
||||
0xff66ffcc, 0xff33ffcc, 0xff00ffcc, 0xffffcccc, 0xffcccccc, 0xff99cccc, 0xff66cccc, 0xff33cccc,
|
||||
0xff00cccc, 0xffff99cc, 0xffcc99cc, 0xff9999cc, 0xff6699cc, 0xff3399cc, 0xff0099cc, 0xffff66cc,
|
||||
0xffcc66cc, 0xff9966cc, 0xff6666cc, 0xff3366cc, 0xff0066cc, 0xffff33cc, 0xffcc33cc, 0xff9933cc,
|
||||
0xff6633cc, 0xff3333cc, 0xff0033cc, 0xffff00cc, 0xffcc00cc, 0xff9900cc, 0xff6600cc, 0xff3300cc,
|
||||
0xff0000cc, 0xffffff99, 0xffccff99, 0xff99ff99, 0xff66ff99, 0xff33ff99, 0xff00ff99, 0xffffcc99,
|
||||
0xffcccc99, 0xff99cc99, 0xff66cc99, 0xff33cc99, 0xff00cc99, 0xffff9999, 0xffcc9999, 0xff999999,
|
||||
0xff669999, 0xff339999, 0xff009999, 0xffff6699, 0xffcc6699, 0xff996699, 0xff666699, 0xff336699,
|
||||
0xff006699, 0xffff3399, 0xffcc3399, 0xff993399, 0xff663399, 0xff333399, 0xff003399, 0xffff0099,
|
||||
0xffcc0099, 0xff990099, 0xff660099, 0xff330099, 0xff000099, 0xffffff66, 0xffccff66, 0xff99ff66,
|
||||
0xff66ff66, 0xff33ff66, 0xff00ff66, 0xffffcc66, 0xffcccc66, 0xff99cc66, 0xff66cc66, 0xff33cc66,
|
||||
0xff00cc66, 0xffff9966, 0xffcc9966, 0xff999966, 0xff669966, 0xff339966, 0xff009966, 0xffff6666,
|
||||
0xffcc6666, 0xff996666, 0xff666666, 0xff336666, 0xff006666, 0xffff3366, 0xffcc3366, 0xff993366,
|
||||
0xff663366, 0xff333366, 0xff003366, 0xffff0066, 0xffcc0066, 0xff990066, 0xff660066, 0xff330066,
|
||||
0xff000066, 0xffffff33, 0xffccff33, 0xff99ff33, 0xff66ff33, 0xff33ff33, 0xff00ff33, 0xffffcc33,
|
||||
0xffcccc33, 0xff99cc33, 0xff66cc33, 0xff33cc33, 0xff00cc33, 0xffff9933, 0xffcc9933, 0xff999933,
|
||||
0xff669933, 0xff339933, 0xff009933, 0xffff6633, 0xffcc6633, 0xff996633, 0xff666633, 0xff336633,
|
||||
0xff006633, 0xffff3333, 0xffcc3333, 0xff993333, 0xff663333, 0xff333333, 0xff003333, 0xffff0033,
|
||||
0xffcc0033, 0xff990033, 0xff660033, 0xff330033, 0xff000033, 0xffffff00, 0xffccff00, 0xff99ff00,
|
||||
0xff66ff00, 0xff33ff00, 0xff00ff00, 0xffffcc00, 0xffcccc00, 0xff99cc00, 0xff66cc00, 0xff33cc00,
|
||||
0xff00cc00, 0xffff9900, 0xffcc9900, 0xff999900, 0xff669900, 0xff339900, 0xff009900, 0xffff6600,
|
||||
0xffcc6600, 0xff996600, 0xff666600, 0xff336600, 0xff006600, 0xffff3300, 0xffcc3300, 0xff993300,
|
||||
0xff663300, 0xff333300, 0xff003300, 0xffff0000, 0xffcc0000, 0xff990000, 0xff660000, 0xff330000,
|
||||
0xff0000ee, 0xff0000dd, 0xff0000bb, 0xff0000aa, 0xff000088, 0xff000077, 0xff000055, 0xff000044,
|
||||
0xff000022, 0xff000011, 0xff00ee00, 0xff00dd00, 0xff00bb00, 0xff00aa00, 0xff008800, 0xff007700,
|
||||
0xff005500, 0xff004400, 0xff002200, 0xff001100, 0xffee0000, 0xffdd0000, 0xffbb0000, 0xffaa0000,
|
||||
0xff880000, 0xff770000, 0xff550000, 0xff440000, 0xff220000, 0xff110000, 0xffeeeeee, 0xffdddddd,
|
||||
0xffbbbbbb, 0xffaaaaaa, 0xff888888, 0xff777777, 0xff555555, 0xff444444, 0xff222222, 0xff111111
|
||||
};
|
||||
|
||||
static Error parse_string(FileAccess &f, String &s) {
|
||||
const int size = f.get_32();
|
||||
|
||||
// Sanity checks
|
||||
ERR_FAIL_COND_V(size < 0, ERR_INVALID_DATA);
|
||||
ERR_FAIL_COND_V(size > 4096, ERR_INVALID_DATA);
|
||||
|
||||
static thread_local std::vector<char> bytes;
|
||||
bytes.resize(size);
|
||||
ERR_FAIL_COND_V(f.get_buffer((uint8_t *)bytes.data(), bytes.size()) != bytes.size(), ERR_PARSE_ERROR);
|
||||
|
||||
s.clear();
|
||||
ERR_FAIL_COND_V(s.parse_utf8(bytes.data(), bytes.size()), ERR_PARSE_ERROR);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
static Error parse_dictionary(FileAccess &f, std::unordered_map<String, String> &dict) {
|
||||
const int item_count = f.get_32();
|
||||
|
||||
// Sanity checks
|
||||
ERR_FAIL_COND_V(item_count < 0, ERR_INVALID_DATA);
|
||||
ERR_FAIL_COND_V(item_count > 256, ERR_INVALID_DATA);
|
||||
|
||||
dict.clear();
|
||||
|
||||
for (int i = 0; i < item_count; ++i) {
|
||||
String key;
|
||||
Error key_err = parse_string(f, key);
|
||||
ERR_FAIL_COND_V(key_err != OK, key_err);
|
||||
|
||||
String value;
|
||||
Error value_err = parse_string(f, value);
|
||||
ERR_FAIL_COND_V(value_err != OK, value_err);
|
||||
|
||||
dict[key] = value;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
// MagicaVoxel uses a Z-up coordinate system similar to 3DS Max.
|
||||
// Here we read the data such that it follows OpenGL coordinate system.
|
||||
//
|
||||
// Z Y
|
||||
// | Y | X
|
||||
// |/ |/
|
||||
// o----X o----Z
|
||||
//
|
||||
// MagicaVoxel OpenGL
|
||||
//
|
||||
inline Vector3i magica_to_opengl(const Vector3i &src) {
|
||||
Vector3i dst;
|
||||
dst.x = src.y;
|
||||
dst.y = src.z;
|
||||
dst.z = src.x;
|
||||
return dst;
|
||||
}
|
||||
|
||||
static Basis parse_basis(uint8_t data) {
|
||||
// bits 0 and 1 are the index of the non-zero entry in the first row
|
||||
const int xi = (data >> 0) & 0x03;
|
||||
// bits 2 and 3 are the index of the non-zero entry in the second row
|
||||
const int yi = (data >> 2) & 0x03;
|
||||
// The index of the non-zero entry in the last row can be deduced as the last not "occupied" index
|
||||
bool occupied[3] = { false };
|
||||
occupied[xi] = true;
|
||||
occupied[yi] = true;
|
||||
const int zi = occupied[0] == false ? 0 : occupied[1] == false ? 1 : 2;
|
||||
|
||||
const int x_sign = ((data >> 4) & 0x01) == 0 ? 1 : -1;
|
||||
const int y_sign = ((data >> 5) & 0x01) == 0 ? 1 : -1;
|
||||
const int z_sign = ((data >> 6) & 0x01) == 0 ? 1 : -1;
|
||||
|
||||
Vector3i x, y, z;
|
||||
x[xi] = x_sign;
|
||||
y[yi] = y_sign;
|
||||
z[zi] = z_sign;
|
||||
|
||||
// TODO Not sure yet if that also works
|
||||
x = magica_to_opengl(x);
|
||||
y = magica_to_opengl(y);
|
||||
z = magica_to_opengl(z);
|
||||
|
||||
Basis b;
|
||||
b.set(
|
||||
x.x, x.y, x.z,
|
||||
y.x, y.y, y.z,
|
||||
z.x, z.y, z.z);
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
Error parse_node_common_header(Node &node, FileAccess &f,
|
||||
const std::unordered_map<int, std::unique_ptr<Node>> &scene_graph) {
|
||||
//
|
||||
const int node_id = f.get_32();
|
||||
ERR_FAIL_COND_V_MSG(scene_graph.find(node_id) != scene_graph.end(), ERR_INVALID_DATA,
|
||||
String("Node with ID {0} already exists").format(varray(node_id)));
|
||||
|
||||
node.id = node_id;
|
||||
|
||||
const Error attributes_err = parse_dictionary(f, node.attributes);
|
||||
ERR_FAIL_COND_V(attributes_err != OK, attributes_err);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Data::clear() {
|
||||
_models.clear();
|
||||
_scene_graph.clear();
|
||||
_layers.clear();
|
||||
_root_node_id = -1;
|
||||
}
|
||||
|
||||
Error Data::load_from_file(String fpath) {
|
||||
const Error err = _load_from_file(fpath);
|
||||
if (err != OK) {
|
||||
clear();
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
Error Data::_load_from_file(String fpath) {
|
||||
// https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox.txt
|
||||
// https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox-extension.txt
|
||||
|
||||
PRINT_VERBOSE(String("Loading ") + fpath);
|
||||
|
||||
Error open_err;
|
||||
FileAccessRef f_ref = FileAccess::open(fpath, FileAccess::READ, &open_err);
|
||||
if (f_ref == nullptr) {
|
||||
return open_err;
|
||||
}
|
||||
FileAccess &f = *f_ref;
|
||||
|
||||
char magic[5] = { 0 };
|
||||
ERR_FAIL_COND_V(f.get_buffer((uint8_t *)magic, 4) != 4, ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(strcmp(magic, "VOX ") != 0, ERR_PARSE_ERROR);
|
||||
|
||||
const uint32_t version = f.get_32();
|
||||
ERR_FAIL_COND_V(version != 150, ERR_PARSE_ERROR);
|
||||
|
||||
const size_t file_length = f.get_len();
|
||||
|
||||
Vector3i last_size;
|
||||
|
||||
clear();
|
||||
|
||||
while (f.get_position() < file_length) {
|
||||
char chunk_id[5] = { 0 };
|
||||
ERR_FAIL_COND_V(f.get_buffer((uint8_t *)chunk_id, 4) != 4, ERR_PARSE_ERROR);
|
||||
|
||||
const uint32_t chunk_size = f.get_32();
|
||||
f.get_32(); // child_chunks_size
|
||||
|
||||
PRINT_VERBOSE(String("Reading chunk {0} at {1}, size={2}")
|
||||
.format(varray(chunk_id, f.get_position(), chunk_size)));
|
||||
|
||||
if (strcmp(chunk_id, "SIZE") == 0) {
|
||||
Vector3i size;
|
||||
size.x = f.get_32();
|
||||
size.y = f.get_32();
|
||||
size.z = f.get_32();
|
||||
ERR_FAIL_COND_V(size.x > 0xff, ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(size.y > 0xff, ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(size.z > 0xff, ERR_PARSE_ERROR);
|
||||
last_size = magica_to_opengl(size);
|
||||
|
||||
} else if (strcmp(chunk_id, "XYZI") == 0) {
|
||||
std::unique_ptr<Model> model = std::make_unique<Model>();
|
||||
model->color_indexes.resize(last_size.x * last_size.y * last_size.z, 0);
|
||||
model->size = last_size;
|
||||
|
||||
const uint32_t num_voxels = f.get_32();
|
||||
|
||||
for (uint32_t i = 0; i < num_voxels; ++i) {
|
||||
Vector3i pos;
|
||||
pos.x = f.get_8();
|
||||
pos.y = f.get_8();
|
||||
pos.z = f.get_8();
|
||||
const uint32_t c = f.get_8();
|
||||
pos = magica_to_opengl(pos);
|
||||
ERR_FAIL_COND_V(pos.x >= model->size.x || pos.x < 0, ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(pos.y >= model->size.y || pos.y < 0, ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(pos.z >= model->size.z || pos.z < 0, ERR_PARSE_ERROR);
|
||||
model->color_indexes[pos.get_zxy_index(model->size)] = c;
|
||||
}
|
||||
|
||||
_models.push_back(std::move(model));
|
||||
|
||||
} else if (strcmp(chunk_id, "RGBA") == 0) {
|
||||
_palette[0] = Color8{ 0, 0, 0, 0 };
|
||||
for (uint32_t i = 1; i < _palette.size(); ++i) {
|
||||
Color8 c;
|
||||
c.r = f.get_8();
|
||||
c.g = f.get_8();
|
||||
c.b = f.get_8();
|
||||
c.a = f.get_8();
|
||||
_palette[i] = c;
|
||||
}
|
||||
f.get_32();
|
||||
|
||||
} else if (strcmp(chunk_id, "nTRN") == 0) {
|
||||
std::unique_ptr<TransformNode> node_ptr = std::make_unique<TransformNode>();
|
||||
TransformNode &node = *node_ptr;
|
||||
|
||||
Error header_err = parse_node_common_header(node, f, _scene_graph);
|
||||
ERR_FAIL_COND_V(header_err != OK, header_err);
|
||||
|
||||
auto name_it = node.attributes.find("_name");
|
||||
if (name_it != node.attributes.end()) {
|
||||
node.name = name_it->second;
|
||||
}
|
||||
|
||||
auto hidden_it = node.attributes.find("_hidden");
|
||||
if (hidden_it != node.attributes.end()) {
|
||||
node.hidden = (hidden_it->second == "1");
|
||||
} else {
|
||||
node.hidden = false;
|
||||
}
|
||||
|
||||
node.child_node_id = f.get_32();
|
||||
|
||||
const int reserved_id = f.get_32();
|
||||
ERR_FAIL_COND_V(reserved_id != -1, ERR_INVALID_DATA);
|
||||
|
||||
node.layer_id = f.get_32();
|
||||
|
||||
const int frame_count = f.get_32();
|
||||
ERR_FAIL_COND_V(frame_count != 1, ERR_INVALID_DATA);
|
||||
|
||||
//for (int frame_index = 0; frame_index < frame_count; ++frame_index) {
|
||||
|
||||
std::unordered_map<String, String> frame;
|
||||
const Error frame_err = parse_dictionary(f, frame);
|
||||
ERR_FAIL_COND_V(frame_err != OK, frame_err);
|
||||
|
||||
auto r_it = frame.find("_r");
|
||||
if (r_it != frame.end()) {
|
||||
Rotation rot;
|
||||
// TODO Is it really an integer formatted as text?
|
||||
rot.data = r_it->second.to_int();
|
||||
rot.basis = parse_basis(rot.data);
|
||||
node.rotation = rot;
|
||||
}
|
||||
|
||||
auto t_it = frame.find("_t");
|
||||
if (t_it != frame.end()) {
|
||||
// It is 3 integers formatted as text
|
||||
Vector<float> coords = t_it->second.split_floats(" ");
|
||||
ERR_FAIL_COND_V(coords.size() < 3, ERR_PARSE_ERROR);
|
||||
node.position = magica_to_opengl(Vector3i(coords[0], coords[1], coords[2]));
|
||||
}
|
||||
|
||||
//}
|
||||
|
||||
_scene_graph[node.id] = std::move(node_ptr);
|
||||
|
||||
} else if (strcmp(chunk_id, "nGRP") == 0) {
|
||||
std::unique_ptr<GroupNode> node_ptr = std::make_unique<GroupNode>();
|
||||
GroupNode &node = *node_ptr;
|
||||
|
||||
Error header_err = parse_node_common_header(node, f, _scene_graph);
|
||||
ERR_FAIL_COND_V(header_err != OK, header_err);
|
||||
|
||||
const unsigned int child_count = f.get_32();
|
||||
// Sanity check
|
||||
ERR_FAIL_COND_V(child_count > 65536, ERR_INVALID_DATA);
|
||||
node.child_node_ids.resize(child_count);
|
||||
|
||||
for (unsigned int i = 0; i < child_count; ++i) {
|
||||
node.child_node_ids[i] = f.get_32();
|
||||
}
|
||||
|
||||
_scene_graph[node.id] = std::move(node_ptr);
|
||||
|
||||
} else if (strcmp(chunk_id, "nSHP") == 0) {
|
||||
std::unique_ptr<ShapeNode> node_ptr = std::make_unique<ShapeNode>();
|
||||
ShapeNode &node = *node_ptr;
|
||||
|
||||
Error header_err = parse_node_common_header(node, f, _scene_graph);
|
||||
ERR_FAIL_COND_V(header_err != OK, header_err);
|
||||
|
||||
const unsigned int model_count = f.get_32();
|
||||
ERR_FAIL_COND_V(model_count != 1, ERR_INVALID_DATA);
|
||||
|
||||
//for (unsigned int i = 0; i < model_count; ++i) {
|
||||
|
||||
node.model_id = f.get_32();
|
||||
ERR_FAIL_COND_V(node.model_id > 65536, ERR_INVALID_DATA);
|
||||
ERR_FAIL_COND_V(node.model_id < 0, ERR_INVALID_DATA);
|
||||
|
||||
Error model_attributes_err = parse_dictionary(f, node.model_attributes);
|
||||
ERR_FAIL_COND_V(model_attributes_err != OK, model_attributes_err);
|
||||
|
||||
//}
|
||||
|
||||
_scene_graph[node.id] = std::move(node_ptr);
|
||||
|
||||
} else if (strcmp(chunk_id, "LAYR") == 0) {
|
||||
std::unique_ptr<Layer> layer_ptr = std::make_unique<Layer>();
|
||||
Layer &layer = *layer_ptr;
|
||||
|
||||
const int layer_id = f.get_32();
|
||||
for (unsigned int i = 0; i < _layers.size(); ++i) {
|
||||
const Layer *existing_layer = _layers[i].get();
|
||||
CRASH_COND(existing_layer == nullptr);
|
||||
ERR_FAIL_COND_V_MSG(existing_layer->id == layer_id, ERR_INVALID_DATA,
|
||||
String("Layer with ID {0} already exists").format(varray(layer_id)));
|
||||
}
|
||||
layer.id = layer_id;
|
||||
|
||||
Error attributes_err = parse_dictionary(f, layer.attributes);
|
||||
ERR_FAIL_COND_V(attributes_err != OK, attributes_err);
|
||||
|
||||
auto name_it = layer.attributes.find("_name");
|
||||
if (name_it != layer.attributes.end()) {
|
||||
layer.name = name_it->second;
|
||||
}
|
||||
|
||||
auto hidden_it = layer.attributes.find("_hidden");
|
||||
if (hidden_it != layer.attributes.end()) {
|
||||
layer.hidden = (hidden_it->second == "1");
|
||||
} else {
|
||||
layer.hidden = false;
|
||||
}
|
||||
|
||||
const int reserved_id = f.get_32();
|
||||
ERR_FAIL_COND_V(reserved_id != -1, ERR_INVALID_DATA);
|
||||
|
||||
_layers.push_back(std::move(layer_ptr));
|
||||
|
||||
} else if (strcmp(chunk_id, "MATL") == 0) {
|
||||
std::unique_ptr<Material> material_ptr = std::make_unique<Material>();
|
||||
Material &material = *material_ptr;
|
||||
|
||||
const int material_id = f.get_32();
|
||||
ERR_FAIL_COND_V(material_id < 1 || material_id > _palette.size(), ERR_INVALID_DATA);
|
||||
ERR_FAIL_COND_V_MSG(_materials.find(material_id) != _materials.end(), ERR_INVALID_DATA,
|
||||
String("Material ID {0} already exists").format(varray(material_id)));
|
||||
material.id = material_id;
|
||||
|
||||
std::unordered_map<String, String> attributes;
|
||||
Error attributes_err = parse_dictionary(f, attributes);
|
||||
ERR_FAIL_COND_V(attributes_err != OK, attributes_err);
|
||||
|
||||
auto type_it = attributes.find("_type");
|
||||
if (type_it != attributes.end()) {
|
||||
String type_name = type_it->second;
|
||||
if (type_name == "_diffuse") {
|
||||
material.type = Material::TYPE_DIFFUSE;
|
||||
} else if (type_name == "_metal") {
|
||||
material.type = Material::TYPE_METAL;
|
||||
} else if (type_name == "_glass") {
|
||||
material.type = Material::TYPE_GLASS;
|
||||
} else if (type_name == "_emit") {
|
||||
material.type = Material::TYPE_EMIT;
|
||||
}
|
||||
}
|
||||
|
||||
auto weight_it = attributes.find("_weight");
|
||||
if (weight_it != attributes.end()) {
|
||||
material.weight = weight_it->second.to_float();
|
||||
}
|
||||
|
||||
auto rough_it = attributes.find("_rough");
|
||||
if (rough_it != attributes.end()) {
|
||||
material.roughness = rough_it->second.to_float();
|
||||
}
|
||||
|
||||
auto spec_it = attributes.find("_spec");
|
||||
if (spec_it != attributes.end()) {
|
||||
material.specular = spec_it->second.to_float();
|
||||
}
|
||||
|
||||
auto ior_it = attributes.find("_ior");
|
||||
if (ior_it != attributes.end()) {
|
||||
material.ior = ior_it->second.to_float();
|
||||
}
|
||||
|
||||
auto att_it = attributes.find("_att");
|
||||
if (att_it != attributes.end()) {
|
||||
material.att = att_it->second.to_float();
|
||||
}
|
||||
|
||||
auto flux_it = attributes.find("_flux");
|
||||
if (flux_it != attributes.end()) {
|
||||
material.flux = flux_it->second.to_float();
|
||||
}
|
||||
|
||||
// auto plastic_it = attributes.find("_plastic");
|
||||
// if (plastic_it != attributes.end()) {
|
||||
// // ???
|
||||
// }
|
||||
|
||||
_materials.insert(std::make_pair(material_id, std::move(material_ptr)));
|
||||
|
||||
} else {
|
||||
PRINT_VERBOSE(String("Skipping chunk ") + chunk_id);
|
||||
// Ignore chunk
|
||||
f.seek(f.get_position() + chunk_size);
|
||||
}
|
||||
}
|
||||
|
||||
// There is no indication on the official spec to detect the root node of the scene graph.
|
||||
// It might just be the first one we find in the file, but the specification does not explicitely enforce that.
|
||||
// So we have to do it the long way, marking which nodes are referenced by others.
|
||||
std::unordered_set<int> referenced_nodes;
|
||||
|
||||
// Validate scene graph
|
||||
for (auto it = _scene_graph.begin(); it != _scene_graph.end(); ++it) {
|
||||
const Node *node = it->second.get();
|
||||
CRASH_COND(node == nullptr);
|
||||
|
||||
// TODO We should check for cycles too...
|
||||
|
||||
switch (node->type) {
|
||||
case Node::TYPE_TRANSFORM: {
|
||||
const TransformNode *transform_node = reinterpret_cast<const TransformNode *>(node);
|
||||
|
||||
const int child_id = transform_node->child_node_id;
|
||||
ERR_FAIL_COND_V_MSG(_scene_graph.find(child_id) == _scene_graph.end(), ERR_INVALID_DATA,
|
||||
String("Child node {0} does not exist").format(varray(child_id)));
|
||||
|
||||
referenced_nodes.insert(child_id);
|
||||
|
||||
const int layer_id = transform_node->layer_id;
|
||||
// Apparently it is possible to find nodes that are not attached to any layer
|
||||
if (layer_id != -1) {
|
||||
bool layer_exists = false;
|
||||
for (size_t i = 0; i < _layers.size(); ++i) {
|
||||
const Layer *layer = _layers[i].get();
|
||||
CRASH_COND(layer == nullptr);
|
||||
if (layer->id == layer_id) {
|
||||
layer_exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ERR_FAIL_COND_V_MSG(!layer_exists, ERR_INVALID_DATA,
|
||||
String("Layer {0} does not exist").format(varray(layer_id)));
|
||||
}
|
||||
} break;
|
||||
|
||||
case Node::TYPE_GROUP: {
|
||||
const GroupNode *group_node = reinterpret_cast<const GroupNode *>(node);
|
||||
for (size_t i = 0; i < group_node->child_node_ids.size(); ++i) {
|
||||
const int child_id = group_node->child_node_ids[i];
|
||||
ERR_FAIL_COND_V_MSG(_scene_graph.find(child_id) == _scene_graph.end(), ERR_INVALID_DATA,
|
||||
String("Child node {0} does not exist").format(varray(child_id)));
|
||||
referenced_nodes.insert(child_id);
|
||||
}
|
||||
} break;
|
||||
|
||||
case Node::TYPE_SHAPE: {
|
||||
const ShapeNode *shape_node = reinterpret_cast<const ShapeNode *>(node);
|
||||
const int model_id = shape_node->model_id;
|
||||
ERR_FAIL_COND_V_MSG(model_id < 0 || model_id >= static_cast<int>(_models.size()), ERR_INVALID_DATA,
|
||||
String("Model {0} does not exist").format(varray(model_id)));
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto it = _scene_graph.begin(); it != _scene_graph.end(); ++it) {
|
||||
const int node_id = it->first;
|
||||
if (referenced_nodes.find(node_id) != referenced_nodes.end()) {
|
||||
continue;
|
||||
}
|
||||
ERR_FAIL_COND_V_MSG(_root_node_id != -1, ERR_INVALID_DATA, "More than one root node was found");
|
||||
_root_node_id = node_id;
|
||||
}
|
||||
|
||||
// Some vox files don't have scene graph chunks
|
||||
if (_scene_graph.size() > 0) {
|
||||
// But if they do, they must have a root.
|
||||
// If this fails, that means there is a cycle (the opposite is not true)
|
||||
ERR_FAIL_COND_V_MSG(_root_node_id == -1, ERR_INVALID_DATA, "Root node not found");
|
||||
}
|
||||
|
||||
PRINT_VERBOSE(String("Done loading ") + fpath);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
unsigned int Data::get_model_count() const {
|
||||
return _models.size();
|
||||
}
|
||||
|
||||
const Model &Data::get_model(unsigned int index) const {
|
||||
CRASH_COND(index >= _models.size());
|
||||
const Model *model = _models[index].get();
|
||||
CRASH_COND(model == nullptr);
|
||||
return *model;
|
||||
}
|
||||
|
||||
const Node *Data::get_node(int id) const {
|
||||
auto it = _scene_graph.find(id);
|
||||
CRASH_COND(it == _scene_graph.end());
|
||||
const Node *node = it->second.get();
|
||||
CRASH_COND(node == nullptr);
|
||||
return node;
|
||||
}
|
||||
|
||||
int Data::get_root_node_id() const {
|
||||
return _root_node_id;
|
||||
}
|
||||
|
||||
unsigned int Data::get_layer_count() const {
|
||||
return _layers.size();
|
||||
}
|
||||
|
||||
const Layer &Data::get_layer_by_index(unsigned int index) const {
|
||||
CRASH_COND(index >= _layers.size());
|
||||
const Layer *layer = _layers[index].get();
|
||||
CRASH_COND(layer == nullptr);
|
||||
return *layer;
|
||||
}
|
||||
|
||||
const int Data::get_material_id_for_palette_index(unsigned int palette_index) const {
|
||||
auto it = _materials.find(palette_index);
|
||||
if (it == _materials.end()) {
|
||||
return -1;
|
||||
}
|
||||
return it->first;
|
||||
}
|
||||
|
||||
const Material &Data::get_material_by_id(int id) const {
|
||||
auto it = _materials.find(id);
|
||||
CRASH_COND(it == _materials.end());
|
||||
const Material *material = it->second.get();
|
||||
CRASH_COND(material == nullptr);
|
||||
return *material;
|
||||
}
|
||||
|
||||
} // namespace vox
|
149
streams/vox_data.h
Normal file
149
streams/vox_data.h
Normal file
@ -0,0 +1,149 @@
|
||||
#ifndef VOX_DATA_H
|
||||
#define VOX_DATA_H
|
||||
|
||||
#include "../util/fixed_array.h"
|
||||
#include "../util/math/color8.h"
|
||||
#include "../util/math/vector3i.h"
|
||||
|
||||
#include <core/math/basis.h>
|
||||
#include <core/ustring.h>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace std {
|
||||
template <>
|
||||
struct hash<String> {
|
||||
size_t operator()(const String &v) const {
|
||||
return v.hash();
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
namespace vox {
|
||||
|
||||
struct Model {
|
||||
Vector3i size;
|
||||
// TODO Optimization: implement lazy loading/streaming to reduce intermediary memory allocations?
|
||||
// Loading a full 256^3 model needs 16 megabytes, but a lot of areas might actually be uniform,
|
||||
// and we might not need the actual model immediately
|
||||
std::vector<uint8_t> color_indexes;
|
||||
};
|
||||
|
||||
struct Shape {
|
||||
unsigned int model_id;
|
||||
Vector3i position;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
enum Type {
|
||||
TYPE_TRANSFORM = 0,
|
||||
TYPE_GROUP,
|
||||
TYPE_SHAPE
|
||||
};
|
||||
|
||||
int id;
|
||||
// Depending on the type, a node pointer can be casted to different structs
|
||||
const Type type;
|
||||
std::unordered_map<String, String> attributes;
|
||||
|
||||
Node(Type p_type) :
|
||||
type(p_type) {}
|
||||
|
||||
virtual ~Node() {}
|
||||
};
|
||||
|
||||
struct Rotation {
|
||||
uint8_t data;
|
||||
Basis basis;
|
||||
};
|
||||
|
||||
struct TransformNode : public Node {
|
||||
int child_node_id;
|
||||
int layer_id;
|
||||
Vector3i position;
|
||||
Rotation rotation;
|
||||
String name;
|
||||
bool hidden;
|
||||
|
||||
TransformNode() :
|
||||
Node(Node::TYPE_TRANSFORM) {}
|
||||
};
|
||||
|
||||
struct GroupNode : public Node {
|
||||
std::vector<int> child_node_ids;
|
||||
|
||||
GroupNode() :
|
||||
Node(Node::TYPE_GROUP) {}
|
||||
};
|
||||
|
||||
struct ShapeNode : public Node {
|
||||
int model_id; // corresponds to index in the array of models
|
||||
std::unordered_map<String, String> model_attributes;
|
||||
|
||||
ShapeNode() :
|
||||
Node(Node::TYPE_SHAPE) {}
|
||||
};
|
||||
|
||||
struct Layer {
|
||||
int id;
|
||||
std::unordered_map<String, String> attributes;
|
||||
String name;
|
||||
bool hidden;
|
||||
};
|
||||
|
||||
struct Material {
|
||||
enum Type {
|
||||
TYPE_DIFFUSE,
|
||||
TYPE_METAL,
|
||||
TYPE_GLASS,
|
||||
TYPE_EMIT
|
||||
};
|
||||
int id;
|
||||
Type type = TYPE_DIFFUSE;
|
||||
float weight = 0.f;
|
||||
float roughness = 1.f;
|
||||
float specular = 0.f;
|
||||
float ior = 1.f; // refraction
|
||||
float flux = 0.f;
|
||||
// TODO I don't know what `_att` means
|
||||
float att = 0.f;
|
||||
// TODO WTF is `_plastic`?
|
||||
};
|
||||
|
||||
class Data {
|
||||
public:
|
||||
void clear();
|
||||
Error load_from_file(String fpath);
|
||||
|
||||
unsigned int get_model_count() const;
|
||||
const Model &get_model(unsigned int index) const;
|
||||
|
||||
// Can return -1 if there is no scene graph
|
||||
int get_root_node_id() const;
|
||||
const Node *get_node(int id) const;
|
||||
|
||||
unsigned int get_layer_count() const;
|
||||
const Layer &get_layer_by_index(unsigned int index) const;
|
||||
|
||||
const int get_material_id_for_palette_index(unsigned int palette_index) const;
|
||||
const Material &get_material_by_id(int id) const;
|
||||
|
||||
inline const FixedArray<Color8, 256> &get_palette() const {
|
||||
return _palette;
|
||||
}
|
||||
|
||||
private:
|
||||
Error _load_from_file(String fpath);
|
||||
|
||||
std::vector<std::unique_ptr<Model>> _models;
|
||||
std::vector<std::unique_ptr<Layer>> _layers;
|
||||
std::unordered_map<int, std::unique_ptr<Node>> _scene_graph;
|
||||
// Material IDs are supposedly tied to palette indices
|
||||
std::unordered_map<int, std::unique_ptr<Material>> _materials;
|
||||
int _root_node_id = -1;
|
||||
FixedArray<Color8, 256> _palette;
|
||||
};
|
||||
|
||||
} // namespace vox
|
||||
|
||||
#endif // VOX_DATA_H
|
@ -1,156 +1,40 @@
|
||||
#include "vox_loader.h"
|
||||
#include "../meshers/cubes/voxel_color_palette.h"
|
||||
#include "../storage/voxel_buffer.h"
|
||||
#include <core/os/file_access.h>
|
||||
|
||||
namespace vox {
|
||||
|
||||
const uint32_t PALETTE_SIZE = 256;
|
||||
|
||||
uint32_t g_default_palette[PALETTE_SIZE] = {
|
||||
0x00000000, 0xffffffff, 0xffccffff, 0xff99ffff, 0xff66ffff, 0xff33ffff, 0xff00ffff, 0xffffccff,
|
||||
0xffccccff, 0xff99ccff, 0xff66ccff, 0xff33ccff, 0xff00ccff, 0xffff99ff, 0xffcc99ff, 0xff9999ff,
|
||||
0xff6699ff, 0xff3399ff, 0xff0099ff, 0xffff66ff, 0xffcc66ff, 0xff9966ff, 0xff6666ff, 0xff3366ff,
|
||||
0xff0066ff, 0xffff33ff, 0xffcc33ff, 0xff9933ff, 0xff6633ff, 0xff3333ff, 0xff0033ff, 0xffff00ff,
|
||||
0xffcc00ff, 0xff9900ff, 0xff6600ff, 0xff3300ff, 0xff0000ff, 0xffffffcc, 0xffccffcc, 0xff99ffcc,
|
||||
0xff66ffcc, 0xff33ffcc, 0xff00ffcc, 0xffffcccc, 0xffcccccc, 0xff99cccc, 0xff66cccc, 0xff33cccc,
|
||||
0xff00cccc, 0xffff99cc, 0xffcc99cc, 0xff9999cc, 0xff6699cc, 0xff3399cc, 0xff0099cc, 0xffff66cc,
|
||||
0xffcc66cc, 0xff9966cc, 0xff6666cc, 0xff3366cc, 0xff0066cc, 0xffff33cc, 0xffcc33cc, 0xff9933cc,
|
||||
0xff6633cc, 0xff3333cc, 0xff0033cc, 0xffff00cc, 0xffcc00cc, 0xff9900cc, 0xff6600cc, 0xff3300cc,
|
||||
0xff0000cc, 0xffffff99, 0xffccff99, 0xff99ff99, 0xff66ff99, 0xff33ff99, 0xff00ff99, 0xffffcc99,
|
||||
0xffcccc99, 0xff99cc99, 0xff66cc99, 0xff33cc99, 0xff00cc99, 0xffff9999, 0xffcc9999, 0xff999999,
|
||||
0xff669999, 0xff339999, 0xff009999, 0xffff6699, 0xffcc6699, 0xff996699, 0xff666699, 0xff336699,
|
||||
0xff006699, 0xffff3399, 0xffcc3399, 0xff993399, 0xff663399, 0xff333399, 0xff003399, 0xffff0099,
|
||||
0xffcc0099, 0xff990099, 0xff660099, 0xff330099, 0xff000099, 0xffffff66, 0xffccff66, 0xff99ff66,
|
||||
0xff66ff66, 0xff33ff66, 0xff00ff66, 0xffffcc66, 0xffcccc66, 0xff99cc66, 0xff66cc66, 0xff33cc66,
|
||||
0xff00cc66, 0xffff9966, 0xffcc9966, 0xff999966, 0xff669966, 0xff339966, 0xff009966, 0xffff6666,
|
||||
0xffcc6666, 0xff996666, 0xff666666, 0xff336666, 0xff006666, 0xffff3366, 0xffcc3366, 0xff993366,
|
||||
0xff663366, 0xff333366, 0xff003366, 0xffff0066, 0xffcc0066, 0xff990066, 0xff660066, 0xff330066,
|
||||
0xff000066, 0xffffff33, 0xffccff33, 0xff99ff33, 0xff66ff33, 0xff33ff33, 0xff00ff33, 0xffffcc33,
|
||||
0xffcccc33, 0xff99cc33, 0xff66cc33, 0xff33cc33, 0xff00cc33, 0xffff9933, 0xffcc9933, 0xff999933,
|
||||
0xff669933, 0xff339933, 0xff009933, 0xffff6633, 0xffcc6633, 0xff996633, 0xff666633, 0xff336633,
|
||||
0xff006633, 0xffff3333, 0xffcc3333, 0xff993333, 0xff663333, 0xff333333, 0xff003333, 0xffff0033,
|
||||
0xffcc0033, 0xff990033, 0xff660033, 0xff330033, 0xff000033, 0xffffff00, 0xffccff00, 0xff99ff00,
|
||||
0xff66ff00, 0xff33ff00, 0xff00ff00, 0xffffcc00, 0xffcccc00, 0xff99cc00, 0xff66cc00, 0xff33cc00,
|
||||
0xff00cc00, 0xffff9900, 0xffcc9900, 0xff999900, 0xff669900, 0xff339900, 0xff009900, 0xffff6600,
|
||||
0xffcc6600, 0xff996600, 0xff666600, 0xff336600, 0xff006600, 0xffff3300, 0xffcc3300, 0xff993300,
|
||||
0xff663300, 0xff333300, 0xff003300, 0xffff0000, 0xffcc0000, 0xff990000, 0xff660000, 0xff330000,
|
||||
0xff0000ee, 0xff0000dd, 0xff0000bb, 0xff0000aa, 0xff000088, 0xff000077, 0xff000055, 0xff000044,
|
||||
0xff000022, 0xff000011, 0xff00ee00, 0xff00dd00, 0xff00bb00, 0xff00aa00, 0xff008800, 0xff007700,
|
||||
0xff005500, 0xff004400, 0xff002200, 0xff001100, 0xffee0000, 0xffdd0000, 0xffbb0000, 0xffaa0000,
|
||||
0xff880000, 0xff770000, 0xff550000, 0xff440000, 0xff220000, 0xff110000, 0xffeeeeee, 0xffdddddd,
|
||||
0xffbbbbbb, 0xffaaaaaa, 0xff888888, 0xff777777, 0xff555555, 0xff444444, 0xff222222, 0xff111111
|
||||
};
|
||||
|
||||
Error load_vox(const String &fpath, Data &data) {
|
||||
// https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox.txt
|
||||
|
||||
Error err;
|
||||
FileAccessRef f = FileAccess::open(fpath, FileAccess::READ, &err);
|
||||
if (f == nullptr) {
|
||||
return err;
|
||||
}
|
||||
|
||||
char magic[5] = { 0 };
|
||||
ERR_FAIL_COND_V(f->get_buffer((uint8_t *)magic, 4) != 4, ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(strcmp(magic, "VOX ") != 0, ERR_PARSE_ERROR);
|
||||
|
||||
const uint32_t version = f->get_32();
|
||||
ERR_FAIL_COND_V(version != 150, ERR_PARSE_ERROR);
|
||||
|
||||
data.color_indexes.clear();
|
||||
data.size = Vector3i();
|
||||
data.has_palette = false;
|
||||
const size_t flen = f->get_len();
|
||||
|
||||
while (f->get_position() < flen) {
|
||||
char chunk_id[5] = { 0 };
|
||||
ERR_FAIL_COND_V(f->get_buffer((uint8_t *)chunk_id, 4) != 4, ERR_PARSE_ERROR);
|
||||
|
||||
const uint32_t chunk_size = f->get_32();
|
||||
f->get_32(); // child_chunks_size
|
||||
|
||||
if (strcmp(chunk_id, "SIZE") == 0) {
|
||||
data.size.x = f->get_32();
|
||||
data.size.y = f->get_32();
|
||||
data.size.z = f->get_32();
|
||||
ERR_FAIL_COND_V(data.size.x > 0xff, ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(data.size.y > 0xff, ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(data.size.z > 0xff, ERR_PARSE_ERROR);
|
||||
|
||||
} else if (strcmp(chunk_id, "XYZI") == 0) {
|
||||
data.color_indexes.resize(data.size.x * data.size.y * data.size.z, 0);
|
||||
|
||||
const uint32_t num_voxels = f->get_32();
|
||||
|
||||
for (uint32_t i = 0; i < num_voxels; ++i) {
|
||||
const uint32_t z = f->get_8();
|
||||
const uint32_t x = f->get_8();
|
||||
const uint32_t y = f->get_8();
|
||||
const uint32_t c = f->get_8();
|
||||
ERR_FAIL_COND_V(x >= static_cast<uint32_t>(data.size.x), ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(y >= static_cast<uint32_t>(data.size.y), ERR_PARSE_ERROR);
|
||||
ERR_FAIL_COND_V(z >= static_cast<uint32_t>(data.size.z), ERR_PARSE_ERROR);
|
||||
data.color_indexes[Vector3i(x, y, z).get_zxy_index(data.size)] = c;
|
||||
}
|
||||
|
||||
} else if (strcmp(chunk_id, "RGBA") == 0) {
|
||||
data.palette[0] = Color8{ 0, 0, 0, 0 };
|
||||
data.has_palette = true;
|
||||
for (uint32_t i = 1; i < data.palette.size(); ++i) {
|
||||
Color8 c;
|
||||
c.r = f->get_8();
|
||||
c.g = f->get_8();
|
||||
c.b = f->get_8();
|
||||
c.a = f->get_8();
|
||||
data.palette[i] = c;
|
||||
}
|
||||
f->get_32();
|
||||
|
||||
} else {
|
||||
// Ignore chunk
|
||||
f->seek(f->get_position() + chunk_size);
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
} // namespace vox
|
||||
#include "vox_data.h"
|
||||
|
||||
Error VoxelVoxLoader::load_from_file(String fpath, Ref<VoxelBuffer> voxels, Ref<VoxelColorPalette> palette) {
|
||||
ERR_FAIL_COND_V(voxels.is_null(), ERR_INVALID_PARAMETER);
|
||||
|
||||
const Error err = vox::load_vox(fpath, _data);
|
||||
ERR_FAIL_COND_V(err != OK, err);
|
||||
vox::Data data;
|
||||
Error load_err = data.load_from_file(fpath);
|
||||
ERR_FAIL_COND_V(load_err != OK, load_err);
|
||||
|
||||
const vox::Model &model = data.get_model(0);
|
||||
|
||||
const VoxelBuffer::ChannelId channel = VoxelBuffer::CHANNEL_COLOR;
|
||||
|
||||
const Span<Color8> src_palette =
|
||||
_data.has_palette ?
|
||||
Span<Color8>(_data.palette) :
|
||||
Span<Color8>(reinterpret_cast<Color8 *>(vox::g_default_palette), 0, vox::PALETTE_SIZE);
|
||||
|
||||
Span<const Color8> src_palette = to_span_const(data.get_palette());
|
||||
const VoxelBuffer::Depth depth = voxels->get_channel_depth(VoxelBuffer::CHANNEL_COLOR);
|
||||
|
||||
Span<uint8_t> dst_raw;
|
||||
voxels->create(_data.size);
|
||||
voxels->create(model.size);
|
||||
voxels->decompress_channel(channel);
|
||||
CRASH_COND(!voxels->get_channel_raw(channel, dst_raw));
|
||||
|
||||
if (palette.is_valid() && _data.has_palette) {
|
||||
if (palette.is_valid()) {
|
||||
for (size_t i = 0; i < src_palette.size(); ++i) {
|
||||
palette->set_color8(i, src_palette[i]);
|
||||
}
|
||||
|
||||
switch (depth) {
|
||||
case VoxelBuffer::DEPTH_8_BIT: {
|
||||
memcpy(dst_raw.data(), _data.color_indexes.data(), _data.color_indexes.size());
|
||||
memcpy(dst_raw.data(), model.color_indexes.data(), model.color_indexes.size());
|
||||
} break;
|
||||
|
||||
case VoxelBuffer::DEPTH_16_BIT: {
|
||||
Span<uint16_t> dst = dst_raw.reinterpret_cast_to<uint16_t>();
|
||||
for (size_t i = 0; i < dst.size(); ++i) {
|
||||
dst[i] = _data.color_indexes[i];
|
||||
dst[i] = model.color_indexes[i];
|
||||
}
|
||||
} break;
|
||||
|
||||
@ -163,7 +47,7 @@ Error VoxelVoxLoader::load_from_file(String fpath, Ref<VoxelBuffer> voxels, Ref<
|
||||
switch (depth) {
|
||||
case VoxelBuffer::DEPTH_8_BIT: {
|
||||
for (size_t i = 0; i < dst_raw.size(); ++i) {
|
||||
const uint8_t ci = _data.color_indexes[i];
|
||||
const uint8_t ci = model.color_indexes[i];
|
||||
dst_raw[i] = src_palette[ci].to_u8();
|
||||
}
|
||||
} break;
|
||||
@ -171,7 +55,7 @@ Error VoxelVoxLoader::load_from_file(String fpath, Ref<VoxelBuffer> voxels, Ref<
|
||||
case VoxelBuffer::DEPTH_16_BIT: {
|
||||
Span<uint16_t> dst = dst_raw.reinterpret_cast_to<uint16_t>();
|
||||
for (size_t i = 0; i < dst.size(); ++i) {
|
||||
const uint8_t ci = _data.color_indexes[i];
|
||||
const uint8_t ci = model.color_indexes[i];
|
||||
dst[i] = src_palette[ci].to_u16();
|
||||
}
|
||||
} break;
|
||||
@ -182,7 +66,7 @@ Error VoxelVoxLoader::load_from_file(String fpath, Ref<VoxelBuffer> voxels, Ref<
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
return load_err;
|
||||
}
|
||||
|
||||
void VoxelVoxLoader::_bind_methods() {
|
||||
|
@ -1,25 +1,10 @@
|
||||
#ifndef VOX_LOADER_H
|
||||
#define VOX_LOADER_H
|
||||
|
||||
#include "../meshers/cubes/voxel_color_palette.h"
|
||||
#include "../util/math/vector3i.h"
|
||||
#include <vector>
|
||||
#include <core/reference.h>
|
||||
|
||||
class VoxelBuffer;
|
||||
|
||||
namespace vox {
|
||||
|
||||
struct Data {
|
||||
Vector3i size;
|
||||
std::vector<uint8_t> color_indexes;
|
||||
FixedArray<Color8, 256> palette;
|
||||
bool has_palette;
|
||||
};
|
||||
|
||||
// TODO Eventually, will need specialized loaders, because data structures may vary and memory shouldn't be wasted
|
||||
Error load_vox(const String &fpath, Data &data);
|
||||
|
||||
} // namespace vox
|
||||
class VoxelColorPalette;
|
||||
|
||||
// Simple loader for MagicaVoxel
|
||||
class VoxelVoxLoader : public Reference {
|
||||
@ -32,8 +17,6 @@ public:
|
||||
|
||||
private:
|
||||
static void _bind_methods();
|
||||
|
||||
vox::Data _data;
|
||||
};
|
||||
|
||||
#endif // VOX_LOADER_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user