feat(npc): 添加NPC交互高亮效果及复合纹理支持
实现NPC交互时的绿色高亮效果,通过InteractionHighlightSprite类实现 重构NpcAnimation支持复合纹理渲染,优化高亮效果的性能 添加ShaderManager获取所有加载Shader的方法,优化渲染器uniform更新逻辑
This commit is contained in:
35
Game/src/common/InteractionHighlightSprite.cpp
Normal file
35
Game/src/common/InteractionHighlightSprite.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "common/InteractionHighlightSprite.h"
|
||||
|
||||
namespace frostbite2D {
|
||||
|
||||
InteractionHighlightSprite::InteractionHighlightSprite() {
|
||||
SetName("interactionHighlight");
|
||||
SetShader("outline_sprite");
|
||||
SetColor(Colors::Green);
|
||||
SetVisible(false);
|
||||
}
|
||||
|
||||
void InteractionHighlightSprite::SetGroundAnchorInTexture(
|
||||
const Vec2& groundAnchorInTexture) {
|
||||
Vec2 textureSize = GetSize();
|
||||
if (textureSize.x <= 0.0f || textureSize.y <= 0.0f) {
|
||||
SetAnchor(0.5f, 1.0f);
|
||||
texelSize_ = Vec2::One();
|
||||
return;
|
||||
}
|
||||
|
||||
SetAnchor(groundAnchorInTexture.x / textureSize.x,
|
||||
groundAnchorInTexture.y / textureSize.y);
|
||||
texelSize_ = Vec2(1.0f / textureSize.x, 1.0f / textureSize.y);
|
||||
}
|
||||
|
||||
void InteractionHighlightSprite::ConfigureShader(Shader* shader) const {
|
||||
Sprite::ConfigureShader(shader);
|
||||
if (!shader) {
|
||||
return;
|
||||
}
|
||||
|
||||
shader->setVec2("u_texelSize", texelSize_);
|
||||
}
|
||||
|
||||
} // namespace frostbite2D
|
||||
@@ -1,13 +1,74 @@
|
||||
#include "npc/NpcAnimation.h"
|
||||
#include <frostbite2D/graphics/renderer.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace frostbite2D {
|
||||
namespace {
|
||||
|
||||
constexpr float kCompositePadding = 1.0f;
|
||||
|
||||
Rect transformRectBounds(const Rect& rect, const Transform2D& transform) {
|
||||
Vec2 p0 = transform.transformPoint(Vec2(rect.left(), rect.top()));
|
||||
Vec2 p1 = transform.transformPoint(Vec2(rect.right(), rect.top()));
|
||||
Vec2 p2 = transform.transformPoint(Vec2(rect.left(), rect.bottom()));
|
||||
Vec2 p3 = transform.transformPoint(Vec2(rect.right(), rect.bottom()));
|
||||
|
||||
float minX = std::min(std::min(p0.x, p1.x), std::min(p2.x, p3.x));
|
||||
float minY = std::min(std::min(p0.y, p1.y), std::min(p2.y, p3.y));
|
||||
float maxX = std::max(std::max(p0.x, p1.x), std::max(p2.x, p3.x));
|
||||
float maxY = std::max(std::max(p0.y, p1.y), std::max(p2.y, p3.y));
|
||||
return Rect(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
|
||||
void combineHash(uint64& seed, uint64 value) {
|
||||
constexpr uint64 kOffset = 0x9e3779b97f4a7c15ULL;
|
||||
seed ^= value + kOffset + (seed << 6) + (seed >> 2);
|
||||
}
|
||||
|
||||
NpcAnimation::CompositeFrameInfo combineCompositeFrameInfo(
|
||||
const NpcAnimation::CompositeFrameInfo& lhs,
|
||||
const NpcAnimation::CompositeFrameInfo& rhs) {
|
||||
if (!lhs.valid) {
|
||||
return rhs;
|
||||
}
|
||||
if (!rhs.valid) {
|
||||
return lhs;
|
||||
}
|
||||
|
||||
float left = std::floor(std::min(lhs.localBounds.left(), rhs.localBounds.left()));
|
||||
float top = std::floor(std::min(lhs.localBounds.top(), rhs.localBounds.top()));
|
||||
float right = std::ceil(std::max(lhs.localBounds.right(), rhs.localBounds.right()));
|
||||
float bottom =
|
||||
std::ceil(std::max(lhs.localBounds.bottom(), rhs.localBounds.bottom()));
|
||||
|
||||
NpcAnimation::CompositeFrameInfo result;
|
||||
result.valid = true;
|
||||
result.localBounds =
|
||||
Rect(left, top, std::max(right - left, 1.0f), std::max(bottom - top, 1.0f));
|
||||
result.width =
|
||||
std::max(static_cast<int>(std::lround(result.localBounds.width())), 1);
|
||||
result.height =
|
||||
std::max(static_cast<int>(std::lround(result.localBounds.height())), 1);
|
||||
result.originInTexture =
|
||||
Vec2(-result.localBounds.left(), -result.localBounds.top());
|
||||
result.groundAnchorInTexture = result.originInTexture;
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool NpcAnimation::Init(const npc::NpcConfig& config) {
|
||||
RemoveAllChildren();
|
||||
displayAnimation_ = nullptr;
|
||||
compositeCanvas_ = nullptr;
|
||||
compositeFrameInfo_ = CompositeFrameInfo();
|
||||
animationPath_.clear();
|
||||
direction_ = 1;
|
||||
compositeDirty_ = true;
|
||||
compositeVersion_ = 0;
|
||||
lastCompositeSignature_ = 0;
|
||||
|
||||
if (config.fieldAnimationPath.empty()) {
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||
@@ -15,6 +76,18 @@ bool NpcAnimation::Init(const npc::NpcConfig& config) {
|
||||
return false;
|
||||
}
|
||||
|
||||
compositeCanvas_ = MakePtr<CanvasActor>();
|
||||
if (!compositeCanvas_ || !compositeCanvas_->Init(1, 1)) {
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"NpcAnimation: failed to initialize composite canvas for npc %d",
|
||||
config.id);
|
||||
compositeCanvas_.Reset();
|
||||
} else {
|
||||
compositeCanvas_->SetCustomDrawCallback([this]() {
|
||||
RenderCurrentAnimationToCompositeCanvas();
|
||||
});
|
||||
}
|
||||
|
||||
auto animation = MakePtr<Animation>(config.fieldAnimationPath);
|
||||
if (!animation || !animation->IsUsable()) {
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||
@@ -33,14 +106,37 @@ bool NpcAnimation::Init(const npc::NpcConfig& config) {
|
||||
SetDirection(1);
|
||||
displayAnimation_->Reset();
|
||||
SetDirection(direction_);
|
||||
|
||||
CompositeFrameInfo rightInfo = ComputeCompositeFrameInfo(1);
|
||||
CompositeFrameInfo leftInfo = ComputeCompositeFrameInfo(-1);
|
||||
compositeFrameInfo_ = combineCompositeFrameInfo(rightInfo, leftInfo);
|
||||
lastCompositeSignature_ = CaptureCurrentRenderSignature();
|
||||
return true;
|
||||
}
|
||||
|
||||
void NpcAnimation::Update(float deltaTime) {
|
||||
Actor::Update(deltaTime);
|
||||
|
||||
uint64 renderSignature = CaptureCurrentRenderSignature();
|
||||
if (renderSignature != lastCompositeSignature_) {
|
||||
compositeDirty_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void NpcAnimation::Render() {
|
||||
if (compositeDirty_ && compositeCanvas_ && Renderer::get().isFrameActive()) {
|
||||
RefreshCompositeTextureIfNeeded();
|
||||
}
|
||||
|
||||
Actor::Render();
|
||||
}
|
||||
|
||||
void NpcAnimation::SetDirection(int direction) {
|
||||
direction_ = direction >= 0 ? 1 : -1;
|
||||
if (displayAnimation_) {
|
||||
displayAnimation_->SetDirection(direction_);
|
||||
}
|
||||
MarkCompositeDirty();
|
||||
}
|
||||
|
||||
bool NpcAnimation::IsAnimationFinished() const {
|
||||
@@ -55,4 +151,125 @@ bool NpcAnimation::GetDisplayLocalBounds(Rect& outBounds) const {
|
||||
return displayAnimation_->GetStaticLocalBounds(outBounds);
|
||||
}
|
||||
|
||||
bool NpcAnimation::EnsureCompositeTextureReady() {
|
||||
RefreshCompositeTextureIfNeeded();
|
||||
return HasCompositeTexture();
|
||||
}
|
||||
|
||||
bool NpcAnimation::HasCompositeTexture() const {
|
||||
return compositeCanvas_ && compositeCanvas_->IsCanvasReady() &&
|
||||
GetCurrentCompositeFrameInfo().valid;
|
||||
}
|
||||
|
||||
Ptr<Texture> NpcAnimation::GetCompositeTexture() const {
|
||||
return compositeCanvas_ ? compositeCanvas_->GetOutputTexture() : nullptr;
|
||||
}
|
||||
|
||||
Vec2 NpcAnimation::GetCompositeTextureSize() const {
|
||||
CompositeFrameInfo info = GetCurrentCompositeFrameInfo();
|
||||
return Vec2(static_cast<float>(info.width), static_cast<float>(info.height));
|
||||
}
|
||||
|
||||
Vec2 NpcAnimation::GetCompositeGroundAnchorInTexture() const {
|
||||
return GetCurrentCompositeFrameInfo().groundAnchorInTexture;
|
||||
}
|
||||
|
||||
NpcAnimation::CompositeFrameInfo NpcAnimation::ComputeCompositeFrameInfo(
|
||||
int direction) const {
|
||||
CompositeFrameInfo info;
|
||||
if (!displayAnimation_) {
|
||||
return info;
|
||||
}
|
||||
|
||||
Rect animationBounds;
|
||||
if (!displayAnimation_->GetStaticLocalBounds(direction, animationBounds)) {
|
||||
return info;
|
||||
}
|
||||
|
||||
Rect bounds =
|
||||
transformRectBounds(animationBounds, displayAnimation_->GetLocalTransform());
|
||||
if (bounds.empty()) {
|
||||
return info;
|
||||
}
|
||||
|
||||
float left = std::floor(bounds.left() - kCompositePadding);
|
||||
float top = std::floor(bounds.top() - kCompositePadding);
|
||||
float right = std::ceil(bounds.right() + kCompositePadding);
|
||||
float bottom = std::ceil(bounds.bottom() + kCompositePadding);
|
||||
|
||||
info.valid = true;
|
||||
info.localBounds = Rect(left, top, std::max(right - left, 1.0f),
|
||||
std::max(bottom - top, 1.0f));
|
||||
info.width = std::max(static_cast<int>(std::lround(info.localBounds.width())), 1);
|
||||
info.height = std::max(static_cast<int>(std::lround(info.localBounds.height())), 1);
|
||||
info.originInTexture = Vec2(-info.localBounds.left(), -info.localBounds.top());
|
||||
info.groundAnchorInTexture = info.originInTexture;
|
||||
return info;
|
||||
}
|
||||
|
||||
uint64 NpcAnimation::CaptureCurrentRenderSignature() const {
|
||||
uint64 signature = 1469598103934665603ULL;
|
||||
combineHash(signature, static_cast<uint64>(direction_ >= 0 ? 1 : 0));
|
||||
if (displayAnimation_) {
|
||||
combineHash(signature, displayAnimation_->GetRenderSignature());
|
||||
}
|
||||
return signature;
|
||||
}
|
||||
|
||||
void NpcAnimation::MarkCompositeDirty() {
|
||||
compositeDirty_ = true;
|
||||
if (compositeCanvas_) {
|
||||
compositeCanvas_->SetDirty();
|
||||
}
|
||||
}
|
||||
|
||||
void NpcAnimation::UpdateCompositeCamera() {
|
||||
if (!compositeCanvas_) {
|
||||
return;
|
||||
}
|
||||
|
||||
CompositeFrameInfo info = GetCurrentCompositeFrameInfo();
|
||||
Camera& camera = compositeCanvas_->GetCanvasCamera();
|
||||
camera.setViewport(std::max(info.width, 1), std::max(info.height, 1));
|
||||
camera.setZoom(1.0f);
|
||||
Vec2 cameraOrigin = GetWorldTransform().transformPoint(
|
||||
Vec2(info.localBounds.left(), info.localBounds.top()));
|
||||
camera.setPosition(cameraOrigin);
|
||||
}
|
||||
|
||||
void NpcAnimation::RefreshCompositeTextureIfNeeded() {
|
||||
if (!compositeDirty_ || !compositeCanvas_) {
|
||||
return;
|
||||
}
|
||||
|
||||
CompositeFrameInfo info = GetCurrentCompositeFrameInfo();
|
||||
if (!info.valid) {
|
||||
compositeDirty_ = false;
|
||||
lastCompositeSignature_ = CaptureCurrentRenderSignature();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!compositeCanvas_->SetCanvasSize(std::max(info.width, 1),
|
||||
std::max(info.height, 1))) {
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateCompositeCamera();
|
||||
if (!compositeCanvas_->Redraw()) {
|
||||
return;
|
||||
}
|
||||
|
||||
compositeDirty_ = false;
|
||||
lastCompositeSignature_ = CaptureCurrentRenderSignature();
|
||||
++compositeVersion_;
|
||||
}
|
||||
|
||||
void NpcAnimation::RenderCurrentAnimationToCompositeCanvas() {
|
||||
if (!displayAnimation_) {
|
||||
return;
|
||||
}
|
||||
|
||||
RenderChildren();
|
||||
}
|
||||
|
||||
} // namespace frostbite2D
|
||||
|
||||
@@ -42,10 +42,13 @@ bool NpcObject::Construction(int npcId) {
|
||||
worldPosition_ = CharacterWorldPosition();
|
||||
config_.reset();
|
||||
animation_ = nullptr;
|
||||
interactionHighlight_ = nullptr;
|
||||
nameOutlineLabels_.fill(nullptr);
|
||||
nameLabel_ = nullptr;
|
||||
interactable_ = true;
|
||||
interacting_ = false;
|
||||
interactionHighlighted_ = false;
|
||||
syncedHighlightCompositeVersion_ = 0;
|
||||
|
||||
auto config = npc::loadNpcConfig(npcId);
|
||||
if (!config) {
|
||||
@@ -68,11 +71,13 @@ bool NpcObject::Construction(int npcId) {
|
||||
config_ = *config;
|
||||
npcId_ = npcId;
|
||||
animation_ = animation;
|
||||
EnsureInteractionHighlight();
|
||||
AddChild(animation_);
|
||||
EnsureNameLabel();
|
||||
|
||||
SetWorldPosition({});
|
||||
SetDirection(1);
|
||||
SetInteractionHighlighted(false);
|
||||
RefreshNameLabel();
|
||||
SDL_Log("NpcObject: npc %d construction complete", npcId_);
|
||||
return true;
|
||||
@@ -84,6 +89,7 @@ void NpcObject::SetDirection(int direction) {
|
||||
animation_->SetDirection(direction_);
|
||||
}
|
||||
RefreshNameLabel();
|
||||
SyncInteractionHighlight();
|
||||
}
|
||||
|
||||
void NpcObject::SetNpcPosition(const Vec2& pos) {
|
||||
@@ -108,6 +114,19 @@ void NpcObject::SetInteractable(bool interactable) {
|
||||
}
|
||||
}
|
||||
|
||||
void NpcObject::SetInteractionHighlighted(bool highlighted) {
|
||||
if (interactionHighlighted_ == highlighted) {
|
||||
SyncInteractionHighlight();
|
||||
return;
|
||||
}
|
||||
|
||||
interactionHighlighted_ = highlighted;
|
||||
if (!interactionHighlighted_) {
|
||||
syncedHighlightCompositeVersion_ = 0;
|
||||
}
|
||||
SyncInteractionHighlight();
|
||||
}
|
||||
|
||||
void NpcObject::BeginInteract() {
|
||||
if (CanInteract()) {
|
||||
interacting_ = true;
|
||||
@@ -144,6 +163,19 @@ bool NpcObject::IsAnimationFinished() const {
|
||||
|
||||
void NpcObject::Update(float deltaTime) {
|
||||
Actor::Update(deltaTime);
|
||||
SyncInteractionHighlight();
|
||||
}
|
||||
|
||||
void NpcObject::EnsureInteractionHighlight() {
|
||||
if (interactionHighlight_) {
|
||||
return;
|
||||
}
|
||||
|
||||
interactionHighlight_ = MakePtr<InteractionHighlightSprite>();
|
||||
interactionHighlight_->SetZOrder(-1);
|
||||
interactionHighlight_->SetPosition(0.0f, 0.0f);
|
||||
interactionHighlight_->SetVisible(false);
|
||||
AddChild(interactionHighlight_);
|
||||
}
|
||||
|
||||
void NpcObject::EnsureNameLabel() {
|
||||
@@ -206,6 +238,38 @@ void NpcObject::RefreshNameLabel() {
|
||||
}
|
||||
}
|
||||
|
||||
void NpcObject::SyncInteractionHighlight() {
|
||||
if (!interactionHighlight_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!interactionHighlighted_ || !animation_ ||
|
||||
!animation_->EnsureCompositeTextureReady() ||
|
||||
!animation_->HasCompositeTexture()) {
|
||||
interactionHighlight_->SetVisible(false);
|
||||
return;
|
||||
}
|
||||
|
||||
uint64 compositeVersion = animation_->GetCompositeVersion();
|
||||
if (syncedHighlightCompositeVersion_ != compositeVersion) {
|
||||
Ptr<Texture> compositeTexture = animation_->GetCompositeTexture();
|
||||
Vec2 compositeSize = animation_->GetCompositeTextureSize();
|
||||
if (!compositeTexture || compositeSize.x <= 0.0f || compositeSize.y <= 0.0f) {
|
||||
interactionHighlight_->SetVisible(false);
|
||||
return;
|
||||
}
|
||||
|
||||
interactionHighlight_->SetTexture(compositeTexture);
|
||||
interactionHighlight_->SetSize(compositeSize);
|
||||
interactionHighlight_->SetGroundAnchorInTexture(
|
||||
animation_->GetCompositeGroundAnchorInTexture());
|
||||
interactionHighlight_->SetPosition(0.0f, 0.0f);
|
||||
syncedHighlightCompositeVersion_ = compositeVersion;
|
||||
}
|
||||
|
||||
interactionHighlight_->SetVisible(true);
|
||||
}
|
||||
|
||||
void NpcObject::SyncActorPositionFromWorld() {
|
||||
SetPosition(worldPosition_.ToScreenPosition());
|
||||
SetZOrder(worldPosition_.y);
|
||||
|
||||
@@ -11,6 +11,7 @@ constexpr char kTestMapPath[] = "map/elvengard/d_elvengard.map";
|
||||
constexpr int kTestNpcId = 2;
|
||||
constexpr float kTestNpcOffsetX = 180.0f;
|
||||
constexpr float kTestNpcOffsetY = -24.0f;
|
||||
constexpr float kNpcInteractionHighlightRadius = 40.0f;
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -111,6 +112,18 @@ void GameMapTestScene::onExit() {
|
||||
|
||||
void GameMapTestScene::Update(float deltaTime) {
|
||||
Scene::Update(deltaTime);
|
||||
|
||||
if (npc_) {
|
||||
bool shouldHighlight = false;
|
||||
if (character_) {
|
||||
shouldHighlight =
|
||||
character_->GetWorldPosition().ToGroundPosition().distance(
|
||||
npc_->GetWorldPosition().ToGroundPosition()) <=
|
||||
kNpcInteractionHighlightRadius;
|
||||
}
|
||||
npc_->SetInteractionHighlighted(shouldHighlight);
|
||||
}
|
||||
|
||||
cameraController_.Update(deltaTime);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user