Skip to content

Commit

Permalink
Merge pull request #1234 from ousttrue/fix10/convert_TextureTransform
Browse files Browse the repository at this point in the history
[1.0] TextureTransformBind の Scale/Offset 変換
  • Loading branch information
ousttrue authored Sep 21, 2021
2 parents 8507a3b + b147b06 commit 024dadf
Show file tree
Hide file tree
Showing 23 changed files with 1,136 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static void Export_TextureTransform(Material m, glTFTextureInfo textureInfo, str
{
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));
}
Expand Down
22 changes: 22 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/TextureTransform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


using UnityEngine;

namespace UniGLTF
{
public static class TextureTransform
{
/// <summary>
// 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;
/// </summary>
/// <param name="s"></param>
/// <param name="o"></param>
/// <returns></returns>
public static (Vector2 Scale, Vector2 Offset) VerticalFlipScaleOffset(Vector2 s, Vector2 o)
{
return (new Vector2(s.x, s.y), new Vector2(o.x, 1.0f - o.y - s.y));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ public static (Vector2, Vector2) GetTextureOffsetAndScale(glTF_KHR_texture_trans
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);
Expand Down
8 changes: 8 additions & 0 deletions Assets/UniGLTF/Samples.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/UniGLTF/Samples/ScreenSpace.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions Assets/UniGLTF/Samples/ScreenSpace/ScaleOffset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using UnityEngine;

namespace UniGLTF.Samples
{
/// <summary>
/// ScaleOffset の検証
/// </summary>
public class ScaleOffset : MonoBehaviour
{
[SerializeField]
public Vector2 Scale = Vector2.one;

[SerializeField]
public Vector2 Offset = Vector2.zero;

[SerializeField]
Material Unity;

[SerializeField]
Material Gltf;

void OnValidate()
{
Execute();
}

// Update is called once per frame
void Update()
{
Execute();
}

const string PROP_NAME = "_MainTex";

void Execute()
{
if (Unity == null || Gltf == null)
{
return;
}

Unity.SetTextureScale(PROP_NAME, Scale);
Unity.SetTextureOffset(PROP_NAME, Offset);

var (s, o) = TextureTransform.VerticalFlipScaleOffset(Scale, Offset);

Gltf.SetTextureScale(PROP_NAME, s);
Gltf.SetTextureOffset(PROP_NAME, o);
}
}
}
11 changes: 11 additions & 0 deletions Assets/UniGLTF/Samples/ScreenSpace/ScaleOffset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 024dadf

Please sign in to comment.