1. 规范了一些Live2D实例代码的内容

2. 增加了窗口大小随着模型大小变化而变化的功能
This commit is contained in:
Misaki
2025-12-05 17:26:18 +08:00
parent 5c007aa8d9
commit 09a4492f07
12 changed files with 120 additions and 80 deletions
+21 -2
View File
@@ -48,6 +48,25 @@ public:
*/
int getTapBodyMotionCount();
/**
* @brief 获取 Live2D 模型的 Canvas 宽度像素 (在 Live2D 坐标系下)
* @return Canvas 宽度
*/
Live2D::Cubism::Framework::csmFloat32 GetModelCanvasWidthPixel() const
{
// _model 是 CubismModel 的基类指针
return _model ? _model->GetCanvasWidthPixel() : 0.0f;
}
/**
* @brief 获取 Live2D 模型的 Canvas 高度像素 (在 Live2D 坐标系下)
* @return Canvas 高度
*/
Live2D::Cubism::Framework::csmFloat32 GetModelCanvasHeightPixel() const
{
return _model ? _model->GetCanvasHeightPixel() : 0.0f;
}
/**
* @brief 启动唇形同步并播放指定的 WAV 文件
* @param filePath WAV 文件的路径(csmString 类型)
@@ -64,8 +83,8 @@ public:
/**
* @brief model3.jsonが置かれたディレクトリとファイルパスからモデルを生成する
*
* @brief model3.jsonが置かれたディレクトリとファイルパスからモデルを生成する \n
* 从 model3.json 所在的目录和文件路径生成模型
*/
void LoadAssets(const Csm::csmChar* dir, const Csm::csmChar* fileName);
+6 -6
View File
@@ -63,7 +63,7 @@ bool LAppDelegate::Initialize(GLCore* window)
// Windowの生成_
//_window = glfwCreateWindow(RenderTargetWidth, RenderTargetHeight, "SAMPLE", NULL, NULL);
_window = window; // Misaki 修改
if (_window == NULL)
if (_window == nullptr)
{
if (DebugLogEnable)
{
@@ -217,7 +217,7 @@ void LAppDelegate::update()
LAppDelegate::LAppDelegate():
_cubismOption(),
_window(NULL),
_window(nullptr),
_captured(false),
_mouseX(0.0f),
_mouseY(0.0f),
@@ -259,7 +259,7 @@ void LAppDelegate::InitializeCubism()
void LAppDelegate::OnMouseCallBack(GLFWwindow* window, int button, int action, int modify)
{
if (_view == NULL)
if (_view == nullptr)
{
return;
}
@@ -292,7 +292,7 @@ void LAppDelegate::OnMouseCallBack(GLFWwindow* window, double x, double y)
{
return;
}
if (_view == NULL)
if (_view == nullptr)
{
return;
}
@@ -313,7 +313,7 @@ GLuint LAppDelegate::CreateShader()
" gl_Position = vec4(position, 1.0);"
" vuv = uv;"
"}";
glShaderSource(vertexShaderId, 1, &vertexShader, NULL);
glShaderSource(vertexShaderId, 1, &vertexShader, nullptr);
glCompileShader(vertexShaderId);
if(!CheckShader(vertexShaderId))
{
@@ -330,7 +330,7 @@ GLuint LAppDelegate::CreateShader()
"void main(void){"
" gl_FragColor = texture2D(texture, vuv) * baseColor;"
"}";
glShaderSource(fragmentShaderId, 1, &fragmentShader, NULL);
glShaderSource(fragmentShaderId, 1, &fragmentShader, nullptr);
glCompileShader(fragmentShaderId);
if (!CheckShader(fragmentShaderId))
{
+16 -11
View File
@@ -29,7 +29,7 @@ using namespace LAppDefine;
using namespace std;
namespace {
LAppLive2DManager* s_instance = NULL;
LAppLive2DManager* s_instance = nullptr;
void FinishedMotion(ACubismMotion* self)
{
@@ -45,7 +45,7 @@ namespace {
LAppLive2DManager* LAppLive2DManager::GetInstance()
{
if (s_instance == NULL)
if (s_instance == nullptr)
{
s_instance = new LAppLive2DManager();
}
@@ -55,22 +55,22 @@ LAppLive2DManager* LAppLive2DManager::GetInstance()
void LAppLive2DManager::ReleaseInstance()
{
if (s_instance != NULL)
if (s_instance != nullptr)
{
delete s_instance;
}
s_instance = NULL;
s_instance = nullptr;
}
LAppLive2DManager::LAppLive2DManager()
: _viewMatrix(NULL)
: _viewMatrix(nullptr)
, _sceneIndex(0)
{
_viewMatrix = new CubismMatrix44();
//SetUpModel();
// Resources/Haru/ Haru.model3.json
LoadModelFromPath("Resources/Live2DModels/KITU17/", "KITU17.model3.json");
LoadModelFromPath("Resources/Live2DModels/KITU17/", "KITU17.model3.json"); // 默认加载的模型
//ChangeScene(_sceneIndex);
}
@@ -140,7 +140,7 @@ void LAppLive2DManager::SetUpModel()
if (fs::exists(modelJson))
_modelDir.PushBack(csmString(dirName.c_str()));
}
/* 保持与原代码相同的排序 */
// 保持与原代码相同的排序
qsort(_modelDir.GetPtr(), _modelDir.GetSize(), sizeof(csmString), CompareCsmString);
#endif
}
@@ -162,7 +162,7 @@ LAppModel* LAppLive2DManager::GetModel(csmUint32 no) const
return _models[no];
}
return NULL;
return nullptr;
}
/**
@@ -229,7 +229,7 @@ void LAppLive2DManager::OnUpdate() const
CubismMatrix44 projection;
LAppModel* model = GetModel(i);
if (model->GetModel() == NULL)
if (model->GetModel() == nullptr)
{
LAppPal::PrintLogLn("Failed to model->GetModel().");
continue;
@@ -247,7 +247,7 @@ void LAppLive2DManager::OnUpdate() const
}
// 必要があればここで乗算
if (_viewMatrix != NULL)
if (_viewMatrix != nullptr)
{
projection.MultiplyByMatrix(_viewMatrix);
}
@@ -262,7 +262,7 @@ void LAppLive2DManager::OnUpdate() const
LAppDelegate::GetInstance()->GetView()->PostModelDraw(*model);
}
}
#include <AppContext.h>
void LAppLive2DManager::LoadModelFromPath(const std::string& modelPath, const std::string& fileName)
{
csmString modelPathStr(modelPath.c_str());
@@ -272,6 +272,11 @@ void LAppLive2DManager::LoadModelFromPath(const std::string& modelPath, const st
_models.PushBack(new LAppModel()); // 这样在加载的时候都使用的models[0]这一个位置,自行实现模型选择器要注意注意
_models[0]->LoadAssets(modelPathStr.GetRawString(), modelJsonName.GetRawString());
// 加载完后根据模型大小来重新设置当前窗口大小
const int width = static_cast<int>(_models[0]->GetModel()->GetCanvasWidthPixel() / 15.0);
const int height = static_cast<int>(_models[0]->GetModel()->GetCanvasHeightPixel() / 15.0);
AppContext::GetGLCore()->setWindowSize(width, height);
LAppPal::PrintLogLn("[APP]窗口尺寸重新设置为: W: %d H: %d", width, height);
/*
* 提供一个半透明表示模型的示例。
* 如果定义了USE_RENDER_TARGET或USE_MODEL_RENDER_TARGET
+18 -18
View File
@@ -47,7 +47,7 @@ namespace {
LAppModel::LAppModel()
: CubismUserModel()
, _modelSetting(NULL)
, _modelSetting(nullptr)
, _userTimeSeconds(0.0f)
{
if (MocConsistencyValidationEnable)
@@ -101,7 +101,7 @@ void LAppModel::LoadAssets(const csmChar* dir, const csmChar* fileName)
SetupModel(setting);
if (_model == NULL)
if (_model == nullptr)
{
LAppPal::PrintLogLn("Failed to LoadAssets().");
return;
@@ -123,7 +123,7 @@ void LAppModel::SetupModel(ICubismModelSetting* setting)
csmSizeInt size;
//Cubism Model
if (strcmp(_modelSetting->GetModelFileName(), "") != 0)
if (strcmp(_modelSetting->GetModelFileName(), "") != 0) // 如果模型文件名不为空
{
csmString path = _modelSetting->GetModelFileName();
path = _modelHomeDir + path;
@@ -153,10 +153,10 @@ void LAppModel::SetupModel(ICubismModelSetting* setting)
if (motion)
{
if (_expressions[name] != NULL)
if (_expressions[name] != nullptr)
{
ACubismMotion::Delete(_expressions[name]);
_expressions[name] = NULL;
_expressions[name] = nullptr;
}
_expressions[name] = motion;
}
@@ -240,7 +240,7 @@ void LAppModel::SetupModel(ICubismModelSetting* setting)
}
}
if (_modelSetting == NULL || _modelMatrix == NULL)
if (_modelSetting == nullptr || _modelMatrix == nullptr)
{
LAppPal::PrintLogLn("Failed to SetupModel().");
return;
@@ -258,11 +258,11 @@ void LAppModel::SetupModel(ICubismModelSetting* setting)
const csmChar* group = _modelSetting->GetMotionGroupName(i);
PreloadMotionGroup(group);
}
_motionManager->StopAllMotions();
_updating = false;
_initialized = true;
LAppPal::PrintLogLn("[APP]当前模型像素大小: H: %lf W: %lf", GetModelCanvasHeightPixel(), GetModelCanvasWidthPixel());
}
void LAppModel::PreloadMotionGroup(const csmChar* group)
@@ -301,7 +301,7 @@ void LAppModel::PreloadMotionGroup(const csmChar* group)
}
tmpMotion->SetEffectIds(_eyeBlinkIds, _lipSyncIds);
if (_motions[name] != NULL)
if (_motions[name] != nullptr)
{
ACubismMotion::Delete(_motions[name]);
}
@@ -389,14 +389,14 @@ void LAppModel::Update()
// まばたき 眨眼
if (!motionUpdated)
{
if (_eyeBlink != NULL)
if (_eyeBlink != nullptr)
{
// メインモーションの更新がないとき 当主动作没有更新时
_eyeBlink->UpdateParameters(_model, deltaTimeSeconds); // 目パチ
}
}
if (_expressionManager != NULL)
if (_expressionManager != nullptr)
{
_expressionManager->UpdateMotion(_model, deltaTimeSeconds); // 表情でパラメータ更新(相対変化)
}
@@ -415,13 +415,13 @@ void LAppModel::Update()
_model->AddParameterValue(_idParamEyeBallY, _dragY);
// 呼吸など
if (_breath != NULL)
if (_breath != nullptr)
{
_breath->UpdateParameters(_model, deltaTimeSeconds);
}
// 物理演算の設定
if (_physics != NULL)
if (_physics != nullptr)
{
_physics->Evaluate(_model, deltaTimeSeconds);
}
@@ -455,7 +455,7 @@ void LAppModel::Update()
}
// ポーズの設定
if (_pose != NULL)
if (_pose != nullptr)
{
_pose->UpdateParameters(_model, deltaTimeSeconds);
}
@@ -516,7 +516,7 @@ CubismMotionQueueEntryHandle LAppModel::StartMotion(const csmChar* group, csmInt
CubismMotion* motion = static_cast<CubismMotion*>(_motions[name.GetRawString()]);
csmBool autoDelete = false;
if (motion == NULL)
if (motion == nullptr)
{
csmString path = fileName;
path = _modelHomeDir + path;
@@ -524,7 +524,7 @@ CubismMotionQueueEntryHandle LAppModel::StartMotion(const csmChar* group, csmInt
csmByte* buffer;
csmSizeInt size;
buffer = CreateBuffer(path.GetRawString(), &size);
motion = static_cast<CubismMotion*>(LoadMotion(buffer, size, NULL, onFinishedMotionHandler));
motion = static_cast<CubismMotion*>(LoadMotion(buffer, size, nullptr, onFinishedMotionHandler));
if (motion)
{
@@ -618,7 +618,7 @@ int LAppModel::getTapBodyMotionCount()
void LAppModel::DoDraw()
{
if (_model == NULL)
if (_model == nullptr)
{
return;
}
@@ -628,7 +628,7 @@ void LAppModel::DoDraw()
void LAppModel::Draw(CubismMatrix44& matrix)
{
if (_model == NULL)
if (_model == nullptr)
{
return;
}
@@ -667,7 +667,7 @@ void LAppModel::SetExpression(const csmChar* expressionID)
LAppPal::PrintLogLn("[APP]expression: [%s]", expressionID);
}
if (motion != NULL)
if (motion != nullptr)
{
_expressionManager->StartMotionPriority(motion, false, PriorityForce);
}
+3 -3
View File
@@ -25,7 +25,7 @@ LAppView::LAppView():
//_back(NULL),
//_gear(NULL),
//_power(NULL),
_renderSprite(NULL),
_renderSprite(nullptr),
_renderTarget(SelectTarget_None)
{
_clearColor[0] = 1.0f;
@@ -268,7 +268,7 @@ float LAppView::TransformScreenY(float deviceY) const
void LAppView::PreModelDraw(LAppModel& refModel)
{
// 別のレンダリングターゲットへ向けて描画する場合の使用するフレームバッファ
Csm::Rendering::CubismOffscreenSurface_OpenGLES2* useTarget = NULL;
Csm::Rendering::CubismOffscreenSurface_OpenGLES2* useTarget = nullptr;
if (_renderTarget != SelectTarget_None)
{// 別のレンダリングターゲットへ向けて描画する場合
@@ -299,7 +299,7 @@ void LAppView::PreModelDraw(LAppModel& refModel)
void LAppView::PostModelDraw(LAppModel& refModel)
{
// 別のレンダリングターゲットへ向けて描画する場合の使用するフレームバッファ
Csm::Rendering::CubismOffscreenSurface_OpenGLES2* useTarget = NULL;
Csm::Rendering::CubismOffscreenSurface_OpenGLES2* useTarget = nullptr;
if (_renderTarget != SelectTarget_None)
{// 別のレンダリングターゲットへ向けて描画する場合