prefrontalcortex / UnityGLTF

Runtime GLTF Loader for Unity3D
MIT License
163 stars 41 forks source link

fix: don't invert glossmap scale with map #66

Closed gordonnl closed 2 years ago

gordonnl commented 2 years ago

Problem: When using Unity's Standard shader with a Metallic map and Smoothness data in the Alpha channel, and a Smoothness multiplier of 1, upon export the Smoothness multiplier (_GlossMapScale) is inverted, resulting in a GLTF with a roughness multiplier value of 0. Resulting in an entirely smooth material.

Screen Shot 2022-11-16 at 19 48 08

Screen Shot 2022-11-16 at 19 47 50

Proposed solution: Don't invert the multiplier when a map is attached. If there is no map, then the smoothness should indeed continue to be inverted.

Possible issue: Majority of the time when using a gloss map, the multiplier would be 1, however in cases it is less than 1, as the smoothness data in the map is already inverted, the multiplier should be converted with 1.0 / _GlossMapScale. Eg if the pixel smoothness data is 0.6, and the multiplier is 0.8, the smoothness value would be 0.48. Therefore the desired roughness for that pixel would be 0.52. As the pixel data is inverted, giving 0.4, a multiplier of 1.25 would be required to achieve the desired value of 0.52. Two issues with this, the potential of dividing by 0, and that Unity doesn't support values above 1.

hybridherbst commented 2 years ago

Hey @gordonnl , I believe this is already fixed on latest dev, mind trying that and potentially rebasing there? Thanks!

The actual fix was more complex I think, I decided to actually bake smoothness in since I don't think there's a real conversion from roughness = (1 - (smoothnessFactor * smoothnessMap)) to roughness = roughnessFactor * roughnessMap

gordonnl commented 2 years ago

Oh amazing, I'm glad I asked - I didn't realise dev was so far ahead! Thanks for the prompt reply, and that sounds like a great solution to me.