/* * カメラの 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); } }