Skip to content

Commit ab1abd9

Browse files
committed
fix #61
1 parent 15c6f9a commit ab1abd9

File tree

10 files changed

+1920
-907
lines changed

10 files changed

+1920
-907
lines changed

Framework/src/Model/CubismModel.hpp

Lines changed: 898 additions & 901 deletions
Large diffs are not rendered by default.

Framework/src/Model/CubismModel.hpp.bak

Lines changed: 896 additions & 0 deletions
Large diffs are not rendered by default.

Main/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_subdirectory(src)
22

33
# 在配置阶段立即执行文件修改脚本
4-
include(${CMAKE_CURRENT_SOURCE_DIR}/insert_code.cmake)
4+
include(${CMAKE_CURRENT_SOURCE_DIR}/patch_1.cmake)
5+
include(${CMAKE_CURRENT_SOURCE_DIR}/patch_2.cmake)

Main/patch_2.cmake

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# BY TONGYILINGMA
2+
3+
# 源文件路径
4+
set(INPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../Framework/src/Model/CubismModel.hpp)
5+
# 备份文件路径
6+
set(BACKUP_FILE ${INPUT_FILE}.bak)
7+
8+
if(NOT EXISTS "${BACKUP_FILE}")
9+
# 设置要插入的代码
10+
set(INSERT_INCLUDE "#include <HackProperties.h>\n")
11+
set(INSERT_ADDITIONAL_PROPERTIES " __ADDITIONAL_METHODS__\n")
12+
13+
# 读取整个文件到变量
14+
file(STRINGS ${INPUT_FILE} FILE_CONTENTS)
15+
16+
# 初始化输出内容为空字符串
17+
set(OUTPUT_CONTENTS "")
18+
set(HAS_INSERTED_INCLUDE FALSE)
19+
set(HAS_INSERTED_PROPERTIES FALSE)
20+
21+
# 遍历每一行,检查是否需要插入代码
22+
foreach(line IN LISTS FILE_CONTENTS)
23+
# 插入 include 语句
24+
if("${line}" MATCHES "^#include \"Id/CubismId.hpp\"$" AND NOT HAS_INSERTED_INCLUDE)
25+
set(OUTPUT_CONTENTS "${OUTPUT_CONTENTS}${line}\n")
26+
set(OUTPUT_CONTENTS "${OUTPUT_CONTENTS}${INSERT_INCLUDE}")
27+
set(HAS_INSERTED_INCLUDE TRUE)
28+
elseif("${line}" MATCHES "^public:$" AND NOT HAS_INSERTED_PROPERTIES)
29+
set(OUTPUT_CONTENTS "${OUTPUT_CONTENTS}${line}\n")
30+
set(OUTPUT_CONTENTS "${OUTPUT_CONTENTS}${INSERT_ADDITIONAL_PROPERTIES}")
31+
set(HAS_INSERTED_PROPERTIES TRUE)
32+
else()
33+
set(OUTPUT_CONTENTS "${OUTPUT_CONTENTS}${line}\n")
34+
endif()
35+
endforeach()
36+
37+
# 将原始文件复制为备份
38+
file(COPY_FILE ${INPUT_FILE} ${BACKUP_FILE})
39+
40+
# 将最终的内容写回原文件
41+
file(WRITE ${INPUT_FILE} "${OUTPUT_CONTENTS}")
42+
43+
message("patched")
44+
endif()

Main/src/HackProperties.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,18 @@
88
std::string group; \
99
int no;
1010

11+
#define __ADDITIONAL_METHODS__ \
12+
void AddAndSaveParameterValue(const Csm::CubismId* parameterId, float value, float weight = 1.0f)\
13+
{\
14+
const csmInt32 index = GetParameterIndex(parameterId);\
15+
AddParameterValue(index, value, weight);\
16+
_savedParameters[index] = _parameterValues[index];\
17+
}\
18+
void SetAndSaveParameterValue(const Csm::CubismId* parameterId, float value, float weight = 1.0f)\
19+
{\
20+
const csmInt32 index = GetParameterIndex(parameterId);\
21+
SetParameterValue(index, value, weight);\
22+
_savedParameters[index] = _parameterValues[index];\
23+
}\
1124

1225
#endif // HACKPROPERTIES_H

Main/src/LAppModel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,13 +732,13 @@ bool LAppModel::IsMotionFinished()
732732
void LAppModel::SetParameterValue(const char *paramId, float value, float weight)
733733
{
734734
const Csm::CubismId *paramHanle = CubismFramework::GetIdManager()->GetId(paramId);
735-
_model->SetParameterValue(paramHanle, value, weight);
735+
_model->SetAndSaveParameterValue(paramHanle, value, weight);
736736
}
737737

738738
void LAppModel::AddParameterValue(const char *paramId, float value)
739739
{
740740
const Csm::CubismId *paramHanle = CubismFramework::GetIdManager()->GetId(paramId);
741-
_model->AddParameterValue(paramHanle, value);
741+
_model->AddAndSaveParameterValue(paramHanle, value);
742742
}
743743

744744
void LAppModel::SetAutoBreathEnable(bool enable)
@@ -968,6 +968,7 @@ void LAppModel::ResetParameters()
968968
{
969969
_parameterValues[i] = _defaultParameterValues[i];
970970
}
971+
_model->SaveParameters();
971972
}
972973

973974
void LAppModel::ResetPose()

cmake/Main.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
add_subdirectory(Main/src)
2+
add_subdirectory(Main)
33

44
set_property(TARGET Main PROPERTY CXX_STANDARD 17)
55
set_property(TARGET Main PROPERTY CXX_STANDARD_REQUIRED ON)

package/main_pygame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def main():
6767
model.SetAutoBreathEnable(False)
6868

6969
wavHandler = WavHandler()
70-
lipSyncN = 2.5
70+
lipSyncN = 3
7171

7272
audioPlayed = False
7373

@@ -192,7 +192,7 @@ def getHitFeedback(x, y):
192192

193193
if wavHandler.Update():
194194
# 利用 wav 响度更新 嘴部张合
195-
model.AddParameterValue(
195+
model.SetParameterValue(
196196
StandardParams.ParamMouthOpenY, wavHandler.GetRms() * lipSyncN
197197
)
198198

package/test_set_parameter.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 测试 v3 SetParameterValue 是否正确
2+
# 测试 v3 SetParameterValue 是否能在 Update 和 Draw 之外的地方调用后起作用
3+
4+
import os
5+
import pygame
6+
import live2d.v3 as live2d
7+
import resources
8+
from live2d.v3.params import StandardParams
9+
10+
11+
def main():
12+
pygame.init()
13+
live2d.init()
14+
15+
display = (300, 400)
16+
pygame.display.set_mode(display, pygame.DOUBLEBUF | pygame.OPENGL)
17+
pygame.display.set_caption("pygame window")
18+
19+
live2d.glewInit()
20+
21+
model = live2d.LAppModel()
22+
23+
model.LoadModelJson(
24+
os.path.join(resources.RESOURCES_DIRECTORY, "v3/Haru/Haru.model3.json")
25+
)
26+
27+
model.Resize(*display)
28+
29+
model.SetAutoBlinkEnable(False) # 防止影响测试
30+
31+
# 左眼闭上
32+
model.SetParameterValue(StandardParams.ParamEyeLOpen, 0)
33+
# 右眼睁开
34+
model.SetParameterValue(StandardParams.ParamEyeROpen, 1)
35+
# 张嘴
36+
model.SetParameterValue(StandardParams.ParamMouthOpenY, 1)
37+
38+
running = True
39+
while True:
40+
for event in pygame.event.get():
41+
if event.type == pygame.QUIT:
42+
running = False
43+
break
44+
if event.type == pygame.MOUSEMOTION:
45+
model.Drag(*pygame.mouse.get_pos())
46+
47+
if not running:
48+
break
49+
50+
live2d.clearBuffer()
51+
model.Update()
52+
model.Draw()
53+
54+
pygame.display.flip()
55+
56+
live2d.dispose()
57+
58+
pygame.quit()
59+
60+
if __name__ == "__main__":
61+
main()

0 commit comments

Comments
 (0)