Skip to content

Commit

Permalink
foreachを使っていたところをLinqに置き換えた
Browse files Browse the repository at this point in the history
  • Loading branch information
gatosyocora committed Oct 11, 2020
1 parent 701444f commit 0bbe0b5
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions Assets/VRCAvatarEditor/Editor/DataClass/VRCAvatarBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,35 +113,16 @@ private float CalculateAvatarHeight(Vector3 footPosition,
IEnumerable<SkinnedMeshRenderer> skinnedMeshRenderers,
IEnumerable<MeshRenderer> meshRenderers)
{
var maxHeight = float.MinValue;
foreach (var skinnedMeshRenderer in skinnedMeshRenderers)
{
var meshMaxHeight = CalculateMaxPositionInMesh(
skinnedMeshRenderer.transform,
skinnedMeshRenderer.sharedMesh);

if (maxHeight < meshMaxHeight)
{
maxHeight = meshMaxHeight;
}
}
foreach (var meshRenderer in meshRenderers)
{
var meshMaxHeight = CalculateMaxPositionInMesh(
meshRenderer.transform,
meshRenderer.GetComponent<MeshFilter>().sharedMesh);
// アバターのすべてのメッシュの頂点位置のy座標が最も高いところを取る
var avatarMaxHeight = skinnedMeshRenderers
.Select(r => new { Transform = r.transform, Mesh = r.sharedMesh })
.Concat(meshRenderers.Select(r => new { Transform = r.transform, Mesh = r.GetComponent<MeshFilter>().sharedMesh }))
.SelectMany(r => r.Mesh.vertices.Select(v => r.Transform.TransformPoint(v).y))
.DefaultIfEmpty()
.Max();

if (maxHeight < meshMaxHeight)
{
maxHeight = meshMaxHeight;
}
}
return maxHeight - footPosition.y;
}

private float CalculateMaxPositionInMesh(Transform meshTransform, Mesh mesh)
{
return mesh.vertices.Select(v => meshTransform.TransformPoint(v).y).DefaultIfEmpty().Max();
if (avatarMaxHeight <= 0) return 0;
return avatarMaxHeight - footPosition.y;
}
}
}
Expand Down

0 comments on commit 0bbe0b5

Please sign in to comment.