Update to Cubism 4 SDK for Web R1
This commit is contained in:
100
src/model/cubismmoc.ts
Normal file
100
src/model/cubismmoc.ts
Normal file
@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
import { Live2DCubismFramework as cubismmodel } from './cubismmodel';
|
||||
import CubismModel = cubismmodel.CubismModel;
|
||||
import { CSM_ASSERT } from '../utils/cubismdebug';
|
||||
|
||||
export namespace Live2DCubismFramework {
|
||||
/**
|
||||
* Mocデータの管理
|
||||
*
|
||||
* Mocデータの管理を行うクラス。
|
||||
*/
|
||||
export class CubismMoc {
|
||||
/**
|
||||
* Mocデータの作成
|
||||
*/
|
||||
public static create(mocBytes: ArrayBuffer): CubismMoc {
|
||||
let cubismMoc: CubismMoc = null;
|
||||
const moc: Live2DCubismCore.Moc = Live2DCubismCore.Moc.fromArrayBuffer(
|
||||
mocBytes
|
||||
);
|
||||
|
||||
if (moc) {
|
||||
cubismMoc = new CubismMoc(moc);
|
||||
}
|
||||
|
||||
return cubismMoc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mocデータを削除
|
||||
*
|
||||
* Mocデータを削除する
|
||||
*/
|
||||
public static delete(moc: CubismMoc): void {
|
||||
moc._moc._release();
|
||||
moc._moc = null;
|
||||
moc = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* モデルを作成する
|
||||
*
|
||||
* @return Mocデータから作成されたモデル
|
||||
*/
|
||||
createModel(): CubismModel {
|
||||
let cubismModel: CubismModel = null;
|
||||
|
||||
const model: Live2DCubismCore.Model = Live2DCubismCore.Model.fromMoc(
|
||||
this._moc
|
||||
);
|
||||
|
||||
if (model) {
|
||||
cubismModel = new CubismModel(model);
|
||||
cubismModel.initialize();
|
||||
|
||||
++this._modelCount;
|
||||
}
|
||||
|
||||
return cubismModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* モデルを削除する
|
||||
*/
|
||||
deleteModel(model: CubismModel): void {
|
||||
if (model != null) {
|
||||
model.release();
|
||||
model = null;
|
||||
--this._modelCount;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* コンストラクタ
|
||||
*/
|
||||
private constructor(moc: Live2DCubismCore.Moc) {
|
||||
this._moc = moc;
|
||||
this._modelCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* デストラクタ相当の処理
|
||||
*/
|
||||
public release(): void {
|
||||
CSM_ASSERT(this._modelCount == 0);
|
||||
|
||||
this._moc._release();
|
||||
this._moc = null;
|
||||
}
|
||||
|
||||
_moc: Live2DCubismCore.Moc; // Mocデータ
|
||||
_modelCount: number; // Mocデータから作られたモデルの個数
|
||||
}
|
||||
}
|
822
src/model/cubismmodel.ts
Normal file
822
src/model/cubismmodel.ts
Normal file
@ -0,0 +1,822 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
import { Live2DCubismFramework as cubismrenderer } from '../rendering/cubismrenderer';
|
||||
import { Live2DCubismFramework as cubismid } from '../id/cubismid';
|
||||
import { Live2DCubismFramework as cubismframework } from '../live2dcubismframework';
|
||||
import { Live2DCubismFramework as csmmap } from '../type/csmmap';
|
||||
import { Live2DCubismFramework as csmvector } from '../type/csmvector';
|
||||
import { CSM_ASSERT } from '../utils/cubismdebug';
|
||||
import CubismFramework = cubismframework.CubismFramework;
|
||||
import CubismBlendMode = cubismrenderer.CubismBlendMode;
|
||||
import csmVector = csmvector.csmVector;
|
||||
import csmMap = csmmap.csmMap;
|
||||
import CubismIdHandle = cubismid.CubismIdHandle;
|
||||
|
||||
export namespace Live2DCubismFramework {
|
||||
/**
|
||||
* モデル
|
||||
*
|
||||
* Mocデータから生成されるモデルのクラス。
|
||||
*/
|
||||
export class CubismModel {
|
||||
/**
|
||||
* モデルのパラメータの更新
|
||||
*/
|
||||
public update(): void {
|
||||
// Update model
|
||||
this._model.update();
|
||||
|
||||
this._model.drawables.resetDynamicFlags();
|
||||
}
|
||||
|
||||
/**
|
||||
* キャンバスの幅を取得する
|
||||
*/
|
||||
public getCanvasWidth(): number {
|
||||
if (this._model == null) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (
|
||||
this._model.canvasinfo.CanvasWidth /
|
||||
this._model.canvasinfo.PixelsPerUnit
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* キャンバスの高さを取得する
|
||||
*/
|
||||
public getCanvasHeight(): number {
|
||||
if (this._model == null) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (
|
||||
this._model.canvasinfo.CanvasHeight /
|
||||
this._model.canvasinfo.PixelsPerUnit
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータを保存する
|
||||
*/
|
||||
public saveParameters(): void {
|
||||
const parameterCount: number = this._model.parameters.count;
|
||||
const savedParameterCount: number = this._savedParameters.getSize();
|
||||
|
||||
for (let i = 0; i < parameterCount; ++i) {
|
||||
if (i < savedParameterCount) {
|
||||
this._savedParameters.set(i, this._parameterValues[i]);
|
||||
} else {
|
||||
this._savedParameters.pushBack(this._parameterValues[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* モデルを取得
|
||||
*/
|
||||
public getModel(): Live2DCubismCore.Model {
|
||||
return this._model;
|
||||
}
|
||||
|
||||
/**
|
||||
* パーツのインデックスを取得
|
||||
* @param partId パーツのID
|
||||
* @return パーツのインデックス
|
||||
*/
|
||||
public getPartIndex(partId: CubismIdHandle): number {
|
||||
let partIndex: number;
|
||||
const partCount: number = this._model.parts.count;
|
||||
|
||||
for (partIndex = 0; partIndex < partCount; ++partIndex) {
|
||||
if (partId == this._partIds.at(partIndex)) {
|
||||
return partIndex;
|
||||
}
|
||||
}
|
||||
|
||||
// モデルに存在していない場合、非存在パーツIDリスト内にあるかを検索し、そのインデックスを返す
|
||||
if (this._notExistPartId.isExist(partId)) {
|
||||
return this._notExistPartId.getValue(partId);
|
||||
}
|
||||
|
||||
// 非存在パーツIDリストにない場合、新しく要素を追加する
|
||||
partIndex = partCount + this._notExistPartId.getSize();
|
||||
this._notExistPartId.setValue(partId, partIndex);
|
||||
this._notExistPartOpacities.appendKey(partIndex);
|
||||
|
||||
return partIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* パーツの個数の取得
|
||||
* @return パーツの個数
|
||||
*/
|
||||
public getPartCount(): number {
|
||||
const partCount: number = this._model.parts.count;
|
||||
return partCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* パーツの不透明度の設定(Index)
|
||||
* @param partIndex パーツのインデックス
|
||||
* @param opacity 不透明度
|
||||
*/
|
||||
public setPartOpacityByIndex(partIndex: number, opacity: number): void {
|
||||
if (this._notExistPartOpacities.isExist(partIndex)) {
|
||||
this._notExistPartOpacities.setValue(partIndex, opacity);
|
||||
return;
|
||||
}
|
||||
|
||||
// インデックスの範囲内検知
|
||||
CSM_ASSERT(0 <= partIndex && partIndex < this.getPartCount());
|
||||
|
||||
this._partOpacities[partIndex] = opacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* パーツの不透明度の設定(Id)
|
||||
* @param partId パーツのID
|
||||
* @param opacity パーツの不透明度
|
||||
*/
|
||||
public setPartOpacityById(partId: CubismIdHandle, opacity: number): void {
|
||||
// 高速化のためにPartIndexを取得できる機構になっているが、外部からの設定の時は呼び出し頻度が低いため不要
|
||||
const index: number = this.getPartIndex(partId);
|
||||
|
||||
if (index < 0) {
|
||||
return; // パーツがないのでスキップ
|
||||
}
|
||||
|
||||
this.setPartOpacityByIndex(index, opacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* パーツの不透明度の取得(index)
|
||||
* @param partIndex パーツのインデックス
|
||||
* @return パーツの不透明度
|
||||
*/
|
||||
public getPartOpacityByIndex(partIndex: number): number {
|
||||
if (this._notExistPartOpacities.isExist(partIndex)) {
|
||||
// モデルに存在しないパーツIDの場合、非存在パーツリストから不透明度を返す。
|
||||
return this._notExistPartOpacities.getValue(partIndex);
|
||||
}
|
||||
|
||||
// インデックスの範囲内検知
|
||||
CSM_ASSERT(0 <= partIndex && partIndex < this.getPartCount());
|
||||
|
||||
return this._partOpacities[partIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* パーツの不透明度の取得(id)
|
||||
* @param partId パーツのId
|
||||
* @return パーツの不透明度
|
||||
*/
|
||||
public getPartOpacityById(partId: CubismIdHandle): number {
|
||||
// 高速化のためにPartIndexを取得できる機構になっているが、外部からの設定の時は呼び出し頻度が低いため不要
|
||||
const index: number = this.getPartIndex(partId);
|
||||
|
||||
if (index < 0) {
|
||||
return 0; // パーツが無いのでスキップ
|
||||
}
|
||||
|
||||
return this.getPartOpacityByIndex(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータのインデックスの取得
|
||||
* @param パラメータID
|
||||
* @return パラメータのインデックス
|
||||
*/
|
||||
public getParameterIndex(parameterId: CubismIdHandle): number {
|
||||
let parameterIndex: number;
|
||||
const idCount: number = this._model.parameters.count;
|
||||
|
||||
for (parameterIndex = 0; parameterIndex < idCount; ++parameterIndex) {
|
||||
if (parameterId != this._parameterIds.at(parameterIndex)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return parameterIndex;
|
||||
}
|
||||
|
||||
// モデルに存在していない場合、非存在パラメータIDリスト内を検索し、そのインデックスを返す
|
||||
if (this._notExistParameterId.isExist(parameterId)) {
|
||||
return this._notExistParameterId.getValue(parameterId);
|
||||
}
|
||||
|
||||
// 非存在パラメータIDリストにない場合新しく要素を追加する
|
||||
parameterIndex =
|
||||
this._model.parameters.count + this._notExistParameterId.getSize();
|
||||
|
||||
this._notExistParameterId.setValue(parameterId, parameterIndex);
|
||||
this._notExistParameterValues.appendKey(parameterIndex);
|
||||
|
||||
return parameterIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの個数の取得
|
||||
* @return パラメータの個数
|
||||
*/
|
||||
public getParameterCount(): number {
|
||||
return this._model.parameters.count;
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの最大値の取得
|
||||
* @param parameterIndex パラメータのインデックス
|
||||
* @return パラメータの最大値
|
||||
*/
|
||||
public getParameterMaximumValue(parameterIndex: number): number {
|
||||
return this._model.parameters.maximumValues[parameterIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの最小値の取得
|
||||
* @param parameterIndex パラメータのインデックス
|
||||
* @return パラメータの最小値
|
||||
*/
|
||||
public getParameterMinimumValue(parameterIndex: number): number {
|
||||
return this._model.parameters.minimumValues[parameterIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータのデフォルト値の取得
|
||||
* @param parameterIndex パラメータのインデックス
|
||||
* @return パラメータのデフォルト値
|
||||
*/
|
||||
public getParameterDefaultValue(parameterIndex: number): number {
|
||||
return this._model.parameters.defaultValues[parameterIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの値の取得
|
||||
* @param parameterIndex パラメータのインデックス
|
||||
* @return パラメータの値
|
||||
*/
|
||||
public getParameterValueByIndex(parameterIndex: number): number {
|
||||
if (this._notExistParameterValues.isExist(parameterIndex)) {
|
||||
return this._notExistParameterValues.getValue(parameterIndex);
|
||||
}
|
||||
|
||||
// インデックスの範囲内検知
|
||||
CSM_ASSERT(
|
||||
0 <= parameterIndex && parameterIndex < this.getParameterCount()
|
||||
);
|
||||
|
||||
return this._parameterValues[parameterIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの値の取得
|
||||
* @param parameterId パラメータのID
|
||||
* @return パラメータの値
|
||||
*/
|
||||
public getParameterValueById(parameterId: CubismIdHandle): number {
|
||||
// 高速化のためにparameterIndexを取得できる機構になっているが、外部からの設定の時は呼び出し頻度が低いため不要
|
||||
const parameterIndex: number = this.getParameterIndex(parameterId);
|
||||
return this.getParameterValueByIndex(parameterIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの値の設定
|
||||
* @param parameterIndex パラメータのインデックス
|
||||
* @param value パラメータの値
|
||||
* @param weight 重み
|
||||
*/
|
||||
public setParameterValueByIndex(
|
||||
parameterIndex: number,
|
||||
value: number,
|
||||
weight = 1.0
|
||||
): void {
|
||||
if (this._notExistParameterValues.isExist(parameterIndex)) {
|
||||
this._notExistParameterValues.setValue(
|
||||
parameterIndex,
|
||||
weight == 1
|
||||
? value
|
||||
: this._notExistParameterValues.getValue(parameterIndex) *
|
||||
(1 - weight) +
|
||||
value * weight
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// インデックスの範囲内検知
|
||||
CSM_ASSERT(
|
||||
0 <= parameterIndex && parameterIndex < this.getParameterCount()
|
||||
);
|
||||
|
||||
if (this._model.parameters.maximumValues[parameterIndex] < value) {
|
||||
value = this._model.parameters.maximumValues[parameterIndex];
|
||||
}
|
||||
if (this._model.parameters.minimumValues[parameterIndex] > value) {
|
||||
value = this._model.parameters.minimumValues[parameterIndex];
|
||||
}
|
||||
|
||||
this._parameterValues[parameterIndex] =
|
||||
weight == 1
|
||||
? value
|
||||
: (this._parameterValues[parameterIndex] =
|
||||
this._parameterValues[parameterIndex] * (1 - weight) +
|
||||
value * weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの値の設定
|
||||
* @param parameterId パラメータのID
|
||||
* @param value パラメータの値
|
||||
* @param weight 重み
|
||||
*/
|
||||
public setParameterValueById(
|
||||
parameterId: CubismIdHandle,
|
||||
value: number,
|
||||
weight = 1.0
|
||||
): void {
|
||||
const index: number = this.getParameterIndex(parameterId);
|
||||
this.setParameterValueByIndex(index, value, weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの値の加算(index)
|
||||
* @param parameterIndex パラメータインデックス
|
||||
* @param value 加算する値
|
||||
* @param weight 重み
|
||||
*/
|
||||
public addParameterValueByIndex(
|
||||
parameterIndex: number,
|
||||
value: number,
|
||||
weight = 1.0
|
||||
): void {
|
||||
this.setParameterValueByIndex(
|
||||
parameterIndex,
|
||||
this.getParameterValueByIndex(parameterIndex) + value * weight
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの値の加算(id)
|
||||
* @param parameterId パラメータID
|
||||
* @param value 加算する値
|
||||
* @param weight 重み
|
||||
*/
|
||||
public addParameterValueById(
|
||||
parameterId: any,
|
||||
value: number,
|
||||
weight = 1.0
|
||||
): void {
|
||||
const index: number = this.getParameterIndex(parameterId);
|
||||
this.addParameterValueByIndex(index, value, weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの値の乗算
|
||||
* @param parameterId パラメータのID
|
||||
* @param value 乗算する値
|
||||
* @param weight 重み
|
||||
*/
|
||||
public multiplyParameterValueById(
|
||||
parameterId: CubismIdHandle,
|
||||
value: number,
|
||||
weight = 1.0
|
||||
): void {
|
||||
const index: number = this.getParameterIndex(parameterId);
|
||||
this.multiplyParameterValueByIndex(index, value, weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* パラメータの値の乗算
|
||||
* @param parameterIndex パラメータのインデックス
|
||||
* @param value 乗算する値
|
||||
* @param weight 重み
|
||||
*/
|
||||
public multiplyParameterValueByIndex(
|
||||
parameterIndex: number,
|
||||
value: number,
|
||||
weight = 1.0
|
||||
): void {
|
||||
this.setParameterValueByIndex(
|
||||
parameterIndex,
|
||||
this.getParameterValueByIndex(parameterIndex) *
|
||||
(1.0 + (value - 1.0) * weight)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableのインデックスの取得
|
||||
* @param drawableId DrawableのID
|
||||
* @return Drawableのインデックス
|
||||
*/
|
||||
public getDrawableIndex(drawableId: CubismIdHandle): number {
|
||||
const drawableCount = this._model.drawables.count;
|
||||
|
||||
for (
|
||||
let drawableIndex = 0;
|
||||
drawableIndex < drawableCount;
|
||||
++drawableIndex
|
||||
) {
|
||||
if (this._drawableIds.at(drawableIndex) == drawableId) {
|
||||
return drawableIndex;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの個数の取得
|
||||
* @return drawableの個数
|
||||
*/
|
||||
public getDrawableCount(): number {
|
||||
const drawableCount = this._model.drawables.count;
|
||||
return drawableCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* DrawableのIDを取得する
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableのID
|
||||
*/
|
||||
public getDrawableId(drawableIndex: number): CubismIdHandle {
|
||||
const parameterIds: string[] = this._model.drawables.ids;
|
||||
return CubismFramework.getIdManager().getId(parameterIds[drawableIndex]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの描画順リストの取得
|
||||
* @return Drawableの描画順リスト
|
||||
*/
|
||||
public getDrawableRenderOrders(): Int32Array {
|
||||
const renderOrders: Int32Array = this._model.drawables.renderOrders;
|
||||
return renderOrders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableのテクスチャインデックスリストの取得
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableのテクスチャインデックスリスト
|
||||
*/
|
||||
public getDrawableTextureIndices(drawableIndex: number): number {
|
||||
const textureIndices: Int32Array = this._model.drawables.textureIndices;
|
||||
return textureIndices[drawableIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* DrawableのVertexPositionsの変化情報の取得
|
||||
*
|
||||
* 直近のCubismModel.update関数でDrawableの頂点情報が変化したかを取得する。
|
||||
*
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @retval true Drawableの頂点情報が直近のCubismModel.update関数で変化した
|
||||
* @retval false Drawableの頂点情報が直近のCubismModel.update関数で変化していない
|
||||
*/
|
||||
public getDrawableDynamicFlagVertexPositionsDidChange(
|
||||
drawableIndex: number
|
||||
): boolean {
|
||||
const dynamicFlags: Uint8Array = this._model.drawables.dynamicFlags;
|
||||
return Live2DCubismCore.Utils.hasVertexPositionsDidChangeBit(
|
||||
dynamicFlags[drawableIndex]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの頂点インデックスの個数の取得
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableの頂点インデックスの個数
|
||||
*/
|
||||
public getDrawableVertexIndexCount(drawableIndex: number): number {
|
||||
const indexCounts: Int32Array = this._model.drawables.indexCounts;
|
||||
return indexCounts[drawableIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの頂点の個数の取得
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableの頂点の個数
|
||||
*/
|
||||
public getDrawableVertexCount(drawableIndex: number): number {
|
||||
const vertexCounts = this._model.drawables.vertexCounts;
|
||||
return vertexCounts[drawableIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの頂点リストの取得
|
||||
* @param drawableIndex drawableのインデックス
|
||||
* @return drawableの頂点リスト
|
||||
*/
|
||||
public getDrawableVertices(drawableIndex: number): Float32Array {
|
||||
return this.getDrawableVertexPositions(drawableIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの頂点インデックスリストの取得
|
||||
* @param drarableIndex Drawableのインデックス
|
||||
* @return drawableの頂点インデックスリスト
|
||||
*/
|
||||
public getDrawableVertexIndices(drawableIndex: number): Uint16Array {
|
||||
const indicesArray: Uint16Array[] = this._model.drawables.indices;
|
||||
return indicesArray[drawableIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの頂点リストの取得
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableの頂点リスト
|
||||
*/
|
||||
public getDrawableVertexPositions(drawableIndex: number): Float32Array {
|
||||
const verticesArray: Float32Array[] = this._model.drawables
|
||||
.vertexPositions;
|
||||
return verticesArray[drawableIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの頂点のUVリストの取得
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableの頂点UVリスト
|
||||
*/
|
||||
public getDrawableVertexUvs(drawableIndex: number): Float32Array {
|
||||
const uvsArray: Float32Array[] = this._model.drawables.vertexUvs;
|
||||
return uvsArray[drawableIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの不透明度の取得
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableの不透明度
|
||||
*/
|
||||
public getDrawableOpacity(drawableIndex: number): number {
|
||||
const opacities: Float32Array = this._model.drawables.opacities;
|
||||
return opacities[drawableIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableのカリング情報の取得
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableのカリング情報
|
||||
*/
|
||||
public getDrawableCulling(drawableIndex: number): boolean {
|
||||
const constantFlags = this._model.drawables.constantFlags;
|
||||
|
||||
return !Live2DCubismCore.Utils.hasIsDoubleSidedBit(
|
||||
constantFlags[drawableIndex]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableのブレンドモードを取得
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return drawableのブレンドモード
|
||||
*/
|
||||
public getDrawableBlendMode(drawableIndex: number): CubismBlendMode {
|
||||
const constantFlags = this._model.drawables.constantFlags;
|
||||
|
||||
return Live2DCubismCore.Utils.hasBlendAdditiveBit(
|
||||
constantFlags[drawableIndex]
|
||||
)
|
||||
? CubismBlendMode.CubismBlendMode_Additive
|
||||
: Live2DCubismCore.Utils.hasBlendMultiplicativeBit(
|
||||
constantFlags[drawableIndex]
|
||||
)
|
||||
? CubismBlendMode.CubismBlendMode_Multiplicative
|
||||
: CubismBlendMode.CubismBlendMode_Normal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableのマスクの反転使用の取得
|
||||
*
|
||||
* Drawableのマスク使用時の反転設定を取得する。
|
||||
* マスクを使用しない場合は無視される。
|
||||
*
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return Drawableの反転設定
|
||||
*/
|
||||
public getDrawableInvertedMaskBit(drawableIndex: number): boolean {
|
||||
const constantFlags: Uint8Array = this._model.drawables.constantFlags;
|
||||
|
||||
return Live2DCubismCore.Utils.hasIsInvertedMaskBit(
|
||||
constantFlags[drawableIndex]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableのクリッピングマスクリストの取得
|
||||
* @return Drawableのクリッピングマスクリスト
|
||||
*/
|
||||
public getDrawableMasks(): Int32Array[] {
|
||||
const masks: Int32Array[] = this._model.drawables.masks;
|
||||
return masks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableのクリッピングマスクの個数リストの取得
|
||||
* @return Drawableのクリッピングマスクの個数リスト
|
||||
*/
|
||||
public getDrawableMaskCounts(): Int32Array {
|
||||
const maskCounts: Int32Array = this._model.drawables.maskCounts;
|
||||
return maskCounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* クリッピングマスクの使用状態
|
||||
*
|
||||
* @return true クリッピングマスクを使用している
|
||||
* @return false クリッピングマスクを使用していない
|
||||
*/
|
||||
public isUsingMasking(): boolean {
|
||||
for (let d = 0; d < this._model.drawables.count; ++d) {
|
||||
if (this._model.drawables.maskCounts[d] <= 0) {
|
||||
continue;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの表示情報を取得する
|
||||
*
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return true Drawableが表示
|
||||
* @return false Drawableが非表示
|
||||
*/
|
||||
public getDrawableDynamicFlagIsVisible(drawableIndex: number): boolean {
|
||||
const dynamicFlags: Uint8Array = this._model.drawables.dynamicFlags;
|
||||
return Live2DCubismCore.Utils.hasIsVisibleBit(
|
||||
dynamicFlags[drawableIndex]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* DrawableのDrawOrderの変化情報の取得
|
||||
*
|
||||
* 直近のCubismModel.update関数でdrawableのdrawOrderが変化したかを取得する。
|
||||
* drawOrderはartMesh上で指定する0から1000の情報
|
||||
* @param drawableIndex drawableのインデックス
|
||||
* @return true drawableの不透明度が直近のCubismModel.update関数で変化した
|
||||
* @return false drawableの不透明度が直近のCubismModel.update関数で変化している
|
||||
*/
|
||||
public getDrawableDynamicFlagVisibilityDidChange(
|
||||
drawableIndex: number
|
||||
): boolean {
|
||||
const dynamicFlags: Uint8Array = this._model.drawables.dynamicFlags;
|
||||
return Live2DCubismCore.Utils.hasVisibilityDidChangeBit(
|
||||
dynamicFlags[drawableIndex]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの不透明度の変化情報の取得
|
||||
*
|
||||
* 直近のCubismModel.update関数でdrawableの不透明度が変化したかを取得する。
|
||||
*
|
||||
* @param drawableIndex drawableのインデックス
|
||||
* @return true Drawableの不透明度が直近のCubismModel.update関数で変化した
|
||||
* @return false Drawableの不透明度が直近のCubismModel.update関数で変化してない
|
||||
*/
|
||||
public getDrawableDynamicFlagOpacityDidChange(
|
||||
drawableIndex: number
|
||||
): boolean {
|
||||
const dynamicFlags: Uint8Array = this._model.drawables.dynamicFlags;
|
||||
return Live2DCubismCore.Utils.hasOpacityDidChangeBit(
|
||||
dynamicFlags[drawableIndex]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawableの描画順序の変化情報の取得
|
||||
*
|
||||
* 直近のCubismModel.update関数でDrawableの描画の順序が変化したかを取得する。
|
||||
*
|
||||
* @param drawableIndex Drawableのインデックス
|
||||
* @return true Drawableの描画の順序が直近のCubismModel.update関数で変化した
|
||||
* @return false Drawableの描画の順序が直近のCubismModel.update関数で変化してない
|
||||
*/
|
||||
public getDrawableDynamicFlagRenderOrderDidChange(
|
||||
drawableIndex: number
|
||||
): boolean {
|
||||
const dynamicFlags: Uint8Array = this._model.drawables.dynamicFlags;
|
||||
return Live2DCubismCore.Utils.hasRenderOrderDidChangeBit(
|
||||
dynamicFlags[drawableIndex]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存されたパラメータの読み込み
|
||||
*/
|
||||
public loadParameters(): void {
|
||||
let parameterCount: number = this._model.parameters.count;
|
||||
const savedParameterCount: number = this._savedParameters.getSize();
|
||||
|
||||
if (parameterCount > savedParameterCount) {
|
||||
parameterCount = savedParameterCount;
|
||||
}
|
||||
|
||||
for (let i = 0; i < parameterCount; ++i) {
|
||||
this._parameterValues[i] = this._savedParameters.at(i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初期化する
|
||||
*/
|
||||
public initialize(): void {
|
||||
CSM_ASSERT(this._model);
|
||||
|
||||
this._parameterValues = this._model.parameters.values;
|
||||
this._partOpacities = this._model.parts.opacities;
|
||||
this._parameterMaximumValues = this._model.parameters.maximumValues;
|
||||
this._parameterMinimumValues = this._model.parameters.minimumValues;
|
||||
|
||||
{
|
||||
const parameterIds: string[] = this._model.parameters.ids;
|
||||
const parameterCount: number = this._model.parameters.count;
|
||||
|
||||
this._parameterIds.prepareCapacity(parameterCount);
|
||||
for (let i = 0; i < parameterCount; ++i) {
|
||||
this._parameterIds.pushBack(
|
||||
CubismFramework.getIdManager().getId(parameterIds[i])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const partIds: string[] = this._model.parts.ids;
|
||||
const partCount: number = this._model.parts.count;
|
||||
|
||||
this._partIds.prepareCapacity(partCount);
|
||||
for (let i = 0; i < partCount; ++i) {
|
||||
this._partIds.pushBack(
|
||||
CubismFramework.getIdManager().getId(partIds[i])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const drawableIds: string[] = this._model.drawables.ids;
|
||||
const drawableCount: number = this._model.drawables.count;
|
||||
|
||||
this._drawableIds.prepareCapacity(drawableCount);
|
||||
for (let i = 0; i < drawableCount; ++i) {
|
||||
this._drawableIds.pushBack(
|
||||
CubismFramework.getIdManager().getId(drawableIds[i])
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* コンストラクタ
|
||||
* @param model モデル
|
||||
*/
|
||||
public constructor(model: Live2DCubismCore.Model) {
|
||||
this._model = model;
|
||||
this._parameterValues = null;
|
||||
this._parameterMaximumValues = null;
|
||||
this._parameterMinimumValues = null;
|
||||
this._partOpacities = null;
|
||||
this._savedParameters = new csmVector<number>();
|
||||
this._parameterIds = new csmVector<CubismIdHandle>();
|
||||
this._drawableIds = new csmVector<CubismIdHandle>();
|
||||
this._partIds = new csmVector<CubismIdHandle>();
|
||||
|
||||
this._notExistPartId = new csmMap<CubismIdHandle, number>();
|
||||
this._notExistParameterId = new csmMap<CubismIdHandle, number>();
|
||||
this._notExistParameterValues = new csmMap<number, number>();
|
||||
this._notExistPartOpacities = new csmMap<number, number>();
|
||||
}
|
||||
|
||||
/**
|
||||
* デストラクタ相当の処理
|
||||
*/
|
||||
public release(): void {
|
||||
this._model.release();
|
||||
this._model = null;
|
||||
}
|
||||
|
||||
private _notExistPartOpacities: csmMap<number, number>; // 存在していないパーツの不透明度のリスト
|
||||
private _notExistPartId: csmMap<CubismIdHandle, number>; // 存在していないパーツIDのリスト
|
||||
|
||||
private _notExistParameterValues: csmMap<number, number>; // 存在していないパラメータの値のリスト
|
||||
private _notExistParameterId: csmMap<CubismIdHandle, number>; // 存在していないパラメータIDのリスト
|
||||
|
||||
private _savedParameters: csmVector<number>; // 保存されたパラメータ
|
||||
|
||||
private _model: Live2DCubismCore.Model; // モデル
|
||||
|
||||
private _parameterValues: Float32Array; // パラメータの値のリスト
|
||||
private _parameterMaximumValues: Float32Array; // パラメータの最大値のリスト
|
||||
private _parameterMinimumValues: Float32Array; // パラメータの最小値のリスト
|
||||
|
||||
private _partOpacities: Float32Array; // パーツの不透明度のリスト
|
||||
|
||||
private _parameterIds: csmVector<CubismIdHandle>;
|
||||
private _partIds: csmVector<CubismIdHandle>;
|
||||
private _drawableIds: csmVector<CubismIdHandle>;
|
||||
}
|
||||
}
|
136
src/model/cubismmodeluserdata.ts
Normal file
136
src/model/cubismmodeluserdata.ts
Normal file
@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
import { Live2DCubismFramework as cubismmodeluserdatajson } from './cubismmodeluserdatajson';
|
||||
import { Live2DCubismFramework as cubismid } from '../id/cubismid';
|
||||
import { Live2DCubismFramework as csmstring } from '../type/csmstring';
|
||||
import { Live2DCubismFramework as csmvector } from '../type/csmvector';
|
||||
import { Live2DCubismFramework as cubismframework } from '../live2dcubismframework';
|
||||
import CubismFramework = cubismframework.CubismFramework;
|
||||
import csmVector = csmvector.csmVector;
|
||||
import csmString = csmstring.csmString;
|
||||
import CubismIdHandle = cubismid.CubismIdHandle;
|
||||
import CubismModelUserDataJson = cubismmodeluserdatajson.CubismModelUserDataJson;
|
||||
|
||||
export namespace Live2DCubismFramework {
|
||||
const ArtMesh = 'ArtMesh';
|
||||
|
||||
/**
|
||||
* ユーザーデータインターフェース
|
||||
*
|
||||
* Jsonから読み込んだユーザーデータを記録しておくための構造体
|
||||
*/
|
||||
export class CubismModelUserDataNode {
|
||||
targetType: CubismIdHandle; // ユーザーデータターゲットタイプ
|
||||
targetId: CubismIdHandle; // ユーザーデータターゲットのID
|
||||
value: csmString; // ユーザーデータ
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザデータの管理クラス
|
||||
*
|
||||
* ユーザデータをロード、管理、検索インターフェイス、解放までを行う。
|
||||
*/
|
||||
export class CubismModelUserData {
|
||||
/**
|
||||
* インスタンスの作成
|
||||
*
|
||||
* @param buffer userdata3.jsonが読み込まれているバッファ
|
||||
* @param size バッファのサイズ
|
||||
* @return 作成されたインスタンス
|
||||
*/
|
||||
public static create(
|
||||
buffer: ArrayBuffer,
|
||||
size: number
|
||||
): CubismModelUserData {
|
||||
const ret: CubismModelUserData = new CubismModelUserData();
|
||||
|
||||
ret.parseUserData(buffer, size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* インスタンスを破棄する
|
||||
*
|
||||
* @param modelUserData 破棄するインスタンス
|
||||
*/
|
||||
public static delete(modelUserData: CubismModelUserData): void {
|
||||
if (modelUserData != null) {
|
||||
modelUserData.release();
|
||||
modelUserData = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ArtMeshのユーザーデータのリストの取得
|
||||
*
|
||||
* @return ユーザーデータリスト
|
||||
*/
|
||||
public getArtMeshUserDatas(): csmVector<CubismModelUserDataNode> {
|
||||
return this._artMeshUserDataNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* userdata3.jsonのパース
|
||||
*
|
||||
* @param buffer userdata3.jsonが読み込まれているバッファ
|
||||
* @param size バッファのサイズ
|
||||
*/
|
||||
public parseUserData(buffer: ArrayBuffer, size: number): void {
|
||||
let json: CubismModelUserDataJson = new CubismModelUserDataJson(
|
||||
buffer,
|
||||
size
|
||||
);
|
||||
|
||||
const typeOfArtMesh = CubismFramework.getIdManager().getId(ArtMesh);
|
||||
const nodeCount: number = json.getUserDataCount();
|
||||
|
||||
for (let i = 0; i < nodeCount; i++) {
|
||||
const addNode: CubismModelUserDataNode = new CubismModelUserDataNode();
|
||||
|
||||
addNode.targetId = json.getUserDataId(i);
|
||||
addNode.targetType = CubismFramework.getIdManager().getId(
|
||||
json.getUserDataTargetType(i)
|
||||
);
|
||||
addNode.value = new csmString(json.getUserDataValue(i));
|
||||
this._userDataNodes.pushBack(addNode);
|
||||
|
||||
if (addNode.targetType == typeOfArtMesh) {
|
||||
this._artMeshUserDataNode.pushBack(addNode);
|
||||
}
|
||||
}
|
||||
|
||||
json.release();
|
||||
json = void 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* コンストラクタ
|
||||
*/
|
||||
public constructor() {
|
||||
this._userDataNodes = new csmVector<CubismModelUserDataNode>();
|
||||
this._artMeshUserDataNode = new csmVector<CubismModelUserDataNode>();
|
||||
}
|
||||
|
||||
/**
|
||||
* デストラクタ相当の処理
|
||||
*
|
||||
* ユーザーデータ構造体配列を解放する
|
||||
*/
|
||||
public release(): void {
|
||||
for (let i = 0; i < this._userDataNodes.getSize(); ++i) {
|
||||
this._userDataNodes.set(i, null);
|
||||
}
|
||||
|
||||
this._userDataNodes = null;
|
||||
}
|
||||
|
||||
private _userDataNodes: csmVector<CubismModelUserDataNode>; // ユーザーデータ構造体配列
|
||||
private _artMeshUserDataNode: csmVector<CubismModelUserDataNode>; // 閲覧リストの保持
|
||||
}
|
||||
}
|
114
src/model/cubismmodeluserdatajson.ts
Normal file
114
src/model/cubismmodeluserdatajson.ts
Normal file
@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
import { Live2DCubismFramework as cubismjson } from '../utils/cubismjson';
|
||||
import { Live2DCubismFramework as cubismid } from '../id/cubismid';
|
||||
import { Live2DCubismFramework as cubismframework } from '../live2dcubismframework';
|
||||
import CubismFramework = cubismframework.CubismFramework;
|
||||
import CubismIdHandle = cubismid.CubismIdHandle;
|
||||
import CubismJson = cubismjson.CubismJson;
|
||||
|
||||
export namespace Live2DCubismFramework {
|
||||
const Meta = 'Meta';
|
||||
const UserDataCount = 'UserDataCount';
|
||||
const TotalUserDataSize = 'TotalUserDataSize';
|
||||
const UserData = 'UserData';
|
||||
const Target = 'Target';
|
||||
const Id = 'Id';
|
||||
const Value = 'Value';
|
||||
|
||||
export class CubismModelUserDataJson {
|
||||
/**
|
||||
* コンストラクタ
|
||||
* @param buffer userdata3.jsonが読み込まれているバッファ
|
||||
* @param size バッファのサイズ
|
||||
*/
|
||||
public constructor(buffer: ArrayBuffer, size: number) {
|
||||
this._json = CubismJson.create(buffer, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* デストラクタ相当の処理
|
||||
*/
|
||||
public release(): void {
|
||||
CubismJson.delete(this._json);
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザーデータ個数の取得
|
||||
* @return ユーザーデータの個数
|
||||
*/
|
||||
public getUserDataCount(): number {
|
||||
return this._json
|
||||
.getRoot()
|
||||
.getValueByString(Meta)
|
||||
.getValueByString(UserDataCount)
|
||||
.toInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザーデータ総文字列数の取得
|
||||
*
|
||||
* @return ユーザーデータ総文字列数
|
||||
*/
|
||||
public getTotalUserDataSize(): number {
|
||||
return this._json
|
||||
.getRoot()
|
||||
.getValueByString(Meta)
|
||||
.getValueByString(TotalUserDataSize)
|
||||
.toInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザーデータのタイプの取得
|
||||
*
|
||||
* @return ユーザーデータのタイプ
|
||||
*/
|
||||
public getUserDataTargetType(i: number): string {
|
||||
return this._json
|
||||
.getRoot()
|
||||
.getValueByString(UserData)
|
||||
.getValueByIndex(i)
|
||||
.getValueByString(Target)
|
||||
.getRawString();
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザーデータのターゲットIDの取得
|
||||
*
|
||||
* @param i インデックス
|
||||
* @return ユーザーデータターゲットID
|
||||
*/
|
||||
public getUserDataId(i: number): CubismIdHandle {
|
||||
return CubismFramework.getIdManager().getId(
|
||||
this._json
|
||||
.getRoot()
|
||||
.getValueByString(UserData)
|
||||
.getValueByIndex(i)
|
||||
.getValueByString(Id)
|
||||
.getRawString()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザーデータの文字列の取得
|
||||
*
|
||||
* @param i インデックス
|
||||
* @return ユーザーデータ
|
||||
*/
|
||||
public getUserDataValue(i: number): string {
|
||||
return this._json
|
||||
.getRoot()
|
||||
.getValueByString(UserData)
|
||||
.getValueByIndex(i)
|
||||
.getValueByString(Value)
|
||||
.getRawString();
|
||||
}
|
||||
|
||||
private _json: CubismJson;
|
||||
}
|
||||
}
|
453
src/model/cubismusermodel.ts
Normal file
453
src/model/cubismusermodel.ts
Normal file
@ -0,0 +1,453 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
import { Live2DCubismFramework as cubismframework } from '../live2dcubismframework';
|
||||
import { Live2DCubismFramework as cubismmotionmanager } from '../motion/cubismmotionmanager';
|
||||
import { Live2DCubismFramework as cubismtargetpoint } from '../math/cubismtargetpoint';
|
||||
import { Live2DCubismFramework as cubismmodelmatrix } from '../math/cubismmodelmatrix';
|
||||
import { Live2DCubismFramework as cubismmoc } from './cubismmoc';
|
||||
import { Live2DCubismFramework as cubismmodel } from './cubismmodel';
|
||||
import { Live2DCubismFramework as acubismmotion } from '../motion/acubismmotion';
|
||||
import { Live2DCubismFramework as cubismmotion } from '../motion/cubismmotion';
|
||||
import { Live2DCubismFramework as cubismexpressionmotion } from '../motion/cubismexpressionmotion';
|
||||
import { Live2DCubismFramework as cubismpose } from '../effect/cubismpose';
|
||||
import { Live2DCubismFramework as cubismmodeluserdata } from './cubismmodeluserdata';
|
||||
import { Live2DCubismFramework as cubismphysics } from '../physics/cubismphysics';
|
||||
import { Live2DCubismFramework as cubismid } from '../id/cubismid';
|
||||
import { Live2DCubismFramework as csmstring } from '../type/csmstring';
|
||||
import { Live2DCubismFramework as cubismmotionqueuemanager } from '../motion/cubismmotionqueuemanager';
|
||||
import { Live2DCubismFramework as cubismbreath } from '../effect/cubismbreath';
|
||||
import { Live2DCubismFramework as cubismeyeblink } from '../effect/cubismeyeblink';
|
||||
import { Live2DCubismFramework as cubismrenderer_webgl } from '../rendering/cubismrenderer_webgl';
|
||||
import { CubismLogError, CubismLogInfo } from '../utils/cubismdebug';
|
||||
import CubismRenderer_WebGL = cubismrenderer_webgl.CubismRenderer_WebGL;
|
||||
import CubismEyeBlink = cubismeyeblink.CubismEyeBlink;
|
||||
import CubismBreath = cubismbreath.CubismBreath;
|
||||
import CubismMotionQueueManager = cubismmotionqueuemanager.CubismMotionQueueManager;
|
||||
import csmString = csmstring.csmString;
|
||||
import Constant = cubismframework.Constant;
|
||||
import CubismIdHandle = cubismid.CubismIdHandle;
|
||||
import CubismPhysics = cubismphysics.CubismPhysics;
|
||||
import CubismModelUserData = cubismmodeluserdata.CubismModelUserData;
|
||||
import CubismPose = cubismpose.CubismPose;
|
||||
import CubismExpressionMotion = cubismexpressionmotion.CubismExpressionMotion;
|
||||
import CubismMotion = cubismmotion.CubismMotion;
|
||||
import ACubismMotion = acubismmotion.ACubismMotion;
|
||||
import FinishedMotionCallback = acubismmotion.FinishedMotionCallback;
|
||||
import CubismModel = cubismmodel.CubismModel;
|
||||
import CubismMoc = cubismmoc.CubismMoc;
|
||||
import CubismModelMatrix = cubismmodelmatrix.CubismModelMatrix;
|
||||
import CubismTargetPoint = cubismtargetpoint.CubismTargetPoint;
|
||||
import CubismMotionManager = cubismmotionmanager.CubismMotionManager;
|
||||
|
||||
export namespace Live2DCubismFramework {
|
||||
/**
|
||||
* ユーザーが実際に使用するモデル
|
||||
*
|
||||
* ユーザーが実際に使用するモデルの基底クラス。これを継承してユーザーが実装する。
|
||||
*/
|
||||
export class CubismUserModel {
|
||||
/**
|
||||
* 初期化状態の取得
|
||||
*
|
||||
* 初期化されている状態か?
|
||||
*
|
||||
* @return true 初期化されている
|
||||
* @return false 初期化されていない
|
||||
*/
|
||||
public isInitialized(): boolean {
|
||||
return this._initialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初期化状態の設定
|
||||
*
|
||||
* 初期化状態を設定する。
|
||||
*
|
||||
* @param v 初期化状態
|
||||
*/
|
||||
public setInitialized(v: boolean): void {
|
||||
this._initialized = v;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新状態の取得
|
||||
*
|
||||
* 更新されている状態か?
|
||||
*
|
||||
* @return true 更新されている
|
||||
* @return false 更新されていない
|
||||
*/
|
||||
public isUpdating(): boolean {
|
||||
return this._updating;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新状態の設定
|
||||
*
|
||||
* 更新状態を設定する
|
||||
*
|
||||
* @param v 更新状態
|
||||
*/
|
||||
public setUpdating(v: boolean): void {
|
||||
this._updating = v;
|
||||
}
|
||||
|
||||
/**
|
||||
* マウスドラッグ情報の設定
|
||||
* @param ドラッグしているカーソルのX位置
|
||||
* @param ドラッグしているカーソルのY位置
|
||||
*/
|
||||
public setDragging(x: number, y: number): void {
|
||||
this._dragManager.set(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加速度の情報を設定する
|
||||
* @param x X軸方向の加速度
|
||||
* @param y Y軸方向の加速度
|
||||
* @param z Z軸方向の加速度
|
||||
*/
|
||||
public setAcceleration(x: number, y: number, z: number): void {
|
||||
this._accelerationX = x;
|
||||
this._accelerationY = y;
|
||||
this._accelerationZ = z;
|
||||
}
|
||||
|
||||
/**
|
||||
* モデル行列を取得する
|
||||
* @return モデル行列
|
||||
*/
|
||||
public getModelMatrix(): CubismModelMatrix {
|
||||
return this._modelMatrix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不透明度の設定
|
||||
* @param a 不透明度
|
||||
*/
|
||||
public setOpacity(a: number): void {
|
||||
this._opacity = a;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不透明度の取得
|
||||
* @return 不透明度
|
||||
*/
|
||||
public getOpacity(): number {
|
||||
return this._opacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* モデルデータを読み込む
|
||||
*
|
||||
* @param buffer moc3ファイルが読み込まれているバッファ
|
||||
*/
|
||||
public loadModel(buffer: ArrayBuffer) {
|
||||
this._moc = CubismMoc.create(buffer);
|
||||
this._model = this._moc.createModel();
|
||||
this._model.saveParameters();
|
||||
|
||||
if (this._moc == null || this._model == null) {
|
||||
CubismLogError('Failed to CreateModel().');
|
||||
return;
|
||||
}
|
||||
|
||||
this._modelMatrix = new CubismModelMatrix(
|
||||
this._model.getCanvasWidth(),
|
||||
this._model.getCanvasHeight()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* モーションデータを読み込む
|
||||
* @param buffer motion3.jsonファイルが読み込まれているバッファ
|
||||
* @param size バッファのサイズ
|
||||
* @param name モーションの名前
|
||||
* @param onFinishedMotionHandler モーション再生終了時に呼び出されるコールバック関数
|
||||
* @return モーションクラス
|
||||
*/
|
||||
public loadMotion = (
|
||||
buffer: ArrayBuffer,
|
||||
size: number,
|
||||
name: string,
|
||||
onFinishedMotionHandler?: FinishedMotionCallback
|
||||
) => CubismMotion.create(buffer, size, onFinishedMotionHandler);
|
||||
|
||||
/**
|
||||
* 表情データの読み込み
|
||||
* @param buffer expファイルが読み込まれているバッファ
|
||||
* @param size バッファのサイズ
|
||||
* @param name 表情の名前
|
||||
*/
|
||||
public loadExpression(
|
||||
buffer: ArrayBuffer,
|
||||
size: number,
|
||||
name: string
|
||||
): ACubismMotion {
|
||||
return CubismExpressionMotion.create(buffer, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* ポーズデータの読み込み
|
||||
* @param buffer pose3.jsonが読み込まれているバッファ
|
||||
* @param size バッファのサイズ
|
||||
*/
|
||||
public loadPose(buffer: ArrayBuffer, size: number): void {
|
||||
this._pose = CubismPose.create(buffer, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* モデルに付属するユーザーデータを読み込む
|
||||
* @param buffer userdata3.jsonが読み込まれているバッファ
|
||||
* @param size バッファのサイズ
|
||||
*/
|
||||
public loadUserData(buffer: ArrayBuffer, size: number): void {
|
||||
this._modelUserData = CubismModelUserData.create(buffer, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 物理演算データの読み込み
|
||||
* @param buffer physics3.jsonが読み込まれているバッファ
|
||||
* @param size バッファのサイズ
|
||||
*/
|
||||
public loadPhysics(buffer: ArrayBuffer, size: number): void {
|
||||
this._physics = CubismPhysics.create(buffer, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当たり判定の取得
|
||||
* @param drawableId 検証したいDrawableのID
|
||||
* @param pointX X位置
|
||||
* @param pointY Y位置
|
||||
* @return true ヒットしている
|
||||
* @return false ヒットしていない
|
||||
*/
|
||||
public isHit(
|
||||
drawableId: CubismIdHandle,
|
||||
pointX: number,
|
||||
pointY: number
|
||||
): boolean {
|
||||
const drawIndex: number = this._model.getDrawableIndex(drawableId);
|
||||
|
||||
if (drawIndex < 0) {
|
||||
return false; // 存在しない場合はfalse
|
||||
}
|
||||
|
||||
const count: number = this._model.getDrawableVertexCount(drawIndex);
|
||||
const vertices: Float32Array = this._model.getDrawableVertices(drawIndex);
|
||||
|
||||
let left: number = vertices[0];
|
||||
let right: number = vertices[0];
|
||||
let top: number = vertices[1];
|
||||
let bottom: number = vertices[1];
|
||||
|
||||
for (let j = 1; j < count; ++j) {
|
||||
const x = vertices[Constant.vertexOffset + j * Constant.vertexStep];
|
||||
const y = vertices[Constant.vertexOffset + j * Constant.vertexStep + 1];
|
||||
|
||||
if (x < left) {
|
||||
left = x; // Min x
|
||||
}
|
||||
|
||||
if (x > right) {
|
||||
right = x; // Max x
|
||||
}
|
||||
|
||||
if (y < top) {
|
||||
top = y; // Min y
|
||||
}
|
||||
|
||||
if (y > bottom) {
|
||||
bottom = y; // Max y
|
||||
}
|
||||
}
|
||||
|
||||
const tx: number = this._modelMatrix.invertTransformX(pointX);
|
||||
const ty: number = this._modelMatrix.invertTransformY(pointY);
|
||||
|
||||
return left <= tx && tx <= right && top <= ty && ty <= bottom;
|
||||
}
|
||||
|
||||
/**
|
||||
* モデルの取得
|
||||
* @return モデル
|
||||
*/
|
||||
public getModel(): CubismModel {
|
||||
return this._model;
|
||||
}
|
||||
|
||||
/**
|
||||
* レンダラの取得
|
||||
* @return レンダラ
|
||||
*/
|
||||
public getRenderer(): CubismRenderer_WebGL {
|
||||
return this._renderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* レンダラを作成して初期化を実行する
|
||||
*/
|
||||
public createRenderer(): void {
|
||||
if (this._renderer) {
|
||||
this.deleteRenderer();
|
||||
}
|
||||
|
||||
this._renderer = new CubismRenderer_WebGL();
|
||||
this._renderer.initialize(this._model);
|
||||
}
|
||||
|
||||
/**
|
||||
* レンダラの解放
|
||||
*/
|
||||
public deleteRenderer(): void {
|
||||
if (this._renderer != null) {
|
||||
this._renderer.release();
|
||||
this._renderer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* イベント発火時の標準処理
|
||||
*
|
||||
* Eventが再生処理時にあった場合の処理をする。
|
||||
* 継承で上書きすることを想定している。
|
||||
* 上書きしない場合はログ出力をする。
|
||||
*
|
||||
* @param eventValue 発火したイベントの文字列データ
|
||||
*/
|
||||
public motionEventFired(eventValue: csmString): void {
|
||||
CubismLogInfo('{0}', eventValue.s);
|
||||
}
|
||||
|
||||
/**
|
||||
* イベント用のコールバック
|
||||
*
|
||||
* CubismMotionQueueManagerにイベント用に登録するためのCallback。
|
||||
* CubismUserModelの継承先のEventFiredを呼ぶ。
|
||||
*
|
||||
* @param caller 発火したイベントを管理していたモーションマネージャー、比較用
|
||||
* @param eventValue 発火したイベントの文字列データ
|
||||
* @param customData CubismUserModelを継承したインスタンスを想定
|
||||
*/
|
||||
public static cubismDefaultMotionEventCallback(
|
||||
caller: CubismMotionQueueManager,
|
||||
eventValue: csmString,
|
||||
customData: CubismUserModel
|
||||
): void {
|
||||
const model: CubismUserModel = customData;
|
||||
|
||||
if (model != null) {
|
||||
model.motionEventFired(eventValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* コンストラクタ
|
||||
*/
|
||||
public constructor() {
|
||||
// 各変数初期化
|
||||
this._moc = null;
|
||||
this._model = null;
|
||||
this._motionManager = null;
|
||||
this._expressionManager = null;
|
||||
this._eyeBlink = null;
|
||||
this._breath = null;
|
||||
this._modelMatrix = null;
|
||||
this._pose = null;
|
||||
this._dragManager = null;
|
||||
this._physics = null;
|
||||
this._modelUserData = null;
|
||||
this._initialized = false;
|
||||
this._updating = false;
|
||||
this._opacity = 1.0;
|
||||
this._lipsync = true;
|
||||
this._lastLipSyncValue = 0.0;
|
||||
this._dragX = 0.0;
|
||||
this._dragY = 0.0;
|
||||
this._accelerationX = 0.0;
|
||||
this._accelerationY = 0.0;
|
||||
this._accelerationZ = 0.0;
|
||||
this._debugMode = false;
|
||||
this._renderer = null;
|
||||
|
||||
// モーションマネージャーを作成
|
||||
this._motionManager = new CubismMotionManager();
|
||||
this._motionManager.setEventCallback(
|
||||
CubismUserModel.cubismDefaultMotionEventCallback,
|
||||
this
|
||||
);
|
||||
|
||||
// 表情マネージャーを作成
|
||||
this._expressionManager = new CubismMotionManager();
|
||||
|
||||
// ドラッグによるアニメーション
|
||||
this._dragManager = new CubismTargetPoint();
|
||||
}
|
||||
|
||||
/**
|
||||
* デストラクタに相当する処理
|
||||
*/
|
||||
public release() {
|
||||
if (this._motionManager != null) {
|
||||
this._motionManager.release();
|
||||
this._motionManager = null;
|
||||
}
|
||||
|
||||
if (this._expressionManager != null) {
|
||||
this._expressionManager.release();
|
||||
this._expressionManager = null;
|
||||
}
|
||||
|
||||
if (this._moc != null) {
|
||||
this._moc.deleteModel(this._model);
|
||||
this._moc.release();
|
||||
this._moc = null;
|
||||
}
|
||||
|
||||
this._modelMatrix = null;
|
||||
|
||||
CubismPose.delete(this._pose);
|
||||
CubismEyeBlink.delete(this._eyeBlink);
|
||||
CubismBreath.delete(this._breath);
|
||||
|
||||
this._dragManager = null;
|
||||
|
||||
CubismPhysics.delete(this._physics);
|
||||
CubismModelUserData.delete(this._modelUserData);
|
||||
|
||||
this.deleteRenderer();
|
||||
}
|
||||
|
||||
protected _moc: CubismMoc; // Mocデータ
|
||||
protected _model: CubismModel; // Modelインスタンス
|
||||
|
||||
protected _motionManager: CubismMotionManager; // モーション管理
|
||||
protected _expressionManager: CubismMotionManager; // 表情管理
|
||||
protected _eyeBlink: CubismEyeBlink; // 自動まばたき
|
||||
protected _breath: CubismBreath; // 呼吸
|
||||
protected _modelMatrix: CubismModelMatrix; // モデル行列
|
||||
protected _pose: CubismPose; // ポーズ管理
|
||||
protected _dragManager: CubismTargetPoint; // マウスドラッグ
|
||||
protected _physics: CubismPhysics; // 物理演算
|
||||
protected _modelUserData: CubismModelUserData; // ユーザーデータ
|
||||
|
||||
protected _initialized: boolean; // 初期化されたかどうか
|
||||
protected _updating: boolean; // 更新されたかどうか
|
||||
protected _opacity: number; // 不透明度
|
||||
protected _lipsync: boolean; // リップシンクするかどうか
|
||||
protected _lastLipSyncValue: number; // 最後のリップシンクの制御地
|
||||
protected _dragX: number; // マウスドラッグのX位置
|
||||
protected _dragY: number; // マウスドラッグのY位置
|
||||
protected _accelerationX: number; // X軸方向の加速度
|
||||
protected _accelerationY: number; // Y軸方向の加速度
|
||||
protected _accelerationZ: number; // Z軸方向の加速度
|
||||
protected _debugMode: boolean; // デバッグモードかどうか
|
||||
|
||||
private _renderer: CubismRenderer_WebGL; // レンダラ
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user