Skip to content

Commit

Permalink
wip for materials only import, not pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Jul 13, 2023
1 parent b026b3f commit deaa017
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
26 changes: 17 additions & 9 deletions UnityGLTF/Assets/UnityGLTF/Editor/Scripts/GLTFImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ string GetUniqueName(string desiredName)

try
{
CreateGLTFScene(context, out gltfScene, out animations);
CreateGLTFScene(context, out gltfScene, out animations, out var gltfRoot);
var rootGltfComponent = gltfScene.GetComponent<InstantiatedGLTFObject>();
if (rootGltfComponent) DestroyImmediate(rootGltfComponent);

Expand Down Expand Up @@ -408,6 +408,12 @@ string GetUniqueName(string desiredName)
});
}).Distinct().ToArray();

if (renderers.Length == 0)
{
// add materials directly from glTF
// materials = gltfRoot.Materials
}

// apply material remap
foreach(var r in renderers)
{
Expand Down Expand Up @@ -595,10 +601,11 @@ private static void UpdateColorSpace()
}
#endif

private void CreateGLTFScene(GLTFImportContext context, out GameObject scene, out AnimationClip[] animationClips)
private void CreateGLTFScene(GLTFImportContext context, out GameObject scene, out AnimationClip[] animationClips, out GLTFRoot root)
{
var projectFilePath = context.AssetContext.assetPath;
// TODO: replace with GltFImportContext

// TODO: replace with GltfImportContext
var importOptions = new ImportOptions
{
DataLoader = new FileLoader(Path.GetDirectoryName(projectFilePath)),
Expand All @@ -607,27 +614,27 @@ private void CreateGLTFScene(GLTFImportContext context, out GameObject scene, ou
AnimationLoopPose = _animationLoopPose,
ImportContext = context
};

using (var stream = File.OpenRead(projectFilePath))
{
GLTFRoot gLTFRoot;
GLTFParser.ParseJson(stream, out gLTFRoot);
GLTFParser.ParseJson(stream, out var gltfRoot);
stream.Position = 0; // Make sure the read position is changed back to the beginning of the file
var loader = new GLTFSceneImporter(gLTFRoot, stream, importOptions);
var loader = new GLTFSceneImporter(gltfRoot, stream, importOptions);
loader.MaximumLod = _maximumLod;
loader.IsMultithreaded = true;

// Need to call with RunSync, otherwise the draco loader will freeze the editor
AsyncHelpers.RunSync( () => loader.LoadSceneAsync());

if (gLTFRoot.ExtensionsUsed != null)
if (gltfRoot.ExtensionsUsed != null)
{
_extensions = gLTFRoot.ExtensionsUsed
_extensions = gltfRoot.ExtensionsUsed
.Select(x => new ExtensionInfo()
{
name = x,
supported = true,
used = true,
required = gLTFRoot.ExtensionsRequired?.Contains(x) ?? false,
required = gltfRoot.ExtensionsRequired?.Contains(x) ?? false,
})
.ToList();
}
Expand All @@ -643,6 +650,7 @@ private void CreateGLTFScene(GLTFImportContext context, out GameObject scene, ou

scene = loader.LastLoadedScene;
animationClips = loader.CreatedAnimationClips;
root = gltfRoot;
}
}

Expand Down
17 changes: 13 additions & 4 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ private void InitializeGltfTopLevelObject()
);
}

private GameObject NoSceneRoot;

/// <summary>
/// Creates a scene based off loaded JSON. Includes loading in binary and image data to construct the meshes required.
/// </summary>
Expand All @@ -565,7 +567,7 @@ protected async Task _LoadScene(int sceneIndex = -1, bool showSceneObj = true, C

if (scene == null)
{
throw new GLTFLoadException("No default scene in gltf file.");
// throw new GLTFLoadException("No default scene in gltf file.");
}

try
Expand All @@ -578,7 +580,7 @@ protected async Task _LoadScene(int sceneIndex = -1, bool showSceneObj = true, C
Debug.LogException(e);
}

GetGtlfContentTotals(scene);
GetGltfContentTotals(scene);

await ConstructScene(scene, showSceneObj, cancellationToken);

Expand All @@ -600,13 +602,13 @@ protected async Task _LoadScene(int sceneIndex = -1, bool showSceneObj = true, C
}
}

private void GetGtlfContentTotals(GLTFScene scene)
private void GetGltfContentTotals(GLTFScene scene)
{
// Count Nodes
Queue<NodeId> nodeQueue = new Queue<NodeId>();

// Add scene nodes
if (scene.Nodes != null)
if (scene != null && scene.Nodes != null)
{
for (int i = 0; i < scene.Nodes.Count; ++i)
{
Expand Down Expand Up @@ -909,6 +911,13 @@ protected async Task ConstructBuffer(GLTFBuffer buffer, int bufferIndex)

protected virtual async Task ConstructScene(GLTFScene scene, bool showSceneObj, CancellationToken cancellationToken)
{
if (scene == null)
{
if (!NoSceneRoot) NoSceneRoot = new GameObject("__NoSceneRoot");
CreatedObject = NoSceneRoot;
return;
}

var sceneObj = new GameObject(string.IsNullOrEmpty(scene.Name) ? ("Scene") : scene.Name);

try
Expand Down

0 comments on commit deaa017

Please sign in to comment.