feature: 添加了批量新建棍子的方法

refact: 重构了Bond和VirutalSkeleton类
This commit is contained in:
jackfiled 2023-02-27 11:07:44 +08:00
parent 7199424893
commit 5113929728
7 changed files with 214 additions and 146 deletions

View File

@ -1,52 +0,0 @@
using UnityEngine;
namespace Models
{
public class Bonds
{
public GameObject start;
public GameObject end;
private GameObject bond;
public Bonds(GameObject start,GameObject end,float scale)
{
this.start = start;
this.end = end;
bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2);
//这里可以设置材质,具体自己设置
bond.GetComponent<Renderer>().material.color = Color.cyan;
}
public void UpdateBond()
{
var startpos = start.transform.position;
var endpos = end.transform.position;
Vector3 rightPosition = (startpos + endpos) / 2;
Vector3 rightRotation = endpos - startpos;
float HalfLength = Vector3.Distance(startpos, endpos) / 2;
float LThickness = 0.1f;//线的粗细
//创建圆柱体
//bond.gameObject.transform.parent = transform;
bond.transform.position = rightPosition;
bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation);
bond.transform.localScale = new Vector3(LThickness, HalfLength, LThickness);
}
}
}

View File

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using Models;
using UnityEngine;
@ -6,11 +5,11 @@
public class MainBehaviour : MonoBehaviour
{
private readonly List<GameObject> _nodes = new List<GameObject>();
private readonly List<Bonds> _bonds = new List<Bonds>();
private readonly List<Bond> _bonds = new List<Bond>();
private readonly List<VirtualSkeleton> _virtualSkeletons = new List<VirtualSkeleton>();
private readonly List<PoseTransform> _poseTransforms = new List<PoseTransform>();
private readonly UdpListener _listener = new UdpListener();
private const float _scale = 0.2f;
private const float Scale = 0.2f;
private GameObject sample ;
// Start is called before the first frame update
@ -63,7 +62,7 @@ private void CreateNodes()
{
var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
ball.transform.localScale = new Vector3(_scale, _scale, _scale);
ball.transform.localScale = new Vector3(Scale, Scale, Scale);
if (i <= 10)
{
@ -88,123 +87,191 @@ private void CreateNodes()
private void CreateBonds()
{
var temp = new Bonds(_nodes[0], _nodes[4], _scale);
/*var temp = new Bonds(_nodes[0], _nodes[4], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[0], _nodes[1], _scale);
temp = new Bonds(_nodes[0], _nodes[1], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[1], _nodes[2], _scale);
temp = new Bonds(_nodes[1], _nodes[2], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[2], _nodes[3], _scale);
temp = new Bonds(_nodes[2], _nodes[3], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[3], _nodes[7], _scale);
temp = new Bonds(_nodes[3], _nodes[7], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[4], _nodes[5], _scale);
temp = new Bonds(_nodes[4], _nodes[5], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[5], _nodes[6], _scale);
temp = new Bonds(_nodes[5], _nodes[6], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[6], _nodes[8], _scale);
temp = new Bonds(_nodes[6], _nodes[8], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[9], _nodes[10], _scale);
temp = new Bonds(_nodes[9], _nodes[10], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[2], _nodes[3], _scale);
temp = new Bonds(_nodes[2], _nodes[3], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[11], _nodes[12], _scale);
temp = new Bonds(_nodes[11], _nodes[12], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[12], _nodes[14], _scale);
temp = new Bonds(_nodes[12], _nodes[14], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[14], _nodes[16], _scale);
temp = new Bonds(_nodes[14], _nodes[16], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[16], _nodes[22], _scale);
temp = new Bonds(_nodes[16], _nodes[22], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[16], _nodes[18], _scale);
temp = new Bonds(_nodes[16], _nodes[18], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[18], _nodes[20], _scale);
temp = new Bonds(_nodes[18], _nodes[20], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[16], _nodes[20], _scale);
temp = new Bonds(_nodes[16], _nodes[20], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[11], _nodes[13], _scale);
temp = new Bonds(_nodes[11], _nodes[13], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[13], _nodes[15], _scale);
temp = new Bonds(_nodes[13], _nodes[15], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[15], _nodes[17], _scale);
temp = new Bonds(_nodes[15], _nodes[17], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[15], _nodes[21], _scale);
temp = new Bonds(_nodes[15], _nodes[21], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[15], _nodes[19], _scale);
temp = new Bonds(_nodes[15], _nodes[19], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[17], _nodes[19], _scale);
temp = new Bonds(_nodes[17], _nodes[19], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[12], _nodes[24], _scale);
temp = new Bonds(_nodes[12], _nodes[24], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[23], _nodes[24], _scale);
temp = new Bonds(_nodes[23], _nodes[24], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[11], _nodes[23], _scale);
temp = new Bonds(_nodes[11], _nodes[23], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[24], _nodes[26], _scale);
temp = new Bonds(_nodes[24], _nodes[26], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[26], _nodes[28], _scale);
temp = new Bonds(_nodes[26], _nodes[28], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[28], _nodes[32], _scale);
temp = new Bonds(_nodes[28], _nodes[32], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[28], _nodes[30], _scale);
temp = new Bonds(_nodes[28], _nodes[30], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[23], _nodes[25], _scale);
temp = new Bonds(_nodes[23], _nodes[25], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[25], _nodes[27], _scale);
temp = new Bonds(_nodes[25], _nodes[27], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[27], _nodes[29], _scale);
temp = new Bonds(_nodes[27], _nodes[29], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[27], _nodes[31], _scale);
temp = new Bonds(_nodes[27], _nodes[31], Scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[29], _nodes[31], _scale);
_bonds.Add(temp);
temp = new Bonds(_nodes[29], _nodes[31], Scale);
_bonds.Add(temp);*/
/*foreach (var bond in _bonds)
var headBonds = new[]
{
var temp2 = new VirtualSkeleton(bond.start, bond.end, _scale);
_virtualSkeletons.Add(temp2);
}*/
PoseLandmarkType.RightEar,
PoseLandmarkType.RightEyeOuter,
PoseLandmarkType.RightEye,
PoseLandmarkType.RightEyeInner,
PoseLandmarkType.Nose,
PoseLandmarkType.LeftEyeInner,
PoseLandmarkType.LeftEye,
PoseLandmarkType.LeftEyeOuter,
PoseLandmarkType.LeftEar
};
_bonds.AddRange(GenerateBondsList(headBonds));
var monthBonds = new[]
{
PoseLandmarkType.MouthLeft,
PoseLandmarkType.MouthRight,
};
_bonds.AddRange(GenerateBondsList(monthBonds));
var leftArmBonds = new[]
{
PoseLandmarkType.LeftShoulder,
PoseLandmarkType.LeftElbow,
PoseLandmarkType.LeftWrist,
PoseLandmarkType.LeftPinky,
PoseLandmarkType.LeftIndex,
PoseLandmarkType.LeftWrist,
PoseLandmarkType.LeftThumb
};
_bonds.AddRange(GenerateBondsList(leftArmBonds));
var rightArmBonds = new[]
{
PoseLandmarkType.RightShoulder,
PoseLandmarkType.RightElbow,
PoseLandmarkType.RightWrist,
PoseLandmarkType.RightPinky,
PoseLandmarkType.RightIndex,
PoseLandmarkType.RightWrist,
PoseLandmarkType.RightThumb
};
_bonds.AddRange(GenerateBondsList(rightArmBonds));
var leftLegBonds = new[]
{
PoseLandmarkType.LeftShoulder,
PoseLandmarkType.LeftHip,
PoseLandmarkType.LeftKnee,
PoseLandmarkType.LeftAnkle,
PoseLandmarkType.LeftHeel,
PoseLandmarkType.LeftFootIndex,
PoseLandmarkType.LeftAnkle
};
_bonds.AddRange(GenerateBondsList(leftLegBonds));
var rightLegBonds = new[]
{
PoseLandmarkType.RightShoulder,
PoseLandmarkType.RightHip,
PoseLandmarkType.RightKnee,
PoseLandmarkType.RightAnkle,
PoseLandmarkType.RightHeel,
PoseLandmarkType.RightFootIndex,
PoseLandmarkType.RightAnkle
};
_bonds.AddRange(GenerateBondsList(rightLegBonds));
// 最后手动添加身体上的两条横线
_bonds.Add(new Bond(
_nodes[(int)PoseLandmarkType.LeftShoulder],
_nodes[(int)PoseLandmarkType.RightShoulder],
Scale));
_bonds.Add(new Bond(
_nodes[(int)PoseLandmarkType.LeftHip],
_nodes[(int)PoseLandmarkType.RightHip],
Scale));
}
private void OnReceive(List<PoseLandmark> landmarks)
{
foreach (var landmark in landmarks)
@ -217,4 +284,24 @@ private void OnReceive(List<PoseLandmark> landmarks)
_poseTransforms[(int)landmark.Type] = poseTransform;
}
}
/// <summary>
/// 创建棍子列表
/// </summary>
/// <param name="nodes">需要连接起来的关键点 需要按顺序设置</param>
/// <returns></returns>
private List<Bond> GenerateBondsList(PoseLandmarkType[] nodes)
{
var bonds = new List<Bond>();
for (var i = 0; i < nodes.Length - 1; i++)
{
bonds.Add(new Bond(
_nodes[(int)nodes[i]],
_nodes[(int)nodes[i + 1]],
Scale));
}
return bonds;
}
}

