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

Change flip texture coordinates with flip texture #562

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 49 additions & 4 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,17 @@ public void SaveGLTFandBin(string path, string fileName)
binFile.Close();
#endif
ExportImages(path);
}

private Texture2D FlipTexture(Texture source, Texture2D flipped, RenderTextureFormat format, RenderTextureReadWrite readWrite)
{
var flippedRenderTexture = RenderTexture.GetTemporary(source.width, source.height, 0, format, readWrite);
Graphics.Blit(source, flippedRenderTexture, new Vector2(1.0f, -1.0f), new Vector2(0.0f, 1.0f));
flipped.name = source.name;
flipped.ReadPixels(new Rect(0, 0, flippedRenderTexture.width, flippedRenderTexture.height), 0, 0);
flipped.Apply();
RenderTexture.ReleaseTemporary(flippedRenderTexture);
return flipped;
}

private void ExportImages(string outputPath)
Expand Down Expand Up @@ -349,6 +359,8 @@ private void ExportImages(string outputPath)
/// <param name="outputPath">The location to export the texture</param>
private void ExportMetallicGlossTexture(Texture2D texture, string outputPath)
{
var flipped = new Texture2D(texture.width, texture.height);
texture = FlipTexture(texture, flipped, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
var destRenderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);

Graphics.Blit(texture, destRenderTexture, _metalGlossChannelSwapMaterial);
Expand All @@ -364,10 +376,12 @@ private void ExportMetallicGlossTexture(Texture2D texture, string outputPath)
if (Application.isEditor)
{
GameObject.DestroyImmediate(exportTexture);
GameObject.DestroyImmediate(flipped);
}
else
{
GameObject.Destroy(exportTexture);
GameObject.Destroy(flipped);
}
}

Expand All @@ -379,6 +393,8 @@ private void ExportMetallicGlossTexture(Texture2D texture, string outputPath)
/// <param name="outputPath">The location to export the texture</param>
private void ExportNormalTexture(Texture2D texture, string outputPath)
{
var flipped = new Texture2D(texture.width, texture.height);
texture = FlipTexture(texture, flipped, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
var destRenderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);

Graphics.Blit(texture, destRenderTexture, _normalChannelMaterial);
Expand All @@ -394,15 +410,19 @@ private void ExportNormalTexture(Texture2D texture, string outputPath)
if (Application.isEditor)
{
GameObject.DestroyImmediate(exportTexture);
GameObject.DestroyImmediate(flipped);
}
else
{
GameObject.Destroy(exportTexture);
GameObject.Destroy(flipped);
}
}

private void ExportTexture(Texture2D texture, string outputPath)
{
var flipped = new Texture2D(texture.width, texture.height);
texture = FlipTexture(texture, flipped, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
var destRenderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);

Graphics.Blit(texture, destRenderTexture);
Expand All @@ -418,10 +438,12 @@ private void ExportTexture(Texture2D texture, string outputPath)
if (Application.isEditor)
{
GameObject.DestroyImmediate(exportTexture);
GameObject.DestroyImmediate(flipped);
}
else
{
GameObject.Destroy(exportTexture);
GameObject.Destroy(flipped);
}
}

Expand Down Expand Up @@ -750,7 +772,8 @@ private MeshPrimitive[] ExportPrimitive(GameObject gameObject, GLTFMesh mesh)
}

AccessorId aPosition = null, aNormal = null, aTangent = null,
aTexcoord0 = null, aTexcoord1 = null, aColor0 = null;
aTexcoord0 = null, aTexcoord1 = null, aTexcoord2 = null, aTexcoord3 = null,
aColor0 = null, aJoint0 = null, aWeight0 = null;

aPosition = ExportAccessor(SchemaExtensions.ConvertVector3CoordinateSpaceAndCopy(meshObj.vertices, SchemaExtensions.CoordinateSpaceConversionScale));

Expand All @@ -760,12 +783,30 @@ private MeshPrimitive[] ExportPrimitive(GameObject gameObject, GLTFMesh mesh)
if (meshObj.tangents.Length != 0)
aTangent = ExportAccessor(SchemaExtensions.ConvertVector4CoordinateSpaceAndCopy(meshObj.tangents, SchemaExtensions.TangentSpaceConversionScale));

// Better not to flip the texture coordinates. If uv* represents something else other than coordinates, we cannot simply perform 1 - uv*.y on it.
//if (meshObj.uv.Length != 0)
// aTexcoord0 = ExportAccessor(SchemaExtensions.FlipTexCoordArrayVAndCopy(meshObj.uv));

//if (meshObj.uv2.Length != 0)
// aTexcoord1 = ExportAccessor(SchemaExtensions.FlipTexCoordArrayVAndCopy(meshObj.uv2));

//if (meshObj.uv3.Length != 0)
// aTexcoord2 = ExportAccessor(SchemaExtensions.FlipTexCoordArrayVAndCopy(meshObj.uv3));

//if (meshObj.uv4.Length != 0)
// aTexcoord3 = ExportAccessor(SchemaExtensions.FlipTexCoordArrayVAndCopy(meshObj.uv4));

if (meshObj.uv.Length != 0)
aTexcoord0 = ExportAccessor(SchemaExtensions.FlipTexCoordArrayVAndCopy(meshObj.uv));
aTexcoord0 = ExportAccessor(meshObj.uv);

if (meshObj.uv2.Length != 0)
aTexcoord1 = ExportAccessor(SchemaExtensions.FlipTexCoordArrayVAndCopy(meshObj.uv2));
aTexcoord1 = ExportAccessor(meshObj.uv2);

if (meshObj.uv3.Length != 0)
aTexcoord2 = ExportAccessor(meshObj.uv3);

if (meshObj.uv4.Length != 0)
aTexcoord3 = ExportAccessor(meshObj.uv4);
if (meshObj.colors.Length != 0)
aColor0 = ExportAccessor(meshObj.colors);

Expand Down Expand Up @@ -793,6 +834,10 @@ private MeshPrimitive[] ExportPrimitive(GameObject gameObject, GLTFMesh mesh)
primitive.Attributes.Add(SemanticProperties.TEXCOORD_0, aTexcoord0);
if (aTexcoord1 != null)
primitive.Attributes.Add(SemanticProperties.TEXCOORD_1, aTexcoord1);
if (aTexcoord2 != null)
primitive.Attributes.Add(SemanticProperties.TEXCOORD_2, aTexcoord2);
if (aTexcoord3 != null)
primitive.Attributes.Add(SemanticProperties.TEXCOORD_3, aTexcoord3);
if (aColor0 != null)
primitive.Attributes.Add(SemanticProperties.COLOR_0, aColor0);

Expand Down Expand Up @@ -1033,7 +1078,7 @@ private void ExportTextureTransform(TextureInfo def, Material mat, string texNam
def.Extensions = new Dictionary<string, IExtension>();

def.Extensions[ExtTextureTransformExtensionFactory.EXTENSION_NAME] = new ExtTextureTransformExtension(
new GLTF.Math.Vector2(offset.x, -offset.y),
new GLTF.Math.Vector2(offset.x, offset.y),
0, // TODO: support rotation
new GLTF.Math.Vector2(scale.x, scale.y),
0 // TODO: support UV channels
Expand Down