Nreal Light ③ 자세를 잡고 불을 켜세요.
이번에는 손끝에서 입자를 가르는 방법, 긴급한 자세(떼는 자세)를 취해 불을 켜는 방법 등 세 부분으로 나눠 초보자도 쉽게 이해할 수 있도록 해설한다.
1부 수부 추적 준비
두 번째 부분의 손끝에서 입자를 방출하다
3부는 비상 자세로 불을 켜고요.
장면에 NRSDK의 다양한 Prefab를 구성하고 설정합니다.
상기 내용 미완성자는 참고하십시오1부 수부 추적 준비.
두 번째 부분의 손끝에서 입자를 방출하다 완성하지 못해도 문제없다.
제비를 뽑다
개발 환경
Unity 2020.3.17f
NRSDK 1.6.0
이 글은 상기 버전을 사용했지만 NRSDK는 최신 버전만 다운로드할 수 있습니다. 아래 링크를 참고하여 새로운 NRSDK와 추천하는 Unity 버전으로 교체하고 적당히 읽으십시오.
스크립트 만들기
프로젝트 창에서 Create→C#Script를 마우스 오른쪽 버튼으로 클릭하여 스크립트를 만들려면 이름을 입력합니다.

여기는 핑치트리거입니다.
PinchTrigger 스크립트, Hierarchy의 NRHand 제작R에 끌어다 놓으세요.

이렇게 NRHand.PinchTrigger 스크립트는 R에 첨부되어 연결되어 있습니다.

다음에 스크립트의 내용을 기술해야 하기 때문에 PinchTrigger 스크립트를 두 번 클릭하고 다음 코드를 기술하는 소프트웨어(IDE)를 열어 기술하십시오.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NRKernal; //NRSDKの機能を使用する場合は必ず記載。
public class PinchTrigger : MonoBehaviour
{
public HandEnum handEnum; //ハンドトラッキングの様々な情報を参照できる。
private bool Pinched = false; //ピンチポーズをしたらtrueに変える。
private bool Pinched2 = false; //Pinchedがtrueになっている時に更にピンチポーズをしたらtrueに変える。
public GameObject pointLight; //ポイントライトをInspectorから入れる。
public float defaultRange; //ポイントライトの範囲と光量をリセットする時の初期値を保管。
private float triggerLine; //どれぐらいの間ピンチポーズをしていたらポイントライトをオンにするかの数値を保管。
public float triggerCharge; //ピンチポーズをしている間triggerLineと同じ数値になるまで増加していく数値を保管。
// Start is called before the first frame update
void Start()
{
pointLight.SetActive(false);
triggerLine = 1.5f;
triggerCharge = 0.0f;
defaultRange = 0.0f;
}
// Update is called once per frame
void Update()
{
CheckPose();
}
void CheckPose()
{
var handState = NRInput.Hands.GetHandState(handEnum); //常に手の状態をチェックして更新する。
if (handState == null)
{
return;
}
else if (handState.pinchStrength > 0.9f) //ピンチポーズしているかどうか判定。
{
triggerCharge += Time.deltaTime; //triggerChargeの数値を増加させる。
if (triggerCharge > triggerLine && Pinched == false && Pinched2 == false) //triggerChargeの数値がtriggerLineの数値まで到達するとポイントライトをオン。
{
Pinched = true;
triggerCharge = 0.0f; //増加した数値をリセット。
pointLight.SetActive(true);
StartCoroutine("BiggerSize");
}
else if (triggerCharge > triggerLine && Pinched == true && Pinched2 == false) //ポイントライトが消える前に更にピンチポーズしたかどうかを判定。
{
Pinched2 = true;
triggerCharge = 0.0f;
}
}
else if (handState.pinchStrength < 0.9f)
{
triggerCharge = 0.0f;
}
}
public IEnumerator BiggerSize()
{
for (float i = 25.0f; i >= defaultRange; i -= 0.5f) //ポイントライトの範囲と光量を大きくして徐々に小さくしていく。
{
pointLight.GetComponent<Light>().range = i;
pointLight.GetComponent<Light>().intensity = i;
yield return new WaitForSeconds(0.1f);
if (Pinched2 == true) //ポイントライトが消える前に更にピンチポーズした場合にもう一度ポイントライトの範囲と光量を大きくして徐々に小さくしていく。
{
pointLight.GetComponent<Light>().range = defaultRange;
pointLight.GetComponent<Light>().intensity = defaultRange;
StartCoroutine("BiggerSize");
break;
}
}
if (Pinched2 == false) //ポイントライトの範囲と光量がdefaultRangeの数値まで減少したらポイントライトをオフにする。
{
Pinched = false;
Pinched2 = false;
pointLight.SetActive(false);
}
else if (Pinched2 == true)
{
Pinched = true;
Pinched2 = false;
}
}
}
점위등 생성하기
Unity 편집의 Hierarchy 창을 마우스 오른쪽 버튼으로 클릭하고 라이트→Point 라이트를 선택하여 포인트 라이트를 만듭니다.