44
Assets/Models/Bond.cs Normal file
View File

@ -0,0 +1,44 @@
using UnityEngine;
namespace Models
{
/// <summary>
/// 捕捉点之间的连接线
/// </summary>
public class Bond
{
protected readonly GameObject Start;
protected readonly GameObject End;
private readonly GameObject _bond;
public Bond(GameObject start,GameObject end,float scale)
{
Start = start;
End = end;
_bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
_bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2);
//这里可以设置材质,具体自己设置
_bond.GetComponent<Renderer>().material.color = Color.cyan;
}
public void UpdateBond()
{
var startPos = Start.transform.position;
var endPos = End.transform.position;
var rightPosition = (startPos + endPos) / 2;
var rightRotation = endPos - startPos;
var halfLength = Vector3.Distance(startPos, endPos) / 2;
var thickness = 0.1f;//线的粗细
//创建圆柱体
//bond.gameObject.transform.parent = transform;
_bond.transform.position = rightPosition;
_bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation);
_bond.transform.localScale = new Vector3(thickness, halfLength, thickness);
}
}
}

View File

@ -0,0 +1,35 @@
using UnityEngine;
namespace Models
{
public class VirtualSkeleton : Bond
{
private readonly GameObject _virtualSkeleton;
public VirtualSkeleton(GameObject start,GameObject end,float scale) : base(start,end,scale)
{
_virtualSkeleton = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
_virtualSkeleton.transform.localScale = new Vector3(scale/2, scale/2, scale/2);
_virtualSkeleton.GetComponent<Renderer>().material.color = Color.white;
}
/// <summary>
/// 覆盖基类的更新方法
/// </summary>
public new void UpdateBond()
{
var startPos = Start.transform.position;
var endPos = End.transform.position;
var rightPosition = (startPos + endPos) / 2;
var rightRotation = endPos - startPos;
var lThickness = 0.2f;
_virtualSkeleton.transform.position = rightPosition;
_virtualSkeleton.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation);
_virtualSkeleton.transform.localScale = new Vector3(lThickness, lThickness, lThickness);
}
}
}

View File

@ -1,46 +0,0 @@
using UnityEngine;
using UnityEngine.PlayerLoop;
namespace Models
{
public class VirtualSkeleton : Bonds
{
//private GameObject start;
//private GameObject end;
private GameObject virtualskeleton;
public VirtualSkeleton(GameObject start,GameObject end,float scale) : base(start,end,scale)
{
this.start = start;
this.end = end;
virtualskeleton = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
virtualskeleton.transform.localScale = new Vector3(scale/2, scale/2, scale/2);
virtualskeleton.GetComponent<Renderer>().material.color = Color.white;
}
public void UpdateVS()
{
var startpos = start.transform.position;
var endpos = end.transform.position;
Vector3 rightPosition = (startpos + endpos) / 2;
Vector3 rightRotation = endpos - startpos;
float LThickness = 0.2f;//线的粗细
//创建圆柱体
//bond.gameObject.transform.parent = transform;
virtualskeleton.transform.position = rightPosition;
virtualskeleton.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation);
//virtualskeleton.transform.rotation = Quaternion.LookRotation(rightRotation, Vector3.forward);
virtualskeleton.transform.localScale = new Vector3(LThickness, LThickness, LThickness);
}
}
}