Skip to content

Instantly share code, notes, and snippets.

@tetya
Last active April 28, 2016 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tetya/9160a726e51b7e01b2cf25daf7804be4 to your computer and use it in GitHub Desktop.
Save tetya/9160a726e51b7e01b2cf25daf7804be4 to your computer and use it in GitHub Desktop.
マウス先を向いてくれない
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using live2d;
using live2d.framework;
[ExecuteInEditMode]
public class SimpleModel : MonoBehaviour
{
public TextAsset mocFile;
public TextAsset physicsFile;
public Texture2D[] textureFiles;
public TextAsset[] mtnFiles; // mtnファイル
private Live2DModelUnity live2DModel;
private EyeBlinkMotion eyeBlink = new EyeBlinkMotion();
private L2DTargetPoint dragMgr = new L2DTargetPoint();
private L2DPhysics physics;
private Matrix4x4 live2DCanvasPos;
private List<Live2DMotion> motionList = new List<Live2DMotion>();
private MotionQueueManager mainMotManager; // モーション管理クラス
void Start(){
//初期化
Live2D.init();
// モーション管理クラスのインスタンスの作成
mainMotManager = new MotionQueueManager();
//モーションデータの用意
SetMotionData();
// モーションの再生
mainMotManager.startMotion( motionList[0], false );
//モデルの読み込み
load();
}
void SetMotionData(){
// モーションのインスタンスの作成(mtnの読み込み)
foreach(TextAsset file in mtnFiles){
motionList.Add(Live2DMotion.loadMotion (file.bytes));
}
//待機モーションはループ設定にしておく
motionList[0].setLoop( true );
}
void load()
{
Debug.Log ("loadが呼ばれました");
live2DModel = Live2DModelUnity.loadModel(mocFile.bytes);
for (int i = 0; i < textureFiles.Length; i++){
live2DModel.setTexture(i, textureFiles[i]);
}
float modelWidth = live2DModel.getCanvasWidth();
live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50.0f, 50.0f);
if (physicsFile != null) physics = L2DPhysics.load(physicsFile.bytes);
//最初は正面を向かせる
live2DModel.setParamFloat("PARAM_ANGLE_X", 0);
live2DModel.setParamFloat("PARAM_ANGLE_Y", 0);
}
void Update(){
//モデルの状態を監視
CheckModel ();
//マウスの方向を向かせる処理
LookAtMouse();
//瞬きの更新(live2DModel.updateより先に呼び出す必要あり)
eyeBlink.setParam(live2DModel);
// 再生中のモーションからモデルパラメータを更新
mainMotManager.updateParam( live2DModel );
//モデルの描画更新
live2DModel.update();
}
void CheckModel(){
if (live2DModel == null) load();
live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);
if (!Application.isPlaying)
{
live2DModel.update();
return;
}
if (physics != null) physics.updateParam(live2DModel);
}
void LookAtMouse(){
var pos = Input.mousePosition;
if (Input.GetMouseButtonDown(0))
{
//
}
else if (Input.GetMouseButton(0))
{
dragMgr.Set(pos.x / Screen.width * 2 - 1, pos.y / Screen.height * 2 - 1);
}
else if (Input.GetMouseButtonUp(0))
{
dragMgr.Set(0, 0);
}
dragMgr.update();
// マウスの座標で顔の角度XYを動かす(加算)
float targetX = 2 * Input.mousePosition.x / Screen.width - 1 ;
float targetY = 2 * Input.mousePosition.y / Screen.height - 1 ;
live2DModel.addToParamFloat ( "PARAM_ANGLE_X", 30 * targetX, 1 );
live2DModel.addToParamFloat ( "PARAM_ANGLE_Y", 30 * targetY, 1 );
}
void OnRenderObject(){
if (live2DModel == null) load();
if (live2DModel.getRenderMode() == Live2D.L2D_RENDER_DRAW_MESH_NOW) live2DModel.draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment