示例#1
0
    void Start()
    {
        //初始化环境
        Live2D.init();
        //string path = Application.dataPath + "/Resources/Epsilon/runtime/Epsilon.moc";
        //Live2DModelUnity.loadModel(path);
        //加载模型文件
        live2dModel = Live2DModelUnity.loadModel(modelTxt.bytes);
        for (int i = 0; i < texture2d.Length; i++)
        {
            live2dModel.setTexture(i, texture2d[i]);
        }
        float modelWidth = live2dModel.getCanvasWidth();

        live2dCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50, 50);

        //加载模型动作文件
        motions = new Live2DMotion[motionTxt.Length];
        for (int i = 0; i < motions.Length; i++)
        {
            motions[i] = Live2DMotion.loadMotion(motionTxt[i].bytes);
        }
        l2dMotionManager = new L2DMotionManager();

        expressionMotions = new L2DExpressionMotion[expressionTxt.Length];
        for (int i = 0; i < expressionMotions.Length; i++)
        {
            expressionMotions[i] = L2DExpressionMotion.loadJson(expressionTxt[i].bytes);
        }

        eyeBlinkMotion = new EyeBlinkMotion();
        eyeBlinkMotion.setParam(live2dModel);
        drag           = new L2DTargetPoint();
        physicHairBack = new PhysicsHair();
        physicHairSide = new PhysicsHair();
        physicHairSide.setup(0.2f, 0.5f, 0.14f);
        PhysicsHair.Src srcX = PhysicsHair.Src.SRC_TO_X;                  //横向摇摆
        physicHairSide.addSrcParam(srcX, "PARAM_ANGLE_X", 0.005f, 1);
        PhysicsHair.Target target = PhysicsHair.Target.TARGET_FROM_ANGLE; //表现形式
        physicHairSide.addTargetParam(target, "PARAM_HAIR_SIDE_L", 0.005f, 1);

        //motions[0].setLoop(true);
        motionQueueManager = new MotionQueueManager();
        //motionQueueManager.startMotion(motions[0]);
    }
示例#2
0
        public static L2DPhysics load(char[] buf)
        {
            L2DPhysics ret = new L2DPhysics();

            Value json = Json.parseFromBytes(buf);


            List <Value> params_  = json.get("physics_hair").getVector(null);
            int          paramNum = params_.Count;

            for (int i = 0; i < paramNum; i++)
            {
                Value param = params_[i];

                PhysicsHair physics = new PhysicsHair();

                Value setup = param.get("setup");

                float length = setup.get("length").toFloat();

                float resist = setup.get("regist").toFloat();

                float mass = setup.get("mass").toFloat();
                physics.setup(length, resist, mass);


                List <Value> srcList = param.get("src").getVector(null);
                int          srcNum  = srcList.Count;
                for (int j = 0; j < srcNum; j++)
                {
                    Value           src     = srcList[j];
                    string          id      = src.get("id").toString();//param ID
                    PhysicsHair.Src type    = PhysicsHair.Src.SRC_TO_X;
                    string          typeStr = src.get("ptype").toString();
                    if (typeStr == "x")
                    {
                        type = PhysicsHair.Src.SRC_TO_X;
                    }
                    else if (typeStr == "y")
                    {
                        type = PhysicsHair.Src.SRC_TO_Y;
                    }
                    else if (typeStr == "angle")
                    {
                        type = PhysicsHair.Src.SRC_TO_G_ANGLE;
                    }
                    else
                    {
                        UtDebug.error("live2d", "Invalid parameter:hysicsHair.Src");
                    }

                    float scale  = src.get("scale").toFloat();
                    float weight = src.get("weight").toFloat();
                    physics.addSrcParam(type, id, scale, weight);
                }


                List <Value> targetList = param.get("targets").getVector(null);
                int          targetNum  = targetList.Count;
                for (int j = 0; j < targetNum; j++)
                {
                    Value              target  = targetList[j];
                    string             id      = target.get("id").toString();//param ID
                    PhysicsHair.Target type    = PhysicsHair.Target.TARGET_FROM_ANGLE;
                    string             typeStr = target.get("ptype").toString();
                    if (typeStr == "angle")
                    {
                        type = PhysicsHair.Target.TARGET_FROM_ANGLE;
                    }
                    else if (typeStr == "angle_v")
                    {
                        type = PhysicsHair.Target.TARGET_FROM_ANGLE_V;
                    }
                    else
                    {
                        UtDebug.error("live2d", "Invalid parameter:PhysicsHair.Target");
                    }

                    float scale  = target.get("scale").toFloat();
                    float weight = target.get("weight").toFloat();
                    physics.addTargetParam(type, id, scale, weight);
                }
                ret.addParam(physics);
            }

            return(ret);
        }
