Skip to content

Commit

Permalink
add: settings for determining objects should be exported based on Gam…
Browse files Browse the repository at this point in the history
…eObject.activeInHierarchy and Camera cullingMask
  • Loading branch information
hybridherbst committed Apr 18, 2021
1 parent cb73b5d commit 46e3378
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
20 changes: 19 additions & 1 deletion UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public class ExportOptions
{
public GLTFSceneExporter.RetrieveTexturePathDelegate TexturePathRetriever = (texture) => texture.name;
public bool ExportInactivePrimitives = true;
public LayerMask ExportLayers = -1;

public ExportOptions()
{
var settings = GLTFSettings.GetOrCreateSettings();
if(settings.UseMainCameraVisibility)
ExportLayers = Camera.main ? Camera.main.cullingMask : -1;
}
}

public class GLTFSceneExporter
Expand Down Expand Up @@ -66,6 +74,7 @@ private struct ImageInfo
private List<Transform> _skinnedNodes;
private Dictionary<SkinnedMeshRenderer, UnityEngine.Mesh> _bakedMeshes;

private int _exportLayerMask;
private ExportOptions _exportOptions;

private Material _metalGlossChannelSwapMaterial;
Expand Down Expand Up @@ -150,6 +159,7 @@ public GLTFSceneExporter(Transform[] rootTransforms, RetrieveTexturePathDelegate
public GLTFSceneExporter(Transform[] rootTransforms, ExportOptions options)
{
_exportOptions = options;
_exportLayerMask = _exportOptions.ExportLayers;

var metalGlossChannelSwapShader = Resources.Load("MetalGlossChannelSwap", typeof(Shader)) as Shader;
_metalGlossChannelSwapMaterial = new Material(metalGlossChannelSwapShader);
Expand Down Expand Up @@ -599,6 +609,13 @@ private void DeclareExtensionUsage(string extension, bool isRequired=false)
}
}

private bool ShouldExportTransform(Transform transform)
{
if (!settings.ExportDisabledGameObjects && !transform.gameObject.activeInHierarchy) return false;
if (settings.UseMainCameraVisibility && (_exportLayerMask >= 0 && _exportLayerMask != (_exportLayerMask | 1 << transform.gameObject.layer))) return false;
return true;
}

private SceneId ExportScene(string name, Transform[] rootObjTransforms)
{
var scene = new GLTFScene();
Expand All @@ -611,6 +628,7 @@ private SceneId ExportScene(string name, Transform[] rootObjTransforms)
scene.Nodes = new List<NodeId>(rootObjTransforms.Length);
foreach (var transform in rootObjTransforms)
{
if(!ShouldExportTransform(transform)) continue;
scene.Nodes.Add(ExportNode(transform));
}

Expand All @@ -632,7 +650,6 @@ private NodeId ExportNode(Transform nodeTransform)
node.Name = nodeTransform.name;
}


if (nodeTransform.GetComponent<UnityEngine.Animation>() || nodeTransform.GetComponent<UnityEngine.Animator>())
{
_animatedNodes.Add(nodeTransform);
Expand Down Expand Up @@ -709,6 +726,7 @@ private NodeId ExportNode(Transform nodeTransform)
node.Children = new List<NodeId>(nonPrimitives.Length);
foreach (var child in nonPrimitives)
{
if(!ShouldExportTransform(child.transform)) continue;
node.Children.Add(ExportNode(child.transform));
}
}
Expand Down
30 changes: 29 additions & 1 deletion UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ public class GLTFSettings : ScriptableObject
[SerializeField]
private bool exportFullPath = true;
[SerializeField]
[Tooltip("Uses Camera.main layer settings to filter which objects are exported")]
private bool useMainCameraVisibility = true;
[SerializeField]
private bool requireExtensions = false;
[SerializeField]
[Tooltip("Exports PNG/JPEG directly from disk instead of re-encoding from Unity's import result. Textures in other formats (PSD, TGA etc) not supported by glTF and in-memory textures (e.g. RenderTextures) are always re-encoded.")]
private bool tryExportTexturesFromDisk = true;
[SerializeField]
[Tooltip("glTF does not support visibility state. If this setting is true, disabled GameObjects will still be exported and be visible in the glTF file.")]
private bool exportDisabledGameObjects = false;
[SerializeField]
private bool exportAnimations = true;
[SerializeField]
Expand Down Expand Up @@ -92,6 +96,19 @@ public bool ExportFullPath
}
}
}

public bool UseMainCameraVisibility
{ get => useMainCameraVisibility;
set {
if(useMainCameraVisibility != value) {
useMainCameraVisibility = value;
#if UNITY_EDITOR
EditorUtility.SetDirty(this);
#endif
}
}
}

public bool RequireExtensions
{ get => requireExtensions;
set {
Expand All @@ -115,7 +132,18 @@ public bool TryExportTexturesFromDisk
}
}
}


public bool ExportDisabledGameObjects
{ get => exportDisabledGameObjects;
set {
if(exportDisabledGameObjects != value) {
exportDisabledGameObjects = value;
#if UNITY_EDITOR
EditorUtility.SetDirty(this);
#endif
}
}
}

public bool ExportAnimations
{ get => exportAnimations;
Expand Down

0 comments on commit 46e3378

Please sign in to comment.