mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-20 17:57:57 -05:00
Separate MeshExtensions
This commit is contained in:
parent
9599ffe1b6
commit
25042439e1
|
|
@ -333,7 +333,10 @@ namespace VRM
|
|||
|
||||
// Meshに乗っているボーンの姿勢を適用する
|
||||
var dstFilter = dst.gameObject.AddComponent<MeshFilter>();
|
||||
dstFilter.sharedMesh = TransformMesh(srcFilter.sharedMesh, src.localToWorldMatrix);
|
||||
|
||||
var dstMesh = srcFilter.sharedMesh.Copy();
|
||||
dstMesh.ApplyRotationAndScale(src.localToWorldMatrix);
|
||||
dstFilter.sharedMesh = dstMesh;
|
||||
|
||||
// Materialをコピー
|
||||
var dstRenderer = dst.gameObject.AddComponent<MeshRenderer>();
|
||||
|
|
@ -373,47 +376,5 @@ namespace VRM
|
|||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="m"></param>
|
||||
/// <returns></returns>
|
||||
static Mesh TransformMesh(Mesh src, Matrix4x4 m)
|
||||
{
|
||||
m.SetColumn(3, new Vector4(0, 0, 0, 1));
|
||||
|
||||
var mesh = new Mesh();
|
||||
mesh.name = src.name + "(transformed)";
|
||||
|
||||
mesh.vertices = src.vertices.Select(x => m.MultiplyPoint(x)).ToArray();
|
||||
if (src.normals != null && src.normals.Length > 0)
|
||||
{
|
||||
mesh.normals = src.normals.Select(x => m.MultiplyVector(x)).ToArray();
|
||||
}
|
||||
if (src.tangents != null && src.tangents.Length > 0)
|
||||
{
|
||||
mesh.tangents = src.tangents.Select(x =>
|
||||
{
|
||||
var t = m.MultiplyVector((Vector3)x);
|
||||
return new Vector4(t.x, t.y, t.z, x.w);
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
if (src.colors != null && src.colors.Length > 0) mesh.colors = src.colors;
|
||||
if (src.uv != null && src.uv.Length > 0) mesh.uv = src.uv;
|
||||
if (src.uv2 != null && src.uv2.Length > 0) mesh.uv2 = src.uv2;
|
||||
if (src.uv3 != null && src.uv3.Length > 0) mesh.uv3 = src.uv3;
|
||||
if (src.uv4 != null && src.uv4.Length > 0) mesh.uv4 = src.uv4;
|
||||
|
||||
mesh.subMeshCount = src.subMeshCount;
|
||||
for(int i=0; i<mesh.subMeshCount; ++i)
|
||||
{
|
||||
mesh.SetIndices(src.GetIndices(i), src.GetTopology(i), i);
|
||||
}
|
||||
|
||||
return mesh;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
58
Scripts/SkinnedMeshUtility/MeshExtensions.cs
Normal file
58
Scripts/SkinnedMeshUtility/MeshExtensions.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using UnityEngine;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
namespace VRM
|
||||
{
|
||||
public static class MeshExtensions
|
||||
{
|
||||
public static Mesh Copy(this Mesh src)
|
||||
{
|
||||
var dst = new Mesh();
|
||||
dst.name = src.name + "(copy)";
|
||||
|
||||
dst.vertices = src.vertices;
|
||||
dst.normals = src.normals;
|
||||
dst.tangents = src.tangents;
|
||||
|
||||
if (src.colors != null && src.colors.Length > 0) dst.colors = src.colors;
|
||||
if (src.uv != null && src.uv.Length > 0) dst.uv = src.uv;
|
||||
if (src.uv2 != null && src.uv2.Length > 0) dst.uv2 = src.uv2;
|
||||
if (src.uv3 != null && src.uv3.Length > 0) dst.uv3 = src.uv3;
|
||||
if (src.uv4 != null && src.uv4.Length > 0) dst.uv4 = src.uv4;
|
||||
|
||||
dst.subMeshCount = src.subMeshCount;
|
||||
for (int i = 0; i < dst.subMeshCount; ++i)
|
||||
{
|
||||
dst.SetIndices(src.GetIndices(i), src.GetTopology(i), i);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
public static void ApplyRotationAndScale(this Mesh src, Matrix4x4 m)
|
||||
{
|
||||
m.SetColumn(3, new Vector4(0, 0, 0, 1));
|
||||
src.ApplyMatrix(m);
|
||||
}
|
||||
|
||||
public static void ApplyMatrix(this Mesh src, Matrix4x4 m)
|
||||
{
|
||||
m.SetColumn(3, new Vector4(0, 0, 0, 1));
|
||||
|
||||
src.vertices = src.vertices.Select(x => m.MultiplyPoint(x)).ToArray();
|
||||
if (src.normals != null && src.normals.Length > 0)
|
||||
{
|
||||
src.normals = src.normals.Select(x => m.MultiplyVector(x)).ToArray();
|
||||
}
|
||||
if (src.tangents != null && src.tangents.Length > 0)
|
||||
{
|
||||
src.tangents = src.tangents.Select(x =>
|
||||
{
|
||||
var t = m.MultiplyVector((Vector3)x);
|
||||
return new Vector4(t.x, t.y, t.z, x.w);
|
||||
}).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Scripts/SkinnedMeshUtility/MeshExtensions.cs.meta
Normal file
12
Scripts/SkinnedMeshUtility/MeshExtensions.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4181f0b5e9a271b45b3e995a38202780
|
||||
timeCreated: 1532506262
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in New Issue
Block a user