示例#3
0
    // Use this for initialization
    void Start()
    {
        //初始化
        Live2D.init();

        //释放
        //Live2D.dispose();

        //读取模型
        //Live2DModelUnity.loadModel(Application.dataPath+ "/Resources/Epsilon/runtime/Epsilon.moc");
        //TextAsset mocFile = Resources.Load<TextAsset>("Epsilon/runtime/Epsilon.moc");

        live2DModel = Live2DModelUnity.loadModel(modelFile.bytes);

        //与贴图建立关联
        //Texture2D texture2D1 = Resources.Load<Texture2D>("Epsilon/runtime/Epsilon.1024/texture_00");
        //Texture2D texture2D2 = Resources.Load<Texture2D>("Epsilon/runtime/Epsilon.1024/texture_01");
        //Texture2D texture2D3 = Resources.Load<Texture2D>("Epsilon/runtime/Epsilon.1024/texture_02");
        //live2DModel.setTexture(0,texture2D1);
        //live2DModel.setTexture(1, texture2D2);
        //live2DModel.setTexture(2, texture2D3);
        for (int i = 0; i < textures.Length; i++)
        {
            live2DModel.setTexture(i, textures[i]);
        }

        //指定显示位置与尺寸(使用正交矩阵与相关API显示图像,再由游戏物体的位置和摄像机的size调整图像到合适的位置)
        float modelWidth = live2DModel.getCanvasWidth();

        live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50, 50);

        //播放动作
        //实例化动作对象
        //live2DMontionIdle = Live2DMotion.loadMotion(Application.dataPath+"");
        //TextAsset mtnFile = Resources.Load<TextAsset>("");
        //live2DMontionIdle= Live2DMotion.loadMotion(mtnFile.bytes);
        motions = new Live2DMotion[motionFiles.Length];
        for (int i = 0; i < motions.Length; i++)
        {
            motions[i] = Live2DMotion.loadMotion(motionFiles[i].bytes);
        }
        //设置某一个动画的一些属性
        //重复播放不淡入。
        motions[0].setLoopFadeIn(false);
        //设置淡入淡出时间,参数单位为毫秒
        motions[0].setFadeOut(1000);
        motions[0].setFadeIn(1000);
        //动画是否循环播放
        //motions[0].setLoop(true);

        //motionQueueManager = new MotionQueueManager();
        //motionQueueManager.startMotion(motions[0]);

        ////播放多个动作
        //motions[5].setLoop(true);

        //motionQueueManagerA = new MotionQueueManager();
        //motionQueueManagerA.startMotion(motions[5]);

        //动作的优先级使用
        l2DMotionManager = new L2DMotionManager();

        //眨眼
        eyeBlinkMotion = new EyeBlinkMotion();

        //鼠标拖拽
        drag = new L2DTargetPoint();


        #region 左右两侧头发的摇摆
        //左测旁边的头发
        physicsHairSideLeft = new PhysicsHair();
        //套用物理运算
        physicsHairSideLeft.setup(0.2f,   // 长度 : 单位是公尺 影响摇摆周期(快慢)
                                  0.5f,   // 空气阻力 : 可设定0~1的值、预设值是0.5 影响摇摆衰減的速度
                                  0.14f); // 质量 : 单位是kg 
        //设置输入参数
        //设置哪一个部分变动时进行哪一种物理运算
        PhysicsHair.Src srcXLeft = PhysicsHair.Src.SRC_TO_X;//横向摇摆

        //第三个参数,"PARAM_ANGLE_X"变动时头发受到0.005倍的影响度的输入参数
        physicsHairSideLeft.addSrcParam(srcXLeft, "PARAM_ANGLE_X", 0.005f, 1);

        //设置输出表现
        PhysicsHair.Target target = PhysicsHair.Target.TARGET_FROM_ANGLE;//表现形式

        physicsHairSideLeft.addTargetParam(target, "PARAM_HAIR_SIDE_L", 0.005f, 1);


        //右侧旁边的头发
        physicsHairSideRight = new PhysicsHair();
        //套用物理运算
        physicsHairSideRight.setup(0.2f,   // 长度 : 单位是公尺 影响摇摆周期(快慢)
                                   0.5f,   // 空气阻力 : 可设定0~1的值、预设值是0.5 影响摇摆衰減的速度
                                   0.14f); // 质量 : 单位是kg 
        //设置输入参数
        //设置哪一个部分变动时进行哪一种物理运算
        PhysicsHair.Src srcXRight = PhysicsHair.Src.SRC_TO_X;//横向摇摆
        //PhysicsHair.Src srcXRight = PhysicsHair.Src.SRC_TO_Y;

        //第三个参数,"PARAM_ANGLE_X"变动时头发受到0.005倍的影响度的输入参数
        physicsHairSideRight.addSrcParam(srcXRight, "PARAM_ANGLE_X", 0.005f, 1);

        //设置输出表现
        PhysicsHair.Target targetRight = PhysicsHair.Target.TARGET_FROM_ANGLE;//表现形式

        physicsHairSideRight.addTargetParam(targetRight, "PARAM_HAIR_SIDE_R", 0.005f, 1);

        #endregion

        #region 左右后边头发的摇摆
        //左边
        physicsHairBackLeft = new PhysicsHair();
        physicsHairBackLeft.setup(0.24f, 0.5f, 0.18f);

        PhysicsHair.Src srcXBackLeft = PhysicsHair.Src.SRC_TO_X;
        PhysicsHair.Src srcZBackLeft = PhysicsHair.Src.SRC_TO_G_ANGLE;

        physicsHairBackLeft.addSrcParam(srcXBackLeft, "PARAM_ANGLE_X", 0.005f, 1);
        physicsHairBackLeft.addSrcParam(srcZBackLeft, "PARAM_ANGLE_Z", 0.8f, 1);

        PhysicsHair.Target targetBackLeft = PhysicsHair.Target.TARGET_FROM_ANGLE;

        physicsHairBackLeft.addTargetParam(targetBackLeft, "PARAM_HAIR_BACK_L", 0.005f, 1);

        //右边
        physicsHairBackRight = new PhysicsHair();
        physicsHairBackRight.setup(0.24f, 0.5f, 0.18f);

        PhysicsHair.Src srcXBackRight = PhysicsHair.Src.SRC_TO_X;
        PhysicsHair.Src srcZBackRight = PhysicsHair.Src.SRC_TO_G_ANGLE;

        physicsHairBackRight.addSrcParam(srcXBackRight, "PARAM_ANGLE_X", 0.005f, 1);
        physicsHairBackRight.addSrcParam(srcZBackRight, "PARAM_ANGLE_Z", 0.8f, 1);

        PhysicsHair.Target targetBackRight = PhysicsHair.Target.TARGET_FROM_ANGLE;

        physicsHairBackRight.addTargetParam(targetBackRight, "PARAM_HAIR_BACK_R", 0.005f, 1);

        #endregion

        //表情
        expresionMotionQueueManager = new MotionQueueManager();
        expressions = new L2DExpressionMotion[expressionFiles.Length];
        for (int i = 0; i < expressions.Length; i++)
        {
            expressions[i] = L2DExpressionMotion.loadJson(expressionFiles[i].bytes);
        }
    }
