Skip to content

Commit

Permalink
fixed missing extexturetransform on textures within extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
pfcDorn committed Aug 11, 2023
1 parent 06077d4 commit 95c1007
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@

namespace GLTF.Extensions
{

public static class JsonWriterExtensions
{
public static void WriteTexture(this JObject token, string name, TextureInfo texture)
{
var jsonWriter = token.CreateWriter();
jsonWriter.WritePropertyName(name);
texture.Serialize(jsonWriter);
}
}

public static class JsonReaderExtensions
{
public static List<string> ReadStringList(this JsonReader reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,17 @@ public JProperty Serialize()

if (clearcoatFactor != 0)
jo.Add(new JProperty(nameof(clearcoatFactor), clearcoatFactor));
if (clearcoatTexture != null) {
jo.Add(new JProperty(nameof(clearcoatTexture),
new JObject(
new JProperty(TextureInfo.INDEX, clearcoatTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, clearcoatTexture.TexCoord)
)
)
);
}
if (clearcoatTexture != null)
jo.WriteTexture(nameof(clearcoatTexture), clearcoatTexture);

if (clearcoatRoughnessFactor != 0)
jo.Add(new JProperty(nameof(clearcoatRoughnessFactor), clearcoatRoughnessFactor));
if (clearcoatRoughnessTexture != null) {
jo.Add(new JProperty(nameof(clearcoatRoughnessTexture),
new JObject(
new JProperty(TextureInfo.INDEX, clearcoatRoughnessTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, clearcoatRoughnessTexture.TexCoord)
)
)
);
}
if (clearcoatNormalTexture != null) {
jo.Add(new JProperty(nameof(clearcoatNormalTexture),
new JObject(
new JProperty(TextureInfo.INDEX, clearcoatNormalTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, clearcoatNormalTexture.TexCoord)
// new JProperty(NormalTextureInfo.SCALE, clearcoatNormalTexture.Scale) // TODO needs better normal scale export, uses only the base normal right now
)
)
);
}
if (clearcoatRoughnessTexture != null)
jo.WriteTexture(nameof(clearcoatRoughnessTexture), clearcoatRoughnessTexture);

if (clearcoatNormalTexture != null)
jo.WriteTexture(nameof(clearcoatNormalTexture), clearcoatNormalTexture);

return jProperty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,12 @@ public JProperty Serialize()
if (iridescenceIor != 1.3f) jo.Add(new JProperty(nameof(iridescenceIor), iridescenceIor));
if (iridescenceThicknessMinimum != 100.0f) jo.Add(new JProperty(nameof(iridescenceThicknessMinimum), iridescenceThicknessMinimum));
if (iridescenceThicknessMaximum != 400.0f) jo.Add(new JProperty(nameof(iridescenceThicknessMaximum), iridescenceThicknessMaximum));
if (iridescenceTexture != null) {
jo.Add(new JProperty(nameof(iridescenceTexture),
new JObject(
new JProperty(TextureInfo.INDEX, iridescenceTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, iridescenceTexture.TexCoord)
)
)
);
}
if (iridescenceThicknessTexture != null) {
jo.Add(new JProperty(nameof(iridescenceThicknessTexture),
new JObject(
new JProperty(TextureInfo.INDEX, iridescenceThicknessTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, iridescenceThicknessTexture.TexCoord)
)
)
);
}
if (iridescenceTexture != null)
jo.WriteTexture(nameof(iridescenceTexture), iridescenceTexture);

if (iridescenceThicknessTexture != null)
jo.WriteTexture(nameof(iridescenceThicknessTexture), iridescenceThicknessTexture);

return jProperty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,12 @@ public JProperty Serialize()

if (specularFactor != 1) jo.Add(new JProperty(nameof(specularFactor), specularFactor));
if (specularColorFactor != COLOR_DEFAULT) jo.Add(new JProperty(nameof(specularColorFactor), new JArray(specularColorFactor.R, specularColorFactor.G, specularColorFactor.B)));
if (specularTexture != null) {
jo.Add(new JProperty(nameof(specularTexture),
new JObject(
new JProperty(TextureInfo.INDEX, specularTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, specularTexture.TexCoord)
)
)
);
}
if (specularColorTexture != null) {
jo.Add(new JProperty(nameof(specularColorTexture),
new JObject(
new JProperty(TextureInfo.INDEX, specularColorTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, specularColorTexture.TexCoord)
)
)
);
}
if (specularTexture != null)
jo.WriteTexture(nameof(specularTexture), specularTexture);

if (specularColorTexture != null)
jo.WriteTexture(nameof(specularColorTexture), specularColorTexture);

return jProperty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ public JProperty Serialize()
var jo = new JObject();
if (transmissionFactor != 0) jo.Add(new JProperty(nameof(transmissionFactor), transmissionFactor));
if (transmissionTexture != null)
jo.Add(new JProperty(nameof(transmissionTexture),
new JObject(
new JProperty(TextureInfo.INDEX, transmissionTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, transmissionTexture.TexCoord) // TODO don't write if default
)
)
);
jo.WriteTexture(nameof(transmissionTexture), transmissionTexture);

JProperty jProperty = new JProperty(KHR_materials_transmission_Factory.EXTENSION_NAME, jo);
return jProperty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,9 @@ public JProperty Serialize()
jo.Add(new JProperty(nameof(attenuationDistance), attenuationDistance));
}
if (attenuationColor != COLOR_DEFAULT) jo.Add(new JProperty(nameof(attenuationColor), new JArray(attenuationColor.R, attenuationColor.G, attenuationColor.B)));
if (thicknessTexture != null) {
jo.Add(new JProperty(nameof(thicknessTexture),
new JObject(
new JProperty(TextureInfo.INDEX, thicknessTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, thicknessTexture.TexCoord) // TODO don't write if default
)
)
);
}
if (thicknessTexture != null)
jo.WriteTexture(nameof(thicknessTexture), thicknessTexture);

return jProperty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exp
if (material.HasProperty(transmissionFactor))
vt.transmissionFactor = material.GetFloat(transmissionFactor);
if (material.HasProperty(transmissionTexture) && material.GetTexture(transmissionTexture))
vt.transmissionTexture = exporter.ExportTextureInfo(material.GetTexture(transmissionTexture), nameof(transmissionTexture));
vt.transmissionTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(transmissionTexture), nameof(transmissionTexture));
}