Inspector에서 Transform까지 다음과 같이 설정할 포인트 라이트를 선택하십시오.
Transform
Position X 0 Y 0 Z 0

또한 점광의 변화를 쉽게 이해하기 위해 DirectionalLight의 각도를 바꾸어 장면을 어둡게 한다.
Transform
Rotation X 270 Y -30 Z 0

생성된 점위등을 스크립트에 적용
Hierarchy의 NRHand.Inspector에서 방금 부착된 PinchTrigger(Script)의 HandEnum을 RightHand로 설정하려면 R을 선택합니다.
그런 다음 HandEnum 아래에 표시된 Point Light 상자에 생성된 점위등을 드래그합니다.

Unity 편집에서 장면을 재현합니다.
게임 뷰 레이블을 선택한 후 Shift를 누르면서 마우스를 이동하고 왼쪽 단추를 잠시 누르면 점위등이 켜지면 조금씩 작아지는 것을 확인할 수 있다.
큐브 같은 객체를 만들어 시야에 두면 변화를 쉽게 분별할 수 있다.

두 번째 스크립트와 병합할 때
2부에서 제작한 쇼핸드 파티컬 스크립트와 이번 3부에서 제작한 핑치트릭 스크립트를 하나의 스크립트로 정리하려면 스크립트를 다시 제작해 다음 내용을 기술해 주시기 바랍니다.
이름은 MyHandArrange입니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NRKernal; //NRSDKの機能を使用する場合は必ず記載。
public class MyHandArrange : MonoBehaviour
{
public HandEnum handEnum;
public GameObject[] particles = new GameObject[2];
private Vector3[] fingerPositions = new Vector3[2];
private bool Pinched = false;
private bool Pinched2 = false;
public GameObject pointLight;
public float defaultRange;
private float triggerLine;
public float triggerCharge;
// Start is called before the first frame update
void Start()
{
pointLight.SetActive(false);
triggerLine = 1.5f;
triggerCharge = 0.0f;
defaultRange = 0.0f;
}
// Update is called once per frame
void Update()
{
var handState = NRInput.Hands.GetHandState(handEnum);
if (handState.isTracked)
{
fingerPositions[0] = handState.GetJointPose(HandJointID.ThumbTip).position;
fingerPositions[1] = handState.GetJointPose(HandJointID.IndexTip).position;
for (int i = 0; i < fingerPositions.Length; i++)
{
particles[i].SetActive(true);
particles[i].transform.position = fingerPositions[i];
if (particles[1])
{
particles[1].transform.position = fingerPositions[1];
}
}
}
else if (!handState.isTracked)
{
for (int i = 0; i < fingerPositions.Length; i++)
{
particles[i].SetActive(false);
}
}
CheckPose();
}
void CheckPose()
{
var handState = NRInput.Hands.GetHandState(handEnum);
if (handState == null)
{
return;
}
else if (handState.pinchStrength > 0.9f)
{
triggerCharge += Time.deltaTime;
if (triggerCharge > triggerLine && Pinched == false && Pinched2 == false)
{
Pinched = true;
triggerCharge = 0.0f;
pointLight.SetActive(true);
StartCoroutine("BiggerSize");
}
else if (triggerCharge > triggerLine && Pinched == true && Pinched2 == false)
{
Pinched2 = true;
triggerCharge = 0.0f;
}
}
else if (handState.pinchStrength < 0.9f)
{
triggerCharge = 0.0f;
}
}
public IEnumerator BiggerSize()
{
for (float i = 25.0f; i >= defaultRange; i -= 0.5f)
{
pointLight.GetComponent<Light>().range = i;
pointLight.GetComponent<Light>().intensity = i;
yield return new WaitForSeconds(0.1f);
if (Pinched2 == true)
{
pointLight.GetComponent<Light>().range = defaultRange;
pointLight.GetComponent<Light>().intensity = defaultRange;
StartCoroutine("BiggerSize");
break;
}
}
if (Pinched2 == false)
{
Pinched = false;
Pinched2 = false;
pointLight.SetActive(false);
}
else if (Pinched2 == true)
{
Pinched = true;
Pinched2 = false;
}
}
}
NRHand_R에서 ShowHandParticle 및 PinchTrigger를 다시 시작하고 새로 제작된 MyHandArrange 스크립트를 첨부하십시오.
또한 Inspector의 MyHandArrange(Script)에서 HandEnum 설정과 입자 및 포인트 라이트 드래그를 잊지 마십시오.

3부부터 손 모형을 숨기고 싶을 때.R의 아들 NRhand CapsulevisualR을 선택하고 Inspector에서 선택 취소합니다.

Reference
이 문제에 관하여(Nreal Light ③ 자세를 잡고 불을 켜세요.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/alucky7th/articles/926f5cb50d1734텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)