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

Found fix for texture exporting #134

Closed
wants to merge 10 commits into from
Closed

Found fix for texture exporting #134

wants to merge 10 commits into from

Conversation

ziedbha
Copy link
Contributor

@ziedbha ziedbha commented Feb 27, 2018

I found a fix for the texture exporting, specifically for the bug that had the baseColor be too bright or too dark. The idea is to disable/enable sRGB conversion during the blit operation for each image exported. This is a test run and seems to work, but the most important change I'm working on is to distinguish between Metallic and Base Color (need separate treatment for sRGB for each).

Copy link
Contributor

@stevenvergenz stevenvergenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to the project!

@@ -96,18 +96,21 @@ public void SaveGLTFandBin(string path, string fileName)
gltfFile.Close();
binFile.Close();
#endif

GL.sRGBWrite = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find! Though really, these two lines should be the only content of this PR. None of the other changes should be included, please strip them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I wanted to make a PR ASAP to people can look it at it, but I am planning on making a cleaner one as soon as possible, with only the requested changes included. Expect one within this week :)

@ziedbha
Copy link
Contributor Author

ziedbha commented Mar 5, 2018

Now my fork shouldn't have conflicts with the base repo. I only edited the Exporter script. Please try exporting some models and see if the color space problem is still there.

FYI, these are the texture types that are in sRGB/Gamma space:
Emissive, BaseCoolor, Occlusion
Please let me know if that's not correct.

@ziedbha
Copy link
Contributor Author

ziedbha commented Mar 5, 2018

@justinctlam I also liked the idea you had in the PR. My fix here only addresses Color space issues. So the whole idea of the ImageWrapper doesn't need to be there, as long as we can specify what textures are in what color space, we should be fine

@justinctlam
Copy link
Contributor

@ziedbha Sounds good : )

@ziedbha
Copy link
Contributor Author

ziedbha commented Mar 6, 2018

@justinctlam checkout the newest version of this PR. I integrated changes in commit d72c74a

enum TextureMapType
{
//sRGB
Main,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got some odd spacing issues in your changes. I think this project uses tabs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird, I'll fix that!

@@ -1 +1 @@
m_EditorVersion: 5.6.5f1
m_EditorVersion: 5.6.1f1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably revert this. It's going back in version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was back in the first PR I made, which wasn't organized at all. My bad. There is probably more stuff to revert (only a couple of scripts should be edited...)


namespace UnityGLTF
{
public class GLTFSceneExporter
{
enum TextureMapType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add a Boolean that indicates whether the image is sRGB or not instead of a type and then a switch to determine it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was part of @justinctlam's changes. I just didn't want to add more to the code, but I can definitely do that. Thanks for pointing that out!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow how my changes are related to sRGB?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do not, which is why I added it. We need to export textures in the proper color space, or they won't look right.

exportTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
exportTexture.Apply();
File.WriteAllBytes(Path.Combine(path, image.name + ".png"), exportTexture.EncodeToPNG());
private void exportImages(string outputPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor coding convention issue: functions are all supposed to be upper camel case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks!

}

if (material.HasProperty("_Roughness"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newer versions of Unity uses _Roughness instead. I think you will break that code if you make this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what happened there, will fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: since I integrated PR #133 with this one, it pulled changes in that PR too. I think @justinctlam might have some explanation as to why he took out the _Roughness factor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which Unity uses _Roughness? I am on Unity 2017.3.1f1. I looked at the Standard.shader and StandardRoughness.shader and I don't see the property. Can you point me to it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. This is my mistake. So, this never worked before then, right?

@ziedbha
Copy link
Contributor Author

ziedbha commented Mar 8, 2018

I tried exporting with this PR that includes @justinctlam changes and my changes, and these are the results I am getting. I attached an exported BoomBox, Lantern, and Corset, which are 1-1 with the original models.

boombox.zip
corset.zip
lantern.zip

@bghgary
Copy link
Contributor

bghgary commented Mar 8, 2018

What happened to setting GL.sRGBWrite?

@ziedbha
Copy link
Contributor Author

ziedbha commented Mar 8, 2018

For some reason, removing the Apply() line after the ReadPixels() line fixed it. After removing that line, I noticed that the GL.sRGBWrite line didn't matter anymore. I'm still trying to understand why, but this version gave me the correct results.

EDIT: It might be because of the updateMipmaps argument, which defaults to true. https://docs.unity3d.com/ScriptReference/Texture2D.Apply.html

@ziedbha
Copy link
Contributor Author

ziedbha commented Mar 11, 2018

Put this on hold until Monday, I found a bug I need to fix!

@blgrossMS
Copy link

Let me know where you are ready for this to get checked in. Also how your work relate to the work that is being done in PR #133 ?

@ziedbha
Copy link
Contributor Author

ziedbha commented Mar 11, 2018

@blgrossMS will ping you when it's ready! The changes are simply to use the Blit method we have right now to export the images out. PR #133 did not use that method (and needed many passes on the pixels), so it was slower.

@ziedbha
Copy link
Contributor Author

ziedbha commented Mar 29, 2018

@blgrossMS you can ignore this PR. However, please check PR #152

@blgrossMS blgrossMS closed this Apr 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants