【Android】用Cubism 2制作自己的Live2D——官方App样例源码学习(4)!

前言-

这是最后一个重要的类了——LAppLive2DManager,流程什么的也清晰了,话不多说我们来康康吧!

 

LAppLive2DManager-

public class LAppLive2DManager{
    public void releaseModel(){}   
    public void update(GL10 gl){  }
    public LAppModel getModel(int no){}
    public int getModelNum(){}
    public LAppView  createView(Activity act){}
    public void onSurfaceChanged(GL10 gl, int width, int height){}
    public void changeModel(){}
    public boolean tapEvent(float x,float y){}

    public void flickEvent(float x,float y){}

    public void shakeEvent(){}

    public void setDrag(float x,float y){}
    public L2DViewMatrix getViewMatrix(){}
}
结构

 

model切换-

public void releaseModel(){
        for(int i=0;i<models.size();i++){
            models.get(i).release();
        }

        models.clear();
    }

    public void update(GL10 gl){
        view.update();
        if(reloadFlg){
            
            reloadFlg=false;

            int no = modelCount % 4;

            try {
                switch (no) {
                case 0:
                    releaseModel();

                    models.add(new LAppModel());
                    models.get(0).load(gl, LAppDefine.MODEL_HARU);
                    models.get(0).feedIn();
                    break;
                case 1:
                    releaseModel();

                    models.add(new LAppModel());
                    models.get(0).load(gl, LAppDefine.MODEL_SHIZUKU);
                    models.get(0).feedIn();
                    break;
                case 2:
                    releaseModel();

                    models.add(new LAppModel());
                    models.get(0).load(gl, LAppDefine.MODEL_WANKO);
                    models.get(0).feedIn();
                    break;
                case 3:
                    releaseModel();

                    models.add(new LAppModel());
                    models.get(0).load(gl, LAppDefine.MODEL_HARU_A);
                    models.get(0).feedIn();

                    models.add(new LAppModel());
                    models.get(1).load(gl, LAppDefine.MODEL_HARU_B);
                    models.get(1).feedIn();
                    break;
                default:

                    break;
                }
            } catch (Exception e) {
                
                Log.e(TAG,"Failed to load."+e.getStackTrace());
                SampleApplication.exit();
            }
        }
    }

每次点击左下角按钮以后,每次都会先releaseModel()一下然后再add新的model,如果需要load更多的model,就像最后一个那样就好了。

 

触摸事件-

public boolean tapEvent(float x,float y)
    {
        if(LAppDefine.DEBUG_LOG)Log.d(TAG, "tapEvent view x:"+x+" y:"+y);

        for (int i=0; i<models.size(); i++)
        {
            if(models.get(i).hitTest(  LAppDefine.HIT_AREA_HEAD,x, y ))
            {
                
                if(LAppDefine.DEBUG_LOG)Log.d(TAG, "Tap face.");
                models.get(i).setRandomExpression();
            }
            else if(models.get(i).hitTest( LAppDefine.HIT_AREA_BODY,x, y))
            {
                if(LAppDefine.DEBUG_LOG)Log.d(TAG, "Tap body.");
                models.get(i).startRandomMotion(LAppDefine.MOTION_GROUP_TAP_BODY, LAppDefine.PRIORITY_NORMAL );
            }
        }
        return true;
    }
tapEvent()里实现了脸部和身体的触摸的功能
脸部触摸后调用setRandomExpression而身体触摸以后调用startRandomMotion

轻触和震动事件-

//轻触事件
public void flickEvent(float x,float y)
    {
        if(LAppDefine.DEBUG_LOG)Log.d(TAG, "flick x:"+x+" y:"+y);

        for (int i=0; i<models.size(); i++)
        {
            if(models.get(i).hitTest( LAppDefine.HIT_AREA_HEAD, x, y ))
            {
                if(LAppDefine.DEBUG_LOG)Log.d(TAG, "Flick head.");
                models.get(i).startRandomMotion(LAppDefine.MOTION_GROUP_FLICK_HEAD, LAppDefine.PRIORITY_NORMAL );
            }
        }
    }

//震动事件
public void shakeEvent()
    {
        if(LAppDefine.DEBUG_LOG)Log.d(TAG, "Shake event.");

        for (int i=0; i<models.size(); i++)
        {
            models.get(i).startRandomMotion(LAppDefine.MOTION_GROUP_SHAKE,LAppDefine.PRIORITY_FORCE );
        }
    }

 

至此项目的大概已经明白了,剩下的就是转换成自己的东西了

posted @ 2019-04-12 17:23  YFEYI  阅读(879)  评论(0编辑  收藏  举报