Skip to content
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

Stroke colors saved in materials for export #452

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
69 changes: 40 additions & 29 deletions Assets/Scripts/Export/ExportCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,39 +273,50 @@ static ExportUtils.GroupPayload BuildGroupPayload(SceneStatePayload payload,
GeometryPool geometry = batch.pool;
List<Stroke> strokes = batch.strokes;

string legacyUniqueName = $"{desc.m_DurableName}_{desc.m_Guid}_{group.id}_i{batchIndex}";
string friendlyGeometryName = $"brush_{desc.m_DurableName}_g{group.id}_b{batchIndex}";
int strokeIndex = 0;
foreach (var stroke in strokes)
{
Color32 color = geometry.m_Colors[geometry.m_Tris[stroke.m_BatchSubset.m_iTriIndex]];

UnityEngine.Profiling.Profiler.BeginSample("ConvertToMetersAndChangeBasis");
ExportUtils.ConvertUnitsAndChangeBasis(geometry, payload);
UnityEngine.Profiling.Profiler.EndSample();
string legacyUniqueName = $"{desc.m_DurableName}_{desc.m_Guid}_{group.id}_i{batchIndex}_s{strokeIndex}_c{color.r:X2}{color.g:X2}{color.b:X2}{color.a:X2}";
string friendlyGeometryName = $"brush_{desc.m_DurableName}_g{group.id}_b{batchIndex}_s{strokeIndex}_c{color.r:X2}{color.g:X2}{color.b:X2}{color.a:X2}";

if (payload.reverseWinding)
{
// Note: this triangle flip intentionally un-does the Unity FBX import flip.
ExportUtils.ReverseTriangleWinding(geometry, 1, 2);
}
UnityEngine.Profiling.Profiler.BeginSample("ConvertToMetersAndChangeBasis");
ExportUtils.ConvertUnitsAndChangeBasis(geometry, payload);
UnityEngine.Profiling.Profiler.EndSample();

if (App.PlatformConfig.EnableExportMemoryOptimization &&
payload.temporaryDirectory != null)
{
string filename = Path.Combine(
payload.temporaryDirectory,
legacyUniqueName + ".Gpoo");
geometry.MakeGeometryNotResident(filename);
if (payload.reverseWinding)
{
// Note: this triangle flip intentionally un-does the Unity FBX import flip.
ExportUtils.ReverseTriangleWinding(geometry, 1, 2);
}

if (App.PlatformConfig.EnableExportMemoryOptimization &&
payload.temporaryDirectory != null)
{
string filename = Path.Combine(
payload.temporaryDirectory,
legacyUniqueName + ".Gpoo");
geometry.MakeGeometryNotResident(filename);
}

List<Stroke> oneStroke = new List<Stroke>(1);
oneStroke.Add(stroke);
brush.m_desc.ColorParams["Color"] = new Color(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);

group.brushMeshes.Add(new ExportUtils.BrushMeshPayload(
payload.groupIdMapping.GetId(exportGroup.m_group))
{
legacyUniqueName = legacyUniqueName,
// This is the only instance of the mesh, so the node doesn't need an extra instance id
nodeName = friendlyGeometryName,
xform = Matrix4x4.identity,
geometry = geometry,
geometryName = friendlyGeometryName,
exportableMaterial = brush.m_desc,
strokes = oneStroke,
});
}
group.brushMeshes.Add(new ExportUtils.BrushMeshPayload(
payload.groupIdMapping.GetId(exportGroup.m_group))
{
legacyUniqueName = legacyUniqueName,
// This is the only instance of the mesh, so the node doesn't need an extra instance id
nodeName = friendlyGeometryName,
xform = Matrix4x4.identity,
geometry = geometry,
geometryName = friendlyGeometryName,
exportableMaterial = brush.m_desc,
strokes = strokes,
});
}
}
return group;
Expand Down
12 changes: 7 additions & 5 deletions Assets/Scripts/Export/ExportFbx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ public FbxSurfaceMaterial GetOrCreateFbxMaterial(
// Since TBT can't detect reference-equality of FbxMaterial, we have to help it by
// making name-equality the same as reference-equality. IOW distinct materials need
// distinct names.
if (m_createdMaterials.TryGetValue(exportableMaterial, out FbxSurfaceMaterial mtl)) {
return mtl;
} else {
// if (m_createdMaterials.TryGetValue(exportableMaterial, out FbxSurfaceMaterial mtl)) {
// return mtl;
// } else {
FbxSurfaceMaterial newMtl = ExportFbx.CreateFbxMaterial(
this, meshNamespace, exportableMaterial, m_createdMaterialNames);
m_createdMaterials[exportableMaterial] = newMtl;
return newMtl;
}
// }
}
}

Expand Down Expand Up @@ -260,7 +260,9 @@ internal static FbxSurfaceMaterial CreateFbxMaterial(
FbxSurfaceLambert material = FbxSurfaceLambert.Create(G.m_scene, materialName);

material.Ambient.Set(new FbxDouble3(0, 0, 0));
material.Diffuse.Set(new FbxDouble3(1.0, 1.0, 1.0));
Color color = Color.white;
exportableMaterial.ColorParams.TryGetValue("Color", out color);
material.Diffuse.Set(new FbxDouble3(color.r, color.g, color.b));
if (exportableMaterial.EmissiveFactor > 0) {
material.EmissiveFactor.Set(exportableMaterial.EmissiveFactor);
material.Emissive.Set(new FbxDouble3(1.0, 1.0, 1.0));
Expand Down