Skip to content

Commit

Permalink
import lights
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmiwei committed Sep 27, 2021
1 parent 22689c8 commit 9b1b34e
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ packages.config
# UPM
upm-ci~

# Unity meta files
*.meta

# dll files
*.dll

# Ignore generated files and usd plugin files
**/generated
**/Plugins

# Build directory
build/

TestProject/Library
TestProject/ProjectSettings
TestProject/Temp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using UnityEngine;

namespace USD.NET.Unity
{
[System.Serializable]
public class LightSampleBase : XformSample
{
// Core Light parameters
public Color color;
public float intensity;
public LightShadows shadowType;

public LightSampleBase()
{
}

public void CopyFromLight(UnityEngine.Light light, bool convertTransformToUsd = true)
{
color = light.color;
intensity = light.intensity;
shadowType = light.shadows;

var tr = light.transform;
transform = UnityEngine.Matrix4x4.TRS(tr.localPosition,
tr.localRotation,
tr.localScale);
if (convertTransformToUsd)
{
ConvertTransform();
}
}

public void CopyToLight(UnityEngine.Light light, bool setTransform)
{
light.color = color;
light.intensity = intensity;
light.shadows = shadowType;

if (setTransform)
{
var tr = light.transform;
var xf = transform;
UnityTypeConverter.SetTransform(xf, tr);
}
}
}

[System.Serializable]
[UsdSchema("DistantLight")]
public class DistantLightSample : LightSampleBase
{
public DistantLightSample()
{
}

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

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

[System.Serializable]
[UsdSchema("SphereLight")]
public class PointLightSample : LightSampleBase
{
// Core Light parameters
public float range;

public PointLightSample()
{
}

public PointLightSample(UnityEngine.Light fromLight)
{
range = fromLight.range;
base.CopyFromLight(fromLight);
}

new public void CopyToLight(UnityEngine.Light light, bool setTransform)
{
light.type = LightType.Point;
light.range = range;
base.CopyToLight(light, setTransform);
}
}

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

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

new 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);
}

new public void CopyToLight(UnityEngine.Light light, bool setTransform)
{
light.type = LightType.Disc;
base.CopyToLight(light, setTransform);
}
}
}
Empty file.
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<PointLightSample>();
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.PointLights = 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[] PointLights { 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;
PointLights = null;
RectLights = null;
DiscLights = null;
}
}
}
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 @@ -750,6 +750,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<PointLightSample>(primMap.PointLights);
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

0 comments on commit 9b1b34e

Please sign in to comment.