@@ -14,6 +14,7 @@ import { csmVector } from '../type/csmvector';
1414import {
1515 CSM_ASSERT ,
1616 CubismLogDebug ,
17+ CubismLogError ,
1718 CubismLogWarning
1819} from '../utils/cubismdebug' ;
1920import { ACubismMotion , FinishedMotionCallback } from './acubismmotion' ;
@@ -254,14 +255,26 @@ export class CubismMotion extends ACubismMotion {
254255 ) : CubismMotion {
255256 const ret = new CubismMotion ( ) ;
256257
257- ret . parse ( buffer , size ) ;
258- ret . _sourceFrameRate = ret . _motionData . fps ;
259- ret . _loopDurationSeconds = ret . _motionData . duration ;
260- ret . _onFinishedMotion = onFinishedMotionHandler ;
258+ try {
259+ ret . parse ( buffer , size ) ;
261260
262- // NOTE: Editorではループありのモーション書き出しは非対応
263- // ret->_loop = (ret->_motionData->Loop > 0);
264- return ret ;
261+ // Check if motion data was successfully parsed
262+ if ( ! ret . _motionData ) {
263+ CubismLogError ( 'Failed to parse motion data - motion data is null' ) ;
264+ return null ;
265+ }
266+
267+ ret . _sourceFrameRate = ret . _motionData . fps ;
268+ ret . _loopDurationSeconds = ret . _motionData . duration ;
269+ ret . _onFinishedMotion = onFinishedMotionHandler ;
270+
271+ // NOTE: Editorではループありのモーション書き出しは非対応
272+ // ret->_loop = (ret->_motionData->Loop > 0);
273+ return ret ;
274+ } catch ( error ) {
275+ CubismLogError ( `Failed to create motion: ${ error } ` ) ;
276+ return null ;
277+ }
265278 }
266279
267280 /**
@@ -767,13 +780,51 @@ export class CubismMotion extends ACubismMotion {
767780 CubismMotionCurve ,
768781 true
769782 ) ;
783+
784+ // Pre-calculate actual required sizes by analyzing the motion data
785+ let totalRequiredSegments = 0 ;
786+ let totalRequiredPoints = 0 ;
787+
788+ for ( let curveCount = 0 ; curveCount < this . _motionData . curveCount ; ++ curveCount ) {
789+ const segmentCount = json . getMotionCurveSegmentCount ( curveCount ) ;
790+
791+ for ( let segmentPos = 0 ; segmentPos < segmentCount ; ) {
792+ totalRequiredSegments ++ ;
793+
794+ if ( segmentPos == 0 ) {
795+ totalRequiredPoints += 1 ; // First point
796+ segmentPos += 2 ;
797+ } else {
798+ const segmentType : CubismMotionSegmentType = json . getMotionCurveSegment ( curveCount , segmentPos ) ;
799+
800+ switch ( segmentType ) {
801+ case CubismMotionSegmentType . CubismMotionSegmentType_Linear :
802+ case CubismMotionSegmentType . CubismMotionSegmentType_Stepped :
803+ case CubismMotionSegmentType . CubismMotionSegmentType_InverseStepped :
804+ totalRequiredPoints += 1 ;
805+ segmentPos += 3 ;
806+ break ;
807+ case CubismMotionSegmentType . CubismMotionSegmentType_Bezier :
808+ totalRequiredPoints += 3 ;
809+ segmentPos += 7 ;
810+ break ;
811+ default :
812+ segmentPos += 3 ; // Default fallback
813+ totalRequiredPoints += 1 ;
814+ break ;
815+ }
816+ }
817+ }
818+ }
819+
820+ // Use the calculated sizes (with a small buffer for safety)
770821 this . _motionData . segments . updateSize (
771- json . getMotionTotalSegmentCount ( ) ,
822+ totalRequiredSegments ,
772823 CubismMotionSegment ,
773824 true
774825 ) ;
775826 this . _motionData . points . updateSize (
776- json . getMotionTotalPointCount ( ) ,
827+ totalRequiredPoints ,
777828 CubismMotionPoint ,
778829 true
779830 ) ;
@@ -859,6 +910,13 @@ export class CubismMotion extends ACubismMotion {
859910 this . _motionData . segments . at ( totalSegmentCount ) . evaluate =
860911 linearEvaluate ;
861912
913+ // Check if we have enough points in the array, expand if needed
914+ if ( totalPointCount >= this . _motionData . points . getSize ( ) ) {
915+ const newSize = Math . max ( totalPointCount + 1 , this . _motionData . points . getSize ( ) * 2 ) ;
916+ CubismLogWarning ( `Expanding motion points array from ${ this . _motionData . points . getSize ( ) } to ${ newSize } for Linear segment` ) ;
917+ this . _motionData . points . updateSize ( newSize , CubismMotionPoint , true ) ;
918+ }
919+
862920 this . _motionData . points . at ( totalPointCount ) . time =
863921 json . getMotionCurveSegment ( curveCount , segmentPosition + 1 ) ;
864922 this . _motionData . points . at ( totalPointCount ) . value =
0 commit comments