#06 (Bonus) Diving into the codes

You may not be familiar with programming, but the fundamentals are easy for anyone to learn about !
There are commentary within the sample codes to guide you through. Feel free to give programming a try.


   About the Sample Code

(1)In Unity, select "Live2DModel" under "Hierarchy", then double-click "Simple Model" in the "Script" field under "Inspector". 


(2)MonoDevelop should appear. It displays the codes in the script which you may modify.


There are about 100 lines of codes, divided into three main parts.

The execution goes through this order:   
Start() → Update() → OnRenderObject() .
"Render" is referring to the drawing (visual). Update() and OnRenderObject() is executed once every frame.

Let's first look into the "Start" section! 


【 Start processing 】
The Start process of Live2D's script first calls the Initialization method and Load method.
The Initialization method is necessary for a Live2D drawing to appear.
The Load method loads up files such as the moc files and textures.
  1. void Start()
  2. {
  3.     // Instantiates the Live2D model
  4.     Live2D.init();
  5.     // Calls the load method
  6.     load();
  7. }
  8.  
  9. void load()
  10. {
  11.     // Load the model from the moc
  12.     live2DModel = Live2DModelUnity.loadModel(mocFile.bytes);
  13.  
  14.     for (int i = 0; i < textureFiles.Length; i++)
  15.     {
  16.         // Apply the texture to the model
  17.         live2DModel.setTexture(i, textureFiles[i]);
  18.     }
  19.     // Specifies the positoin of the Live2D drawing
  20.     float modelWidth = live2DModel.getCanvasWidth();
  21.     live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50.0f, 50.0f);
  22.  
  23.     // Load in physics if a file is available
  24.     if (physicsFile != null) physics = L2DPhysics.load(physicsFile.bytes);
  25. } 

【 Update process 】
The Update process is a detector for mouse clicks.
  1. void Update()
  2. {
  3.     var pos = Input.mousePosition;
  4.     // When left mouse is clicked
  5.     if (Input.GetMouseButtonDown(0))
  6.     {
  7.         // do nothing
  8.     }
  9.     // When holding down the left mouse button
  10.     else if (Input.GetMouseButton(0))
  11.     {
  12.         dragMgr.Set(pos.x / Screen.width*2-1, pos.y / Screen.height*2-1);
  13.     }
  14.     // When left click is released
  15.     else if (Input.GetMouseButtonUp(0))
  16.     {
  17.         dragMgr.Set(00);
  18.     }
  19. } 



【 OnRenderObject process 】
OnRenderObject takes the mouse click commands and controls how the model should behave (moving the face, eyes, body, etc.)

  1. void OnRenderObject()
  2. {
  3.     if (live2DModel == null)
  4.     {
  5.         load();
  6.     }
  7.  
  8.     live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);
  9.  
  10.     if ( ! Application.isPlaying)
  11.     {
  12.         live2DModel.update();
  13.         live2DModel.draw();
  14.         return;
  15.     }
  16.  
  17.     dragMgr.update();
  18.     // Direction of the face
  19.     live2DModel.setParamFloat("PARAM_ANGLE_X" , dragMgr.getX(30);
  20.     live2DModel.setParamFloat("PARAM_ANGLE_Y", dragMgr.getY() * 30);
  21.     // Direction of the body
  22.     live2DModel.setParamFloat("PARAM_BODY_ANGLE_X", dragMgr.getX() * 10);
  23.     // Eye movement
  24.     live2DModel.setParamFloat("PARAM_EYE_BALL_X", -dragMgr.getX());
  25.     live2DModel.setParamFloat("PARAM_EYE_BALL_Y", -dragMgr.getY());
  26.     // Breathing
  27.     double timeSec = UtSystem.getUserTimeMSec() / 1000.0;
  28.     double t = timeSec * 2 * Math.PI;
  29.     live2DModel.setParamFloat("PARAM_BREATH"(float)(0.5f + 0.5f * Math.Sin(t / 3.0)));
  30.     // Blink
  31.     eyeBlink.setParam(live2DModel);
  32.  
  33.     if (physics != null) physics.updateParam(live2DModel);
  34.  
  35.     live2DModel.update();
  36.     live2DModel.draw();
  37. }


In other words, by writing a line of code that holds reference to any of these parameter IDs, your application may make the model respond to mouse command in the way you desire.



Although its only been a very brief introduction, the basic concept of the script is worth knowing about. 
With so many values available to play around with, programming can be very fun!