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

[1.0] TextureTransformBind の Scale/Offset 変換 #1234

Merged
merged 5 commits into from
Sep 21, 2021
Merged
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -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));
}
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, Vector2) VerticalFlipScaleOffset(Vector2 s, Vector2 o)
Copy link
Contributor

Choose a reason for hiding this comment

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

(どちらでもよい)タプルに名前を付けておいた方が、メンテ性はあるかも。

public static (Vector2 scale, Vector2 offset) VerticalFlipScaleOffset(Vector2 scale, Vector2 offset)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

たしかに。わりと OffsetScale と言っていることがある。

Copy link
Contributor

@Santarh Santarh Sep 21, 2021

Choose a reason for hiding this comment

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

計算を適用する順序としては uv * scale + offset なので ScaleOffset の方が間違いが少ないという気持ちはある
微妙なところ

{
return (new Vector2(s.x, s.y), new Vector2(o.x, 1.0f - o.y - s.y));
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}
}
}

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
@@ -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);
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