Skip to content

Commit

Permalink
move more internal settings to plugin/plugincontext
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Dec 30, 2023
1 parent 122f991 commit 7b79289
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Runtime/Scripts/Extensions/DracoImport.cs
@@ -1,6 +1,6 @@
using UnityGLTF.Plugins;

namespace Scripts.Extensions
namespace UnityGLTF.Extensions
{
public class DracoImport: GltfImportPlugin
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Extensions/Ktx2Import.cs
@@ -1,6 +1,6 @@
using UnityGLTF.Plugins;

namespace Scripts.Extensions
namespace UnityGLTF.Extensions
{
public class Ktx2Import: GltfImportPlugin
{
Expand Down
9 changes: 7 additions & 2 deletions Runtime/Scripts/Extensions/LightsPunctualPlugin.cs
@@ -1,6 +1,6 @@
using UnityGLTF.Plugins;

namespace Scripts.Extensions
namespace UnityGLTF.Extensions
{
public class LightsPunctualPlugin: GltfImportPlugin
{
Expand All @@ -9,7 +9,12 @@ public class LightsPunctualPlugin: GltfImportPlugin
public override GltfImportPluginContext CreateInstance(GLTFImportContext context)
{
// always enabled
return null;
return new LightsPunctualImportContext();
}
}

public class LightsPunctualImportContext: GltfImportPluginContext
{

}
}
2 changes: 1 addition & 1 deletion Runtime/Scripts/Extensions/MaterialExtensions.cs
Expand Up @@ -49,7 +49,7 @@ public override GltfExportPluginContext CreateInstance(ExportContext context)
return new MaterialExtensions(this);
}

public override string DisplayName => "PBR Next Material Extensions";
public override string DisplayName => "KHR_materials_* PBR Next Extensions";
public override string Description => "Exports various glTF PBR Material model extensions.";
}

Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Extensions/MaterialImportPlugin.cs
Expand Up @@ -11,7 +11,7 @@ public class MaterialImportPlugin: GltfImportPlugin
public bool KHR_materials_pbrSpecularGlossiness = true;
public bool KHR_materials_emissive_strength = true;

public override string DisplayName => "PBR Next Material Extensions";
public override string DisplayName => "KHR_materials_* PBR Next Extensions";
public override string Description => "Import support for various glTF material extensions.";
public override GltfImportPluginContext CreateInstance(GLTFImportContext context)
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Extensions/MeshoptImport.cs
@@ -1,6 +1,6 @@
using UnityGLTF.Plugins;

namespace a
namespace UnityGLTF.Extensions
{
public class MeshoptImport: GltfImportPlugin
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Extensions/TextMeshExport.cs
Expand Up @@ -8,7 +8,7 @@ namespace UnityGLTF
{
public class TextMeshExport : GltfExportPlugin
{
public override string DisplayName => "TextMeshPro GameObjects: Bake to Mesh";
public override string DisplayName => "Bake to Mesh: TextMeshPro GameObjects";
public override string Description => "Bakes 3D TextMeshPro objects (not UI/Canvas) into meshes and attempts to faithfully apply their shader settings to generate the font texture.";
public override GltfExportPluginContext CreateInstance(ExportContext context)
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Extensions/TextureTransformsPlugin.cs
@@ -1,6 +1,6 @@
using UnityGLTF.Plugins;

namespace Scripts.Extensions
namespace UnityGLTF.Extensions
{
public class TextureTransformsPlugin: GltfImportPlugin
{
Expand Down
5 changes: 4 additions & 1 deletion Runtime/Scripts/GLTFSceneImporter.cs
Expand Up @@ -400,7 +400,10 @@ public async Task LoadSceneAsync(int sceneIndex = -1, bool showSceneObj = true,
}

#if HAVE_MESHOPT_DECOMPRESS
await MeshOptDecodeBuffer(_gltfRoot);
if (Context.TryGetPlugin<MeshoptImportContext>(out _))
{
await MeshOptDecodeBuffer(_gltfRoot);
}
#endif
await _LoadScene(sceneIndex, showSceneObj, cancellationToken);

Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/GLTFSettings.cs
Expand Up @@ -115,7 +115,7 @@ internal static void DrawGLTFSettingsGUI()
internal static void OnPluginsGUI(IEnumerable<GltfPlugin> plugins)
{
EditorGUI.indentLevel++;
foreach (var plugin in plugins)
foreach (var plugin in plugins.OrderBy(x => x ? x.DisplayName : "ZZZ"))
{
if (!plugin) continue;
var displayName = plugin.DisplayName ?? plugin.name;
Expand Down
15 changes: 15 additions & 0 deletions Runtime/Scripts/Plugins/ImportContext.cs
Expand Up @@ -34,5 +34,20 @@ internal GLTFImportContext(IReadOnlyList<GltfImportPluginContext> plugins)
{
Plugins = plugins;
}

public bool TryGetPlugin<T>(out GltfImportPluginContext o) where T: GltfImportPluginContext
{
foreach (var plugin in Plugins)
{
if (plugin is T t)
{
o = t;
return true;
}
}

o = null;
return false;
}
}
}
23 changes: 18 additions & 5 deletions Runtime/Scripts/SceneExporter/ExporterAnimation.cs
Expand Up @@ -13,6 +13,7 @@
using GLTF.Schema;
using UnityEngine;
using UnityGLTF.Extensions;
using UnityGLTF.Plugins;
using Object = UnityEngine.Object;

#if UNITY_2020_2_OR_NEWER
Expand All @@ -37,6 +38,18 @@ public partial class GLTFSceneExporter
private static bool BakeAnimationData = true;
#endif

private bool? _useAnimationPointer = null;

private bool UseAnimationPointer
{
get
{
if (_useAnimationPointer == null)
_useAnimationPointer = _plugins.Any(x => x is AnimationPointerExportContext);
return _useAnimationPointer.Value;
}
}

// Parses Animation/Animator component and generate a glTF animation for the active clip
// This may need additional work to fully support animatorControllers
public void ExportAnimationFromNode(ref Transform transform)
Expand Down Expand Up @@ -776,7 +789,7 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
int alreadyExportedChannelTargetId = GetTransformIndex(alreadyExportedTransform);
animation.Channels.RemoveAll(x => x.Target.Node != null && x.Target.Node.Id == alreadyExportedChannelTargetId);

if (settings.UseAnimationPointer)
if (UseAnimationPointer)
{
animation.Channels.RemoveAll(x =>
{
Expand Down Expand Up @@ -892,7 +905,7 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,

// arbitrary properties require the KHR_animation_pointer extension
bool sampledAnimationData = false;
if (settings.UseAnimationPointer && curve.propertyCurves != null && curve.propertyCurves.Count > 0)
if (UseAnimationPointer && curve.propertyCurves != null && curve.propertyCurves.Count > 0)
{
var curves = curve.propertyCurves;
foreach (KeyValuePair<string, PropertyCurve> c in curves)
Expand Down Expand Up @@ -1026,7 +1039,7 @@ private void CollectClipCurves(GameObject root, AnimationClip clip, Dictionary<s
var containsBlendShapeWeight = binding.propertyName.StartsWith("blendShape.", StringComparison.Ordinal);
var containsCompatibleData = containsPosition || containsScale || containsRotation || containsEuler || containsBlendShapeWeight;

if (!containsCompatibleData && !settings.UseAnimationPointer)
if (!containsCompatibleData && !UseAnimationPointer)
{
Debug.LogWarning("No compatible animation data found in clip binding: " + binding.propertyName + ". You may want to turn KHR_animation_pointer export on.", clip);
continue;
Expand Down Expand Up @@ -1088,7 +1101,7 @@ private void CollectClipCurves(GameObject root, AnimationClip clip, Dictionary<s
var weightName = binding.propertyName.Substring("blendShape.".Length);
current.weightCurves.Add(weightName, curve);
}
else if (settings.UseAnimationPointer)
else if (UseAnimationPointer)
{
var obj = AnimationUtility.GetAnimatedObject(root, binding);
if (obj)
Expand All @@ -1105,7 +1118,7 @@ private void CollectClipCurves(GameObject root, AnimationClip clip, Dictionary<s

// object reference curves - in some cases animated data can be contained in here, e.g. for SpriteRenderers.
// this only makes sense when AnimationPointer is on, and someone needs to resolve the data to something in the glTF later via KHR_animation_pointer_Resolver
if (settings.UseAnimationPointer)
if (UseAnimationPointer)
{
var objectBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip);
foreach (var binding in objectBindings)
Expand Down
6 changes: 3 additions & 3 deletions Runtime/Scripts/SceneExporter/ExporterAnimationPointer.cs
Expand Up @@ -385,7 +385,7 @@ public void AddAnimationData(Object animatedObject, string propertyName, GLTFAni
// Debug.LogWarning($"Implicitly handling animated property \"{propertyName}\" for target {animatedObject}", animatedObject);

// filtering for what to include / what not to include based on whether its target can be resolved
if (settings.UseAnimationPointer && animatedObject is Component _)
if (UseAnimationPointer && animatedObject is Component _)
{
var couldResolve = false;
var prop = $"/nodes/{channelTargetId}/{propertyName}";
Expand Down Expand Up @@ -590,7 +590,7 @@ public void AddAnimationData(Object animatedObject, string propertyName, GLTFAni
animation.Samplers.Add(Tsampler);
animation.Channels.Add(Tchannel);

if (settings.UseAnimationPointer)
if (UseAnimationPointer)
ConvertToAnimationPointer(animatedObject, propertyName, TchannelTarget);

// in some cases, extensions aren't required even when we think they might, e.g. for emission color animation.
Expand Down Expand Up @@ -627,7 +627,7 @@ public void AddAnimationData(Object animatedObject, string propertyName, GLTFAni
animation.Samplers.Add(Tsampler2);
animation.Channels.Add(Tchannel2);

if (settings.UseAnimationPointer)
if (UseAnimationPointer)
ConvertToAnimationPointer(animatedObject, secondPropertyName, TchannelTarget2);
}
}
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Scripts/SceneImporter/ImporterLights.cs
Expand Up @@ -10,6 +10,9 @@ public partial class GLTFSceneImporter
{
private void ConstructLights(GameObject nodeObj, Node node)
{
var useLightsExtension = Context.TryGetPlugin<LightsPunctualImportContext>(out _);
if (!useLightsExtension) return;

// TODO this should be handled by the lights extension directly, not here
const string lightExt = KHR_lights_punctualExtensionFactory.EXTENSION_NAME;
KHR_LightsPunctualNodeExtension lightsExtension = null;
Expand Down
13 changes: 9 additions & 4 deletions Runtime/Scripts/SceneImporter/ImporterMeshes.cs
Expand Up @@ -52,11 +52,17 @@ protected virtual async Task ConstructMesh(GLTFMesh mesh, int meshIndex, Cancell

var anyHadDraco = mesh.Primitives.Any(p => p.Extensions != null && p.Extensions.ContainsKey(KHR_draco_mesh_compression_Factory.EXTENSION_NAME));
#if HAVE_DRACO

if (anyHadDraco)
{
await ConstructDracoMesh(mesh, meshIndex, cancellationToken);
return;
if (Context.TryGetPlugin<DracoImportContext>(out _))
{
await ConstructDracoMesh(mesh, meshIndex, cancellationToken);
return;
}
else
{
throw new NotSupportedException("Can't import model because it uses the KHR_draco_mesh_compression extension. Please add the package \"com.atteneder.draco\" to your project to import this file.");
}
}
#else
if (anyHadDraco)
Expand Down Expand Up @@ -188,7 +194,6 @@ protected virtual async Task ConstructDracoMesh(GLTFMesh mesh, int meshIndex, Ca
#endif

#if HAVE_MESHOPT_DECOMPRESS

private async Task MeshOptDecodeBuffer(GLTFRoot root)
{
if (root.BufferViews == null)
Expand Down
35 changes: 21 additions & 14 deletions Runtime/Scripts/SceneImporter/ImporterTextures.cs
Expand Up @@ -147,27 +147,34 @@ async Task<Texture2D> CheckMimeTypeAndLoadImage(GLTFImage image, Texture2D textu
break;
case "image/ktx2":
string textureName = texture.name;

#if HAVE_KTX
if (Context.TryGetPlugin<Ktx2ImportContext>(out _))
{
#if UNITY_EDITOR
Texture.DestroyImmediate(texture);
Texture.DestroyImmediate(texture);
#else
Texture.Destroy(texture);
Texture.Destroy(texture);
#endif
var ktxTexture = new KtxUnity.KtxTexture();
var ktxTexture = new KtxUnity.KtxTexture();

using (var alloc = new Unity.Collections.NativeArray<byte>(data, Unity.Collections.Allocator.Persistent))
{
var resultTextureData = await ktxTexture.LoadFromBytes(alloc, false);
texture = resultTextureData.texture;
texture.name = textureName;
}
using (var alloc = new Unity.Collections.NativeArray<byte>(data, Unity.Collections.Allocator.Persistent))
{
var resultTextureData = await ktxTexture.LoadFromBytes(alloc, false);
texture = resultTextureData.texture;
texture.name = textureName;
}

ktxTexture.Dispose();
#else
Debug.Log(LogType.Warning, $"Can't import texture \"{image.Name}\" from \"{_gltfFileName}\" because it is a KTX2 file using the KHR_texture_basisu extension. Please add the package \"com.atteneder.ktx\" version v1.3+ to your project to import KTX2 textures.");
await Task.CompletedTask;
texture = null;
ktxTexture.Dispose();

}
else
#endif
{
Debug.Log(LogType.Warning, $"Can't import texture \"{image.Name}\" from \"{_gltfFileName}\" because it is a KTX2 file using the KHR_texture_basisu extension. Please add the package \"com.atteneder.ktx\" version v1.3+ to your project to import KTX2 textures.");
await Task.CompletedTask;
texture = null;
}
break;
default:
texture.LoadImage(data, markGpuOnly);
Expand Down

0 comments on commit 7b79289

Please sign in to comment.