public void OnDrawGizmos() { if (node0 == null) { GameObject n0 = GameObject.Find("camera_nearControl"); if (n0 != null) { node0 = n0.transform; } } if (node1 == null) { GameObject n1 = GameObject.Find("camera_midControl"); if (n1 != null) { node1 = n1.transform; } } if (node2 == null) { GameObject n2 = GameObject.Find("camera_farControl"); if (n2 != null) { node2 = n2.transform; } } if (cam_node == null) { GameObject nc = GameObject.Find("Main Camera"); if (nc != null) { cam_node = nc.transform; } } if (target_node == null) { GameObject go = GameObject.Find("camera_target"); if (go != null) { target_node = go.transform; } } Matrix4x4 mat = Matrix4x4.TRS(target_node.position, Quaternion.AngleAxis(camera_pitch, Vector3.right), Vector3.one) * Matrix4x4.TRS(-target_node.position, Quaternion.identity, Vector3.one); node0.localPosition = new Vector3(0.0f, node0.localPosition.y, node0.localPosition.z); node1.localPosition = new Vector3(0.0f, node1.localPosition.y, node1.localPosition.z); node2.localPosition = new Vector3(0.0f, node2.localPosition.y, node2.localPosition.z); target_node.localPosition = new Vector3(0.0f, target_node.localPosition.y, 0.0f); if (target_node != null) { GameFramework.Utility.DrawGizmosCircle(target_node.position, 0.5f); } if ((node0 != null && node1 != null)) { Debug.DrawLine(node0.position, node1.position); Debug.DrawLine(mat.MultiplyPoint(node0.position), mat.MultiplyPoint(node1.position)); } if ((node2 != null && node1 != null)) { Debug.DrawLine(node1.position, node2.position); Debug.DrawLine(mat.MultiplyPoint(node1.position), mat.MultiplyPoint(node2.position)); } if ((node0 != null) && (node1 != null) && (node2 != null)) { SkillViewerCameraSetting.DrawBezier2Line(node0.position, node1.position, node2.position); //DrawBezier2Line SkillViewerCameraSetting.DrawBezier2Line(mat.MultiplyPoint(node0.position), mat.MultiplyPoint(node1.position), mat.MultiplyPoint(node2.position)); //DrawBezier2Line } Vector3 un_rotate_node = SkillViewerCameraSetting.Bezier2(node0.position, node1.position, node2.position, lerpDegree); Debug.DrawLine(un_rotate_node, target_node.position); //cam_node.position = mat.MultiplyPoint(CameraSetting.Bezier2(node0.position, node1.position, node2.position, lerpDegree)); //cam_node.LookAt(target_node.position); Debug.DrawLine(cam_node.position, target_node.position); }
public void Update() { if (camera == null) { return; } #region hide this if (curState == State.Lookat) { camera.transform.position = followPose.pos; } else if (curState == State.FixPose) { camera.transform.position = fixPose.pos; } else if (curState == State.ToLookat) { float t = (Time.time - timeToStartSwitch) / (timeToEndSwitch - timeToStartSwitch); float t2 = Mathf.SmoothStep(0, 1, t); camera.transform.position = Vector3.Lerp(startPos.pos, followPose.pos, t2); camera.transform.rotation = Quaternion.Lerp(startPos.rot, followPose.rot, t2); Debug.Log("turn to follow:" + startPos.pos + "," + followPose.pos); if (Time.time > timeToEndSwitch) { SetState(State.Lookat); } } else if (curState == State.ToFixPose) { float t = (Time.time - timeToStartSwitch) / (timeToEndSwitch - timeToStartSwitch); float t2 = Mathf.SmoothStep(0, 1, t); camera.transform.position = Vector3.Lerp(startPos.pos, fixPose.pos, t2); camera.transform.rotation = Quaternion.Lerp(startPos.rot, fixPose.rot, t2); if (Time.time > timeToEndSwitch) { SetState(State.FixPose); } } #endregion if (followTargetId > 0) { GameObject obj = PluginFramework.Instance.GetGameObject(followTargetId); if (null != obj) { if (cameraSetting.yaw > 180.0f) { cameraSetting.yaw -= 360.0f; } if (cameraSetting.yaw < -180.0f) { cameraSetting.yaw += 360.0f; } Vector3 a = new Vector3(0.0f, cameraSetting.cameraPath_Near.x, cameraSetting.cameraPath_Near.y); Vector3 b = new Vector3(0.0f, cameraSetting.cameraPath_ControlPoint.x, cameraSetting.cameraPath_ControlPoint.y); Vector3 c = new Vector3(0.0f, cameraSetting.cameraPath_Far.x, cameraSetting.cameraPath_Far.y); // rotate matrix Matrix4x4 rotateMat = Matrix4x4.TRS(Vector3.zero, Quaternion.AngleAxis(cameraSetting.yaw, Vector3.up), Vector3.one); // transform matrix for camera pos Matrix4x4 mat = rotateMat * Matrix4x4.TRS(new Vector3(0.0f, cameraSetting.up, 0.0f), Quaternion.AngleAxis(cameraSetting.pitch, Vector3.right), Vector3.one) * Matrix4x4.TRS(-new Vector3(0.0f, cameraSetting.up, 0.0f), Quaternion.identity, Vector3.one); // calculate camera pos in world position followPose.pos = obj.transform.position + mat.MultiplyPoint(SkillViewerCameraSetting.Bezier2(a, b, c, cameraSetting.distance)); //followPose.pos = followPose.pos + new Vector3(0, Mathf.Cos(Mathf.Deg2Rad*Time.timeSinceLevelLoad*10.0f), 0); // update camera position and rotation camera.transform.position = followPose.pos; Vector3 targetV3 = obj.transform.position + new Vector3(0, cameraSetting.up, 0); camera.transform.LookAt(targetV3); Vector3 dir = followPose.pos - targetV3; float length = dir.magnitude; dir.Normalize(); Ray ray = new Ray(targetV3, dir); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, 1000.0f)) { if (hitInfo.distance * 0.85f < length) { Vector3 v3 = targetV3 + (hitInfo.distance * 0.85f) * dir;// camera.transform.position = v3; camera.transform.LookAt(targetV3); } } // debug start Debug.DrawLine(obj.transform.position, obj.transform.position + new Vector3(0.0f, cameraSetting.up, 0.0f)); Debug.DrawLine(followPose.pos, obj.transform.position + new Vector3(0.0f, cameraSetting.up, 0.0f)); Vector3 pos0 = obj.transform.position + mat.MultiplyPoint(a); Vector3 pos1 = obj.transform.position + mat.MultiplyPoint(b); Vector3 pos2 = obj.transform.position + mat.MultiplyPoint(c); Debug.DrawLine(pos0, pos1); Debug.DrawLine(pos1, pos2); SkillViewerCameraSetting.DrawBezier2Line(pos0, pos1, pos2); cameraSetting.targetRoot = obj.transform.position; // debug end } else { followTargetId = 0; } } Debug.DrawLine(followPose.pos, followPose.pos + new Vector3(0.0f, 1.0f, 0.0f)); Vector2 cameraShakeOffset = Vector2.zero; if (cameraSetting.enableCameraShake) { cameraSetting.enableCameraShake = false; cameraSetting.inShaking = true; shakeTrigerTime = Time.realtimeSinceStartup; shakeRandomStart = Random.Range(-1000.0f, 1000.0f); } if (cameraSetting.inShaking) { float elapsed = Time.realtimeSinceStartup - shakeTrigerTime; if (elapsed < cameraSetting.shakeDuration) { float percentComplete = elapsed / cameraSetting.shakeDuration; float damper = 1.0f - Mathf.Clamp(2.0f * percentComplete - 1.0f, 0.0f, 1.0f); float alpha = shakeRandomStart + cameraSetting.shakeSpeed * percentComplete; // map to [-1, 1] float x = Mathf.PerlinNoise(alpha, 0.0f) * 2.0f - 1.0f; float y = Mathf.PerlinNoise(0.0f, alpha) * 2.0f - 1.0f; x *= cameraSetting.shakeMagnitude * damper; y *= cameraSetting.shakeMagnitude * damper; cameraShakeOffset = new Vector2(x, y) * (cameraSetting.distance + 0.1f); } else { cameraSetting.inShaking = false; } } if (cameraSetting.inShaking) { Vector3 o = camera.transform.right * cameraShakeOffset.x + camera.transform.up * cameraShakeOffset.y; camera.transform.position += o; } }