2021년 7월 22일 목요일

[Unity] 기즈모 카메라

 


기즈모 방법 요약

1_기즈모캡쳐
Camera_Gizmo  Compass Gizmo Control를 넣는다

2 3DOBJ
- 객체마다 BoxCollider를 가지고있고 Compass Mouse Event코드를 넣는다

-이쁘게 하기위한 Sprite_Base도 넣는다(Sprite Renderer)

3 Canvas

ort,per 나누기위한용
-Compass Ui Control 코드 넣기

Toggle은 각자 위 코드를 참조하여 모드를 변경한다.





기즈모를 화면에 안보이는 상단위에 위치시킨다




카메라의 빈부모 Pivot을 잡는다




기즈모를 보여줄 Base카메라를 하나 더 올린다
Compass Gizmo Control 코드를 추가한다.









실제 기즈모 객체는 LocalPosition으로 적용될것임
가장 부모인 3dObjectsCompass_Gizmo의 자식으로 두고 0,0,0에위치하도록한다.


실제 기즈모 객체를 만든다_(트랜스폼 위치참조)

실제 기즈모 객체를 만든다_(트랜스폼 위치참조)

실제 기즈모 객체 바닥을 만든다(Sprite Renderer)






기즈모카메라에 보일 캔버스를 하나 준비하여 토글 기능을 추가


Screen Space - Camera에 두고
Compass Ui Control 코드 추가

각 토글에는 알맞는 토글기능을 넣는다

각 토글에는 알맞는 토글기능을 넣는다











CompassGizmoControl

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

[RequireComponent(typeof(Camera))]
public class CompassGizmoControl : MonoBehaviour
{

    //public CameraMovement cameraMovement;
    public CameraMove cameraMovement;

    [HideInInspector]
    public bool IsGizmoClick = false;

    private float ScreenWidth = 400;
    private float ScreenHeight = 300;
    public float xPos = 750;
    public float yPos = 300f;
    private Vector2 lastScreenSize;

    private const float CAMERA_MOVE_TIME_DURATION = 0.5f;
    private const float DEFAULT_CAMERA_PIVOT_DISTANT = 10F; 축을 어디로 둘지 정하는 변수

    private Camera cam_Gizmo;
    private Transform cam_Gizmo_Pivot;

    private GameObject mainCameraTargetObject;
    private Coroutine c;
    private Rect viewportRect;
    private float viewportX = 0.88f;

    void Reset()
    {
        cam_Gizmo = gameObject.GetComponent<Camera>();
        cam_Gizmo.gameObject.name = "Camera_Gizmo";
        cam_Gizmo.clearFlags = CameraClearFlags.Depth;
        cam_Gizmo.depth = 100;

        cam_Gizmo.pixelRect = new Rect(Screen.width - xPosScreen.height - yPosScreenWidthScreenHeight);
        cam_Gizmo.fieldOfView = 25f;
        cam_Gizmo.nearClipPlane = 0.01f;
        cam_Gizmo.farClipPlane = 10f;

        cam_Gizmo.transform.localPosition = transform.parent.transform.forward * -8f;//new Vector3(4f, 4f, 6f);
        cam_Gizmo.transform.LookAt(transform.parent.transform);
    }

    void Start()
    {
        cam_Gizmo = gameObject.GetComponent<Camera>();
        cam_Gizmo_Pivot = transform.parent.transform;
        mainCameraTargetObject = new GameObject();

        viewportRect = cam_Gizmo.rect;
    }

    void Update()
    {
        if (Camera.main != null)
        {
            cam_Gizmo_Pivot.localRotation = Camera.main.transform.rotation;
            //ResizeScreenCheck();
        }
    }

    private void ResizeScreenCheck()
    {
        //Vector2 screenSize = new Vector2(Screen.width, Screen.height);
        //if (this.lastScreenSize != screenSize)
        //{
        //    this.lastScreenSize = screenSize;
        //    cam_Gizmo.pixelRect = new Rect(Screen.width - xPos, Screen.height - yPos, ScreenWidth, ScreenHeight);
        //}
    }


    /// called from CompassMouseEvent.cs
    public void clickedGizmoObject(Transform clickedGizmoObject)
    {
        if (EventSystem.current.IsPointerOverGameObject())
            return;

        IsGizmoClick = true;

        Camera.main.transform.parent = null;

        Vector3 normalVector3 = Vector3.Normalize(clickedGizmoObject.localPosition);
        Vector3 MainCameraPivot = findMainCameraPivot();

        mainCameraTargetObject.transform.position = MainCameraPivot + (normalVector3 * Vector3.Distance(Camera.main.transform.positionMainCameraPivot));
        mainCameraTargetObject.transform.LookAt(MainCameraPivot);

        if (c != null)
        {
            StopCoroutine(c);
            c = null;
        }

        c = StartCoroutine(moveCamera(mainCameraTargetObject.transform));
    }



