22
33import java .util .function .Consumer ;
44
5+ /**
6+ * Represents a Live2D Cubism user model.
7+ * <p>
8+ * This class provides a high-level API to interact with a Live2D model, including loading assets
9+ * (MOC3, physics, pose, expressions), updating the model state, and rendering it.
10+ */
511public class CubismUserModel extends Native {
612 private Consumer <String > motionFinishedCallback ;
713
14+ /**
15+ * Creates a new empty user model instance.
16+ */
817 public CubismUserModel () {
918 super (createNative ());
1019 linkNative ();
@@ -13,70 +22,155 @@ public CubismUserModel() {
1322 private static native long createNative ();
1423 private native void linkNative ();
1524
25+ /**
26+ * Loads the model data (MOC3) from a byte buffer.
27+ * @param buffer The byte array containing MOC3 data.
28+ */
1629 public void loadModel (byte [] buffer ) { loadModelNative (_ptr , buffer ); }
1730 private static native void loadModelNative (long ptr , byte [] buffer );
1831
32+ /**
33+ * Loads the physics data from a byte buffer.
34+ * @param buffer The byte array containing physics3.json data.
35+ */
1936 public void loadPhysics (byte [] buffer ) { loadPhysicsNative (_ptr , buffer ); }
2037 private static native void loadPhysicsNative (long ptr , byte [] buffer );
2138
39+ /**
40+ * Loads the pose data from a byte buffer.
41+ * @param buffer The byte array containing pose3.json data.
42+ */
2243 public void loadPose (byte [] buffer ) { loadPoseNative (_ptr , buffer ); }
2344 private static native void loadPoseNative (long ptr , byte [] buffer );
2445
46+ /**
47+ * Loads an expression from a byte buffer and assigns it a name.
48+ * @param buffer The byte array containing exp3.json data.
49+ * @param name The unique name to identify this expression.
50+ */
2551 public void loadExpression (byte [] buffer , String name ) { loadExpressionNative (_ptr , buffer , name ); }
2652 private static native void loadExpressionNative (long ptr , byte [] buffer , String name );
2753
54+ /**
55+ * Sets the current active expression by name.
56+ * @param name The name of the expression to activate.
57+ */
2858 public void setExpression (String name ) { setExpressionNative (_ptr , name ); }
2959 private static native void setExpressionNative (long ptr , String name );
3060
61+ /**
62+ * Creates the renderer for this model.
63+ * Must be called before drawing.
64+ */
3165 public void createRenderer () { createRendererNative (_ptr ); }
3266 private static native void createRendererNative (long ptr );
3367
68+ /**
69+ * Registers a texture with the model's renderer.
70+ * @param index The texture index (as defined in the model data).
71+ * @param textureId The OpenGL texture ID.
72+ */
3473 public void registerTexture (int index , int textureId ) { registerTextureNative (_ptr , index , textureId ); }
3574 private static native void registerTextureNative (long ptr , int index , int textureId );
3675
76+ /**
77+ * Sets the dragging coordinates for interaction (e.g., look-at behavior).
78+ * @param x The x-coordinate in model space.
79+ * @param y The y-coordinate in model space.
80+ */
3781 public void setDragging (float x , float y ) { setDraggingNative (_ptr , x , y ); }
3882 private static native void setDraggingNative (long ptr , float x , float y );
3983
84+ /**
85+ * Checks if a point hits a specific drawable part (HitArea).
86+ * @param drawableId The ID of the drawable to check.
87+ * @param x The x-coordinate to test.
88+ * @param y The y-coordinate to test.
89+ * @return true if the point hits the drawable, false otherwise.
90+ */
4091 public boolean isHit (String drawableId , float x , float y ) { return isHitNative (_ptr , drawableId , x , y ); }
4192 private static native boolean isHitNative (long ptr , String drawableId , float x , float y );
4293
94+ /**
95+ * Starts playing a motion.
96+ *
97+ * @param buffer The byte array containing motion3.json data.
98+ * @param priority The priority of the motion (1=Low, 2=Normal, 3=Force).
99+ * @param loop Whether the motion should loop.
100+ * @param onFinished Callback to be invoked when the motion finishes.
101+ */
43102 public void startMotion (byte [] buffer , int priority , boolean loop , Consumer <String > onFinished ) {
44103 this .motionFinishedCallback = onFinished ;
45104 startMotionNative (_ptr , buffer , priority , loop );
46105 }
47106 private static native void startMotionNative (long ptr , byte [] buffer , int priority , boolean loop );
48107
108+ /**
109+ * Checks if the currently playing motion has finished.
110+ * @return true if finished, false otherwise.
111+ */
49112 public boolean isMotionFinished () { return isMotionFinishedNative (_ptr ); }
50113 private static native boolean isMotionFinishedNative (long ptr );
51114
115+ // Called from JNI
52116 private void onMotionFinished (String name ) {
53117 if (motionFinishedCallback != null ) {
54118 motionFinishedCallback .accept (name );
55119 }
56120 }
57121
122+ /**
123+ * Updates the model state. Should be called every frame.
124+ * @param deltaTime The time elapsed since the last frame in seconds.
125+ */
58126 public void update (float deltaTime ) { updateNative (_ptr , deltaTime ); }
59127 private static native void updateNative (long ptr , float deltaTime );
60128
129+ /**
130+ * Sets a parameter value.
131+ * @param id The parameter ID.
132+ * @param value The value to set.
133+ */
61134 public void setParameterValue (String id , float value ) { setParameterValueNative (_ptr , id , value ); }
62135 private static native void setParameterValueNative (long ptr , String id , float value );
63136
137+ /**
138+ * Gets the current value of a parameter.
139+ * @param id The parameter ID.
140+ * @return The current value.
141+ */
64142 public float getParameterValue (String id ) { return getParameterValueNative (_ptr , id ); }
65143 private static native float getParameterValueNative (long ptr , String id );
66144
145+ /**
146+ * Gets the width of the model's canvas.
147+ * @return The canvas width.
148+ */
67149 public float getCanvasWidth () { return getCanvasWidthNative (_ptr ); }
68150 private static native float getCanvasWidthNative (long ptr );
69151
152+ /**
153+ * Gets the height of the model's canvas.
154+ * @return The canvas height.
155+ */
70156 public float getCanvasHeight () { return getCanvasHeightNative (_ptr ); }
71157 private static native float getCanvasHeightNative (long ptr );
72158
159+ /**
160+ * Gets the list of drawable IDs in the model.
161+ * @return An array of drawable ID strings.
162+ */
73163 public String [] getDrawableIds () { return getDrawableIdsNative (_ptr ); }
74164 private static native String [] getDrawableIdsNative (long ptr );
75165
166+ /**
167+ * Draws the model using the provided MVP matrix.
168+ * @param mvpMatrix The 4x4 Model-View-Projection matrix.
169+ */
76170 public void draw (float [] mvpMatrix ) { drawNative (_ptr , mvpMatrix ); }
77171 private static native void drawNative (long ptr , float [] mvpMatrix );
78172
79173 @ Override
80174 public void close () { deleteNative (_ptr ); }
81175 private static native void deleteNative (long ptr );
82- }
176+ }
0 commit comments