From f371932eea98268a247b062fd9845e9602d2a626 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 17 Sep 2021 20:40:19 +0900 Subject: [PATCH] TextureTransform.VerticalFlipScaleOffset --- .../UniGLTF/IO/MaterialIO/MaterialExporter.cs | 2 +- .../UniGLTF/IO/MaterialIO/TextureTransform.cs | 22 +++++++++++++++++++ .../IO/MaterialIO/TextureTransform.cs.meta | 11 ++++++++++ .../IO/TextureIO/GltfTextureImporter.cs | 4 +--- .../Samples/ScreenSpace/ScaleOffset.cs | 22 ++++++------------- 5 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs index 14a14cec3..551fc7957 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs @@ -206,7 +206,7 @@ namespace UniGLTF { var offset = m.GetTextureOffset(propertyName); var scale = m.GetTextureScale(propertyName); - offset.y = 1.0f - offset.y - scale.y; + (scale, offset) = TextureTransform.VerticalFlipScaleOffset(scale, offset); glTF_KHR_texture_transform.Serialize(textureInfo, (offset.x, offset.y), (scale.x, scale.y)); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs new file mode 100644 index 000000000..214a2b2ee --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs @@ -0,0 +1,22 @@ + + +using UnityEngine; + +namespace UniGLTF +{ + public static class TextureTransform + { + /// + // UV Coordinate Conversion: glTF(top-left origin) to Unity(bottom-left origin) + /// https://github.com/vrm-c/UniVRM/issues/930 + /// offset.y = 1.0f - offset.y - scale.y; + /// + /// + /// + /// + public static (Vector2, Vector2) VerticalFlipScaleOffset(Vector2 s, Vector2 o) + { + return (new Vector2(s.x, s.y), new Vector2(o.x, 1.0f - o.y - s.y)); + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs.meta new file mode 100644 index 000000000..2b967208c --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a652f0fdb4236434a9a64aa35a3cb44f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs index 5cc96bc8c..68a89cb87 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs @@ -118,9 +118,7 @@ namespace UniGLTF scale = new Vector2(textureTransform.scale[0], textureTransform.scale[1]); } - // UV Coordinate Conversion: glTF(top-left origin) to Unity(bottom-left origin) - // Formula: https://github.com/vrm-c/UniVRM/issues/930 - offset.y = 1.0f - offset.y - scale.y; + (scale, offset) = TextureTransform.VerticalFlipScaleOffset(scale, offset); } return (offset, scale); diff --git a/Assets/UniGLTF/Samples/ScreenSpace/ScaleOffset.cs b/Assets/UniGLTF/Samples/ScreenSpace/ScaleOffset.cs index 557882fb6..a3fc728e5 100644 --- a/Assets/UniGLTF/Samples/ScreenSpace/ScaleOffset.cs +++ b/Assets/UniGLTF/Samples/ScreenSpace/ScaleOffset.cs @@ -1,6 +1,4 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +using UnityEngine; namespace UniGLTF.Samples { @@ -34,23 +32,17 @@ namespace UniGLTF.Samples const string PROP_NAME = "_MainTex"; - /// - /// https://github.com/vrm-c/UniVRM/issues/930 - /// - /// - /// - /// - public static (Vector2, Vector2) ConvScaleOffset(Vector2 s, Vector2 o) - { - return (new Vector2(s.x, s.y), new Vector2(o.x, 1 - o.y - s.y)); - } - void Execute() { + if (Unity == null || Gltf == null) + { + return; + } + Unity.SetTextureScale(PROP_NAME, Scale); Unity.SetTextureOffset(PROP_NAME, Offset); - var (s, o) = ConvScaleOffset(Scale, Offset); + var (s, o) = TextureTransform.VerticalFlipScaleOffset(Scale, Offset); Gltf.SetTextureScale(PROP_NAME, s); Gltf.SetTextureOffset(PROP_NAME, o);