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

fix: Apply TextureST #435

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- GameObject から TexTransTool のほとんどのコンポーネントが追加できるようになりました (#411)
- AtlasTexture に強制的に優先度のサイズに変更する ForcePrioritySize が追加されました (#431)
- AtlasTexture 複数のマテリアルが衝突しないテクスチャを持つ場合に、同一のアイランドが割り当てられるようになりました (#431)
- AtlasTexture が Scale Transition(Tiling) を使用しているマテリアルのテクスチャを逆補正する機能が追加されました (#431 #435)

### Changed

Expand Down
13 changes: 3 additions & 10 deletions Editor/Decal/RealTimePreviewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void RegtPreviewRenderTexture(Material material, string propertyName, Bl

var souseTexture = material.GetTexture(propertyName) as Texture2D;
var newTarget = RenderTexture.GetTemporary(blendTexture.RenderTexture.descriptor);
CopyFilWrap(newTarget, blendTexture.RenderTexture);
newTarget.CopyFilWrap(blendTexture.RenderTexture);

editableMat.SetTexture(propertyName, newTarget);

Expand Down Expand Up @@ -194,7 +194,7 @@ public void RegtAbstractDecal(AbstractDecal abstractDecal)
{ continue; }
}

CopyFilWrap(Rt, tex);
Rt.CopyFilWrap(tex);

var blendTex = new BlendRenderTextureClass(Rt, abstractDecal.BlendTypeKey);
blends.Add(blendTex);
Expand All @@ -213,14 +213,7 @@ public void RegtAbstractDecal(AbstractDecal abstractDecal)
ListPool<Material>.Release(TargetMats);
}

internal static void CopyFilWrap(Texture t, Texture s)
{
t.filterMode = s.filterMode;
t.wrapMode = s.wrapMode;
t.wrapModeU = s.wrapModeU;
t.wrapModeV = s.wrapModeV;
t.wrapModeW = s.wrapModeW;
}


public bool IsRealTimePreview(AbstractDecal abstractDecal) => RealTimePreviews.ContainsKey(abstractDecal);
public void UnRegtAbstractDecal(AbstractDecal abstractDecal)
Expand Down
2 changes: 2 additions & 0 deletions Runtime/IDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ public static RenderTexture GetOriginTempRt(this IOriginTexture origin, Texture2
{
var originSize = origin.GetOriginalTextureSize(texture2D);
var tempRt = RenderTexture.GetTemporary(originSize, originSize, 0);
tempRt.CopyFilWrap(texture2D);
origin.WriteOriginalTexture(texture2D, tempRt);
return tempRt;
}
public static RenderTexture GetOriginTempRt(this IOriginTexture origin, Texture2D texture2D, int size)
{
var tempRt = RenderTexture.GetTemporary(size, size, 0);
tempRt.CopyFilWrap(texture2D);
tempRt.Clear();
origin.WriteOriginalTexture(texture2D, tempRt);
return tempRt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void Initialize()
{
s_defaultSupporter = ScriptableObject.CreateInstance<AtlasShaderSupportScriptableObject>();
s_defaultSupporter.SupportedShaderComparer = new AnythingShader();
s_defaultSupporter.AtlasTargetDefines = new() { new() { TexturePropertyName = "_MainTex", AtlasDefineConstraints = new Anything() } };
s_defaultSupporter.AtlasTargetDefines = new() { new() { TexturePropertyName = "_MainTex", AtlasDefineConstraints = new Anything(), BakePropertyNames = new() } };
}

atlasShaderSupportList.Add(s_defaultSupporter);
Expand Down
12 changes: 10 additions & 2 deletions Runtime/TextureAtlas/AtlasTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ Dictionary<string, RenderTexture> ZipDictAndOffset(IEnumerable<Dictionary<string
{
if (kv.Any(i => i.Value.Texture2D != null) == false) { continue; }
var atlasTex = kv.First(i => i.Value.Texture2D != null).Value;
if (atlasTex.TextureScale == Vector2.zero && atlasTex.TextureTranslation == Vector2.zero)
if (atlasTex.TextureScale == Vector2.one && atlasTex.TextureTranslation == Vector2.zero)
{
dict[kv.Key] = texManage.GetOriginTempRt(atlasTex.Texture2D);
}
Expand Down Expand Up @@ -416,9 +416,17 @@ Dictionary<string, RenderTexture> ZipDictAndOffset(IEnumerable<Dictionary<string
atlasTexDict.TryGetValue(propName, out var atlasTex);
var sTex = atlasTex?.Texture2D != null ? texManage.GetOriginTempRt(atlasTex.Texture2D) : null;

if (sTex != null) { sTex.ApplyTextureST(atlasTex.TextureScale, atlasTex.TextureTranslation); }
if (sTex != null && (atlasTex.TextureScale != Vector2.one || atlasTex.TextureTranslation != Vector2.zero)) { sTex.ApplyTextureST(atlasTex.TextureScale, atlasTex.TextureTranslation); }

if (shaderSupport.BakeShader == null)
{
texDict[propName] = sTex != null ? sTex : RenderTexture.GetTemporary(2, 2); ;
continue;
}

var bakedTex = sTex != null ? sTex.CloneTemp() : RenderTexture.GetTemporary(2, 2);


if (atlasTex != null)
{
foreach (var bakeProp in atlasTex.BakeProperties)
Expand Down
10 changes: 5 additions & 5 deletions TexTransCore/TransTextureCore/ShaderAsset/TextureSTApply.shader
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Shader "Hidden/TextureSTApply"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_OffSetTex ("Texture", 2D) = "white" {}
}
SubShader
{
Expand All @@ -29,20 +29,20 @@ Shader "Hidden/TextureSTApply"
float2 uv : TEXCOORD0;
};

sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _OffSetTex;
float4 _OffSetTex_ST;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv , _MainTex);
o.uv = TRANSFORM_TEX(v.uv , _OffSetTex);
return o;
}

float4 frag (v2f i) : SV_Target
{
return tex2D(_MainTex,i.uv);
return tex2D(_OffSetTex ,i.uv);
}
ENDHLSL
}
Expand Down
26 changes: 15 additions & 11 deletions TexTransCore/TransTextureCore/Utils/TextureUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static Texture2D ResizeTexture(Texture2D souse, Vector2Int size)

RenderTexture.ReleaseTemporary(rt);
Profiler.EndSample();

return resizedTexture;
}
}
Expand Down Expand Up @@ -134,18 +134,21 @@ public static Texture2D CreateFillTexture(Vector2Int size, Color fillColor)
public static RenderTexture CloneTemp(this RenderTexture renderTexture)
{
var newTemp = RenderTexture.GetTemporary(renderTexture.descriptor);
newTemp.filterMode = renderTexture.filterMode;
newTemp.wrapMode = renderTexture.wrapMode;
newTemp.wrapModeU = renderTexture.wrapModeU;
newTemp.wrapModeV = renderTexture.wrapModeV;
newTemp.wrapModeW = renderTexture.wrapModeW;
newTemp.CopyFilWrap(renderTexture);
Graphics.CopyTexture(renderTexture, newTemp);
return newTemp;
}

internal static void CopyFilWrap(this Texture t, Texture s)
{
t.filterMode = s.filterMode;
t.wrapMode = s.wrapMode;
t.wrapModeU = s.wrapModeU;
t.wrapModeV = s.wrapModeV;
t.wrapModeW = s.wrapModeW;
}


public const string ST_APPLY_SHADER = "Hidden/TransTexture";
public const string ST_APPLY_SHADER = "Hidden/TextureSTApply";
static Shader s_stApplyShader;

[TexTransInitialize]
Expand All @@ -160,10 +163,11 @@ public static void ApplyTextureST(Texture Souse, Vector2 s, Vector2 t, RenderTex
if (s_TempMat == null) { s_TempMat = new Material(s_stApplyShader); }
s_TempMat.shader = s_stApplyShader;

s_TempMat.SetTextureScale("_MainTex", s);
s_TempMat.SetTextureOffset("_MainTex", t);
s_TempMat.SetTexture("_OffSetTex", Souse);
s_TempMat.SetTextureScale("_OffSetTex", s);
s_TempMat.SetTextureOffset("_OffSetTex", t);

Graphics.Blit(Souse, write, s_TempMat);
Graphics.Blit(null, write, s_TempMat);
}
}
public static void ApplyTextureST(this RenderTexture rt, Vector2 s, Vector2 t)
Expand Down