Skip to content

Commit

Permalink
null許容参照型を有効化
Browse files Browse the repository at this point in the history
  • Loading branch information
esperecyan committed Feb 23, 2024
1 parent a5a76d7 commit 1891517
Show file tree
Hide file tree
Showing 24 changed files with 172 additions and 109 deletions.
95 changes: 42 additions & 53 deletions Editor/Components/BlendShapeReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Linq;
using System.Collections.Generic;
Expand Down Expand Up @@ -188,7 +189,7 @@ internal static void Apply(
GameObject avatar,
IEnumerable<VRMBlendShapeClip> clips,
bool useShapeKeyNormalsAndTangents,
VRMBlendShapeClip vrmBlendShapeForFINGERPOINT,
VRMBlendShapeClip? vrmBlendShapeForFINGERPOINT,
Converter.OSCComponents oscComponents
)
{
Expand Down Expand Up @@ -237,33 +238,18 @@ bool useShapeKeyNormalsAndTangents
continue;
}

Vector3[] deltaVertices = null;
foreach (Vector3[] vertices in values.SelectMany(presetAndWeight =>
BlendShapeReplacer.SubtractNeutralShapeKeyValues(clips.First(clip => clip.Preset == presetAndWeight.Key).ShapeKeyValues, clips)
.Select(shapeKeyNameAndWeight => shapeKeys
.First(shapeKey => shapeKey.Name == shapeKeyNameAndWeight.Key)
.Positions
.Select(vertix => vertix * (shapeKeyNameAndWeight.Value / VRMUtility.MaxBlendShapeBindingWeight * presetAndWeight.Value))
.ToArray()
)
))
{
if (deltaVertices == null)
{
deltaVertices = vertices;
continue;
}

for (var i = 0; i < deltaVertices.Length; i++)
{
deltaVertices[i] += vertices[i];
}
}

mesh.AddBlendShapeFrame(
newName,
BlendShapeReplacer.MaxBlendShapeFrameWeight,
deltaVertices,
BlendShapeReplacer.SumVerticesList(values
.SelectMany(presetAndWeight =>
BlendShapeReplacer.SubtractNeutralShapeKeyValues(clips.First(clip => clip.Preset == presetAndWeight.Key).ShapeKeyValues, clips)
.Select(shapeKeyNameAndWeight => shapeKeys
.First(shapeKey => shapeKey.Name == shapeKeyNameAndWeight.Key)
.Positions
.Select(vertix => vertix * (shapeKeyNameAndWeight.Value / VRMUtility.MaxBlendShapeBindingWeight * presetAndWeight.Value))
)
)).ToArray(),
null,
null
);
Expand All @@ -283,27 +269,29 @@ bool useShapeKeyNormalsAndTangents
/// <param name="namesAndWeights">シェイプキー名と0〜100のウェイトの連想配列。</param>
/// <param name="shapeKeys"><see cref="SkinnedMeshUtility.GetAllShapeKeys"/>の戻り値。</param>
/// <returns></returns>
private static Vector3[] GenerateShapeKey(
private static IEnumerable<Vector3> GenerateShapeKey(
IDictionary<string, float> namesAndWeights,
IEnumerable<BlendShape> shapeKeys

)
{
Vector3[] deltaVertices = null;
foreach (var (name, weight) in namesAndWeights)
{
Vector3[] vertices = shapeKeys.First(shapeKey => shapeKey.Name == name).Positions.ToArray();
if (deltaVertices == null)
{
deltaVertices = new Vector3[vertices.Length];
}
return BlendShapeReplacer.SumVerticesList(namesAndWeights
.Select(nameAndWeight => shapeKeys.First(shapeKey => shapeKey.Name == nameAndWeight.Key)
.Positions
.Select(vertix => vertix * (nameAndWeight.Value / VRMUtility.MaxBlendShapeBindingWeight)))
);
}

for (var i = 0; i < deltaVertices.Length; i++)
{
deltaVertices[i] += vertices[i] * (weight / VRMUtility.MaxBlendShapeBindingWeight);
}
}
return deltaVertices;
/// <summary>
/// 要素数が同じな複数のVector3配列で、同じインデックス同士を加算して返します。
/// </summary>
/// <param name="verticesList"></param>
/// <returns></returns>
private static IEnumerable<Vector3> SumVerticesList(IEnumerable<IEnumerable<Vector3>> verticesList)
{
return verticesList.SelectMany(vertices => vertices.Select((vertix, index) => (vertix, index)))
.GroupBy(vertixAndIndex => vertixAndIndex.index, vertixAndIndex => vertixAndIndex.vertix)
.Select(vertices => vertices.ToList().Aggregate((accumulate, source) => accumulate + source));
}

/// <summary>
Expand Down Expand Up @@ -347,26 +335,27 @@ private static void EnableEyeLook(
Converter.OSCComponents oscComponents
)
{
VRMBlendShapeClip clip = oscComponents.HasFlag(Converter.OSCComponents.Blink)
? null
: clips.FirstOrDefault(c => c.Preset == BlendShapePreset.Blink);
var oscBlinkEnabled = oscComponents.HasFlag(Converter.OSCComponents.Blink);
var lookAtBoneApplyer = avatar.GetComponent<VRMLookAtBoneApplyer>();
if (!clip && !lookAtBoneApplyer)
if (!oscBlinkEnabled && !lookAtBoneApplyer)
{
return;
}

var renderer = avatar.transform.Find(VRChatUtility.AutoBlinkMeshPath).GetComponent<SkinnedMeshRenderer>();
var mesh = renderer.sharedMesh;
if (clip && mesh.GetBlendShapeIndex(BlendShapeReplacer.BlinkShapeKeyName) == -1)
if (oscBlinkEnabled && mesh.GetBlendShapeIndex(BlendShapeReplacer.BlinkShapeKeyName) == -1)
{
mesh.AddBlendShapeFrame(
BlendShapeReplacer.BlinkShapeKeyName,
BlendShapeReplacer.MaxBlendShapeFrameWeight,
BlendShapeReplacer.GenerateShapeKey(
BlendShapeReplacer.SubtractNeutralShapeKeyValues(clip.ShapeKeyValues, clips),
BlendShapeReplacer.SubtractNeutralShapeKeyValues(
clips.FirstOrDefault(c => c.Preset == BlendShapePreset.Blink).ShapeKeyValues,
clips
),
SkinnedMeshUtility.GetAllShapeKeys(mesh, useShapeKeyNormalsAndTangents)
),
).ToArray(),
null,
null
);
Expand All @@ -378,7 +367,7 @@ Converter.OSCComponents oscComponents

var settings = new VRCAvatarDescriptor.CustomEyeLookSettings();

if (clip)
if (oscBlinkEnabled)
{
settings.eyelidType = VRCAvatarDescriptor.EyelidType.Blendshapes;
settings.eyelidsSkinnedMesh = renderer;
Expand Down Expand Up @@ -535,7 +524,7 @@ IEnumerable<float> secondsList
private static void SetFeelings(
GameObject avatar,
IEnumerable<VRMBlendShapeClip> clips,
VRMBlendShapeClip vrmBlendShapeForFINGERPOINT,
VRMBlendShapeClip? vrmBlendShapeForFINGERPOINT,
Converter.OSCComponents oscComponents
)
{
Expand Down Expand Up @@ -623,14 +612,14 @@ var blendTree
= (BlendTree)childStates.First(childState => childState.state.name == "FaceBlend").state.motion;
var motions = blendTree.children;

AnimationClip neutral = null;
AnimationClip? neutral = null;

foreach (var preset in BlendShapeReplacer.MappingBlendShapeToVRChatAnim.Keys.Concat(new[] { BlendShapePreset.Neutral }))
{
VRMBlendShapeClip blendShapeClip = preset == BlendShapePreset.Unknown
VRMBlendShapeClip? blendShapeClip = preset == BlendShapePreset.Unknown
? vrmBlendShapeForFINGERPOINT
: clips.FirstOrDefault(c => c.Preset == preset);
if (!blendShapeClip)
if (blendShapeClip == null)
{
if (preset == BlendShapePreset.Neutral)
{
Expand Down Expand Up @@ -674,7 +663,7 @@ var blendTree
.GetComponent<SkinnedMeshRenderer>().sharedMaterials;

foreach (var clip in usedPresets.Select(preset => clips.First(clip => preset == BlendShapePreset.Unknown
? clip.BlendShapeName == vrmBlendShapeForFINGERPOINT.BlendShapeName
? (vrmBlendShapeForFINGERPOINT != null && clip.BlendShapeName == vrmBlendShapeForFINGERPOINT.BlendShapeName)
: clip.Preset == preset)))
{
foreach (var animationClip in animationClips)
Expand Down
3 changes: 2 additions & 1 deletion Editor/Components/ComponentsReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Linq;
using System.Collections.Generic;
Expand All @@ -24,7 +25,7 @@ internal class ComponentsReplacer
internal static IEnumerable<(string, MessageType)> Apply(
GameObject avatar,
Converter.SwayingObjectsConverterSetting swayingObjectsConverterSetting,
VRMSpringBonesToVRCPhysBonesConverter.ParametersConverter swayingParametersConverter
VRMSpringBonesToVRCPhysBonesConverter.ParametersConverter? swayingParametersConverter
)
{
var messages = new List<(string, MessageType)>();
Expand Down
1 change: 1 addition & 0 deletions Editor/Components/GeometryCorrector.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
Expand Down
5 changes: 3 additions & 2 deletions Editor/Components/VRChatsBugsWorkaround.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Linq;
using System.Collections.Generic;
Expand Down Expand Up @@ -108,7 +109,7 @@ private static void EnableAnimationOvrride(GameObject avatar)
/// 再度メソッドを呼び出すと変更は失われます。</param>
private static void ApplyAvatarDescription(
GameObject avatar,
Action<HumanDescription> humanDescriptionModifier = null
Action<HumanDescription>? humanDescriptionModifier = null
)
{
var humanoidDescription = avatar.GetComponent<VRMHumanoidDescription>();
Expand Down Expand Up @@ -218,7 +219,7 @@ in EditorUtility.CollectDependencies(new[] { avatar }).Where(obj => obj is Textu
}

var importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (!importer || !importer.mipmapEnabled || importer.streamingMipmaps)
if (importer == null || !importer.mipmapEnabled || importer.streamingMipmaps)
{
continue;
}
Expand Down
12 changes: 6 additions & 6 deletions Editor/Converter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Linq;
using System.Collections.Generic;
Expand Down Expand Up @@ -73,14 +74,14 @@ public enum OSCComponents
bool forQuest,
SwayingObjectsConverterSetting swayingObjectsConverterSetting,
bool takingOverSwayingParameters = true,
VRMSpringBonesToVRCPhysBonesConverter.ParametersConverter swayingParametersConverter = null,
VRMBlendShapeClip vrmBlendShapeForFINGERPOINT = null,
VRMSpringBonesToVRCPhysBonesConverter.ParametersConverter? swayingParametersConverter = null,
VRMBlendShapeClip? vrmBlendShapeForFINGERPOINT = null,
bool keepingUpperChest = false,
float addedShouldersPositionY = 0.0f,
float addedArmaturePositionY = 0.0f,
bool useShapeKeyNormalsAndTangents = false,
OSCComponents oscComponents = OSCComponents.Blink,
PostConverting postConverting = null
PostConverting? postConverting = null
)
{
AssetDatabase.SaveAssets();
Expand All @@ -102,8 +103,7 @@ public enum OSCComponents
swayingObjectsConverterSetting: swayingObjectsConverterSetting,
swayingParametersConverter: takingOverSwayingParameters
? swayingParametersConverter
: null,
forQuest
: null
));
messages.AddRange(VRChatsBugsWorkaround.Apply(
avatar: prefabInstance,
Expand Down Expand Up @@ -146,7 +146,7 @@ void Handler()
EditorApplication.update -= Handler;

taskCompleteSource.SetResult(
request.Result.FirstOrDefault(info => info.name == "jp.pokemori.vrm-converter-for-vrchat")?.version
request.Result.FirstOrDefault(info => info.name == "jp.pokemori.vrm-converter-for-vrchat").version
);
}

Expand Down
26 changes: 14 additions & 12 deletions Editor/Duplicator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -41,8 +42,8 @@ public class Duplicator
public static GameObject Duplicate(
GameObject sourceAvatar,
string destinationPath,
IEnumerable<string> notCombineRendererObjectNames = null,
bool combineMeshesAndSubMeshes = true
IEnumerable<string> notCombineRendererObjectNames,
bool combineMeshesAndSubMeshes
)
{
var destinationPrefab = Duplicator.DuplicatePrefab(sourceAvatar, destinationPath);
Expand Down Expand Up @@ -153,7 +154,7 @@ internal static T DuplicateAssetToFolder<T>(
internal static T CreateObjectToFolder<T>(
T source,
string prefabPath,
string destinationFileName = null
string? destinationFileName = null
) where T : Object
{
var path = AssetDatabase.GetAssetPath(source);
Expand Down Expand Up @@ -201,7 +202,7 @@ internal static T CreateObjectToFolder<T>(
internal static T CreateObjectToFolder<T>(
T source,
GameObject prefabInstance,
string destinationFileName = null
string destinationFileName
) where T : Object
{
return Duplicator.CreateObjectToFolder<T>(
Expand Down Expand Up @@ -270,7 +271,7 @@ private static T DuplicateAsset<T>(T source, string destinationPath) where T : O
{
if (source is AnimatorController controller)
{
return Duplicator.DuplicateAnimatorControllerAsset(controller, destinationPath) as T;
return (T)(object)Duplicator.DuplicateAnimatorControllerAsset(controller, destinationPath);
}

var sourceUnityPath = UnityPath.FromAsset(source);
Expand Down Expand Up @@ -384,17 +385,18 @@ private static void DuplicateAndCombineMeshes(
IEnumerable<string> notCombineRendererObjectNames
)
{
var faceMeshRenderer
= combineMeshesAndSubMeshes ? null : Duplicator.GetFaceMeshRenderer(prefabInstance: prefabInstance);
var faceMeshTransform = combineMeshesAndSubMeshes
? null
: Duplicator.GetFaceMeshRenderer(prefabInstance: prefabInstance).transform;

var sameNameTransform = prefabInstance.transform.Find(VRChatUtility.AutoBlinkMeshPath);
if (sameNameTransform && (combineMeshesAndSubMeshes || faceMeshRenderer.transform != sameNameTransform))
if (sameNameTransform && (faceMeshTransform == null || faceMeshTransform != sameNameTransform))
{
sameNameTransform.name += "-" + VRChatUtility.AutoBlinkMeshPath;
}

var prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(prefabInstance);
if (combineMeshesAndSubMeshes)
if (faceMeshTransform == null)
{
CombineMeshesAndSubMeshes.Combine(
root: prefabInstance,
Expand All @@ -404,10 +406,10 @@ var faceMeshRenderer
}
else
{
if (faceMeshRenderer.transform != sameNameTransform)
if (faceMeshTransform != sameNameTransform)
{
faceMeshRenderer.transform.parent = prefabInstance.transform;
faceMeshRenderer.transform.name = VRChatUtility.AutoBlinkMeshPath;
faceMeshTransform.parent = prefabInstance.transform;
faceMeshTransform.name = VRChatUtility.AutoBlinkMeshPath;
}
}
PrefabUtility.SaveAsPrefabAssetAndConnect(prefabInstance, prefabPath, InteractionMode.AutomatedAction);
Expand Down
1 change: 1 addition & 0 deletions Editor/Locales.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
Expand Down
9 changes: 7 additions & 2 deletions Editor/UI/ErrorDialog.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using UnityEngine;
using UnityEditor;
Expand All @@ -14,10 +15,14 @@ internal class ErrorDialog : ScriptableWizard
private static readonly string IssuesURL = "https://github.com/esperecyan/VRMConverterForVRChat/issues";
private static readonly string VRChatSDKVersionFilePath = "Assets/VRCSDK/version.txt";

private string version;
private Exception exception;
private string version = null!;
private Exception exception = null!;
private Vector2 errorMessageScrollPosition;

private ErrorDialog()
{
}

/// <summary>
/// ダイアログを開きます。
/// </summary>
Expand Down
Loading

0 comments on commit 1891517

Please sign in to comment.