[Android] Izradite svoj vlastiti Live2D s Cubism 2 - da biste napravili žive pozadine!

Create Your Own Live2d With Cubism 2 Make Live Wallpapers



Predgovor -

Preuzimanje Andorid SDK-a Oblak Baidu Šifra ekstrakcije: 19jm

Ovaj put proučit ćemo liveWallPaper, jedan od službenih primjera, a to je razvoj Android pozadine uživo.



Pogledajmo rezultate ovog primjera pokretanja:



liveWallPaper



Još uvijek prilično slatko, onda krenimo!



Imenik projekata -

Okvir je potrebna klasa koju treba koristiti live2d



Ispod udova nalazi se teglica koja se mora koristiti za razvoj.

-GLWallpaperService.jar

-live2d_android.jar

Uzorak je fokus live2DRenderer je klasa koja crta model, LiveWallpaperService nasljeđuje od GLWallPaperService

- Ova klasa GLWallPaperService osnovna je klasa za žive pozadine. Ova klasa pruža uslužno sučelje za razvoj projekata tapeta. Integriranjem klase GLWallPaperService i nadjačavanjem onCreateEngine () za implementaciju funkcije pozadine.

U REDU! Otprilike je isto kao i jučerašnji rad! Početak!



Čas crtanja modela live2DRenderer-

Jednostavno razumite prethodni članak u Rendereru -> Kliknite ovdje za skok

class SampleGLRenderer implements Renderer{ @Override //Perform rendering work public void onDrawFrame(GL10 gl){} @Override // Callback when the rendering window size changes or the screen method changes public void onSurfaceChanged(GL10 gl, int width, int height){} @Override //The processing that needs to be done after the surface is created. public void onSurfaceCreated(GL10 gl, EGLConfig config){} }

Međutim, model nacrtan u prethodnom primjeru ima glavu samo ukočenu lijevo i desno, a nema značajnih opterećenja, fizičkih učinaka (gibanje i fizika).

Zatim pogledajte kako se ti predmeti uvlače u telefon!

onDrawFrame (GL10 gl)

 1 public void onDrawFrame(GL10 gl) {  2 // Your rendering code goes here  3 //For OpenGL settings  4  gl.glMatrixMode(GL10.GL_MODELVIEW )  5  gl.glLoadIdentity()  6  gl.glClear( GL10.GL_COLOR_BUFFER_BIT )  7  gl.glEnable( GL10.GL_BLEND )  8  gl.glBlendFunc( GL10.GL_ONE , GL10.GL_ONE_MINUS_SRC_ALPHA )  9  gl.glDisable( GL10.GL_DEPTH_TEST ) 10  gl.glDisable( GL10.GL_CULL_FACE ) 11 12 13  live2DModel.loadParam() 14 15 /*motionMgr is MotionQueueManager.class, a queue for storing Motion(.mtn) files from the name.*/ 16 if(motionMgr.isFinished()) 17  { 18 /*To play animations, use the startQueue function of MotionQueueManager. 19  * The second parameter can be set to delete after the action ends (when set to true, Live2D will automatically call the delete() function) 20 */ 21 motionMgr.startMotion(motion, false) 22  } 23 else 24  { 25 /*Of course, Motion is definitely not moving, so you must call the updateParam() function to set the action parameters that are being played to the model.*/ 26  motionMgr.updateParam(live2DModel) 27  } 28 //Temporarily save all current values 29  live2DModel.saveParam() 30 /*dragMgr is the L2DTargetPoint class, which is used to manage drag and drop coordinates. 31  About the acquisition of the touch, the transformation method of the device coordinates, it is also the realization of where the model clicks on the screen in the wallpaper.*/ 32  dragMgr.update() 33 34 float dragX=dragMgr.getX() 35 float dragY=dragMgr.getY() 36 37 //Adjust the direction of the face 38 live2DModel.addToParamFloat(L2DStandardID.PARAM_ANGLE_X, dragX*30) 39 live2DModel.addToParamFloat(L2DStandardID.PARAM_ANGLE_Y, dragY*30) 40 41 //Adjust the direction of the body 42 live2DModel.addToParamFloat(L2DStandardID.PARAM_BODY_ANGLE_X, dragX*10) 43 44 //Adjust the direction of the eye, not needed in this case 45 /*live2DModel.addToParamFloat( 'PARAM_EYE_BALL_X', dragX , 1 ) 46  live2DModel.addToParamFloat( 'PARAM_EYE_BALL_Y', dragY , 1 )*/ 47 48 /*This class is a wrapper class for easy use of physics in the Live2D library. 49  The physics calculation can also be set without using it. 50  The load function of the L2DPhysics class is used to read JSON files. 51  When updating, the update function is called to apply the parameters to the model.*/ 52  physics.updateParam(live2DModel) 53 54 //Configuration drawing environment 55  live2DModel.setGL( gl ) 56 //Update vertex 57  live2DModel.update() 58 //draw 59  live2DModel.draw() 60 }
Crtež modela

Za neke konfiguracije OpenGL-a, pogledajte prethodnu funkciju oDrawFrame ().



onSurfaceCreated (GL10 gl, EGLConfig config) -

 1 public void onSurfaceCreated(GL10 gl, EGLConfig config)  2 {  3 AssetManager mngr = con.getAssets()  4 try  5  {  6 InputStream in = mngr.open( MODEL_PATH )  7 live2DModel = Live2DModelAndroid.loadModel( in )  8  in.close()  9  } 10 catch (IOException e) 11  { 12  e.printStackTrace() 13  } 14 15 try 16  { 17 //texture 18 for (int i = 0 i  ) 19  { 20 InputStream in = mngr.open( TEXTURE_PATHS[i] ) 21 int texNo = UtOpenGL.loadTexture(gl , in , true ) 22  live2DModel.setTexture( i , texNo ) 23  in.close() 24  } 25  } 26 catch (IOException e) 27  { 28  e.printStackTrace() 29  } 30 31 try 32  { 33 InputStream in = mngr.open( MOTION_PATH ) 34 motion = Live2DMotion.loadMotion( in ) 35  in.close() 36 37 in=mngr.open(PHYSICS_PATH) 38 physics=L2DPhysics.load(in) 39  in.close() 40  } 41 catch (Exception e) 42  { 43  e.printStackTrace() 44  } 45 }

To služi za korištenje streama za učitavanje datoteka u mapi Assets na mjesto gdje vam trebaju. Ne razlikuje se puno od prethodne. Mnogo je više pokreta i fizike, a metoda je ista.



onSurfaceChanged (GL10 gl, int širina, intvisina)-

public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport( 0 , 0 , width , height ) gl.glMatrixMode( GL10.GL_PROJECTION ) gl.glLoadIdentity() float modelWidth = live2DModel.getCanvasWidth() gl.glOrthof( 0 , modelWidth , modelWidth * height / width, 0 , 0.5f , -0.5f ) glWidth=width glHeight=height }
Pogledajte kod

Nema promjena, ali ovaj put sam pronašao opis ovih parametara crtanja u kineskom priručniku.



Klasa LiveWallpaperService -

/*This GLWallPaperService class is the basic class of live wallpaper. This class provides a service interface for the development of wallpaper projects. By integrating the GLWallPaperService class, rewriting onCreateEngine() and other methods to implement the wallpaper function.*/ public class LiveWallpaperService extends GLWallpaperService { public Engine onCreateEngine() {} class MyEngine extends GLEngine { public MyEngine() {} //Get touch events public void onTouchEvent(MotionEvent event) {} } }

Struktura klase tapeta je ovakva, dolje je cjeloviti kod.

 1 public class LiveWallpaperService extends GLWallpaperService {  2 public LiveWallpaperService() {  3 super()  4  }  5  6 public Engine onCreateEngine() {  7 MyEngine engine = new MyEngine()  8 return engine  9  } 10 11 class MyEngine extends GLEngine { 12  Live2DRenderer renderer 13 14 public MyEngine() { 15 super() 16 // handle prefs, other initialization 17 renderer = new Live2DRenderer(getApplicationContext()) 18  setRenderer(renderer) 19  setRenderMode(RENDERMODE_CONTINUOUSLY) 20  } 21 22  @Override 23 public void onTouchEvent(MotionEvent event) { 24 switch (event.getAction()) { 25 case MotionEvent.ACTION_DOWN: 26 break 27 case MotionEvent.ACTION_UP: 28  renderer.resetDrag() 29 break 30 case MotionEvent.ACTION_MOVE: 31  renderer.drag(event.getX(), event.getY()) 32 break 33 case MotionEvent.ACTION_CANCEL: 34 break 35  } 36  } 37 38 public void onDestroy() { 39 super.onDestroy() 40 if (renderer != null) { 41  renderer.release() 42  } 43 renderer = null 44  } 45  } 46 }


Evo rezultata mog vlastitog pokušaja:

-

Pretiskano na: https://www.cnblogs.com/YFEYI/p/10622269.html