mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-03-21 18:05:03 -05:00
Merge pull request #2761 from tdw46/Custom-Normals-Import
Fix: blendshape normals import (UniGLTF/UniVRM)
This commit is contained in:
commit
768d7ad226
|
|
@ -52,8 +52,8 @@ namespace UniGLTF.MeshUtility
|
|||
|
||||
if (copyBlendShape)
|
||||
{
|
||||
var vertices = src.vertices;
|
||||
var normals = src.normals;
|
||||
var deltaVertices = new Vector3[src.vertexCount];
|
||||
var deltaNormals = new Vector3[src.vertexCount];
|
||||
Vector3[] tangents = null;
|
||||
if (Symbols.VRM_NORMALIZE_BLENDSHAPE_TANGENT)
|
||||
{
|
||||
|
|
@ -62,14 +62,18 @@ namespace UniGLTF.MeshUtility
|
|||
|
||||
for (int i = 0; i < src.blendShapeCount; ++i)
|
||||
{
|
||||
src.GetBlendShapeFrameVertices(i, 0, vertices, normals, tangents);
|
||||
dst.AddBlendShapeFrame(
|
||||
src.GetBlendShapeName(i),
|
||||
src.GetBlendShapeFrameWeight(i, 0),
|
||||
vertices,
|
||||
normals,
|
||||
tangents
|
||||
);
|
||||
var frameCount = src.GetBlendShapeFrameCount(i);
|
||||
for (int f = 0; f < frameCount; ++f)
|
||||
{
|
||||
src.GetBlendShapeFrameVertices(i, f, deltaVertices, deltaNormals, tangents);
|
||||
dst.AddBlendShapeFrame(
|
||||
src.GetBlendShapeName(i),
|
||||
src.GetBlendShapeFrameWeight(i, f),
|
||||
deltaVertices,
|
||||
deltaNormals,
|
||||
tangents
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -283,6 +283,138 @@ namespace UniGLTF
|
|||
}
|
||||
}
|
||||
|
||||
private static NativeArray<Vector3> GetMorphTargetVec3(GltfData data, int accessorIndex, string attribute)
|
||||
{
|
||||
if (accessorIndex < 0) return data.NativeArrayManager.CreateNativeArray<Vector3>(0);
|
||||
|
||||
var accessor = data.GLTF.accessors[accessorIndex];
|
||||
if (accessor.type != "VEC3")
|
||||
{
|
||||
throw new ArgumentException($"unknown {attribute} type: {accessor.componentType}:{accessor.type}");
|
||||
}
|
||||
|
||||
static float NormalizeSByte(sbyte v)
|
||||
{
|
||||
// glTF normalized signed integer maps min to -1.0 exactly.
|
||||
return Mathf.Max(v / 127.0f, -1.0f);
|
||||
}
|
||||
|
||||
static float NormalizeShort(short v)
|
||||
{
|
||||
// glTF normalized signed integer maps min to -1.0 exactly.
|
||||
return Mathf.Max(v / 32767.0f, -1.0f);
|
||||
}
|
||||
|
||||
switch (accessor.componentType)
|
||||
{
|
||||
case glComponentType.FLOAT:
|
||||
return data.GetArrayFromAccessor<Vector3>(accessorIndex);
|
||||
|
||||
case glComponentType.BYTE:
|
||||
{
|
||||
var src = data.GetArrayFromAccessor<SByte3>(accessorIndex);
|
||||
var dst = data.NativeArrayManager.CreateNativeArray<Vector3>(src.Length);
|
||||
if (accessor.normalized)
|
||||
{
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
var v = src[i];
|
||||
dst[i] = new Vector3(
|
||||
NormalizeSByte(v.x),
|
||||
NormalizeSByte(v.y),
|
||||
NormalizeSByte(v.z));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
var v = src[i];
|
||||
dst[i] = new Vector3(v.x, v.y, v.z);
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
case glComponentType.UNSIGNED_BYTE:
|
||||
{
|
||||
var src = data.GetArrayFromAccessor<Byte3>(accessorIndex);
|
||||
var dst = data.NativeArrayManager.CreateNativeArray<Vector3>(src.Length);
|
||||
if (accessor.normalized)
|
||||
{
|
||||
const float factor = 1.0f / 255.0f;
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
var v = src[i];
|
||||
dst[i] = new Vector3(v.x * factor, v.y * factor, v.z * factor);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
var v = src[i];
|
||||
dst[i] = new Vector3(v.x, v.y, v.z);
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
case glComponentType.SHORT:
|
||||
{
|
||||
var src = data.GetArrayFromAccessor<Short3>(accessorIndex);
|
||||
var dst = data.NativeArrayManager.CreateNativeArray<Vector3>(src.Length);
|
||||
if (accessor.normalized)
|
||||
{
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
var v = src[i];
|
||||
dst[i] = new Vector3(
|
||||
NormalizeShort(v.x),
|
||||
NormalizeShort(v.y),
|
||||
NormalizeShort(v.z));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
var v = src[i];
|
||||
dst[i] = new Vector3(v.x, v.y, v.z);
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
case glComponentType.UNSIGNED_SHORT:
|
||||
{
|
||||
var src = data.GetArrayFromAccessor<UShort3>(accessorIndex);
|
||||
var dst = data.NativeArrayManager.CreateNativeArray<Vector3>(src.Length);
|
||||
if (accessor.normalized)
|
||||
{
|
||||
const float factor = 1.0f / 65535.0f;
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
var v = src[i];
|
||||
dst[i] = new Vector3(v.x * factor, v.y * factor, v.z * factor);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
var v = src[i];
|
||||
dst[i] = new Vector3(v.x, v.y, v.z);
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new NotImplementedException($"unknown {attribute} type: {accessor.componentType}:{accessor.type}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 各 primitive の attribute の要素が同じでない。=> uv が有るものと無いものが混在するなど
|
||||
/// glTF 的にはありうる。
|
||||
|
|
@ -438,7 +570,7 @@ namespace UniGLTF
|
|||
var blendShape = GetOrCreateBlendShape(i);
|
||||
if (primTarget.POSITION != -1)
|
||||
{
|
||||
var array = data.GetArrayFromAccessor<Vector3>(primTarget.POSITION);
|
||||
var array = GetMorphTargetVec3(data, primTarget.POSITION, "POSITION");
|
||||
if (array.Length != positions.Length)
|
||||
{
|
||||
throw new Exception("different length");
|
||||
|
|
@ -449,7 +581,7 @@ namespace UniGLTF
|
|||
|
||||
if (primTarget.NORMAL != -1)
|
||||
{
|
||||
var array = data.GetArrayFromAccessor<Vector3>(primTarget.NORMAL);
|
||||
var array = GetMorphTargetVec3(data, primTarget.NORMAL, "NORMAL");
|
||||
if (array.Length != positions.Length)
|
||||
{
|
||||
throw new Exception("different length");
|
||||
|
|
@ -460,7 +592,7 @@ namespace UniGLTF
|
|||
|
||||
if (primTarget.TANGENT != -1)
|
||||
{
|
||||
var array = data.GetArrayFromAccessor<Vector3>(primTarget.TANGENT);
|
||||
var array = GetMorphTargetVec3(data, primTarget.TANGENT, "TANGENT");
|
||||
if (array.Length != positions.Length)
|
||||
{
|
||||
throw new Exception("different length");
|
||||
|
|
@ -579,7 +711,7 @@ namespace UniGLTF
|
|||
|
||||
if (hasPosition)
|
||||
{
|
||||
var morphPositions = data.GetArrayFromAccessor<Vector3>(primTarget.POSITION);
|
||||
var morphPositions = GetMorphTargetVec3(data, primTarget.POSITION, "POSITION");
|
||||
blendShape.Positions.Capacity = morphPositions.Length;
|
||||
for (var j = 0; j < positions.Length; ++j)
|
||||
{
|
||||
|
|
@ -589,7 +721,7 @@ namespace UniGLTF
|
|||
|
||||
if (hasNormal)
|
||||
{
|
||||
var morphNormals = data.GetArrayFromAccessor<Vector3>(primTarget.NORMAL);
|
||||
var morphNormals = GetMorphTargetVec3(data, primTarget.NORMAL, "NORMAL");
|
||||
blendShape.Normals.Capacity = morphNormals.Length;
|
||||
for (var j = 0; j < positions.Length; ++j)
|
||||
{
|
||||
|
|
@ -600,7 +732,7 @@ namespace UniGLTF
|
|||
|
||||
if (hasTangent)
|
||||
{
|
||||
var morphTangents = data.GetArrayFromAccessor<Vector3>(primTarget.TANGENT);
|
||||
var morphTangents = GetMorphTargetVec3(data, primTarget.TANGENT, "TANGENT");
|
||||
blendShape.Tangents.Capacity = morphTangents.Length;
|
||||
for (var j = 0; j < positions.Length; ++j)
|
||||
{
|
||||
|
|
@ -639,4 +771,4 @@ namespace UniGLTF
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Profiling;
|
||||
|
|
@ -41,28 +40,33 @@ namespace UniGLTF
|
|||
}
|
||||
}
|
||||
|
||||
private static async Task BuildBlendShapeAsync(IAwaitCaller awaitCaller, Mesh mesh, BlendShape blendShape,
|
||||
private static async Task BuildBlendShapeAsync(
|
||||
IAwaitCaller awaitCaller,
|
||||
Mesh mesh,
|
||||
BlendShape blendShape,
|
||||
Vector3[] emptyVertices)
|
||||
{
|
||||
Vector3[] positions = null;
|
||||
Vector3[] normals = null;
|
||||
await awaitCaller.Run(() =>
|
||||
{
|
||||
positions = blendShape.Positions.ToArray();
|
||||
if (blendShape.Normals != null)
|
||||
{
|
||||
normals = blendShape.Normals.ToArray();
|
||||
}
|
||||
positions = blendShape.Positions != null ? blendShape.Positions.ToArray() : Array.Empty<Vector3>();
|
||||
normals = blendShape.Normals != null ? blendShape.Normals.ToArray() : Array.Empty<Vector3>();
|
||||
});
|
||||
|
||||
Profiler.BeginSample("MeshUploader.BuildBlendShapeAsync");
|
||||
var hasPositions = positions.Length == mesh.vertexCount;
|
||||
var hasNormals = normals.Length == mesh.vertexCount;
|
||||
|
||||
if (positions.Length > 0)
|
||||
{
|
||||
if (positions.Length == mesh.vertexCount)
|
||||
if (hasPositions)
|
||||
{
|
||||
var deltaNormals = hasNormals ? normals : null;
|
||||
|
||||
mesh.AddBlendShapeFrame(blendShape.Name, FrameWeight,
|
||||
positions,
|
||||
normals.Length == mesh.vertexCount && normals.Length == positions.Length ? normals : null,
|
||||
deltaNormals,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
|
@ -76,7 +80,7 @@ namespace UniGLTF
|
|||
// add empty blend shape for keep blend shape index
|
||||
mesh.AddBlendShapeFrame(blendShape.Name, FrameWeight,
|
||||
emptyVertices,
|
||||
null,
|
||||
normals.Length == mesh.vertexCount ? normals : null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
|
@ -132,7 +136,11 @@ namespace UniGLTF
|
|||
var emptyVertices = new Vector3[mesh.vertexCount];
|
||||
foreach (var blendShape in data.BlendShapes)
|
||||
{
|
||||
await BuildBlendShapeAsync(awaitCaller, mesh, blendShape, emptyVertices);
|
||||
await BuildBlendShapeAsync(
|
||||
awaitCaller,
|
||||
mesh,
|
||||
blendShape,
|
||||
emptyVertices);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
25
Packages/UniGLTF/Runtime/UniGLTF/IO/SByte3.cs
Normal file
25
Packages/UniGLTF/Runtime/UniGLTF/IO/SByte3.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public readonly struct SByte3 : IEquatable<SByte3>
|
||||
{
|
||||
public readonly sbyte x;
|
||||
public readonly sbyte y;
|
||||
public readonly sbyte z;
|
||||
|
||||
public SByte3(sbyte _x, sbyte _y, sbyte _z)
|
||||
{
|
||||
x = _x;
|
||||
y = _y;
|
||||
z = _z;
|
||||
}
|
||||
|
||||
public bool Equals(SByte3 other)
|
||||
{
|
||||
return x == other.x && y == other.y && z == other.z;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Packages/UniGLTF/Runtime/UniGLTF/IO/SByte3.cs.meta
Normal file
2
Packages/UniGLTF/Runtime/UniGLTF/IO/SByte3.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 93c772ed657e23c448e9b036d9c9071c
|
||||
25
Packages/UniGLTF/Runtime/UniGLTF/IO/Short3.cs
Normal file
25
Packages/UniGLTF/Runtime/UniGLTF/IO/Short3.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public readonly struct Short3 : IEquatable<Short3>
|
||||
{
|
||||
public readonly short x;
|
||||
public readonly short y;
|
||||
public readonly short z;
|
||||
|
||||
public Short3(short _x, short _y, short _z)
|
||||
{
|
||||
x = _x;
|
||||
y = _y;
|
||||
z = _z;
|
||||
}
|
||||
|
||||
public bool Equals(Short3 other)
|
||||
{
|
||||
return x == other.x && y == other.y && z == other.z;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Packages/UniGLTF/Runtime/UniGLTF/IO/Short3.cs.meta
Normal file
2
Packages/UniGLTF/Runtime/UniGLTF/IO/Short3.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f74b316e688844c428c7b45dcb38ee54
|
||||
Loading…
Reference in New Issue
Block a user