void OnPostRender() { Live2DModelUnity live2DModel = (Live2DModelUnity)_model.getLive2DModel(); var w = live2DModel.getCanvasWidth(); var h = live2DModel.getCanvasHeight(); L2DModelMatrix matrix = new L2DModelMatrix(w, h); matrix.setWidth(_w); matrix.setCenterPosition(0.5f, 0.5f); matrix.setX(_x); matrix.setY(_y); Matrix4x4 m1 = Matrix4x4.identity; float[] m2 = matrix.getArray(); for (int i = 0; i < 16; i++) { m1[i] = m2[i]; } live2DModel.setMatrix(m1); //_model.Update();_model.Draw 中有调用 UpdateMouth(); live2DModel.update(); _model.Draw(Live2dViewType); }
/* * カメラの Projection が Orthographic である場合(2Dカメラ) */ protected void UpdateTouchPos_2DCamera(Vector3 inputPos) { // Debug.Log("UpdateTouchPos_2DCamera----------------------" + transform+"----------------------"); //--- calc local plane coord --- { Vector3 worldP_LT = transform.TransformPoint(localP_LT); Vector3 worldP_RT = transform.TransformPoint(localP_RT); Vector3 worldP_LB = transform.TransformPoint(localP_LB); Vector3 ScreenLT = camera.WorldToScreenPoint(worldP_LT); Vector3 ScreenRT = camera.WorldToScreenPoint(worldP_RT); Vector3 ScreenLB = camera.WorldToScreenPoint(worldP_LB); Vector3 sVX = ScreenRT - ScreenLT; Vector3 sVY = ScreenLB - ScreenLT; // ScreenLTを原点としてsVX、sVYのベクトルで入力のinputPosを成分分解する。 float x = inputPos.x; float y = inputPos.y; float vax = sVX.x; float vay = sVX.y; float vbx = sVY.x; float vby = sVY.y; float p0x = ScreenLT.x; float p0y = ScreenLT.y; float f = vbx * vay - vby * vax; float g = p0y * vax - p0x * vay - y * vax + x * vay; if (f == 0 || vax == 0) { //not update value } else { float t = g / f; float k = (x - p0x - t * vbx) / vax; touchPos_onPlane.x = k; touchPos_onPlane.y = t; } } //--- calc touchPos on the model canvas { // 0..1の座標を-1..1の座標に揃える。Yは左上を原点にしているため反転。 float touchPos_plane2x2_X = touchPos_onPlane.x * 2 - 1; float touchPos_plane2x2_Y = -touchPos_onPlane.y * 2 + 1; L2DModelMatrix m = model.getModelMatrix(); touchPos_onModelCanvas.x = m.invertTransformX(touchPos_plane2x2_X); touchPos_onModelCanvas.y = m.invertTransformY(touchPos_plane2x2_Y); } }
protected void UpdateTouchPos_2DCamera(Vector3 inputPos) { //--- calc local plane coord --- { Vector3 worldP_LT = transform.TransformPoint(localP_LT); Vector3 worldP_RT = transform.TransformPoint(localP_RT); Vector3 worldP_LB = transform.TransformPoint(localP_LB); Vector3 ScreenLT = Camera.main.WorldToScreenPoint(worldP_LT); Vector3 ScreenRT = Camera.main.WorldToScreenPoint(worldP_RT); Vector3 ScreenLB = Camera.main.WorldToScreenPoint(worldP_LB); Vector3 sVX = ScreenRT - ScreenLT; Vector3 sVY = ScreenLB - ScreenLT; float x = inputPos.x; float y = inputPos.y; float vax = sVX.x; float vay = sVX.y; float vbx = sVY.x; float vby = sVY.y; float p0x = ScreenLT.x; float p0y = ScreenLT.y; float f = vbx * vay - vby * vax; float g = p0y * vax - p0x * vay - y * vax + x * vay; if (f == 0 || vax == 0) { //not update value } else { float t = g / f; float k = (x - p0x - t * vbx) / vax; touchPos_onPlane.x = k; touchPos_onPlane.y = t; } } //--- calc touchPos on the model canvas { float touchPos_plane2x2_X = touchPos_onPlane.x * 2 - 1; float touchPos_plane2x2_Y = -touchPos_onPlane.y * 2 + 1; L2DModelMatrix m = model.getModelMatrix(); touchPos_onModelCanvas.x = m.invertTransformX(touchPos_plane2x2_X); touchPos_onModelCanvas.y = m.invertTransformY(touchPos_plane2x2_Y); } }
/* * カメラの Projection が Perspective である場合(3Dカメラ) */ protected void UpdateTouchPos_3DCamera(Vector3 inputPos) { Debug.Log("UpdateTouchPos_3DCamera----------------------" + transform + "----------------------"); //--- calc local plane coord --- { Ray ray = Camera.main.ScreenPointToRay(inputPos); Vector3 worldP_LT = transform.TransformPoint(localP_LT); Vector3 worldP_RT = transform.TransformPoint(localP_RT); Vector3 worldP_LB = transform.TransformPoint(localP_LB); Vector3 PO = worldP_LT; Vector3 VX = worldP_RT - worldP_LT; Vector3 VY = worldP_LB - worldP_LT; Vector3 PL = ray.origin; Vector3 VL = ray.direction; float Dx = PO.x - PL.x; float Dy = PO.y - PL.y; float Dz = PO.z - PL.z; float E = (VX.x * VL.y - VX.y * VL.x); float F = (VY.x * VL.y - VY.y * VL.x); float G = (Dx * VL.y - Dy * VL.x); float H = (VX.x * VL.z - VX.z * VL.x); float I = (VY.x * VL.z - VY.z * VL.x); float J = (Dx * VL.z - Dz * VL.x); float tmp = (F * H - E * I); if (tmp == 0) { //not update value } else { touchPos_onPlane.x = (G * I - F * J) / tmp; touchPos_onPlane.y = (E * J - G * H) / tmp; } } //--- calc touchPos on the model canvas { float touchPos_plane2x2_X = touchPos_onPlane.x * 2 - 1; float touchPos_plane2x2_Y = -touchPos_onPlane.y * 2 + 1; L2DModelMatrix m = model.getModelMatrix(); touchPos_onModelCanvas.x = m.invertTransformX(touchPos_plane2x2_X); touchPos_onModelCanvas.y = m.invertTransformY(touchPos_plane2x2_Y); } }
public void loadModelData(string modelFile, string[] texFiles) { if (modelFile == null || texFiles == null) { return; } try { if (LAppDefine.DEBUG_LOG) { Debug.Log("Load model : " + modelFile); } TextAsset modelMoc = FileManager.loadTextAsset(modelFile); live2DModel = Live2DModelUnity.loadModel(modelMoc.bytes); for (int i = 0; i < texFiles.Length; i++) { var texPath = texFiles[i]; if (LAppDefine.DEBUG_LOG) { Debug.Log("Load texture " + texPath); } texPath = Regex.Replace(texPath, ".png$", "");//不要な拡張子を削除 Texture2D texture = (Texture2D)Resources.Load(texPath, typeof(Texture2D)); live2DModel.setTexture(i, texture); } } catch (IOException e) { Debug.Log(e.StackTrace); throw new Exception(); } modelMatrix = new L2DModelMatrix(live2DModel.getCanvasWidth(), live2DModel.getCanvasHeight()); modelMatrix.setWidth(2); modelMatrix.multScale(1, 1, -1); modelMatrix.setCenterPosition(0, 0); }