示例#4
0
    void Start()
    {
        //初始化
        Live2D.init();

        //读取模型
        //Live2DModelUnity.loadModel(Application.dataPath+"/Resources/Epsilon/runtime/Epsilon.moc");

        //第二种读取形式
        //TextAsset mocFile = Resources.Load<TextAsset>("Epsilon/runtime/Epsilon.moc");
        live2DModel = Live2DModelUnity.loadModel(modelFile.bytes);
        #region
        //与贴图建立关联
        //Texture2D texture2D1 = Resources.Load<Texture2D>("Epsilon/runtime/Epsilon.1024/texture_00");
        //Texture2D texture2D2 = Resources.Load<Texture2D>("Epsilon/runtime/Epsilon.1024/texture_01");
        //Texture2D texture2D3 = Resources.Load<Texture2D>("Epsilon/runtime/Epsilon.1024/texture_02");

        //live2DModel.setTexture(0,texture2D1);
        //live2DModel.setTexture(1,texture2D2);
        //live2DModel.setTexture(2,texture2D3);
        #endregion
        for (int i = 0; i < textures.Length; i++)
        {
            live2DModel.setTexture(i, textures[i]);
        }

        //指定显示位置与尺寸
        float modelWidth = live2DModel.getCanvasWidth();

        live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50, 50);

        //播放动作
        //实例化动作
        //live2DMotionIdle = Live2DMotion.loadMotion(Application.dataPath + "/Resources/Epsilon/runtime/motions/Epsilon_idle_01");
        motions = new Live2DMotion[motionFiles.Length];
        for (int i = 0; i < motionFiles.Length; i++)
        {
            motions[i] = Live2DMotion.loadMotion(motionFiles[i].bytes);
        }
        #region
        //设置某一动画的一些属性
        //重复播放不淡入
        motions[0].setLoopFadeIn(false);
        motions[0].setFadeOut(1000);//毫秒
        motions[0].setFadeIn(1000);
        motions[0].setLoop(true);

        //motionQueueManager = new MotionQueueManager();
        //motionQueueManager.startMotion(motions[motionIndex]);

        //motions[5].setLoop(true);
        //motionQueueManagerB = new MotionQueueManager();
        //motionQueueManagerB.startMotion(motions[5]);
        #endregion

        //动作优先级使用
        motionManager = new L2DMotionManager();

        //眨眼
        eyeBlinkMotion = new EyeBlinkMotion();

        drag = new L2DTargetPoint();
        #region 头发物理效果处理
        sideHairLeft  = new PhysicsHair();
        sideHairRight = new PhysicsHair();
        backHairLeft  = new PhysicsHair();
        backHairRight = new PhysicsHair();

        //套用物理运算
        sideHairLeft.setup(0.2f, 0.5f, 0.14f);
        sideHairRight.setup(0.2f, 0.5f, 0.14f);
        PhysicsHair.Src srcX = PhysicsHair.Src.SRC_TO_X;//横向摇摆
        PhysicsHair.Src srcZ = PhysicsHair.Src.SRC_TO_G_ANGLE;

        sideHairLeft.addSrcParam(srcX, "PARAM_ANGLE_X", 0.005f, 1);
        sideHairRight.addSrcParam(srcX, "PARAM_ANGLE_X", 0.005f, 1);

        backHairLeft.setup(0.24f, 0.5f, 0.18f);
        backHairRight.setup(0.24f, 0.5f, 0.18f);

        backHairLeft.addSrcParam(srcX, "PARAM_ANGLE_X", 0.005f, 1);
        backHairLeft.addSrcParam(srcZ, "PARAM_ANGLE_Z", 0.8f, 1);
        backHairRight.addSrcParam(srcX, "PARAM_ANGLE_X", 0.005f, 1);
        backHairRight.addSrcParam(srcZ, "PARAM_ANGLE_Z", 0.8f, 1);

        //设置输出表现
        PhysicsHair.Target target = PhysicsHair.Target.TARGET_FROM_ANGLE;
        sideHairLeft.addTargetParam(target, "PARAM_HAIR_SIDE_L", 0.005f, 1);
        sideHairRight.addTargetParam(target, "PARAM_HAIR_SIDE_R", 0.005f, 1);
        backHairLeft.addTargetParam(target, "PARAM_HAIR_BACK_L", 0.005f, 1);
        backHairRight.addTargetParam(target, "PARAM_HAIR_BACK_R", 0.005f, 1);
        #endregion

        //表情
        expressionManager = new MotionQueueManager();
        expressions       = new L2DExpressionMotion[expressionFiles.Length];
        for (int i = 0; i < expressionFiles.Length; i++)
        {
            expressions[i] = L2DExpressionMotion.loadJson(expressionFiles[i].bytes);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        Live2D.init();//Live2D的初始化

        //Live2D.dispose();//Live2D的释放

        //读取模型

        //moc读取
        //Application.dataPath 就是Assets文件夹下目录
        //Live2DModelUnity.loadModel(Application.dataPath+ "/Resources/Epsilon/runtime/Epsilon.moc");

        //json读取 和之前替换模型一样操作 moc复制出来一个后缀加.bytes
        //modleFile = Resources.Load<TextAsset>("Epsilon/runtime/Epsilon.moc");
        live2DModel = Live2DModelUnity.loadModel(modleFile.bytes);

        //与贴图建立关联

        //加载所有贴图
        //tex = Resources.LoadAll<Texture2D>("Epsilon/runtime/Epsilon.1024");
        for (int i = 0; i < tex.Length; i++)
        {
            live2DModel.setTexture(i, tex[i]);
        }

        //制定显示位置和尺寸 参数分别为左右下上 近视口距离(离相机近的距离)50 远视口距离(离相机远的距离)-50 (官方建议参数)
        //正交矩阵与相关Api设置显示图像
        float modelWidth  = live2DModel.getCanvasWidth();
        float modelHeight = live2DModel.getCanvasHeight();

        live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, 50, -50);

        //播放动作
        //实例化动作对象
        //live2DMotionIdle = Live2DMotion.loadMotion(Application.dataPath+"");
        //TextAsset mtnFile = Resources.Load<TextAsset>();
        //live2DMotionIdle = Live2DMotion.loadMotion(mtnFile.bytes);

        motions = new Live2DMotion[motionFiles.Length];
        for (int i = 0; i < motions.Length; i++)
        {
            motions[i] = Live2DMotion.loadMotion(motionFiles[i].bytes);
        }

        //设置某一个动画的一些属性
        //重复播放是否淡入 不带很僵硬
        motions[0].setLoopFadeIn(false);
        //设置淡入淡出时间 参数毫秒
        motions[0].setFadeOut(1000);
        motions[0].setFadeIn(1000);

        ////动画是否循环
        //motions[0].setLoop(true);

        //motions[5].setLoop(true);
        ////第二个参数播放完是否删除
        //motionQueueManager.startMotion(motions[0]);
        //motionQueueManagerA.startMotion(motions[5]);


        #region  左右两侧头发的摇摆
        //头发摇摆左
        //套用物理运算 长度影响摇摆周期(短慢长快)
        //空气阻力0-1也影响速度 默认0.5
        //质量
        physicsHairSideLeft.setup(0.2f, 0.5f, 0.14f);

        //设置输入参数
        //设置哪一个部分变动时进行哪一种物理运算
        PhysicsHair.Src srcXLeft = PhysicsHair.Src.SRC_TO_X;//横向摇摆
        //第三个参数 头发影响度 变动时头发受到0.005影响速度 json表推荐 第四个权重
        physicsHairSideLeft.addSrcParam(srcXLeft, "PARAM_ANGLE_X", 0.005f, 1);
        //设置输出表现
        PhysicsHair.Target targetLeft = PhysicsHair.Target.TARGET_FROM_ANGLE;//根据角度表现`angleV会很快到极限值然后停留一段

        physicsHairSideLeft.addTargetParam(targetLeft, "PARAM_HAIR_SIDE_L", 0.005f, 1);

        //头发摇摆右
        //套用物理运算 长度影响摇摆周期(短慢长快)
        //空气阻力0-1也影响速度 默认0.5
        //质量
        physicsHairSideRight.setup(0.2f, 0.5f, 0.14f);

        //设置输入参数
        //设置哪一个部分变动时进行哪一种物理运算
        PhysicsHair.Src srcXRight = PhysicsHair.Src.SRC_TO_X;//横向摇摆
        //第三个参数 头发影响度 变动时头发受到0.005影响速度 json表推荐 第四个权重
        physicsHairSideRight.addSrcParam(srcXRight, "PARAM_ANGLE_X", 0.005f, 1);
        //设置输出表现
        PhysicsHair.Target targetRight = PhysicsHair.Target.TARGET_FROM_ANGLE;//根据角度表现`

        physicsHairSideRight.addTargetParam(targetRight, "PARAM_HAIR_SIDE_R", 0.005f, 1);
        #endregion

        #region 前后头发摇摆
        //左边
        physicsHairBackLeft.setup(0.24f, 0.5f, 0.18f);
        PhysicsHair.Src srcXBackLeft = PhysicsHair.Src.SRC_TO_X;
        PhysicsHair.Src srcZBackLeft = PhysicsHair.Src.SRC_TO_G_ANGLE;
        physicsHairBackLeft.addSrcParam(srcXBackLeft, "PARAM_ANGLE_X", 0.005f, 1);
        physicsHairBackLeft.addSrcParam(srcZBackLeft, "PARAM_ANGLE_Z", 0.8f, 1);

        PhysicsHair.Target targetBackLeft = PhysicsHair.Target.TARGET_FROM_ANGLE;
        physicsHairBackLeft.addTargetParam(targetBackLeft, "PARAM_HAIR_BACK_L", 0.005f, 1);

        //右边
        physicsHairBackRight.setup(0.24f, 0.5f, 0.18f);
        PhysicsHair.Src srcXBackRight = PhysicsHair.Src.SRC_TO_X;
        PhysicsHair.Src srcZBackRight = PhysicsHair.Src.SRC_TO_G_ANGLE;
        physicsHairBackRight.addSrcParam(srcXBackRight, "PARAM_ANGLE_X", 0.005f, 1);
        physicsHairBackRight.addSrcParam(srcZBackRight, "PARAM_ANGLE_Z", 0.8f, 1);

        PhysicsHair.Target targetBackRight = PhysicsHair.Target.TARGET_FROM_ANGLE;
        physicsHairBackRight.addTargetParam(targetBackRight, "PARAM_HAIR_BACK_R", 0.005f, 1);
        #endregion

        ////加载表情数据
        expressions = new L2DExpressionMotion[expressionFiles.Length];
        for (int i = 0; i < expressions.Length; i++)
        {
            expressions[i] = L2DExpressionMotion.loadJson(expressionFiles[i].bytes);
        }
    }