mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
Merge pull request #1041 from MihailRis/new-assets-funcs
new assets funcs
This commit is contained in:
commit
1498eaaba0
3 changed files with 126 additions and 32 deletions
|
|
@ -25,10 +25,10 @@ assets.load_texture(
|
|||
|
||||
-- Parses and loads a 3D model
|
||||
assets.parse_model(
|
||||
-- Model file format (xml / vcm)
|
||||
-- Model file format (xml / vcm / obj / vec3)
|
||||
format: str,
|
||||
-- Contents of the model file
|
||||
content: str,
|
||||
content: str | Bytearray (.vec3),
|
||||
-- Model name after loading
|
||||
name: str,
|
||||
-- The skeleton name after loading. May be the same as the model name.
|
||||
|
|
@ -37,6 +37,16 @@ assets.parse_model(
|
|||
skeleton_name: string
|
||||
)
|
||||
|
||||
-- Parses and loads a skeleton definition
|
||||
assets.parse_skeleton(
|
||||
-- Skeleton file format (json / vcm / xml)
|
||||
format: str,
|
||||
-- Contents of the skeleton file
|
||||
content: str,
|
||||
-- Skeleton name after loading
|
||||
name: str
|
||||
)
|
||||
|
||||
-- Parses and loads animation
|
||||
assets.parse_animation(
|
||||
-- Animation file format (vca)
|
||||
|
|
|
|||
|
|
@ -26,10 +26,10 @@ assets.load_texture(
|
|||
|
||||
-- Парсит и загружает 3D модель
|
||||
assets.parse_model(
|
||||
-- Формат файла модели (xml / vcm)
|
||||
-- Формат файла модели (xml / vcm / obj / vec3)
|
||||
format: string,
|
||||
-- Содержимое файла модели
|
||||
content: string,
|
||||
content: string | Bytearray (.vec3),
|
||||
-- Имя модели после загрузки
|
||||
name: string,
|
||||
-- Имя скелета после загрузки. Может совпадать с именем модели.
|
||||
|
|
@ -38,6 +38,16 @@ assets.parse_model(
|
|||
skeleton_name: string
|
||||
)
|
||||
|
||||
-- Парсит и загружает определение скелета
|
||||
assets.parse_skeleton(
|
||||
-- Формат файла скелета (json / vcm / xml)
|
||||
format: string,
|
||||
-- Содержимое файла скелета
|
||||
content: string,
|
||||
-- Имя скелета после загрузки
|
||||
name: string
|
||||
)
|
||||
|
||||
-- Парсит и загружает анимацию
|
||||
assets.parse_animation(
|
||||
-- Формат файла анимации (vca)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include "coders/obj.hpp"
|
||||
#include "coders/png.hpp"
|
||||
#include "coders/vcm.hpp"
|
||||
#include "coders/vec3.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
|
|
@ -27,6 +28,35 @@ static void load_texture(
|
|||
}
|
||||
}
|
||||
|
||||
static void store_vcm(
|
||||
Assets& assets,
|
||||
const std::string& modelName,
|
||||
const std::string& skeletonName,
|
||||
vcm::VcmModel& vcmModel
|
||||
) {
|
||||
auto skeleton = std::make_unique<rigging::SkeletonConfig>(
|
||||
std::move(*vcmModel.skeleton)
|
||||
);
|
||||
if (vcmModel.parts.size() > 1) {
|
||||
for (auto& [partName, model] : vcmModel.parts) {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(std::move(model)),
|
||||
modelName + "." + partName
|
||||
);
|
||||
}
|
||||
for (auto& bone : skeleton->getBones()) {
|
||||
bone->setModel(modelName + "." + bone->model.name);
|
||||
}
|
||||
} else {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(std::move(vcmModel.parts["root"])),
|
||||
modelName
|
||||
);
|
||||
skeleton->getRoot()->setModel(modelName);
|
||||
}
|
||||
assets.store(std::move(skeleton), skeletonName);
|
||||
}
|
||||
|
||||
static int l_request_texture(lua::State* L) {
|
||||
std::string filename = lua::require_string(L, 1);
|
||||
std::string alias = lua::require_string(L, 2);
|
||||
|
|
@ -85,7 +115,6 @@ static int l_parse_model(lua::State* L) {
|
|||
auto& assets = engine->requireAssets();
|
||||
|
||||
auto format = lua::require_lstring(L, 1);
|
||||
auto string = lua::require_lstring(L, 2);
|
||||
std::string name = lua::require_string(L, 3);
|
||||
std::string skeletonName;
|
||||
if (lua::isstring(L, 4)) {
|
||||
|
|
@ -93,10 +122,71 @@ static int l_parse_model(lua::State* L) {
|
|||
}
|
||||
|
||||
if (format == "obj") {
|
||||
auto string = lua::require_lstring(L, 2);
|
||||
assets.store(obj::parse(name, string), name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (format == "vec3") {
|
||||
auto content = lua::bytearray_as_string(L, 2);
|
||||
util::Buffer<ubyte> buffer(content.size());
|
||||
for (size_t i = 0; i < content.size(); i++) {
|
||||
buffer[i] = static_cast<ubyte>(content[i]);
|
||||
}
|
||||
auto vec3File = vec3::load(name, buffer);
|
||||
for (auto& [modelName, model] : vec3File.models) {
|
||||
auto fullName = name;
|
||||
if (name != modelName) {
|
||||
fullName += "." + modelName;
|
||||
}
|
||||
model.model.translate(model.origin);
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(std::move(model.model)),
|
||||
fullName
|
||||
);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (format != "xml" && format != "vcm") {
|
||||
throw std::runtime_error(
|
||||
"unknown format " + util::quote(std::string(format))
|
||||
);
|
||||
}
|
||||
|
||||
auto string = lua::require_lstring(L, 2);
|
||||
auto vcmModel = vcm::parse(name, string, format == "xml");
|
||||
|
||||
if (skeletonName.empty()) {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(std::move(vcmModel.squash())), name
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!vcmModel.skeleton) {
|
||||
throw std::runtime_error(
|
||||
"model format " + util::quote(std::string(format)) +
|
||||
" does not define a skeleton"
|
||||
);
|
||||
}
|
||||
store_vcm(assets, name, skeletonName, vcmModel);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_parse_skeleton(lua::State* L) {
|
||||
auto& assets = engine->requireAssets();
|
||||
|
||||
auto format = lua::require_lstring(L, 1);
|
||||
auto string = lua::require_lstring(L, 2);
|
||||
std::string name = lua::require_string(L, 3);
|
||||
|
||||
if (format == "json") {
|
||||
auto skeleton = rigging::SkeletonConfig::parse(string, name, name);
|
||||
assets.store(std::move(skeleton), name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (format != "xml" && format != "vcm") {
|
||||
throw std::runtime_error(
|
||||
"unknown format " + util::quote(std::string(format))
|
||||
|
|
@ -104,34 +194,17 @@ static int l_parse_model(lua::State* L) {
|
|||
}
|
||||
|
||||
auto vcmModel = vcm::parse(name, string, format == "xml");
|
||||
|
||||
if (skeletonName.empty()) {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(std::move(vcmModel.squash())), name
|
||||
if (!vcmModel.skeleton) {
|
||||
throw std::runtime_error(
|
||||
"model format " + util::quote(std::string(format)) +
|
||||
" does not define a skeleton"
|
||||
);
|
||||
} else {
|
||||
auto skeleton = std::make_unique<rigging::SkeletonConfig>(
|
||||
std::move(*vcmModel.skeleton)
|
||||
);
|
||||
if (vcmModel.parts.size() > 1) {
|
||||
for (auto& [partName, model] : vcmModel.parts) {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(std::move(model)),
|
||||
name + "." + partName
|
||||
);
|
||||
}
|
||||
for (auto& bone : skeleton->getBones()) {
|
||||
bone->setModel(name + "." + bone->model.name);
|
||||
}
|
||||
} else {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(
|
||||
std::move(vcmModel.parts["root"])), name
|
||||
);
|
||||
skeleton->getRoot()->setModel(name);
|
||||
}
|
||||
assets.store(std::move(skeleton), skeletonName);
|
||||
}
|
||||
|
||||
assets.store(
|
||||
std::make_unique<rigging::SkeletonConfig>(std::move(*vcmModel.skeleton)),
|
||||
name
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -151,7 +224,7 @@ static int l_to_canvas(lua::State* L) {
|
|||
return 0;
|
||||
}
|
||||
auto atlasName = alias.substr(0, sep);
|
||||
|
||||
|
||||
if (auto atlas = assets.get<Atlas>(std::string(atlasName))) {
|
||||
auto textureName = std::string(alias.substr(sep + 1));
|
||||
auto image = atlas->shareImageData();
|
||||
|
|
@ -178,6 +251,7 @@ const luaL_Reg assetslib[] = {
|
|||
{"load_texture", lua::wrap<l_load_texture>},
|
||||
{"parse_animation", lua::wrap<l_parse_animation>},
|
||||
{"parse_model", lua::wrap<l_parse_model>},
|
||||
{"parse_skeleton", lua::wrap<l_parse_skeleton>},
|
||||
{"to_canvas", lua::wrap<l_to_canvas>},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue