Skip to content

Commit

Permalink
Merge pull request #49 from Live2D/develop
Browse files Browse the repository at this point in the history
Update to Cubism 4 SDK for Native R7
  • Loading branch information
itoh-at-live2d-com authored May 25, 2023
2 parents 993ecc6 + 3ad80ca commit 7932887
Show file tree
Hide file tree
Showing 17 changed files with 492 additions and 202 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).


## [4-r.7] - 2023-05-25

### Added

* Add some function for checking consistency of MOC3.
* Add the function of checking consistency on reviving a MOC3. (`CubismMoc::Create`)
* Add the function of checking consistency from unrevived MOC3. (`CubismMoc::HasMocConsistencyFromUnrevivedMoc`)
* Add some functions to change Multiply and Screen colors on a per part basis.

### Changed

* Change access specifier for `CubismExpressionMotion`.
* Change to get opacity according to the current time of the motion.


## [4-r.6.2] - 2023-03-16

### Fixed
Expand Down Expand Up @@ -261,6 +276,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Fix invalid expressions of `CubismCdiJson`.


[4-r.7]: https://github.com/Live2D/CubismNativeFramework/compare/4-r.6.2...4-r.7
[4-r.6.2]: https://github.com/Live2D/CubismNativeFramework/compare/4-r.6.1...4-r.6.2
[4-r.6.1]: https://github.com/Live2D/CubismNativeFramework/compare/4-r.6...4-r.6.1
[4-r.6]: https://github.com/Live2D/CubismNativeFramework/compare/4-r.5.1...4-r.6
Expand Down
32 changes: 29 additions & 3 deletions src/Model/CubismMoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@

namespace Live2D { namespace Cubism { namespace Framework {

CubismMoc* CubismMoc::Create(const csmByte* mocBytes, csmSizeInt size)
CubismMoc* CubismMoc::Create(const csmByte* mocBytes, csmSizeInt size, csmBool shouldCheckMocConsistency)
{
CubismMoc* cubismMoc = NULL;

void* alignedBuffer = CSM_MALLOC_ALLIGNED(size, Core::csmAlignofMoc);
memcpy(alignedBuffer, mocBytes, size);

if (shouldCheckMocConsistency)
{
// .moc3の整合性を確認
csmBool consistency = HasMocConsistency(alignedBuffer, size);
if (!consistency)
{
CSM_FREE_ALLIGNED(alignedBuffer);

// 整合性が確認できなければ処理しない
CubismLogError("Inconsistent MOC3.");
return cubismMoc;
}
}

Core::csmMoc* moc = Core::csmReviveMocInPlace(alignedBuffer, size);
const Core::csmMocVersion version = Core::csmGetMocVersion(alignedBuffer, size);

Expand Down Expand Up @@ -84,8 +98,20 @@ Core::csmMocVersion CubismMoc::GetMocVersion()

csmBool CubismMoc::HasMocConsistency(void* address, const csmUint32 size)
{
csmInt32 consistencyFlags = Core::csmHasMocConsistency(address, size);
return consistencyFlags != 0 ? true : false;
csmInt32 isConsistent = Core::csmHasMocConsistency(address, size);
return isConsistent != 0 ? true : false;
}

csmBool CubismMoc::HasMocConsistencyFromUnrevivedMoc(const csmByte* mocBytes, csmSizeInt size)
{
void* alignedBuffer = CSM_MALLOC_ALLIGNED(size, Core::csmAlignofMoc);
memcpy(alignedBuffer, mocBytes, size);

csmBool consistency = CubismMoc::HasMocConsistency(alignedBuffer, size);

CSM_FREE_ALLIGNED(alignedBuffer);

return consistency;
}

}}}
13 changes: 12 additions & 1 deletion src/Model/CubismMoc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class CubismMoc
*
* @param[in] mocBytes Mocファイルのバッファ
* @param[in] size バッファのサイズ
* @param[in] shouldCheckMocConsistency MOCの整合性チェックフラグ(初期値 : false)
*/
static CubismMoc* Create(const csmByte* mocBytes, csmSizeInt size);
static CubismMoc* Create(const csmByte* mocBytes, csmSizeInt size, csmBool shouldCheckMocConsistency = false);

/**
* @brief Mocデータを削除
Expand Down Expand Up @@ -85,6 +86,16 @@ class CubismMoc
*/
static csmBool HasMocConsistency(void* address, const csmUint32 size);

/**
* @brief Checks consistency of a moc.
*
* @param mocBytes Mocファイルのバッファ
* @param size バッファのサイズ
*
* @return 'true' if Moc is valid; 'false' otherwise.
*/
static csmBool HasMocConsistencyFromUnrevivedMoc(const csmByte* mocBytes, csmSizeInt size);

private:
/**
* @brief コンストラクタ
Expand Down
184 changes: 167 additions & 17 deletions src/Model/CubismModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CubismModel::CubismModel(Core::csmModel* model)
, _isOverwrittenModelMultiplyColors(false)
, _isOverwrittenModelScreenColors(false)
, _isOverwrittenCullings(false)
, _modelOpacity(1.0f)
{ }

CubismModel::~CubismModel()
Expand Down Expand Up @@ -378,15 +379,19 @@ void CubismModel::Initialize()
}
}

const csmInt32 partCount = Core::csmGetPartCount(_model);
{
const csmChar** partIds = Core::csmGetPartIds(_model);
const csmInt32 partCount = Core::csmGetPartCount(_model);

_partIds.PrepareCapacity(partCount);
for (csmInt32 i = 0; i < partCount; ++i)
{
_partIds.PushBack(CubismFramework::GetIdManager()->GetId(partIds[i]));
}

_userPartMultiplyColors.PrepareCapacity(partCount);
_userPartScreenColors.PrepareCapacity(partCount);
_partChildDrawables.Resize(partCount);
}

{
Expand All @@ -398,38 +403,69 @@ void CubismModel::Initialize()
_userScreenColors.PrepareCapacity(drawableCount);
_userCullings.PrepareCapacity(drawableCount);

// カリング設定
DrawableCullingData userCulling;
userCulling.IsOverwritten = false;
userCulling.IsCulling = 0;

// 乗算色
Rendering::CubismRenderer::CubismTextureColor multiplyColor;
multiplyColor.R = 1.0f;
multiplyColor.G = 1.0f;
multiplyColor.B = 1.0f;
multiplyColor.A = 1.0f;
DrawableColorData userMultiplyColor;
userMultiplyColor.IsOverwritten = false;
userMultiplyColor.Color = multiplyColor;

// スクリーン色
Rendering::CubismRenderer::CubismTextureColor screenColor;
screenColor.R = 0.0f;
screenColor.G = 0.0f;
screenColor.B = 0.0f;
screenColor.A = 1.0f;
DrawableColorData userScreenColor;
userScreenColor.IsOverwritten = false;
userScreenColor.Color = screenColor;

// カリング設定
DrawableCullingData userCulling;
userCulling.IsOverwritten = false;
userCulling.IsCulling = 0;

// Parts
{
// 乗算色
PartColorData userMultiplyColor;
userMultiplyColor.IsOverwritten = false;
userMultiplyColor.Color = multiplyColor;

// スクリーン色
PartColorData userScreenColor;
userScreenColor.IsOverwritten = false;
userScreenColor.Color = screenColor;

for (csmInt32 i = 0; i < partCount; ++i)
{
_userPartMultiplyColors.PushBack(userMultiplyColor);
_userPartScreenColors.PushBack(userScreenColor);
}
}

for (csmInt32 i = 0; i < drawableCount; ++i)
// Drawables
{
_drawableIds.PushBack(CubismFramework::GetIdManager()->GetId(drawableIds[i]));
_userMultiplyColors.PushBack(userMultiplyColor);
_userScreenColors.PushBack(userScreenColor);
_userCullings.PushBack(userCulling);
// 乗算色
DrawableColorData userMultiplyColor;
userMultiplyColor.IsOverwritten = false;
userMultiplyColor.Color = multiplyColor;

// スクリーン色
DrawableColorData userScreenColor;
userScreenColor.IsOverwritten = false;
userScreenColor.Color = screenColor;

for (csmInt32 i = 0; i < drawableCount; ++i)
{
_drawableIds.PushBack(CubismFramework::GetIdManager()->GetId(drawableIds[i]));
_userMultiplyColors.PushBack(userMultiplyColor);
_userScreenColors.PushBack(userScreenColor);
_userCullings.PushBack(userCulling);

csmInt32 parentIndex = Core::csmGetDrawableParentPartIndices(_model)[i];
if (parentIndex >= 0)
{
_partChildDrawables[parentIndex].PushBack(i);
}
}
}
}
}
Expand All @@ -440,6 +476,12 @@ CubismIdHandle CubismModel::GetDrawableId(csmInt32 drawableIndex) const
return CubismFramework::GetIdManager()->GetId(parameterIds[drawableIndex]);
}

CubismIdHandle CubismModel::GetPartId(csmUint32 partIndex)
{
const csmChar** partIds = Core::csmGetPartIds(_model);
return CubismFramework::GetIdManager()->GetId(partIds[partIndex]);
}

csmInt32 CubismModel::GetPartCount() const
{
const csmInt32 partCount = Core::csmGetPartCount(_model);
Expand Down Expand Up @@ -675,6 +717,60 @@ void CubismModel::SetScreenColor(csmInt32 drawableIndex, csmFloat32 r, csmFloat3
_userScreenColors[drawableIndex].Color.A = a;
}

Rendering::CubismRenderer::CubismTextureColor CubismModel::GetPartMultiplyColor(csmInt32 partIndex) const
{
return _userPartMultiplyColors[partIndex].Color;
}

Rendering::CubismRenderer::CubismTextureColor CubismModel::GetPartScreenColor(csmInt32 partIndex) const
{
return _userPartScreenColors[partIndex].Color;
}

void CubismModel::SetPartMultiplyColor(csmInt32 partIndex, const Rendering::CubismRenderer::CubismTextureColor& color)
{
SetPartMultiplyColor(partIndex, color.R, color.G, color.B, color.A);
}

void CubismModel::SetPartColor(
csmUint32 partIndex,
csmFloat32 r, csmFloat32 g, csmFloat32 b, csmFloat32 a,
csmVector<PartColorData>& partColors,
csmVector <DrawableColorData>& drawableColors)
{
partColors[partIndex].Color.R = r;
partColors[partIndex].Color.G = g;
partColors[partIndex].Color.B = b;
partColors[partIndex].Color.A = a;

if (partColors[partIndex].IsOverwritten)
{
for (csmUint32 i = 0; i < _partChildDrawables[partIndex].GetSize(); i++)
{
csmUint32 drawableIndex = _partChildDrawables[partIndex][i];
drawableColors[drawableIndex].Color.R = r;
drawableColors[drawableIndex].Color.G = g;
drawableColors[drawableIndex].Color.B = b;
drawableColors[drawableIndex].Color.A = a;
}
}
}

void CubismModel::SetPartMultiplyColor(csmInt32 partIndex, csmFloat32 r, csmFloat32 g, csmFloat32 b, csmFloat32 a)
{
SetPartColor(partIndex, r, g, b, a, _userPartMultiplyColors, _userMultiplyColors);
}

void CubismModel::SetPartScreenColor(csmInt32 partIndex, const Rendering::CubismRenderer::CubismTextureColor& color)
{
SetPartScreenColor(partIndex, color.R, color.G, color.B, color.A);
}

void CubismModel::SetPartScreenColor(csmInt32 partIndex, csmFloat32 r, csmFloat32 g, csmFloat32 b, csmFloat32 a)
{
SetPartColor(partIndex, r, g, b, a, _userPartScreenColors, _userScreenColors);
}

csmBool CubismModel::GetOverwriteFlagForModelMultiplyColors() const
{
return _isOverwrittenModelMultiplyColors;
Expand Down Expand Up @@ -715,6 +811,50 @@ void CubismModel::SetOverwriteFlagForDrawableScreenColors(csmUint32 drawableInde
_userScreenColors[drawableIndex].IsOverwritten = value;
}

csmBool CubismModel::GetOverwriteColorForPartMultiplyColors(csmInt32 partIndex) const
{
return _userPartMultiplyColors[partIndex].IsOverwritten;
}

csmBool CubismModel::GetOverwriteColorForPartScreenColors(csmInt32 partIndex) const
{
return _userPartScreenColors[partIndex].IsOverwritten;
}

void CubismModel::SetOverwriteColorForPartColors(
csmUint32 partIndex,
csmBool value,
csmVector<PartColorData>& partColors,
csmVector <DrawableColorData>& drawableColors)
{
partColors[partIndex].IsOverwritten = value;

for (csmUint32 i = 0; i < _partChildDrawables[partIndex].GetSize(); i++)
{
csmUint32 drawableIndex = _partChildDrawables[partIndex][i];
drawableColors[drawableIndex].IsOverwritten = value;
if (value)
{
drawableColors[drawableIndex].Color.R = partColors[partIndex].Color.R;
drawableColors[drawableIndex].Color.G = partColors[partIndex].Color.G;
drawableColors[drawableIndex].Color.B = partColors[partIndex].Color.B;
drawableColors[drawableIndex].Color.A = partColors[partIndex].Color.A;
}
}
}

void CubismModel::SetOverwriteColorForPartMultiplyColors(csmUint32 partIndex, csmBool value)
{
_userPartMultiplyColors[partIndex].IsOverwritten = value;
SetOverwriteColorForPartColors(partIndex, value, _userPartMultiplyColors, _userMultiplyColors);
}

void CubismModel::SetOverwriteColorForPartScreenColors(csmUint32 partIndex, csmBool value)
{
_userPartScreenColors[partIndex].IsOverwritten = value;
SetOverwriteColorForPartColors(partIndex, value, _userPartScreenColors, _userScreenColors);
}

csmInt32 CubismModel::GetDrawableCulling(csmInt32 drawableIndex) const
{
if (GetOverwriteFlagForModelCullings() || GetOverwriteFlagForDrawableCullings(drawableIndex))
Expand Down Expand Up @@ -751,6 +891,16 @@ void CubismModel::SetOverwriteFlagForDrawableCullings(csmUint32 drawableIndex, c
_userCullings[drawableIndex].IsOverwritten = value;
}

csmFloat32 CubismModel::GetModelOpacity()
{
return _modelOpacity;
}

void CubismModel::SetModelOpacity(csmFloat32 value)
{
_modelOpacity = value;
}

Core::csmModel* CubismModel::GetModel() const
{
return _model;
Expand Down
Loading

0 comments on commit 7932887

Please sign in to comment.