在线live2d使用

为网站增加live2d看板娘

moc3类型是cubism新版模型的类型,而旧版模型的类型是moc模型

1. moc模型使用

html

 <div>
        <div id="landlord">
            <div class="message" style="opacity:0"></div>
            <canvas id="live2d" width="280" height="250" class="live2d"></canvas>
            <div class="hide-button">隐藏</div>
        </div>
    </div>
    

    <link rel="stylesheet" type="text/css" href="live2d/css/live2d.css" />
    <script>
        var message_Path = 'live2d/message.json';
        var home_Path = 'http://127.0.0.1:8080/';               //此处可以修改为你的域名,必须带斜杠
    </script>

    <script src="live2d/js/live2d.js"></script>
    <script src="live2d/js/message.js"></script>
    
    <script type="text/javascript">
        loadlive2d("live2d", "live2d/model/aniya/ANIYA.model3.json");
    </script>


live2d目录
在这里插入图片描述
message.jsonmessage.json里存放对话
在这里插入图片描述
var message_Path = ‘live2d/message.json’;会自动映射为message.jsonmessage.json

2. moc3模型使用

参考 https://letere-gzj.github.io/hugo-stack/p/live2d-moc3/

js

const cdnPath = 'live2dmoc3';
    localStorage.clear();
     localStorage.setItem("modelName", name);
    // console.log("localStorage.getItem('modelName')", localStorage.getItem('modelId'));
    const config = {
      // 资源路径
      path: {
        modelPath: cdnPath + "/Resources/",
        cssPath: cdnPath + "/waifu.css",
        tipsJsonPath: cdnPath + "/waifu-tips.json",
        tipsJsPath: cdnPath + "/waifu-tips.js",
        live2dCorePath: cdnPath + "/Core/live2dcubismcore.js",
        live2dSdkPath: cdnPath + "/live2d-sdk.js"
      },
      // 工具栏
      tools: ["hitokoto", "asteroids", "express", "switch-model", "switch-texture", "info", "quit"],
      // 模型拖拽
      drag: {
        enable: true,
        direction: ["x", "y"]
      },
      // 模型切换(order: 顺序切换,random: 随机切换)
      switchType: "order"
    }
  
    // 加载资源并初始化
    if (screen.width >= 768) {
      Promise.all([
        loadExternalResource(config.path.cssPath, "css"),
        loadExternalResource(config.path.live2dCorePath, "js"),
        loadExternalResource(config.path.live2dSdkPath, "js"),
        loadExternalResource(config.path.tipsJsPath, "js")
      ]).then(() => {
        initWidget({
          waifuPath: config.path.tipsJsonPath,
          cdnPath: config.path.modelPath,
          tools: config.tools,
          dragEnable: config.drag.enable,
          dragDirection: config.drag.direction,
          switchType: config.switchType
        });
      });
    }
  
    // 异步加载资源
    function loadExternalResource(url, type) {
      return new Promise((resolve, reject) => {
        let tag;
        if (type === "css") {
          tag = document.createElement("link");
          tag.rel = "stylesheet";
          tag.href = url;
        }
        else if (type === "js") {
          tag = document.createElement("script");
          tag.src = url;
        }
        if (tag) {
          tag.onload = () => resolve(url);
          tag.onerror = () => reject(url);
          document.head.appendChild(tag);
        }
      });
    }

live2dmoc3目录
在这里插入图片描述
waifu-tips.json存放对话
在这里插入图片描述

3. moc3效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值