don't need normal texture conversion when importing

This commit is contained in:
Masataka SUMI
2021-05-27 20:27:11 +09:00
parent 6b6a219d43
commit 44bced7f3d
4 changed files with 11 additions and 106 deletions

View File

@@ -1,68 +0,0 @@
Shader "UniGLTF/NormalMapEncoder"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag(v2f i) : SV_Target
{
half4 col = tex2D(_MainTex, i.uv);
half4 packedNormal;
packedNormal.x = col.x;
packedNormal.y = col.y;
packedNormal.z = col.z;
packedNormal.w = 1.0;
// normalize as normal vector
float3 normal;
normal.xy = packedNormal.xy * 2 - 1;
if (dot(normal.xy, normal.xy) > 1)
{
normal.xy = normalize(normal.xy);
}
normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
packedNormal.xyz = normal.xyz * 0.5 + 0.5;
return packedNormal;
}
ENDCG
}
}
}

View File

@@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: 3e39586253f31b34f87fa7e133449b1e
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -17,28 +17,13 @@ namespace VRMShaders
}
}
private static Material m_encoder;
private static Material Encoder
{
get
{
if (m_encoder == null)
{
m_encoder = new Material(Shader.Find("UniGLTF/NormalMapEncoder"));
}
return m_encoder;
}
}
// GLTF data to Unity texture
// ConvertToNormalValueFromRawColorWhenCompressionIsRequired
public static Texture2D Import(Texture2D texture)
{
return TextureConverter.CopyTexture(texture, ColorSpace.Linear, false, Encoder);
return texture;
}
// Unity texture to GLTF data
// ConvertToRawColorWhenNormalValueIsCompressed
public static Texture2D Export(Texture texture)
{
return TextureConverter.CopyTexture(texture, ColorSpace.Linear, false, Decoder);

View File

@@ -61,13 +61,9 @@ namespace VRMShaders
}
/// <summary>
/// テクスチャーをロード、必要であれば変換して返す。
/// 同じものはキャッシュを返す
/// テクスチャ生成情報を基に、テクスチャ生成を行う。
/// SubAssetKey が同じ場合はキャッシュを返す。
/// </summary>
/// <param name="texture_type">変換の有無を判断する: METALLIC_GLOSS_PROP</param>
/// <param name="roughnessFactor">METALLIC_GLOSS_PROPの追加パラメーター</param>
/// <param name="indices">gltf の texture index</param>
/// <returns></returns>
public async Task<Texture> GetTextureAsync(TextureDescriptor texDesc)
{
var subAssetKey = texDesc.SubAssetKey;
@@ -86,15 +82,16 @@ namespace VRMShaders
{
case TextureImportTypes.NormalMap:
{
// Runtime/SubAsset 用に変換する
// no conversion. Unity's normal map is same with glTF's.
//
// > contrary to Unity’s usual convention of using Y as “up”
// https://docs.unity3d.com/2018.4/Documentation/Manual/StandardShaderMaterialParameterNormalMap.html
var data0 = await texDesc.Index0();
var rawTexture = await _textureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
var convertedTexture = NormalConverter.Import(rawTexture);
convertedTexture.name = subAssetKey.Name;
convertedTexture.SetSampler(texDesc.Sampler);
_textureCache.Add(subAssetKey, convertedTexture);
DestroyResource(rawTexture);
return convertedTexture;
rawTexture.name = subAssetKey.Name;
rawTexture.SetSampler(texDesc.Sampler);
_textureCache.Add(subAssetKey, rawTexture);
return rawTexture;
}
case TextureImportTypes.StandardMap: