【Android】用Cubism 2制作自己的Live2D——官方App樣例源碼學習(2)!


前言-

明確了項目目錄的結構,但是結構什么的也太"抽象"了。

本篇開始上代碼!

 

模型的繪制-

前幾天的學習中,live2d的繪制有了一些了解了,在Android端使用OpenGL ES繪制模型時需要Surface繪制和Renderer渲染,那么切入點就為GLSurfaceView和Renderer。

在SampleApp1中的sample包下LAppView和LAppRenderer就分別飾演的這兩個角色

LAppRenderer implements GLSurfaceView.Renderer
LAppView extends GLSurfaceView 

 

渲染器類LAppRenderer -

LAppRenderer 類實現了Renderer接口,實現了onSurfaceCreated、onSurfaceChanged、onDrawFrame方法

@Override
public void onSurfaceCreated(GL10 context, EGLConfig arg1) {
        setupBackground(context);
}

//設置背景圖片
private void setupBackground(GL10 context) {
        try {
            InputStream in = FileManager.open(LAppDefine.BACK_IMAGE_NAME);
            bg=new SimpleImage(context,in);
            
            bg.setDrawRect(
                    LAppDefine.VIEW_LOGICAL_MAX_LEFT,
                    LAppDefine.VIEW_LOGICAL_MAX_RIGHT,
                    LAppDefine.VIEW_LOGICAL_MAX_BOTTOM,
                    LAppDefine.VIEW_LOGICAL_MAX_TOP);     
            bg.setUVRect(0.0f,1.0f,0.0f,1.0f);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
onSurfaceCreated()作為surface被創建后需要做的處理,但是這里只加載了背景圖片
在onDrawFrame()方法中有很多的OpenGL繪制時的配置,由於LAppLive2DManager和Utils的存在很多操作都不在這里實現了,這里還是很明顯的繪制出了background和models,方法還是熟悉的方法,只是藏了起來
if(bg!=null){
                gl.glPushMatrix() ;
                {
                    float SCALE_X = 0.25f ;
                    float SCALE_Y = 0.1f ;
                    gl.glTranslatef( -SCALE_X  * accelX , SCALE_Y * accelY , 0 ) ;

                    bg.draw(gl);
                }
                gl.glPopMatrix() ;
            }
            
            for(int i=0;i<delegate.getModelNum();i++)
            {
                LAppModel model = delegate.getModel(i);
                if(model.isInitialized() && ! model.isUpdating())
                {
                    model.update();
                    model.draw(gl);
                }
            }
繪制

而onSurfaceChanged方法的實現就非常interesting了,因為在Manager中也有一個同樣的方法

而且在onSurfaceChanged()的第一行就調用了此方法

 1 //LAppLive2DManager
 2 
 3 public void onSurfaceChanged(GL10 gl, int width, int height){
 4     if(LAppDefine.DEBUG_LOG)Log.d(TAG, "onSurfaceChanged "+width+" "+height);
 5     view.setupView(width,height);
 6 
 7     if(getModelNum()==0){
 8             changeModel();
 9     }
10 }        

***這是為了實現點擊左下角按鈕更換模型這個功能的,當沒有模型的時候會把一個reloadFlg的Flag置為true,通過點擊事件達到模型的更換。

同樣的onSurfaceChanged中也存在着大量的OpenGL的配置,在最后的最后有這麽一行代碼

OffscreenImage.createFrameBuffer(gl, width ,height, 0);

這里挖一個小小的坑,我查了半天說是離屏渲染(OffScreen Renderer),同樣也單獨存在OffscreenImage來實現此功能,但是我還沒有搞懂

 

與這個相同的還有一個setAccel()方法的作用沒有弄明白

 

=====================================================================================================================================================================================================

繪制類LAppView -

這是LAppView類的類圖

相信從上圖中可以看出很多屬性和方法都是干什么的比如:

 

-GestureDetector gestureDetector是Android中的手勢動作

 

-onTouchEvent() 則是觸摸事件,還有很多的Began啊、Moved啊和utils中的TouchManage共同來獲得觸摸的點

 

而上面把Renderer寫好了,但是還記得Renderer怎么畫到Surface上面么?沒錯是setRenderer( renderer ) !這一行代碼躺在setLive2DManager()方法中

 1 public void setLive2DManager( LAppLive2DManager live2DMgr){
 2     this.delegate = live2DMgr ;
 3     this.renderer = new LAppRenderer( live2DMgr  ) ;
 4 
 5     setRenderer(renderer);
 6 
 7     gestureDetector = new GestureDetector(this.getContext()  , simpleOnGestureListener ) ;
 8 
 9 
10         
11         deviceToScreen=new L2DMatrix44();
12 
13         
14         viewMatrix=new L2DViewMatrix();
15 
16         
17         viewMatrix.setMaxScale( LAppDefine.VIEW_MAX_SCALE );
18         viewMatrix.setMinScale( LAppDefine.VIEW_MIN_SCALE );
19 
20 
21         
22         viewMatrix.setMaxScreenRect(
23                 LAppDefine.VIEW_LOGICAL_MAX_LEFT,
24                 LAppDefine.VIEW_LOGICAL_MAX_RIGHT,
25                 LAppDefine.VIEW_LOGICAL_MAX_BOTTOM,
26                 LAppDefine.VIEW_LOGICAL_MAX_TOP
27                 );
28 
29         
30         touchMgr=new TouchManager();
31 
32         dragMgr  = new L2DTargetPoint();
33     }
setLive2DManager

其中為手勢gestureDetector綁定了一個監聽器simpleOnGestureListener

 

-setupView()則是一些數值的運算,尤其是那個ratio是不是感覺很熟悉,熟悉去看simple項目的onSurfaceChanged方法中glOrthof第三個參數

 
 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM