IVRM0SpringBoneRuntime

This commit is contained in:
ousttrue
2024-09-19 14:35:05 +09:00
parent 7b284bb4e4
commit ee3ce66034
9 changed files with 136 additions and 21 deletions

View File

@@ -20,16 +20,20 @@ namespace VRM
}
}
IVRM0SpringBoneRuntime _springBoneRuntime;
public VRMImporterContext(
VRMData data,
IReadOnlyDictionary<SubAssetKey, Object> externalObjectMap = null,
ITextureDeserializer textureDeserializer = null,
IMaterialDescriptorGenerator materialGenerator = null,
ImporterContextSettings settings = null)
ImporterContextSettings settings = null,
IVRM0SpringBoneRuntime springboneRuntime = null)
: base(data.Data, externalObjectMap, textureDeserializer, materialGenerator ?? VrmMaterialDescriptorGeneratorUtility.GetValidVrmMaterialDescriptorGenerator(data.VrmExtension), settings ?? new ImporterContextSettings(false))
{
_data = data;
TextureDescriptorGenerator = new VrmTextureDescriptorGenerator(Data, VRM);
_springBoneRuntime = springboneRuntime ?? new VRMSpringBoneDefaultRuntime();
}
#region OnLoad
@@ -57,8 +61,8 @@ namespace VRM
using (MeasureTime("VRM LoadSecondary"))
{
VRMSpringUtility.LoadSecondary(Root.transform, TryGetNode,
VRM.secondaryAnimation);
VRMSpringUtility.LoadSecondary(Root.transform, TryGetNode, VRM.secondaryAnimation);
await _springBoneRuntime.InitializeAsync(Root, awaitCaller);
}
await awaitCaller.NextFrame();

View File

@@ -73,7 +73,8 @@ namespace VRM
MaterialGeneratorCallback materialGeneratorCallback = null,
MetaCallback metaCallback = null,
ITextureDeserializer textureDeserializer = null,
bool loadAnimation = false
bool loadAnimation = false,
IVRM0SpringBoneRuntime springboneRuntime = null
)
{
if (bytes == null)
@@ -102,7 +103,9 @@ namespace VRM
vrm,
textureDeserializer: textureDeserializer,
materialGenerator: materialGen,
settings: importerContextSettings))
settings: importerContextSettings,
springboneRuntime: springboneRuntime
))
{
if (metaCallback != null)
{

View File

@@ -0,0 +1,11 @@
using System.Threading.Tasks;
using UniGLTF;
using UnityEngine;
namespace VRM
{
public interface IVRM0SpringBoneRuntime
{
public Task InitializeAsync(GameObject vrm, IAwaitCaller awaitCaller);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dde51548cb9bb7546b6a9824b8710315
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,41 @@
using System.Threading.Tasks;
using UniGLTF;
using UniGLTF.SpringBoneJobs;
using UnityEngine;
namespace VRM
{
/// <summary>
/// FastSpringbone(job + singleton) で動作します。
///
/// VRMSpringBone.m_updateType = Manual
///
/// により、各VRMSpringBoneの自力Updateは停止します。
/// FastSpringBoneService に登録します。
/// FastSpringBoneService.LateUpdate[DefaultExecutionOrder(11000)] で動作します。
/// </summary>
public class FastSpringboneRuntime : IVRM0SpringBoneRuntime
{
public async Task InitializeAsync(GameObject vrm, IAwaitCaller awaitCaller)
{
// default update の停止
foreach (VRMSpringBone sb in vrm.GetComponentsInChildren<VRMSpringBone>())
{
sb.m_updateType = VRMSpringBone.SpringBoneUpdateType.Manual;
}
// create
var buffer = await SpringBoneJobs.FastSpringBoneReplacer.MakeBufferAsync(vrm, awaitCaller);
SpringBoneJobs.FastSpringBoneService.Instance.BufferCombiner.Register(buffer);
// disposer
var disposer = vrm.AddComponent<FastSpringBoneDisposer>()
.AddAction(() =>
{
SpringBoneJobs.FastSpringBoneService.Instance.BufferCombiner.Unregister(buffer);
buffer.Dispose();
})
;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b8315a3b319fd3f4a892def8ae68e812
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,25 @@
using System.Threading.Tasks;
using UniGLTF;
using UnityEngine;
namespace VRM
{
/// <summary>
/// デフォルトの VRMSPringBone 実装です。
///
/// VRMSpringBone.m_updateType = LateUpdate
///
/// により、各VRMSpringBoneが自力で LateUpdate に動作します。
/// </summary>
public class VRMSpringBoneDefaultRuntime : IVRM0SpringBoneRuntime
{
public async Task InitializeAsync(GameObject vrm, IAwaitCaller awaitCaller)
{
foreach (VRMSpringBone sb in vrm.GetComponentsInChildren<VRMSpringBone>())
{
sb.m_updateType = VRMSpringBone.SpringBoneUpdateType.LateUpdate;
}
await awaitCaller.NextFrame();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 08914ddaedc7d394ab58a879662341b7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -6,6 +6,7 @@ using System.Threading.Tasks;
using UniGLTF;
using UniGLTF.SpringBoneJobs;
using UniHumanoid;
using Unity.Collections;
using UnityEngine;
using UnityEngine.UI;
@@ -267,6 +268,13 @@ namespace VRM.SimpleViewer
}
}
private void Awake()
{
#if DEBUG
NativeLeakDetection.Mode = NativeLeakDetectionMode.EnabledWithStackTrace;
#endif
}
private void Start()
{
m_version.text = string.Format("VRMViewer {0}.{1}",
@@ -382,23 +390,13 @@ namespace VRM.SimpleViewer
}
// vrm
VrmUtility.MaterialGeneratorCallback materialCallback = (VRM.glTF_VRM_extensions vrm) => GetVrmMaterialGenerator(m_useUrpMaterial.isOn, vrm);
VrmUtility.MaterialGeneratorCallback materialCallback = (glTF_VRM_extensions vrm) => GetVrmMaterialGenerator(m_useUrpMaterial.isOn, vrm);
VrmUtility.MetaCallback metaCallback = m_texts.UpdateMeta;
var instance = await VrmUtility.LoadBytesAsync(path, bytes, GetIAwaitCaller(m_useAsync.isOn), materialCallback, metaCallback, loadAnimation: m_loadAnimation.isOn);
if (m_useFastSpringBone.isOn)
{
// job用バッファ作成
var buffer = await SpringBoneJobs.FastSpringBoneReplacer.MakeBufferAsync(instance.Root);
// 登録
SpringBoneJobs.FastSpringBoneService.Instance.BufferCombiner.Register(buffer);
// 削除登録
instance.Root.AddComponent<FastSpringBoneDisposer>()
.AddAction(()=>{ SpringBoneJobs.FastSpringBoneService.Instance.BufferCombiner.Unregister(buffer);})
.Add(buffer);
}
var instance = await VrmUtility.LoadBytesAsync(path, bytes, GetIAwaitCaller(m_useAsync.isOn),
materialCallback, metaCallback,
loadAnimation: m_loadAnimation.isOn,
springboneRuntime: m_useFastSpringBone.isOn ? new FastSpringboneRuntime() : new VRMSpringBoneDefaultRuntime()
);
instance.EnableUpdateWhenOffscreen();
instance.ShowMeshes();