62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using GLTF.Schema;
|
|
using JetBrains.Annotations;
|
|
using Needle.Engine.Gltf.Experimental.progressive;
|
|
using Needle.Engine.Gltf.ImportSettings;
|
|
using Needle.Engine.Utils;
|
|
using UnityEngine;
|
|
|
|
namespace Needle.Engine.Gltf
|
|
{
|
|
[UsedImplicitly]
|
|
public class ProgressiveMeshSettingsHandler: GltfExtensionHandlerBase
|
|
{
|
|
private NeedleCompressionSettings progressiveLoadingComponent;
|
|
// private bool isUsingProgressiveTextures;
|
|
|
|
public override void OnBeforeExport(GltfExportContext context)
|
|
{
|
|
base.OnBeforeExport(context);
|
|
progressiveLoadingComponent = Object.FindObjectsByType<NeedleCompressionSettings>(FindObjectsSortMode.None).FirstOrDefault(e => e.enabled);
|
|
}
|
|
|
|
public override void OnAfterMeshExport(GltfExportContext context, Mesh mesh, GLTFMesh gltfmesh, int index, List<object> extensions)
|
|
{
|
|
base.OnAfterMeshExport(context, mesh, gltfmesh, index, extensions);
|
|
|
|
var progressiveTexturesSettings = progressiveLoadingComponent;
|
|
if (context.Root.TryGetComponent(out NeedleCompressionSettings rootSettings))
|
|
{
|
|
progressiveTexturesSettings = rootSettings;
|
|
}
|
|
|
|
var guid = mesh.GetId();
|
|
|
|
// check if LOD generation is disabled
|
|
if ( progressiveTexturesSettings?.GenerateMeshLODs == false)
|
|
{
|
|
extensions.Add(new NEEDLE_progressive_mesh_settings(guid, false));
|
|
return;
|
|
}
|
|
|
|
if (NeedleAssetSettings.TryGetSettings(mesh, out var settings))
|
|
{
|
|
if(settings is MeshSettings meshSettings)
|
|
{
|
|
if (meshSettings.useProgressiveMesh == false)
|
|
{
|
|
extensions.Add(new NEEDLE_progressive_mesh_settings(guid, false));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// we dont want to compress e.g. the default cube
|
|
if (mesh.vertexCount > 32)
|
|
{
|
|
extensions.Add(new NEEDLE_progressive_mesh_settings(guid, true));
|
|
}
|
|
}
|
|
}
|
|
} |