Skip to content

Commit 08f95cd

Browse files
author
chenmiwei
committed
import lights
1 parent 22689c8 commit 08f95cd

File tree

7 files changed

+231
-2
lines changed

7 files changed

+231
-2
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using UnityEngine;
2+
3+
namespace USD.NET.Unity
4+
{
5+
[System.Serializable]
6+
public class LightSampleBase : XformSample
7+
{
8+
public LightSampleBase()
9+
{
10+
}
11+
12+
public virtual void CopyFromLight(UnityEngine.Light light, bool convertTransformToUsd = true)
13+
{
14+
var tr = light.transform;
15+
transform = UnityEngine.Matrix4x4.TRS(tr.localPosition,
16+
tr.localRotation,
17+
tr.localScale);
18+
if (convertTransformToUsd)
19+
{
20+
ConvertTransform();
21+
}
22+
}
23+
24+
public virtual void CopyToLight(UnityEngine.Light light, bool setTransform)
25+
{
26+
if (setTransform)
27+
{
28+
var tr = light.transform;
29+
var xf = transform;
30+
UnityTypeConverter.SetTransform(xf, tr);
31+
}
32+
}
33+
}
34+
35+
[System.Serializable]
36+
[UsdSchema("DistantLight")]
37+
public class DistantLightSample : LightSampleBase
38+
{
39+
// Core Light parameters
40+
public float angle;
41+
public float intensity;
42+
43+
public DistantLightSample()
44+
{
45+
}
46+
47+
public DistantLightSample(UnityEngine.Light fromLight)
48+
{
49+
CopyFromLight(fromLight);
50+
}
51+
52+
override public void CopyFromLight(UnityEngine.Light light, bool convertTransformToUsd = true)
53+
{
54+
intensity = light.intensity;
55+
base.CopyFromLight(light, convertTransformToUsd);
56+
}
57+
58+
override public void CopyToLight(UnityEngine.Light light, bool setTransform)
59+
{
60+
light.type = LightType.Directional;
61+
light.intensity = intensity;
62+
base.CopyToLight(light, setTransform);
63+
}
64+
}
65+
66+
[System.Serializable]
67+
[UsdSchema("SphereLight")]
68+
public class SphereLightSample : LightSampleBase
69+
{
70+
// Core Light parameters
71+
public bool treatAsPoint;
72+
public float radius;
73+
74+
[UsdNamespace("shaping:cone")]
75+
public float angle;
76+
77+
public SphereLightSample()
78+
{
79+
}
80+
81+
public SphereLightSample(UnityEngine.Light fromLight)
82+
{
83+
CopyFromLight(fromLight);
84+
}
85+
86+
override public void CopyFromLight(UnityEngine.Light light, bool convertTransformToUsd = true)
87+
{
88+
treatAsPoint = true;
89+
radius = light.range;
90+
if (light.spotAngle > 0)
91+
{
92+
angle = light.spotAngle;
93+
}
94+
base.CopyFromLight(light, convertTransformToUsd);
95+
}
96+
97+
override public void CopyToLight(UnityEngine.Light light, bool setTransform)
98+
{
99+
if (angle > 0)
100+
{
101+
light.type = LightType.Spot;
102+
light.spotAngle = angle;
103+
}
104+
else
105+
{
106+
light.type = LightType.Point;
107+
}
108+
109+
light.range = radius;
110+
base.CopyToLight(light, setTransform);
111+
}
112+
}
113+
114+
[System.Serializable]
115+
[UsdSchema("RectLight")]
116+
public class RectLightSample : LightSampleBase
117+
{
118+
public RectLightSample()
119+
{
120+
}
121+
122+
public RectLightSample(UnityEngine.Light fromLight)
123+
{
124+
base.CopyFromLight(fromLight);
125+
}
126+
127+
override public void CopyToLight(UnityEngine.Light light, bool setTransform)
128+
{
129+
light.type = LightType.Rectangle;
130+
base.CopyToLight(light, setTransform);
131+
}
132+
}
133+
134+
[System.Serializable]
135+
[UsdSchema("DiskLight")]
136+
public class DiskLightSample : LightSampleBase
137+
{
138+
public DiskLightSample()
139+
{
140+
}
141+
142+
public DiskLightSample(UnityEngine.Light fromLight)
143+
{
144+
base.CopyFromLight(fromLight);
145+
}
146+
147+
override public void CopyToLight(UnityEngine.Light light, bool setTransform)
148+
{
149+
light.type = LightType.Disc;
150+
base.CopyToLight(light, setTransform);
151+
}
152+
}
153+
}

package/com.unity.formats.usd/Runtime/Scripts/IO/Geometry/LightExporter.cs

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
using UnityEngine;
4+
using USD.NET.Unity;
5+
6+
namespace Unity.Formats.USD
7+
{
8+
public static class LightImporter<T> where T : LightSampleBase
9+
{
10+
public static void BuildLight(T usdLight,
11+
GameObject go,
12+
SceneImportOptions options)
13+
{
14+
var light = ImporterBase.GetOrAddComponent<Light>(go);
15+
usdLight.CopyToLight(light, setTransform: false);
16+
}
17+
}
18+
}

package/com.unity.formats.usd/Runtime/Scripts/IO/Scene/HierarchyBuilder.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ static JobHandle BeginReading(Scene scene,
133133
{
134134
FindPathsJob.usdRoot = usdRoot;
135135
FindPathsJob.scene = scene;
136-
FindPathsJob.results = new SdfPath[9][];
137-
FindPathsJob.queries = new FindPathsJob.IQuery[9];
136+
FindPathsJob.results = new SdfPath[13][];
137+
FindPathsJob.queries = new FindPathsJob.IQuery[13];
138138

139139
if (options.ShouldBindMaterials)
140140
{
@@ -167,6 +167,15 @@ static JobHandle BeginReading(Scene scene,
167167

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

170+
if (options.importLights)
171+
{
172+
FindPathsJob.queries[9] = (FindPathsJob.IQuery) new FindPathsJob.Query<DistantLightSample>();
173+
FindPathsJob.queries[10] = (FindPathsJob.IQuery) new FindPathsJob.Query<SphereLightSample>();
174+
FindPathsJob.queries[11] = (FindPathsJob.IQuery) new FindPathsJob.Query<RectLightSample>();
175+
FindPathsJob.queries[12] = (FindPathsJob.IQuery) new FindPathsJob.Query<DiskLightSample>();
176+
177+
}
178+
170179
var findPathsJob = new FindPathsJob();
171180
var findHandle = findPathsJob.Schedule(FindPathsJob.queries.Length, 1);
172181
findHandle.Complete();
@@ -185,6 +194,10 @@ static JobHandle BeginReading(Scene scene,
185194
map.SkelRoots = FindPathsJob.results[5];
186195
map.Skeletons = FindPathsJob.results[6];
187196
map.Xforms = FindPathsJob.results[7];
197+
map.DirectionalLights = FindPathsJob.results[9];
198+
map.SphereLights = FindPathsJob.results[10];
199+
map.RectLights = FindPathsJob.results[11];
200+
map.DiscLights = FindPathsJob.results[12];
188201

189202
ReadHierJob.paths = FindPathsJob.results.Where(i => i != null).SelectMany(i => i).ToArray();
190203
ReadHierJob.result = new HierInfo[ReadHierJob.paths.Length];

package/com.unity.formats.usd/Runtime/Scripts/IO/Scene/PrimMap.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public struct InstanceRoot
4747
public SdfPath[] SkelRoots { get; set; }
4848
public SdfPath[] Skeletons { get; set; }
4949
public SdfPath[] Materials { get; set; }
50+
public SdfPath[] DirectionalLights { get; set; }
51+
public SdfPath[] SphereLights { get; set; }
52+
public SdfPath[] RectLights { get; set; }
53+
public SdfPath[] DiscLights { get; set; }
5054

5155
// Normal objects in the hierarchy.
5256
private Dictionary<SdfPath, GameObject> m_prims = new Dictionary<SdfPath, GameObject>();
@@ -166,6 +170,10 @@ public void Clear()
166170
SkelRoots = null;
167171
Skeletons = null;
168172
Materials = null;
173+
DirectionalLights = null;
174+
SphereLights = null;
175+
RectLights = null;
176+
DiscLights = null;
169177
}
170178
}
171179
}

package/com.unity.formats.usd/Runtime/Scripts/IO/Scene/SceneImportOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public class SceneImportOptions
100100

101101
public bool importHierarchy = true;
102102
public bool importCameras = true;
103+
public bool importLights = true;
103104
public bool importMeshes = true;
104105
public bool importSkinning = true;
105106
public bool importSkinWeights = true;

package/com.unity.formats.usd/Runtime/Scripts/IO/Scene/SceneImporter.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,42 @@ public static IEnumerator BuildScene(Scene scene,
750750
Profiler.EndSample();
751751
}
752752

753+
// Lights.
754+
if (importOptions.importLights)
755+
{
756+
Profiler.BeginSample("USD: Lights");
757+
758+
void importLights<T>(pxr.SdfPath[] dest) where T : LightSampleBase, new()
759+
{
760+
foreach (var pathAndSample in scene.ReadAll<T>(dest))
761+
{
762+
try
763+
{
764+
GameObject go = primMap[pathAndSample.path];
765+
NativeImporter.ImportObject(scene, go, scene.GetPrimAtPath(pathAndSample.path), importOptions);
766+
XformImporter.BuildXform(pathAndSample.path, pathAndSample.sample, go, importOptions, scene);
767+
768+
if (scene.AccessMask == null || scene.IsPopulatingAccessMask)
769+
{
770+
LightImporter<T>.BuildLight(pathAndSample.sample, go, importOptions);
771+
}
772+
}
773+
catch (System.Exception ex)
774+
{
775+
Debug.LogException(
776+
new ImportException("Error processing light <" + pathAndSample.path + ">", ex));
777+
}
778+
}
779+
}
780+
781+
importLights<DistantLightSample>(primMap.DirectionalLights);
782+
importLights<SphereLightSample>(primMap.SphereLights);
783+
importLights<RectLightSample>(primMap.RectLights);
784+
importLights<DiskLightSample>(primMap.DiscLights);
785+
786+
Profiler.EndSample();
787+
}
788+
753789
// Build out masters for instancing.
754790
Profiler.BeginSample("USD: Build Instances");
755791
foreach (var masterRootPath in primMap.GetMasterRootPaths())

0 commit comments

Comments
 (0)