add modeltree:get_matrix, :set_matrix
This commit is contained in:
parent
8a10ed24da
commit
42ea88501d
@ -5,8 +5,14 @@ local rig = entity.modeltree
|
|||||||
inair = true
|
inair = true
|
||||||
ready = false
|
ready = false
|
||||||
|
|
||||||
|
|
||||||
|
local rotation = mat4.rotate({0, 1, 0}, math.random() * 360)
|
||||||
|
mat4.rotate(rotation, {1, 0, 0}, math.random() * 360, rotation)
|
||||||
|
mat4.rotate(rotation, {0, 0, 1}, math.random() * 360, rotation)
|
||||||
|
rig:set_matrix(0, rotation)
|
||||||
|
|
||||||
function on_grounded(force)
|
function on_grounded(force)
|
||||||
tsf:set_rot(mat4.rotate({0, 1, 0}, math.random()*360))
|
rig:set_matrix(0, mat4.rotate({0, 1, 0}, math.random()*360))
|
||||||
inair = false
|
inair = false
|
||||||
ready = true
|
ready = true
|
||||||
end
|
end
|
||||||
@ -25,7 +31,9 @@ end
|
|||||||
function on_update()
|
function on_update()
|
||||||
if inair then
|
if inair then
|
||||||
local dt = time.delta();
|
local dt = time.delta();
|
||||||
tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 1, 0}, 240*dt))
|
local matrix = rig:get_matrix(0)
|
||||||
tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 0, 1}, 240*dt))
|
mat4.rotate(matrix, {0, 1, 0}, 240*dt, matrix)
|
||||||
|
mat4.rotate(matrix, {0, 0, 1}, 240*dt, matrix)
|
||||||
|
rig:set_matrix(0, matrix)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -16,8 +16,6 @@ function on_hud_open()
|
|||||||
|
|
||||||
local drop = entities.spawn("base:drop", ppos)
|
local drop = entities.spawn("base:drop", ppos)
|
||||||
drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)))
|
drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)))
|
||||||
drop.transform:set_rot(mat4.rotate(mat4.rotate(mat4.rotate({0, 1, 0}, math.random() * 360),
|
|
||||||
{1, 0, 0}, math.random() * 360), {0, 0, 1}, math.random() * 360))
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -24,7 +24,9 @@ function new_Rigidbody(eid)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local Modeltree = {__index={
|
local Modeltree = {__index={
|
||||||
get_model=function(self) return __modeltree.get_model(self.eid) end,
|
get_model=function(self, i) return __modeltree.get_model(self.eid, i) end,
|
||||||
|
get_matrix=function(self, i) return __modeltree.get_matrix(self.eid, i) end,
|
||||||
|
set_matrix=function(self, i, m) return __modeltree.set_matrix(self.eid, i, m) end,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
function new_Modeltree(eid)
|
function new_Modeltree(eid)
|
||||||
|
|||||||
@ -111,16 +111,43 @@ static int l_get_size(lua::State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int index_range_check(const rigging::Rig& rig, lua::Integer index) {
|
||||||
|
if (static_cast<size_t>(index) >= rig.pose.matrices.size()) {
|
||||||
|
throw std::runtime_error("index out of range [0, " +
|
||||||
|
std::to_string(rig.pose.matrices.size()) +
|
||||||
|
"]");
|
||||||
|
}
|
||||||
|
return static_cast<int>(index);
|
||||||
|
}
|
||||||
|
|
||||||
static int l_modeltree_get_model(lua::State* L) {
|
static int l_modeltree_get_model(lua::State* L) {
|
||||||
if (auto entity = get_entity(L, 1)) {
|
if (auto entity = get_entity(L, 1)) {
|
||||||
auto& rig = entity->getModeltree();
|
auto& rig = entity->getModeltree();
|
||||||
auto* rigConfig = rig.config;
|
auto* rigConfig = rig.config;
|
||||||
auto index = lua::tointeger(L, 2);
|
auto index = index_range_check(rig, lua::tointeger(L, 2));
|
||||||
return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName());
|
return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName());
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_modeltree_get_matrix(lua::State* L) {
|
||||||
|
if (auto entity = get_entity(L, 1)) {
|
||||||
|
auto& rig = entity->getModeltree();
|
||||||
|
auto index = index_range_check(rig, lua::tointeger(L, 2));
|
||||||
|
return lua::pushmat4(L, rig.pose.matrices[index]);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_modeltree_set_matrix(lua::State* L) {
|
||||||
|
if (auto entity = get_entity(L, 1)) {
|
||||||
|
auto& rig = entity->getModeltree();
|
||||||
|
auto index = index_range_check(rig, lua::tointeger(L, 2));
|
||||||
|
rig.pose.matrices[index] = lua::tomat4(L, 3);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg entitylib [] = {
|
const luaL_Reg entitylib [] = {
|
||||||
{"exists", lua::wrap<l_exists>},
|
{"exists", lua::wrap<l_exists>},
|
||||||
{"spawn", lua::wrap<l_spawn>},
|
{"spawn", lua::wrap<l_spawn>},
|
||||||
@ -130,6 +157,8 @@ const luaL_Reg entitylib [] = {
|
|||||||
|
|
||||||
const luaL_Reg modeltreelib [] = {
|
const luaL_Reg modeltreelib [] = {
|
||||||
{"get_model", lua::wrap<l_modeltree_get_model>},
|
{"get_model", lua::wrap<l_modeltree_get_model>},
|
||||||
|
{"get_matrix", lua::wrap<l_modeltree_get_matrix>},
|
||||||
|
{"set_matrix", lua::wrap<l_modeltree_set_matrix>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -78,9 +78,9 @@ entityid_t Entities::spawn(
|
|||||||
auto& scripting = registry.emplace<Scripting>(
|
auto& scripting = registry.emplace<Scripting>(
|
||||||
entity, entity_funcs_set {}, nullptr);
|
entity, entity_funcs_set {}, nullptr);
|
||||||
entities[id] = entity;
|
entities[id] = entity;
|
||||||
|
registry.emplace<rigging::Rig>(entity, rig->instance());
|
||||||
scripting.env = scripting::on_entity_spawn(
|
scripting.env = scripting::on_entity_spawn(
|
||||||
def, id, scripting.funcsset, std::move(args));
|
def, id, scripting.funcsset, std::move(args));
|
||||||
registry.emplace<rigging::Rig>(entity, rig->instance());
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -42,10 +42,11 @@ size_t RigConfig::update(
|
|||||||
glm::mat4 matrix) const
|
glm::mat4 matrix) const
|
||||||
{
|
{
|
||||||
rig.calculated.matrices[index] = matrix * rig.pose.matrices[index];
|
rig.calculated.matrices[index] = matrix * rig.pose.matrices[index];
|
||||||
|
size_t count = 1;
|
||||||
for (auto& subnode : node->getSubnodes()) {
|
for (auto& subnode : node->getSubnodes()) {
|
||||||
index = update(index+1, rig, subnode.get(), rig.calculated.matrices[index]);
|
count += update(index+count, rig, subnode.get(), rig.calculated.matrices[index]);
|
||||||
}
|
}
|
||||||
return index;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RigConfig::update(Rig& rig, glm::mat4 matrix) const {
|
void RigConfig::update(Rig& rig, glm::mat4 matrix) const {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
#ifndef OBJECTS_SKELETON_HPP_
|
#ifndef OBJECTS_RIGGING_HPP_
|
||||||
#define OBJECTS_SKELETON_HPP_
|
#define OBJECTS_RIGGING_HPP_
|
||||||
|
|
||||||
#include "../typedefs.hpp"
|
#include "../typedefs.hpp"
|
||||||
|
|
||||||
@ -23,11 +23,8 @@ namespace rigging {
|
|||||||
struct Pose {
|
struct Pose {
|
||||||
std::vector<glm::mat4> matrices;
|
std::vector<glm::mat4> matrices;
|
||||||
|
|
||||||
Pose(size_t size) : matrices(size) {
|
Pose(size_t size) {
|
||||||
matrices.resize(size);
|
matrices.resize(size, glm::mat4(1.0f));
|
||||||
for (size_t i = 0; i < size; i++) {
|
|
||||||
matrices[i] = glm::mat4(1.0f);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -115,4 +112,4 @@ namespace rigging {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // OBJECTS_SKELETON_HPP_
|
#endif // OBJECTS_RIGGING_HPP_
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user