Skip to content

WIP: import lights #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using UnityEngine;

namespace USD.NET.Unity
{
[System.Serializable]
public class LightSampleBase : XformSample
{
public LightSampleBase()
{
}

public virtual void CopyFromLight(UnityEngine.Light light, bool convertTransformToUsd = true)
{
var tr = light.transform;
transform = UnityEngine.Matrix4x4.TRS(tr.localPosition,
tr.localRotation,
tr.localScale);
if (convertTransformToUsd)
{
ConvertTransform();
}
}

public virtual void CopyToLight(UnityEngine.Light light, bool setTransform)
{
if (setTransform)
{
var tr = light.transform;
var xf = transform;
UnityTypeConverter.SetTransform(xf, tr);
}
}
}

[System.Serializable]
[UsdSchema("DistantLight")]
public class DistantLightSample : LightSampleBase
{
// Core Light parameters
public float angle;
public float intensity;

public DistantLightSample()
{
}

public DistantLightSample(UnityEngine.Light fromLight)
{
CopyFromLight(fromLight);
}

override public void CopyFromLight(UnityEngine.Light light, bool convertTransformToUsd = true)
{
intensity = light.intensity;
base.CopyFromLight(light, convertTransformToUsd);
}

override public void CopyToLight(UnityEngine.Light light, bool setTransform)
{
light.type = LightType.Directional;
light.intensity = intensity;
base.CopyToLight(light, setTransform);
}
}

[System.Serializable]
[UsdSchema("SphereLight")]
public class SphereLightSample : LightSampleBase
{
// Core Light parameters
public bool treatAsPoint;
public float radius;

[UsdNamespace("shaping:cone")]
public float angle;

public SphereLightSample()
{
}

public SphereLightSample(UnityEngine.Light fromLight)
{
CopyFromLight(fromLight);
}

override public void CopyFromLight(UnityEngine.Light light, bool convertTransformToUsd = true)
{
treatAsPoint = true;
radius = light.range;
if (light.spotAngle > 0)
{
angle = light.spotAngle;
}
base.CopyFromLight(light, convertTransformToUsd);
}

override public void CopyToLight(UnityEngine.Light light, bool setTransform)
{
if (angle > 0)
{
light.type = LightType.Spot;
light.spotAngle = angle;
}
else
{
light.type = LightType.Point;
}

light.range = radius;
base.CopyToLight(light, setTransform);
}
}

[System.Serializable]
[UsdSchema("RectLight")]
public class RectLightSample : LightSampleBase
{
public RectLightSample()
{
}

public RectLightSample(UnityEngine.Light fromLight)
{
base.CopyFromLight(fromLight);
}

override public void CopyToLight(UnityEngine.Light light, bool setTransform)
{
light.type = LightType.Rectangle;
base.CopyToLight(light, setTransform);
}
}

[System.Serializable]
[UsdSchema("DiskLight")]
public class DiskLightSample : LightSampleBase
{
public DiskLightSample()
{
}

public DiskLightSample(UnityEngine.Light fromLight)
{
base.CopyFromLight(fromLight);
}

override public void CopyToLight(UnityEngine.Light light, bool setTransform)
{
light.type = LightType.Disc;
base.CopyToLight(light, setTransform);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public string usdFullPath
#if false
[Header("Export Settings")]
public bool m_exportCameras = true;
public bool m_exportLights = true;
public bool m_exportMeshes = true;
public bool m_exportSkinning = true;
public bool m_exportTransforms = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@



using UnityEngine;
using USD.NET;
using USD.NET.Unity;

namespace Unity.Formats.USD
{
public static class LightExporter
{
public static void ExportLight<T>(ObjectContext objContext, ExportContext exportContext) where T : LightSampleBase, new()
{
UnityEngine.Profiling.Profiler.BeginSample("USD: Light Conversion");

T sample = (T)objContext.sample;
Light light = objContext.gameObject.GetComponent<Light>();
var path = objContext.path;
var scene = exportContext.scene;
bool fastConvert = exportContext.basisTransform == BasisTransformation.FastWithNegativeScale;

sample.CopyFromLight(light, convertTransformToUsd: !fastConvert);

if (fastConvert)
{
// Partial change of basis.
var basisChange = Matrix4x4.identity;
// Invert the forward vector.
basisChange[2, 2] = -1;
// Full change of basis would be b*t*b-1, but here we're placing only a single inversion
// at the root of the hierarchy, so all we need to do is get the camera into the same
// space.
sample.transform = sample.transform * basisChange;

// Is this also a root path?
// If so the partial basis conversion must be completed on the camera itself.
if (path.LastIndexOf("/") == 0)
{
sample.transform = basisChange * sample.transform;
}
}

UnityEngine.Profiling.Profiler.EndSample();

UnityEngine.Profiling.Profiler.BeginSample("USD: Light Write");
scene.Write(path, sample);
UnityEngine.Profiling.Profiler.EndSample();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


using UnityEngine;
using USD.NET.Unity;

namespace Unity.Formats.USD
{
public static class LightImporter<T> where T : LightSampleBase
{
public static void BuildLight(T usdLight,
GameObject go,
SceneImportOptions options)
{
var light = ImporterBase.GetOrAddComponent<Light>(go);
usdLight.CopyToLight(light, setTransform: false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ static JobHandle BeginReading(Scene scene,
{
FindPathsJob.usdRoot = usdRoot;
FindPathsJob.scene = scene;
FindPathsJob.results = new SdfPath[9][];
FindPathsJob.queries = new FindPathsJob.IQuery[9];
FindPathsJob.results = new SdfPath[13][];
FindPathsJob.queries = new FindPathsJob.IQuery[13];

if (options.ShouldBindMaterials)
{
Expand Down Expand Up @@ -167,6 +167,15 @@ static JobHandle BeginReading(Scene scene,

FindPathsJob.queries[8] = (FindPathsJob.IQuery) new FindPathsJob.Query<ScopeSample>();

if (options.importLights)
{
FindPathsJob.queries[9] = (FindPathsJob.IQuery) new FindPathsJob.Query<DistantLightSample>();
FindPathsJob.queries[10] = (FindPathsJob.IQuery) new FindPathsJob.Query<SphereLightSample>();
FindPathsJob.queries[11] = (FindPathsJob.IQuery) new FindPathsJob.Query<RectLightSample>();
FindPathsJob.queries[12] = (FindPathsJob.IQuery) new FindPathsJob.Query<DiskLightSample>();

}

var findPathsJob = new FindPathsJob();
var findHandle = findPathsJob.Schedule(FindPathsJob.queries.Length, 1);
findHandle.Complete();
Expand All @@ -185,6 +194,10 @@ static JobHandle BeginReading(Scene scene,
map.SkelRoots = FindPathsJob.results[5];
map.Skeletons = FindPathsJob.results[6];
map.Xforms = FindPathsJob.results[7];
map.DirectionalLights = FindPathsJob.results[9];
map.SphereLights = FindPathsJob.results[10];
map.RectLights = FindPathsJob.results[11];
map.DiscLights = FindPathsJob.results[12];

ReadHierJob.paths = FindPathsJob.results.Where(i => i != null).SelectMany(i => i).ToArray();
ReadHierJob.result = new HierInfo[ReadHierJob.paths.Length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public struct InstanceRoot
public SdfPath[] SkelRoots { get; set; }
public SdfPath[] Skeletons { get; set; }
public SdfPath[] Materials { get; set; }
public SdfPath[] DirectionalLights { get; set; }
public SdfPath[] SphereLights { get; set; }
public SdfPath[] RectLights { get; set; }
public SdfPath[] DiscLights { get; set; }

// Normal objects in the hierarchy.
private Dictionary<SdfPath, GameObject> m_prims = new Dictionary<SdfPath, GameObject>();
Expand Down Expand Up @@ -166,6 +170,10 @@ public void Clear()
SkelRoots = null;
Skeletons = null;
Materials = null;
DirectionalLights = null;
SphereLights = null;
RectLights = null;
DiscLights = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ static void InitExportableObjects(GameObject go,
var mr = go.GetComponent<MeshRenderer>();
var mf = go.GetComponent<MeshFilter>();
var cam = go.GetComponent<Camera>();
var lig = go.GetComponent<Light>();
Transform expRoot = context.exportRoot;

var tmpPath = new pxr.SdfPath(UnityTypeConverter.GetPath(go.transform, expRoot));
Expand Down Expand Up @@ -623,6 +624,22 @@ static void InitExportableObjects(GameObject go,
CreateExportPlan(go, CreateSample<CameraSample>(context), NativeExporter.ExportObject, context,
insertFirst: false);
}
else if (lig != null)
{
if (lig.type == LightType.Directional)
CreateExportPlan(go, CreateSample<DistantLightSample>(context), LightExporter.ExportLight<DistantLightSample>, context);
else if (lig.type == LightType.Spot)
CreateExportPlan(go, CreateSample<SphereLightSample>(context), LightExporter.ExportLight<SphereLightSample>, context);
else if (lig.type == LightType.Point)
CreateExportPlan(go, CreateSample<SphereLightSample>(context), LightExporter.ExportLight<SphereLightSample>, context);
else if (lig.type == LightType.Rectangle)
CreateExportPlan(go, CreateSample<RectLightSample>(context), LightExporter.ExportLight<RectLightSample>, context);
else if (lig.type == LightType.Disc)
CreateExportPlan(go, CreateSample<DiskLightSample>(context), LightExporter.ExportLight<DiskLightSample>, context);

CreateExportPlan(go, CreateSample<DistantLightSample>(context), NativeExporter.ExportObject, context,
insertFirst: false);
}
}

static Transform MergeBonesBelowAnimator(Transform animator, ExportContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class SceneImportOptions

public bool importHierarchy = true;
public bool importCameras = true;
public bool importLights = true;
public bool importMeshes = true;
public bool importSkinning = true;
public bool importSkinWeights = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,42 @@ public static IEnumerator BuildScene(Scene scene,
Profiler.EndSample();
}

// Lights.
if (importOptions.importLights)
{
Profiler.BeginSample("USD: Lights");

void importLights<T>(pxr.SdfPath[] dest) where T : LightSampleBase, new()
{
foreach (var pathAndSample in scene.ReadAll<T>(dest))
{
try
{
GameObject go = primMap[pathAndSample.path];
NativeImporter.ImportObject(scene, go, scene.GetPrimAtPath(pathAndSample.path), importOptions);
XformImporter.BuildXform(pathAndSample.path, pathAndSample.sample, go, importOptions, scene);

if (scene.AccessMask == null || scene.IsPopulatingAccessMask)
{
LightImporter<T>.BuildLight(pathAndSample.sample, go, importOptions);
}
}
catch (System.Exception ex)
{
Debug.LogException(
new ImportException("Error processing light <" + pathAndSample.path + ">", ex));
}
}
}

importLights<DistantLightSample>(primMap.DirectionalLights);
importLights<SphereLightSample>(primMap.SphereLights);
importLights<RectLightSample>(primMap.RectLights);
importLights<DiskLightSample>(primMap.DiscLights);

Profiler.EndSample();
}

// Build out masters for instancing.
Profiler.BeginSample("USD: Build Instances");
foreach (var masterRootPath in primMap.GetMasterRootPaths())
Expand Down