/**
 * 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 { CubismIdHandle } from '../id/cubismid' ; import { CubismFramework } from '../live2dcubismframework' ; import { CubismBlendMode } from '../rendering/cubismrenderer' ; import { csmMap } from '../type/csmmap' ; import { csmVector } from '../type/csmvector' ; import { CSM_ASSERT } from '../utils/cubismdebug' ; /**
 * モデル
 *
 * Mocデータから生成されるモデルのクラス。
 */ export class CubismModel { /**
     * モデルのパラメータの更新
     */ public update ( ) { this . _model . update ( ) ; this . _model . drawables . resetDynamicFlags ( ) ; } /**
     * キャンバスの幅を取得する
     */ public getCanvasWidth ( ) { if ( this . _model == null ) { return 0.0 ; } return ( this . _model . canvasinfo . CanvasWidth / this . _model . canvasinfo . PixelsPerUnit ) ; } /**
     * キャンバスの高さを取得する
     */ public getCanvasHeight ( ) { if ( this . _model == null ) { return 0.0 ; } return ( this . _model . canvasinfo . CanvasHeight / this . _model . canvasinfo . PixelsPerUnit ) ; } /**
     * パラメータを保存する
     */ public saveParameters ( ) { const parameterCount = this . _model . parameters . count ; const savedParameterCount = 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 ( ) { return this . _model ; } param partId return public getPartIndex ( partId ) { let partIndex ; const partCount = this . _model . parts . count ; for ( partIndex = 0 ; partIndex < partCount ; ++ partIndex ) { if ( partId == this . _partIds . at ( partIndex ) ) { return partIndex ; } } if ( this . _notExistPartId . isExist ( partId ) ) { return this . _notExistPartId . getValue ( partId ) ; } partIndex = partCount + this . _notExistPartId . getSize ( ) ; this . _notExistPartId . setValue ( partId , partIndex ) ; this . _notExistPartOpacities . appendKey ( partIndex ) ; return partIndex ; } return public getPartCount ( ) { const partCount = this . _model . parts . count ; return partCount ; } param partIndex param opacity public setPartOpacityByIndex ( partIndex , opacity ) { if ( this . _notExistPartOpacities . isExist ( partIndex ) ) { this . _notExistPartOpacities . setValue ( partIndex , opacity ) ; return ; } CSM_ASSERT ( 0 <= partIndex && partIndex < this . getPartCount ( ) ) ; this . _partOpacities [ partIndex ] = opacity ; } param partId param opacity public setPartOpacityById ( partId , opacity ) { const index = this . getPartIndex ( partId ) ; if ( index < 0 ) { return ; } this . setPartOpacityByIndex ( index , opacity ) ; } param partIndex return public getPartOpacityByIndex ( partIndex ) { if ( this . _notExistPartOpacities . isExist ( partIndex ) ) { return this . _notExistPartOpacities . getValue ( partIndex ) ; } CSM_ASSERT ( 0 <= partIndex && partIndex < this . getPartCount ( ) ) ; return this . _partOpacities [ partIndex ] ; } param partId return public getPartOpacityById ( partId ) { const index = this . getPartIndex ( partId ) ; if ( index < 0 ) { return 0 ; } return this . getPartOpacityByIndex ( index ) ; } param パラメータID return public getParameterIndex ( parameterId ) { let parameterIndex ; const idCount = this . _model . parameters . count ; for ( parameterIndex = 0 ; parameterIndex < idCount ; ++ parameterIndex ) { if ( parameterId != this . _parameterIds . at ( parameterIndex ) ) { continue ; } return parameterIndex ; } if ( this . _notExistParameterId . isExist ( parameterId ) ) { return this . _notExistParameterId . getValue ( parameterId ) ; } parameterIndex = this . _model . parameters . count + this . _notExistParameterId . getSize ( ) ; this . _notExistParameterId . setValue ( parameterId , parameterIndex ) ; this . _notExistParameterValues . appendKey ( parameterIndex ) ; return parameterIndex ; } return public getParameterCount ( ) { return this . _model . parameters . count ; } param parameterIndex return public getParameterMaximumValue ( parameterIndex ) { return this . _model . parameters . maximumValues [ parameterIndex ] ; } param parameterIndex return public getParameterMinimumValue ( parameterIndex ) { return this . _model . parameters . minimumValues [ parameterIndex ] ; } param parameterIndex return public getParameterDefaultValue ( parameterIndex ) { return this . _model . parameters . defaultValues [ parameterIndex ] ; } param parameterIndex return public getParameterValueByIndex ( parameterIndex ) { if ( this . _notExistParameterValues . isExist ( parameterIndex ) ) { return this . _notExistParameterValues . getValue ( parameterIndex ) ; } CSM_ASSERT ( 0 <= parameterIndex && parameterIndex < this . getParameterCount ( ) ) ; return this . _parameterValues [ parameterIndex ] ; } param parameterId return public getParameterValueById ( parameterId ) { const parameterIndex = this . getParameterIndex ( parameterId ) ; return this . getParameterValueByIndex ( parameterIndex ) ; } param parameterIndex param value param weight public setParameterValueByIndex ( parameterIndex , value , weight = 1.0 ) { 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 param value param weight public setParameterValueById ( parameterId , value , weight = 1.0 ) { const index = this . getParameterIndex ( parameterId ) ; this . setParameterValueByIndex ( index , value , weight ) ; } param parameterIndex param value param weight public addParameterValueByIndex ( parameterIndex , value , weight = 1.0 ) { this . setParameterValueByIndex ( parameterIndex , this . getParameterValueByIndex ( parameterIndex ) + value * weight ) ; } param parameterId param value param weight public addParameterValueById ( parameterId , value , weight = 1.0 ) { const index = this . getParameterIndex ( parameterId ) ; this . addParameterValueByIndex ( index , value , weight ) ; } param parameterId param value param weight public multiplyParameterValueById ( parameterId , value , weight = 1.0 ) { const index = this . getParameterIndex ( parameterId ) ; this . multiplyParameterValueByIndex ( index , value , weight ) ; } param parameterIndex param value param weight public multiplyParameterValueByIndex ( parameterIndex , value , weight = 1.0 ) { this . setParameterValueByIndex ( parameterIndex , this . getParameterValueByIndex ( parameterIndex ) * ( 1.0 + ( value - 1.0 ) * weight ) ) ; } param drawableId return public getDrawableIndex ( drawableId ) { const drawableCount = this . _model . drawables . count ; for ( let drawableIndex = 0 ; drawableIndex < drawableCount ; ++ drawableIndex ) { if ( this . _drawableIds . at ( drawableIndex ) == drawableId ) { return drawableIndex ; } } return - 1 ; } return public getDrawableCount ( ) { const drawableCount = this . _model . drawables . count ; return drawableCount ; } param drawableIndex return public getDrawableId ( drawableIndex ) { const parameterIds = this . _model . drawables . ids ; return CubismFramework . getIdManager ( ) . getId ( parameterIds [ drawableIndex ] ) ; } return public getDrawableRenderOrders ( ) { const renderOrders = this . _model . drawables . renderOrders ; return renderOrders ; } param drawableIndex return public getDrawableTextureIndices ( drawableIndex ) { const textureIndices = this . _model . drawables . textureIndices ; return textureIndices [ drawableIndex ] ; } param drawableIndex retval retval public getDrawableDynamicFlagVertexPositionsDidChange ( drawableIndex ) { const dynamicFlags = this . _model . drawables . dynamicFlags ; return Live2DCubismCore . Utils . hasVertexPositionsDidChangeBit ( dynamicFlags [ drawableIndex ] ) ; } param drawableIndex return public getDrawableVertexIndexCount ( drawableIndex ) { const indexCounts = this . _model . drawables . indexCounts ; return indexCounts [ drawableIndex ] ; } param drawableIndex return public getDrawableVertexCount ( drawableIndex ) { const vertexCounts = this . _model . drawables . vertexCounts ; return vertexCounts [ drawableIndex ] ; } param drawableIndex return public getDrawableVertices ( drawableIndex ) { return this . getDrawableVertexPositions ( drawableIndex ) ; } param drarableIndex return public getDrawableVertexIndices ( drawableIndex ) { const indicesArray = this . _model . drawables . indices ; return indicesArray [ drawableIndex ] ; } param drawableIndex return public getDrawableVertexPositions ( drawableIndex ) { const verticesArray = this . _model . drawables . vertexPositions ; return verticesArray [ drawableIndex ] ; } param drawableIndex return public getDrawableVertexUvs ( drawableIndex ) { const uvsArray = this . _model . drawables . vertexUvs ; return uvsArray [ drawableIndex ] ; } param drawableIndex return public getDrawableOpacity ( drawableIndex ) { const opacities = this . _model . drawables . opacities ; return opacities [ drawableIndex ] ; } param drawableIndex return public getDrawableCulling ( drawableIndex ) { const constantFlags = this . _model . drawables . constantFlags ; return ! Live2DCubismCore . Utils . hasIsDoubleSidedBit ( constantFlags [ drawableIndex ] ) ; } param drawableIndex return public getDrawableBlendMode ( drawableIndex ) { 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 ; } param drawableIndex return public getDrawableInvertedMaskBit ( drawableIndex ) { const constantFlags = this . _model . drawables . constantFlags ; return Live2DCubismCore . Utils . hasIsInvertedMaskBit ( constantFlags [ drawableIndex ] ) ; } return public getDrawableMasks ( ) { const masks = this . _model . drawables . masks ; return masks ; } return public getDrawableMaskCounts ( ) { const maskCounts = this . _model . drawables . maskCounts ; return maskCounts ; } return return public isUsingMasking ( ) { for ( let d = 0 ; d < this . _model . drawables . count ; ++ d ) { if ( this . _model . drawables . maskCounts [ d ] <= 0 ) { continue ; } return true ; } return false ; } param drawableIndex return return public getDrawableDynamicFlagIsVisible ( drawableIndex ) { const dynamicFlags = this . _model . drawables . dynamicFlags ; return Live2DCubismCore . Utils . hasIsVisibleBit ( dynamicFlags [ drawableIndex ] ) ; } param drawableIndex return return public getDrawableDynamicFlagVisibilityDidChange ( drawableIndex ) { const dynamicFlags = this . _model . drawables . dynamicFlags ; return Live2DCubismCore . Utils . hasVisibilityDidChangeBit ( dynamicFlags [ drawableIndex ] ) ; } param drawableIndex return return public getDrawableDynamicFlagOpacityDidChange ( drawableIndex ) { const dynamicFlags = this . _model . drawables . dynamicFlags ; return Live2DCubismCore . Utils . hasOpacityDidChangeBit ( dynamicFlags [ drawableIndex ] ) ; } param drawableIndex return return public getDrawableDynamicFlagRenderOrderDidChange ( drawableIndex ) { const dynamicFlags = this . _model . drawables . dynamicFlags ; return Live2DCubismCore . Utils . hasRenderOrderDidChangeBit ( dynamicFlags [ drawableIndex ] ) ; } /**
     * 保存されたパラメータの読み込み
     */ public loadParameters ( ) { let parameterCount = this . _model . parameters . count ; const savedParameterCount = this . _savedParameters . getSize ( ) ; if ( parameterCount > savedParameterCount ) { parameterCount = savedParameterCount ; } for ( let i = 0 ; i < parameterCount ; ++ i ) { this . _parameterValues [ i ] = this . _savedParameters . at ( i ) ; } } /**
     * 初期化する
     */ public initialize ( ) { 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 = this . _model . parameters . ids ; const parameterCount = 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 = this . _model . parts . ids ; const partCount = 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 = this . _model . drawables . ids ; const drawableCount = 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 ) { 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 ( ) { this . _model . release ( ) ; this . _model = null ; } private _notExistPartOpacities : csmMap < number , number > ; private _notExistPartId : csmMap < CubismIdHandle , number > ; private _notExistParameterValues : csmMap < number , number > ; private _notExistParameterId : csmMap < CubismIdHandle , number > ; 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 > ; } import * as $ from './cubismmodel' ; export namespace Live2DCubismFramework { export const CubismModel = $ . CubismModel ; export type CubismModel = $ . CubismModel ; }