Skip to content

Commit

Permalink
Extract metadata from imported file
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Jan 23, 2024
1 parent fe76e7c commit 8ed14fc
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
19 changes: 19 additions & 0 deletions Packages/open-brush-unity-tools/Runtime/Scripts/BatchMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine.Serialization;

public class BatchMetadata : UnityEngine.MonoBehaviour
{
[Serializable]
public class SubsetMetadata
{
public uint m_HeadTimestampMs;
public uint m_TailTimestampMs;
public uint m_StartVertIndex;
public uint m_VertLength;
public uint m_Group;
}

public List<SubsetMetadata> m_Subsets;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 44 additions & 3 deletions Packages/open-brush-unity-tools/Runtime/Scripts/ObImportPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using UnityGLTF.Plugins;
using System;
using System.Collections.Generic;
using GLTF.Extensions;
using UnityGLTF.Plugins;
using GLTF.Schema;
using Newtonsoft.Json.Linq;
using UnityEngine;

namespace OpenBrushUnityTools
Expand All @@ -17,14 +21,51 @@ public override GLTFImportPluginContext CreateInstance(GLTFImportContext context

public class ObImportExtensionConfig : GLTFImportPluginContext
{

private MaterialRemapping m_MaterialDictionary;

public override void OnBeforeImport()
{
m_MaterialDictionary = Resources.Load<MaterialRemapping>("MaterialRemapping");
}

public override void OnAfterImportNode(Node node, int nodeIndex, GameObject nodeObject)
{
var materialDictionary = Resources.Load<MaterialRemapping>("MaterialRemapping");
var strokeJson = node?.Mesh?.Value?.Extras?["ICOSA_strokeInfo"];
if (strokeJson != null)
{
var reader = strokeJson.CreateReader();
var strokeInfo = reader.ReadAsDictionary(() => reader.ReadAsString());
var metadata = nodeObject.AddComponent<StrokeMetadata>();
UInt32.TryParse(strokeInfo["HeadTimestampMs"], out metadata.m_HeadTimestampMs);
UInt32.TryParse(strokeInfo["TailTimestampMs"], out metadata.m_TailTimestampMs);
}
else
{
// Is there ever more than one primitive that could contain metadata?
var batchJson = node?.Mesh?.Value?.Primitives[0].Extras?["ICOSA_batchInfo"];
if (batchJson != null)
{
var metadata = nodeObject.AddComponent<BatchMetadata>();
metadata.m_Subsets = new List<BatchMetadata.SubsetMetadata>();
foreach (var metadataItem in batchJson)
{
var subsetMetadata = new BatchMetadata.SubsetMetadata();
UInt32.TryParse(metadataItem["HeadTimestampMs"].Value<string>(), out subsetMetadata.m_HeadTimestampMs);
UInt32.TryParse(metadataItem["TailTimestampMs"].Value<string>(), out subsetMetadata.m_TailTimestampMs);
UInt32.TryParse(metadataItem["StartVertIndex"].Value<string>(), out subsetMetadata.m_StartVertIndex);
UInt32.TryParse(metadataItem["VertLength"].Value<string>(), out subsetMetadata.m_VertLength);
UInt32.TryParse(metadataItem["Group"].Value<string>(), out subsetMetadata.m_Group);
metadata.m_Subsets.Add(subsetMetadata);
}
}
}

var mr = nodeObject.GetComponent<MeshRenderer>();
if (mr != null)
{
var existingMaterialName = mr.sharedMaterial.name.Replace("(Instance)", "").Trim();
var mat = materialDictionary.GetMaterial(existingMaterialName);
var mat = m_MaterialDictionary.GetMaterial(existingMaterialName);
if (mat != null)
{
mr.sharedMaterial = mat;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Globalization;
using UnityEngine.Serialization;

public class StrokeMetadata : UnityEngine.MonoBehaviour
{
public uint m_HeadTimestampMs;
public uint m_TailTimestampMs;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8ed14fc

Please sign in to comment.