    private IEnumerator moveCamera(Transform target_Point)
    {
        Vector3 destposition = target_Point.position;
        Quaternion destrotation = target_Point.rotation;
        Transform cam_transform = Camera.main.transform;
        float time = 0f;
        float duration_lerf = 0f;

        while (cam_transform.position != destposition && cam_transform.rotation != destrotation)
        {
            time = Mathf.Clamp(time0CAMERA_MOVE_TIME_DURATION);
            duration_lerf = time / CAMERA_MOVE_TIME_DURATION;
            cam_transform.position = Vector3.Lerp(cam_transform.positiondestpositionduration_lerf);
            cam_transform.rotation = Quaternion.Lerp(cam_transform.rotationdestrotationduration_lerf);

            yield return null;
            time += Time.deltaTime;

            cameraMovement.SetTarget();
        }

        cameraMovement.SetTarget();
        IsGizmoClick = false;
    }

    private Vector3 findMainCameraPivot()
    {
        Vector3 v3 = Camera.main.transform.position + Camera.main.transform.forward * DEFAULT_CAMERA_PIVOT_DISTANT;// 레이 타겟이 없을때 카메라 전방 10m

        Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2Screen.height / 2));
        RaycastHit hitpoint;
        if (Physics.Raycast(rayout hitpoint))
        {
            v3 = hitpoint.point;
        }

        return v3;
    }

    private float findMainCameraPivotDist()
    {
        Vector3 MainCameraPivot = findMainCameraPivot();
        return (Vector3.Distance(MainCameraPivotCamera.main.transform.position));
    }

    public void change_camera_OrthographicMode()
    {
        Camera.main.orthographic = true;
        float camOrthographicSize = (Mathf.Tan(Camera.main.fieldOfView / 2f * Mathf.Deg2Rad) * findMainCameraPivotDist());
        Camera.main.orthographicSize = camOrthographicSize;
    }

    public void change_camera_PerspectiveMode()
    {
        Camera.main.orthographic = false;
    }

    public void OnValueChangedEditToggle(bool value)
    {
        if (value)
        {
            cam_Gizmo.rect = viewportRect;
        }
        else
        {
            cam_Gizmo.rect = new Rect(viewportXviewportRect.yMinviewportRect.xMaxviewportRect.yMax);
        }
    }
}




Compass Mouse Event

using UnityEngine;
using UnityEngine.EventSystems;

[RequireComponent(typeof(BoxCollider))]
public class CompassMouseEvent : MonoBehaviour {

    public CompassGizmoControl ref_CompassGizmoControl;


    private Color orgColor;
    private Color OverColor = Color.cyan;
    private Material m_mat;

    void Start () {
        m_mat = GetComponent<MeshRenderer> ().material;
        orgColor = m_mat.color;
    }

    void OnMouseEnter()
    {
        if (!EventSystem.current.IsPointerOverGameObject())
        {
            m_mat.color = OverColor;
        }
    }

    void OnMouseExit()
    {
        m_mat.color = orgColor;
    }

    void OnMouseDown()
    {
        ref_CompassGizmoControl.clickedGizmoObject (this.transform);
    }

}



Compass Ui Control

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CompassUiControl : MonoBehaviour {

    public CompassGizmoControl ref_CompassGizmoControl;

    public void _mainCamera_OrthographicMode(Toggle t)
    {
        if (t.isOn
        {
            ref_CompassGizmoControl.change_camera_OrthographicMode ();
        }
    }

    public void _mainCamera_PerspectiveMode(Toggle t)
    {
        if (t.isOn
        {
            ref_CompassGizmoControl.change_camera_PerspectiveMode();
        }
    }
}












2021년 7월 21일 수요일

[Unity] 조인트 예제(Aim Constraint,Position Constraint)

 





부모객체1
부모객체mesh



(부모객체와 mesh 축은 항상 통일해줘야함)


1.부모객체

- 아무것도없음






회전할 객체


2.회전할 객체

- 아무것도 없음(롤과 같이 회전하는 스크립트만 넣어주면됨)




첫번째 조인트 부모

첫번째 조인트 객체 mesh




3.첫번째 조인트

- aim Constraint은 부모객체에 넣어준다(설정은 사진과 똑같이(Source에 다음 조인트와 연결해준다)




두번째 조인트 부모
두번째 조인트 mesh


4.두번째 조인트

- aim Constraint은 첫번째 조인트와 연결

- Position Constraint 는 상하좌우로 움직이는 객체에 연결



상하좌우 축 부모
상하좌우 축 mesh


5. 상하좌우 축에는 아무것도 안넣어도 되긴하네 일단은 그럼


aim Constraint

소스 게임오브젝트를 따라가도록 일관된 축으로 회전할수있게해준다

UpVector-> 게임오브젝트 위쪽축 지정

World Up Vector -> 위쪽방향 지정

결과: 회전시킬때 위쪽축위쪽방향을 일치시키고 움직임


Position Constraint

소스 게임오브젝트를 따라가도록 게임 오브젝트를 움직임




영상





2021년 7월 16일 금요일

python 가상환경 배치파일로 실행하기

 아나콘다 scripts를 환경변수에 추가하지 않았다면


cd D:\0__Python\Stock_Test2


call activate py37_32

python __init__.py


cmd.exe


이런식으로 acivate를 할수없음


환경변수 -> path -> 편집 -> anaconda3\Scripts 를 환경변수에 추가해주면 됨!!

2021년 7월 14일 수요일

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...