Skip to content

Commit

Permalink
增加 Live 角色追踪模式相机平滑移动功能
Browse files Browse the repository at this point in the history
  • Loading branch information
chinosk6 committed Dec 20, 2023
1 parent 4c8212a commit 452da56
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 10 deletions.
7 changes: 6 additions & 1 deletion resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
"moveStep": 0.1,
"enableLiveDofController": false,
"close_all_blur": false,
"setLiveFovAsGlobal": false
"setLiveFovAsGlobal": false,
"followUmaSmoothCamera": {
"enable": false,
"lookatStep": 0.01,
"positionStep": 0.001
}
},
"homeSettings": {
"free_camera": false
Expand Down
21 changes: 21 additions & 0 deletions resources/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@
"description": "将 Live 的 FOV 应用到游戏全局",
"type": "boolean",
"default": false
},
"followUmaSmoothCamera": {
"description": "角色追踪模式下平滑移动视角",
"type": "object",
"properties": {
"enable": {
"description": "是否启用",
"type": "boolean",
"default": false
},
"lookatStep": {
"description": "LookAt 移动步幅",
"type": "number",
"default": 0.01
},
"positionStep": {
"description": "Position 移动步幅",
"type": "number",
"default": 0.001
}
}
}
},
"additionalProperties": false
Expand Down
21 changes: 21 additions & 0 deletions resources/config_en.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@
"description": "Set live FOV to global",
"type": "boolean",
"default": false
},
"followUmaSmoothCamera": {
"description": "Smooth move camera in follow umamusume mode",
"type": "object",
"properties": {
"enable": {
"description": "Enable",
"type": "boolean",
"default": false
},
"lookatStep": {
"description": "LookAt step",
"type": "number",
"default": 0.01
},
"positionStep": {
"description": "Position step",
"type": "number",
"default": 0.001
}
}
}
},
"additionalProperties": false
Expand Down
21 changes: 21 additions & 0 deletions resources/config_zh_tw.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@
"description": "將 Live 的 FOV 應用到遊戲全域",
"type": "boolean",
"default": false
},
"followUmaSmoothCamera": {
"description": "角色追跡模式下平滑移動視角",
"type": "object",
"properties": {
"enable": {
"description": "是否啟用",
"type": "boolean",
"default": false
},
"lookatStep": {
"description": "LookAt 移動步幅",
"type": "number",
"default": 0.01
},
"positionStep": {
"description": "Position 移動步幅",
"type": "number",
"default": 0.001
}
}
}
},
"additionalProperties": false
Expand Down
66 changes: 60 additions & 6 deletions src/camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,39 @@ namespace UmaCamera {
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}

static float calculateDistance(const Vector3& v1, const Vector3& v2) {
float dx = v2.x - v1.x;
float dy = v2.y - v1.y;
float dz = v2.z - v1.z;

return std::sqrt(dx * dx + dy * dy + dz * dz);
}

float calculateDistance(const Vector3& v1) {
return calculateDistance(*this, v1);
}

// 向指定点移动指定距离
void moveAlongVector(const Vector3& b, float distanceToMove) {
if (distanceToMove == 0.0f) return;
const auto direction = (b - *this).normalized();
const auto newPos = *this + (direction * distanceToMove);
this->x = newPos.x;
this->y = newPos.y;
this->z = newPos.z;
}

void moveAlongVectorByPercentage(const CameraCalc::Vector3& targetPoint, float percentage, float maxDistance = 0.2f) {
const auto distance = calculateDistance(targetPoint);

if (distance != 0) {
if (distance >= maxDistance) {
percentage *= distance / maxDistance * 2.5;
}
moveAlongVector(targetPoint, distance * percentage);
}
}
};


Expand Down Expand Up @@ -486,13 +519,16 @@ namespace UmaCamera {
cameraPos.z = z;
}

void updateFollowCameraPosByLookatAndOffset() {
auto nowAngel = liveFollowCameraOffset.x * M_PI / 180;

cameraPos.x = cameraLookAt.x - sin(nowAngel) * liveFollowCameraOffset.z;
cameraPos.z = cameraLookAt.z - cos(nowAngel) * liveFollowCameraOffset.z;
cameraPos.y = cameraLookAt.y + liveFollowCameraOffset.y;
void updateFollowCameraPosByTargetPosAndOffset(const Vector3_t& target) {
const auto nowAngel = liveFollowCameraOffset.x * M_PI / 180;

cameraPos.x = target.x - sin(nowAngel) * liveFollowCameraOffset.z;
cameraPos.z = target.z - cos(nowAngel) * liveFollowCameraOffset.z;
cameraPos.y = target.y + liveFollowCameraOffset.y;
}

void updateFollowCameraPosByLookatAndOffset() {
updateFollowCameraPosByTargetPosAndOffset(cameraLookAt);
}

void SetCameraLookat(float x, float y, float z) {
Expand All @@ -504,7 +540,25 @@ namespace UmaCamera {
cameraLookAt.x = x;
cameraLookAt.y = y;
cameraLookAt.z = z;
}

void SetCameraLookatAndPosSmooth(float x, float y, float z) {
if (isLiveStart && (liveCameraType == LiveCamera_FOLLOW_UMA)) {
x += liveFollowCameraLookatOffset.x;
y += liveFollowCameraLookatOffset.y;
z += liveFollowCameraLookatOffset.z;
}

CameraCalc::Vector3 newLookAt(cameraLookAt);
CameraCalc::Vector3 newPosLookAt(cameraLookAt);
const CameraCalc::Vector3 targetPos(x, y, z);
newLookAt.moveAlongVectorByPercentage(targetPos, g_live_follow_uma_smooth_lookat_step, 0.6f);
newPosLookAt.moveAlongVectorByPercentage(targetPos, g_live_follow_uma_smooth_pos_step, 0.6f);

cameraLookAt.x = newLookAt.x;
cameraLookAt.y = newLookAt.y;
cameraLookAt.z = newLookAt.z;
updateFollowCameraPosByTargetPosAndOffset({newPosLookAt.x, newPosLookAt.y, newPosLookAt.z});
}

int GetLiveCameraType() {
Expand Down
2 changes: 2 additions & 0 deletions src/camera/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace UmaCamera {
Vector3_t getCameraLookat();
void SetCameraPos(float x, float y, float z);
void SetCameraLookat(float x, float y, float z);
void SetCameraLookatAndPosSmooth(float x, float y, float z);
void updateFollowCameraPosByTargetPosAndOffset(const Vector3_t& target);
void updateFollowCameraPosByLookatAndOffset();
void loadGlobalData();
void setMoveStep(float value);
Expand Down
11 changes: 8 additions & 3 deletions src/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2273,9 +2273,14 @@ namespace

if (isFollowUma) {
// printf("GetCharacterWorldPos: %d (%f, %f, %f)\n", posFlag, ret->x, ret->y, ret->z);
UmaCamera::SetCameraLookat(ret->x, ret->y, ret->z);
// UmaCamera::SetCameraPos(ret->x, ret->y, ret->z + 2.0f, true);
UmaCamera::updateFollowCameraPosByLookatAndOffset();
if (g_enable_live_follow_uma_smooth) {
UmaCamera::SetCameraLookatAndPosSmooth(ret->x, ret->y, ret->z);
}
else {
UmaCamera::SetCameraLookat(ret->x, ret->y, ret->z);
UmaCamera::updateFollowCameraPosByLookatAndOffset();
}

}

return ret;
Expand Down
10 changes: 10 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ float g_virtual_resolution_multiple = 1.0f;
int g_vsync_count = 0;

bool g_live_free_camera = false;
bool g_enable_live_follow_uma_smooth = false;
float g_live_follow_uma_smooth_lookat_step = 0.01f;
float g_live_follow_uma_smooth_pos_step = 0.001f;
bool g_live_force_changeVisibility_false = false;
bool g_live_close_all_blur = false;
float g_live_move_step = 0.2;
Expand Down Expand Up @@ -591,6 +594,13 @@ namespace
g_set_live_fov_as_global = document["live"]["setLiveFovAsGlobal"].GetBool();
UmaCamera::setLiveStart(g_set_live_fov_as_global);
}

if (document["live"].HasMember("followUmaSmoothCamera")) {
auto& followUmaSmoothCamera = document["live"]["followUmaSmoothCamera"];
g_enable_live_follow_uma_smooth = followUmaSmoothCamera["enable"].GetBool();
g_live_follow_uma_smooth_lookat_step = followUmaSmoothCamera["lookatStep"].GetFloat();
g_live_follow_uma_smooth_pos_step = followUmaSmoothCamera["positionStep"].GetFloat();
}
}

if (document.HasMember("race_camera")) {
Expand Down
3 changes: 3 additions & 0 deletions src/stdinclude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ extern float g_virtual_resolution_multiple;
extern int g_vsync_count;
extern bool g_live_free_camera;
extern bool g_live_force_changeVisibility_false;
extern bool g_enable_live_follow_uma_smooth;
extern float g_live_follow_uma_smooth_lookat_step;
extern float g_live_follow_uma_smooth_pos_step;
extern bool g_live_close_all_blur;
extern float g_live_move_step;
extern bool g_set_live_fov_as_global;
Expand Down

0 comments on commit 452da56

Please sign in to comment.