move hand item control to lua
This commit is contained in:
parent
567eec88c8
commit
6f11741310
@ -82,3 +82,42 @@ function on_hud_open()
|
|||||||
|
|
||||||
configure_SSAO()
|
configure_SSAO()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local prev_rotation = mat4.idt()
|
||||||
|
|
||||||
|
function update_hand()
|
||||||
|
local skeleton = __skeleton
|
||||||
|
local pid = hud.get_player()
|
||||||
|
local invid, slot = player.get_inventory(pid)
|
||||||
|
local itemid = inventory.get(invid, slot)
|
||||||
|
|
||||||
|
local cam = cameras.get("core:first-person")
|
||||||
|
local bone = skeleton.index("hand", "item")
|
||||||
|
|
||||||
|
local offset = vec3.mul(vec3.sub(cam:get_pos(), {player.get_pos(pid)}), -1)
|
||||||
|
|
||||||
|
local rotation = cam:get_rot()
|
||||||
|
|
||||||
|
local angle = player.get_rot() - 90
|
||||||
|
local cos = math.cos(angle / (180 / math.pi))
|
||||||
|
local sin = math.sin(angle / (180 / math.pi))
|
||||||
|
|
||||||
|
local newX = offset[1] * cos - offset[3] * sin
|
||||||
|
local newZ = offset[1] * sin + offset[3] * cos
|
||||||
|
|
||||||
|
offset[1] = newX
|
||||||
|
offset[3] = newZ
|
||||||
|
|
||||||
|
local mat = mat4.translate(mat4.idt(), {0.06, 0.035, -0.1})
|
||||||
|
mat4.scale(mat, {0.1, 0.1, 0.1}, mat)
|
||||||
|
mat4.mul(rotation, mat, mat)
|
||||||
|
mat4.rotate(mat, {0, 1, 0}, -90, mat)
|
||||||
|
mat4.translate(mat, offset, mat)
|
||||||
|
|
||||||
|
skeleton.set_matrix("hand", bone, mat)
|
||||||
|
skeleton.set_model("hand", bone, item.model_name(itemid))
|
||||||
|
end
|
||||||
|
|
||||||
|
function on_hud_render()
|
||||||
|
timeit(1, update_hand)
|
||||||
|
end
|
||||||
|
|||||||
@ -4,31 +4,19 @@
|
|||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
#include "ModelBatch.hpp"
|
#include "ModelBatch.hpp"
|
||||||
#include "assets/Assets.hpp"
|
|
||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
#include "graphics/core/Shader.hpp"
|
|
||||||
#include "graphics/commons/Model.hpp"
|
#include "graphics/commons/Model.hpp"
|
||||||
#include "items/Inventory.hpp"
|
|
||||||
#include "items/ItemStack.hpp"
|
|
||||||
#include "items/ItemDef.hpp"
|
|
||||||
#include "objects/Player.hpp"
|
|
||||||
#include "objects/rigging.hpp"
|
#include "objects/rigging.hpp"
|
||||||
#include "world/Level.hpp"
|
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "window/Window.hpp"
|
|
||||||
|
|
||||||
using namespace rigging;
|
using namespace rigging;
|
||||||
|
|
||||||
HandsRenderer::HandsRenderer(
|
HandsRenderer::HandsRenderer(
|
||||||
const Assets& assets,
|
const Assets& assets,
|
||||||
const Level& level,
|
|
||||||
const Player& player,
|
|
||||||
ModelBatch& modelBatch,
|
ModelBatch& modelBatch,
|
||||||
std::shared_ptr<Skeleton> skeleton
|
std::shared_ptr<Skeleton> skeleton
|
||||||
)
|
)
|
||||||
: assets(assets),
|
: assets(assets),
|
||||||
level(level),
|
|
||||||
player(player),
|
|
||||||
modelBatch(modelBatch),
|
modelBatch(modelBatch),
|
||||||
skeleton(std::move(skeleton)) {
|
skeleton(std::move(skeleton)) {
|
||||||
}
|
}
|
||||||
@ -36,57 +24,9 @@ HandsRenderer::HandsRenderer(
|
|||||||
void HandsRenderer::renderHands(
|
void HandsRenderer::renderHands(
|
||||||
const Camera& camera, float delta
|
const Camera& camera, float delta
|
||||||
) {
|
) {
|
||||||
// configure model matrix
|
|
||||||
const glm::vec3 itemOffset(0.06f, 0.035f, -0.1);
|
|
||||||
|
|
||||||
static glm::mat4 prevRotation(1.0f);
|
|
||||||
|
|
||||||
const float speed = 24.0f;
|
|
||||||
glm::mat4 matrix = glm::translate(glm::mat4(1.0f), itemOffset);
|
|
||||||
matrix = glm::scale(matrix, glm::vec3(0.1f));
|
|
||||||
glm::mat4 rotation = camera.rotation;
|
|
||||||
|
|
||||||
// rotation interpolation
|
|
||||||
glm::quat rot0 = glm::quat_cast(prevRotation);
|
|
||||||
glm::quat rot1 = glm::quat_cast(rotation);
|
|
||||||
glm::quat finalRot =
|
|
||||||
glm::slerp(rot0, rot1, static_cast<float>(delta * speed));
|
|
||||||
rotation = glm::mat4_cast(finalRot);
|
|
||||||
prevRotation = rotation;
|
|
||||||
|
|
||||||
// building matrix
|
|
||||||
matrix = rotation * matrix *
|
|
||||||
glm::rotate(
|
|
||||||
glm::mat4(1.0f), -glm::pi<float>() * 0.5f, glm::vec3(0, 1, 0)
|
|
||||||
);
|
|
||||||
|
|
||||||
// getting offset
|
|
||||||
glm::vec3 cameraRotation = player.getRotation();
|
|
||||||
auto offset = -(camera.position - player.getPosition());
|
|
||||||
float angle = glm::radians(cameraRotation.x - 90);
|
|
||||||
float cos = glm::cos(angle);
|
|
||||||
float sin = glm::sin(angle);
|
|
||||||
|
|
||||||
float newX = offset.x * cos - offset.z * sin;
|
|
||||||
float newZ = offset.x * sin + offset.z * cos;
|
|
||||||
offset = glm::vec3(newX, offset.y, newZ);
|
|
||||||
matrix = matrix * glm::translate(glm::mat4(1.0f), offset);
|
|
||||||
|
|
||||||
// get current chosen item
|
|
||||||
auto indices = level.content.getIndices();
|
|
||||||
const auto& inventory = player.getInventory();
|
|
||||||
int slot = player.getChosenSlot();
|
|
||||||
const ItemStack& stack = inventory->getSlot(slot);
|
|
||||||
const auto& def = indices->items.require(stack.getItemId());
|
|
||||||
|
|
||||||
auto& skeleton = *this->skeleton;
|
auto& skeleton = *this->skeleton;
|
||||||
const auto& config = *skeleton.config;
|
const auto& config = *skeleton.config;
|
||||||
|
|
||||||
auto itemBone = config.find("item");
|
|
||||||
size_t itemBoneIndex = itemBone->getIndex();
|
|
||||||
skeleton.modelOverrides.at(itemBoneIndex).model = assets.get<model::Model>(def.modelName);
|
|
||||||
skeleton.pose.matrices.at(itemBoneIndex) = matrix;
|
|
||||||
|
|
||||||
// render
|
// render
|
||||||
modelBatch.setLightsOffset(camera.position);
|
modelBatch.setLightsOffset(camera.position);
|
||||||
config.update(skeleton, glm::mat4(1.0f), glm::vec3());
|
config.update(skeleton, glm::mat4(1.0f), glm::vec3());
|
||||||
|
|||||||
@ -4,8 +4,6 @@
|
|||||||
|
|
||||||
class Assets;
|
class Assets;
|
||||||
class Camera;
|
class Camera;
|
||||||
class Level;
|
|
||||||
class Player;
|
|
||||||
class ModelBatch;
|
class ModelBatch;
|
||||||
|
|
||||||
namespace rigging {
|
namespace rigging {
|
||||||
@ -16,8 +14,6 @@ class HandsRenderer {
|
|||||||
public:
|
public:
|
||||||
HandsRenderer(
|
HandsRenderer(
|
||||||
const Assets& assets,
|
const Assets& assets,
|
||||||
const Level& level,
|
|
||||||
const Player& player,
|
|
||||||
ModelBatch& modelBatch,
|
ModelBatch& modelBatch,
|
||||||
std::shared_ptr<rigging::Skeleton> skeleton
|
std::shared_ptr<rigging::Skeleton> skeleton
|
||||||
);
|
);
|
||||||
@ -25,8 +21,6 @@ public:
|
|||||||
void renderHands(const Camera& camera, float delta);
|
void renderHands(const Camera& camera, float delta);
|
||||||
private:
|
private:
|
||||||
const Assets& assets;
|
const Assets& assets;
|
||||||
const Level& level;
|
|
||||||
const Player& player;
|
|
||||||
ModelBatch& modelBatch;
|
ModelBatch& modelBatch;
|
||||||
std::shared_ptr<rigging::Skeleton> skeleton;
|
std::shared_ptr<rigging::Skeleton> skeleton;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -116,11 +116,7 @@ WorldRenderer::WorldRenderer(
|
|||||||
content.getDefaults()["hand-skeleton"].asString()
|
content.getDefaults()["hand-skeleton"].asString()
|
||||||
);
|
);
|
||||||
hands = std::make_unique<HandsRenderer>(
|
hands = std::make_unique<HandsRenderer>(
|
||||||
*assets,
|
*assets, *modelBatch, skeletons->createSkeleton("hand", &skeletonConfig)
|
||||||
level,
|
|
||||||
player,
|
|
||||||
*modelBatch,
|
|
||||||
skeletons->createSkeleton("hand", &skeletonConfig)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user