if (usesVolume)
Expand All @@ -110,7 +110,7 @@ private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exp
if (material.HasProperty(thicknessFactor))
ve.thicknessFactor = material.GetFloat(thicknessFactor);
if (material.HasProperty(thicknessTexture) && material.GetTexture(thicknessTexture))
ve.thicknessTexture = exporter.ExportTextureInfo(material.GetTexture(thicknessTexture), nameof(thicknessTexture));
ve.thicknessTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(thicknessTexture), nameof(thicknessTexture));
if (material.HasProperty(attenuationDistance))
ve.attenuationDistance = material.GetFloat(attenuationDistance);
if (material.HasProperty(attenuationColor))
Expand Down Expand Up @@ -140,9 +140,9 @@ private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exp
if (material.HasProperty(iridescenceThicknessMaximum))
vir.iridescenceThicknessMaximum = material.GetFloat(iridescenceThicknessMaximum);
if (material.HasProperty(iridescenceTexture) && material.GetTexture(iridescenceTexture))
vir.iridescenceTexture = exporter.ExportTextureInfo(material.GetTexture(iridescenceTexture), nameof(iridescenceTexture));
vir.iridescenceTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(iridescenceTexture), nameof(iridescenceTexture));
if (material.HasProperty(iridescenceThicknessTexture) && material.GetTexture(iridescenceThicknessTexture))
vir.iridescenceThicknessTexture = exporter.ExportTextureInfo(material.GetTexture(iridescenceThicknessTexture), nameof(iridescenceThicknessTexture));
vir.iridescenceThicknessTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(iridescenceThicknessTexture), nameof(iridescenceThicknessTexture));
}

if (material.IsKeywordEnabled("_SPECULAR_ON"))
Expand All @@ -164,9 +164,9 @@ private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exp
if (material.HasProperty(specularColorFactor))
vir.specularColorFactor = material.GetColor(specularColorFactor).ToNumericsColorRaw();
if (material.HasProperty(specularTexture) && material.GetTexture(specularTexture))
vir.specularTexture = exporter.ExportTextureInfo(material.GetTexture(specularTexture), nameof(specularTexture));
vir.specularTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(specularTexture), nameof(specularTexture));
if (material.HasProperty(specularColorTexture) && material.GetTexture(specularColorTexture))
vir.specularColorTexture = exporter.ExportTextureInfo(material.GetTexture(specularColorTexture), nameof(specularColorTexture));
vir.specularColorTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(specularColorTexture), nameof(specularColorTexture));
}

if (material.IsKeywordEnabled("_CLEARCOAT_ON"))
Expand All @@ -186,13 +186,13 @@ private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exp
if (material.HasProperty(clearcoatFactor))
cc.clearcoatFactor = material.GetFloat(clearcoatFactor);
if (material.HasProperty(clearcoatTexture))
cc.clearcoatTexture = exporter.ExportTextureInfo(material.GetTexture(clearcoatTexture), nameof(clearcoatTexture));
cc.clearcoatTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(clearcoatTexture), nameof(clearcoatTexture));
if (material.HasProperty(clearcoatRoughnessFactor))
cc.clearcoatRoughnessFactor = material.GetFloat(clearcoatRoughnessFactor);
if (material.HasProperty(clearcoatRoughnessTexture))
cc.clearcoatRoughnessTexture = exporter.ExportTextureInfo(material.GetTexture(clearcoatRoughnessTexture), nameof(clearcoatRoughnessTexture));
cc.clearcoatRoughnessTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(clearcoatRoughnessTexture), nameof(clearcoatRoughnessTexture));
if (material.HasProperty(clearcoatNormalTexture))
cc.clearcoatNormalTexture = exporter.ExportTextureInfo(material.GetTexture(clearcoatNormalTexture), nameof(clearcoatNormalTexture));
cc.clearcoatNormalTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(clearcoatNormalTexture), nameof(clearcoatNormalTexture));


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ public TextureInfo ExportTextureInfo(Texture texture, string textureSlot, Textur
return info;
}

public TextureInfo ExportTextureInfoWithTextureTransform(Material mat, Texture texture, string textureSlot,
TextureExportSettings exportSettings = default)
{
var tInfo = ExportTextureInfo(texture, textureSlot, exportSettings);
if (texture != null)
ExportTextureTransform(tInfo, mat, textureSlot);
return tInfo;
}

public TextureId ExportTexture(Texture textureObj, string textureSlot, TextureExportSettings exportSettings = default)
{
var uniqueTexture = exportSettings.isValid ?
Expand Down

0 comments on commit 95c1007

Please sign in to comment.