본문 바로가기
개발/Unity

유니티 - 벡터의 내적 / 외적 / 아다마르 곱 (Dot, Cross, Scale)

by 피로물든딸기 2022. 6. 25.
반응형

Unity 전체 링크

 

유니티에서 자주 사용하는 벡터 연산은 내적외적이다.

 

내적의 결과는 스칼라다. (Vector3.Dot)

외적의 결과는 벡터이며, 삼각형의 넓이를 구하는 공식을 참고하자. (Vector3.Cross)

 

그렇다면 벡터의 각 성분만 곱하는 것은 뭐라고 할까?

 

행렬에서는 아다마르 곱(Hadamard Product)이라고 한다.

벡터는 1차원 행렬로 볼 수 있다.

 

두 벡터를 구성 요소별로 곱하기만 하므로 결과는 벡터다. 

(Multiplies Two Vectors Component-Wise / Element Wise Product)

 

유니티에서 Vector3.Scale을 이용하면 된다. 

생각보다 많이 쓰이지만 함수 이름이 잘 생각 안 나거나 어떻게 표현해야 할지 모르는 경우가 많다.

성분 곱이라고 검색하면 대부분 내적이 나오기 때문이다.

 

아래의 스크립트를 실행해서 연산 결과를 확인해보자.

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

public class VectorCalculate : MonoBehaviour
{
    void Start()
    {
        Vector3 a = new Vector3(1, 2, 3); 
        Vector3 b = new Vector3(4, 5, 6);

        // a1 * b1 + a2 * b2 + a3 * b3 = 1 * 4 + 2 * 5 + 3 * 6 = 32
        float dot = Vector3.Dot(a, b); 

        // (-3, 6, -3)
        Vector3 cross = Vector3.Cross(a, b);

        // (a1 * b1, a2 * b2, a3 * b3) = (1 * 4, 2 * 5, 3 * 6) = (4, 10, 18)
        Vector3 scale = Vector3.Scale(a, b);

        Debug.Log("dot : " + dot);
        Debug.Log("cross : " + cross);
        Debug.Log("scale : " + scale);
    }
}

 

Unity Plus:

 

Easy 2D, 3D, VR, & AR software for cross-platform development of games and mobile apps. - Unity Store

Have a 2D, 3D, VR, or AR project that needs cross-platform functionality? We can help. Take a look at the easy-to-use Unity Plus real-time dev platform!

store.unity.com

 

Unity Pro:

 

Unity Pro

The complete solutions for professionals to create and operate.

unity.com

 

Unity 프리미엄 학습:

 

Unity Learn

Advance your Unity skills with live sessions and over 750 hours of on-demand learning content designed for creators at every skill level.

unity.com

반응형

댓글