mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-07-30 22:46:44 -05:00
VRMShaders.TextureConverter
This commit is contained in:
parent
356ab03748
commit
d32ca24f8b
|
|
@ -1,4 +1,5 @@
|
|||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
|
|
@ -34,14 +35,14 @@ namespace UniGLTF
|
|||
// ConvertToNormalValueFromRawColorWhenCompressionIsRequired
|
||||
public static Texture2D Import(Texture2D texture)
|
||||
{
|
||||
return TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, Encoder);
|
||||
return TextureConverter.Convert(texture, TextureImportTypes.NormalMap, null, Encoder);
|
||||
}
|
||||
|
||||
// Unity texture to GLTF data
|
||||
// ConvertToRawColorWhenNormalValueIsCompressed
|
||||
public static Texture2D Export(Texture texture)
|
||||
{
|
||||
return TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, Decoder);
|
||||
return TextureConverter.Convert(texture, TextureImportTypes.NormalMap, null, Decoder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
|
|
@ -28,7 +29,7 @@ namespace UniGLTF
|
|||
{
|
||||
if (metallicRoughnessTexture == occlusionTexture)
|
||||
{
|
||||
var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, TextureImportTypes.StandardMap, null);
|
||||
var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32();
|
||||
for (int i = 0; i < metallicRoughnessPixels.Length; ++i)
|
||||
{
|
||||
|
|
@ -41,9 +42,9 @@ namespace UniGLTF
|
|||
}
|
||||
else
|
||||
{
|
||||
var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, TextureImportTypes.StandardMap, null);
|
||||
var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32();
|
||||
var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, TextureImportTypes.StandardMap, null);
|
||||
var occlusionPixels = copyOcclusion.GetPixels32();
|
||||
if (metallicRoughnessPixels.Length != occlusionPixels.Length)
|
||||
{
|
||||
|
|
@ -61,7 +62,7 @@ namespace UniGLTF
|
|||
}
|
||||
else if (metallicRoughnessTexture != null)
|
||||
{
|
||||
var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, TextureImportTypes.StandardMap, null);
|
||||
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ImportPixel(x, metallicFactor, roughnessFactor, default)).ToArray());
|
||||
copyTexture.Apply();
|
||||
copyTexture.name = metallicRoughnessTexture.name;
|
||||
|
|
@ -69,7 +70,7 @@ namespace UniGLTF
|
|||
}
|
||||
else if (occlusionTexture != null)
|
||||
{
|
||||
var copyTexture = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyTexture = TextureConverter.CopyTexture(occlusionTexture, TextureImportTypes.StandardMap, null);
|
||||
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ImportPixel(default, metallicFactor, roughnessFactor, x)).ToArray());
|
||||
copyTexture.Apply();
|
||||
copyTexture.name = occlusionTexture.name;
|
||||
|
|
@ -100,7 +101,7 @@ namespace UniGLTF
|
|||
{
|
||||
if (metallicSmoothTexture == occlusionTexture)
|
||||
{
|
||||
var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, TextureImportTypes.StandardMap, null);
|
||||
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, x)).ToArray());
|
||||
copyTexture.Apply();
|
||||
copyTexture.name = metallicSmoothTexture.name;
|
||||
|
|
@ -108,9 +109,9 @@ namespace UniGLTF
|
|||
}
|
||||
else
|
||||
{
|
||||
var copyMetallicSmooth = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyMetallicSmooth = TextureConverter.CopyTexture(metallicSmoothTexture, TextureImportTypes.StandardMap, null);
|
||||
var metallicSmoothPixels = copyMetallicSmooth.GetPixels32();
|
||||
var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, TextureImportTypes.StandardMap, null);
|
||||
var occlusionPixels = copyOcclusion.GetPixels32();
|
||||
if (metallicSmoothPixels.Length != occlusionPixels.Length)
|
||||
{
|
||||
|
|
@ -128,7 +129,7 @@ namespace UniGLTF
|
|||
}
|
||||
else if (metallicSmoothTexture)
|
||||
{
|
||||
var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, TextureImportTypes.StandardMap, null);
|
||||
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, default)).ToArray());
|
||||
copyTexture.Apply();
|
||||
copyTexture.name = metallicSmoothTexture.name;
|
||||
|
|
@ -136,7 +137,7 @@ namespace UniGLTF
|
|||
}
|
||||
else if (occlusionTexture)
|
||||
{
|
||||
var copyTexture = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
var copyTexture = TextureConverter.CopyTexture(occlusionTexture, TextureImportTypes.StandardMap, null);
|
||||
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(default, smoothness, x)).ToArray());
|
||||
copyTexture.Apply();
|
||||
copyTexture.name = occlusionTexture.name;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Reflection;
|
||||
using VRMShaders;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
|
@ -126,7 +127,7 @@ namespace UniGLTF
|
|||
}
|
||||
else
|
||||
{
|
||||
texture2D = TextureConverter.CopyTexture(src, glTFTextureTypes.SRGB, null);
|
||||
texture2D = TextureConverter.CopyTexture(src, TextureImportTypes.sRGB, null);
|
||||
}
|
||||
Exported.Add(texture2D);
|
||||
m_exportMap.Add(new ExportKey(src, glTFTextureTypes.SRGB), index);
|
||||
|
|
|
|||
|
|
@ -1,20 +1,15 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
|
||||
namespace UniGLTF
|
||||
namespace VRMShaders
|
||||
{
|
||||
|
||||
public static class TextureConverter
|
||||
{
|
||||
public delegate Color32 ColorConversion(Color32 color);
|
||||
|
||||
public static Texture2D Convert(Texture texture, glTFTextureTypes textureType, ColorConversion colorConversion, Material convertMaterial)
|
||||
public static Texture2D Convert(Texture texture, TextureImportTypes textureType, ColorConversion colorConversion, Material convertMaterial)
|
||||
{
|
||||
var copyTexture = CopyTexture(texture, textureType, convertMaterial);
|
||||
if (colorConversion != null)
|
||||
|
|
@ -57,76 +52,7 @@ namespace UniGLTF
|
|||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR && VRM_DEVELOP
|
||||
[MenuItem("Assets/CopySRGBWrite", true)]
|
||||
static bool CopySRGBWriteIsEnable()
|
||||
{
|
||||
return Selection.activeObject is Texture;
|
||||
}
|
||||
|
||||
[MenuItem("Assets/CopySRGBWrite")]
|
||||
static void CopySRGBWrite()
|
||||
{
|
||||
CopySRGBWrite(true);
|
||||
}
|
||||
|
||||
[MenuItem("Assets/CopyNotSRGBWrite", true)]
|
||||
static bool CopyNotSRGBWriteIsEnable()
|
||||
{
|
||||
return Selection.activeObject is Texture;
|
||||
}
|
||||
|
||||
[MenuItem("Assets/CopyNotSRGBWrite")]
|
||||
static void CopyNotSRGBWrite()
|
||||
{
|
||||
CopySRGBWrite(false);
|
||||
}
|
||||
|
||||
static string AddPath(string path, string add)
|
||||
{
|
||||
return string.Format("{0}/{1}{2}{3}",
|
||||
Path.GetDirectoryName(path),
|
||||
Path.GetFileNameWithoutExtension(path),
|
||||
add,
|
||||
Path.GetExtension(path));
|
||||
}
|
||||
|
||||
static void CopySRGBWrite(bool isSRGB)
|
||||
{
|
||||
var src = Selection.activeObject as Texture;
|
||||
var texturePath = UnityPath.FromAsset(src);
|
||||
|
||||
var path = EditorUtility.SaveFilePanel("save prefab", "Assets",
|
||||
Path.GetFileNameWithoutExtension(AddPath(texturePath.FullPath, ".sRGB")), "prefab");
|
||||
var assetPath = UnityPath.FromFullpath(path);
|
||||
if (!assetPath.IsUnderAssetsFolder)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Debug.LogFormat("[CopySRGBWrite] {0} => {1}", texturePath, assetPath);
|
||||
|
||||
var renderTexture = new RenderTexture(src.width, src.height, 0,
|
||||
RenderTextureFormat.ARGB32,
|
||||
RenderTextureReadWrite.sRGB);
|
||||
using (var scope = new ColorSpaceScope(isSRGB))
|
||||
{
|
||||
Graphics.Blit(src, renderTexture);
|
||||
}
|
||||
|
||||
var dst = new Texture2D(src.width, src.height, TextureFormat.ARGB32, false,
|
||||
RenderTextureReadWrite.sRGB == RenderTextureReadWrite.Linear);
|
||||
dst.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
|
||||
dst.Apply();
|
||||
|
||||
RenderTexture.active = null;
|
||||
|
||||
assetPath.CreateAsset(dst);
|
||||
|
||||
GameObject.DestroyImmediate(renderTexture);
|
||||
}
|
||||
#endif
|
||||
|
||||
public static Texture2D CopyTexture(Texture src, glTFTextureTypes textureType, Material material)
|
||||
public static Texture2D CopyTexture(Texture src, TextureImportTypes textureType, Material material)
|
||||
{
|
||||
Texture2D dst = null;
|
||||
RenderTextureReadWrite colorSpace = textureType.GetColorSpace();
|
||||
|
|
@ -151,11 +77,9 @@ namespace UniGLTF
|
|||
dst.filterMode = src.filterMode;
|
||||
dst.mipMapBias = src.mipMapBias;
|
||||
dst.wrapMode = src.wrapMode;
|
||||
#if UNITY_2017_1_OR_NEWER
|
||||
dst.wrapModeU = src.wrapModeU;
|
||||
dst.wrapModeV = src.wrapModeV;
|
||||
dst.wrapModeW = src.wrapModeW;
|
||||
#endif
|
||||
dst.Apply();
|
||||
|
||||
RenderTexture.active = null;
|
||||
|
|
@ -170,7 +94,4 @@ namespace UniGLTF
|
|||
return dst;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
namespace VRMShaders
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VRMShaders
|
||||
{
|
||||
public enum TextureImportTypes
|
||||
{
|
||||
|
|
@ -25,4 +28,22 @@
|
|||
// TextureImporter.sRGBTexture = false;
|
||||
Linear,
|
||||
}
|
||||
|
||||
public static class TextureImportTypesExtensions
|
||||
{
|
||||
public static RenderTextureReadWrite GetColorSpace(this TextureImportTypes textureType)
|
||||
{
|
||||
switch (textureType)
|
||||
{
|
||||
case TextureImportTypes.sRGB:
|
||||
return RenderTextureReadWrite.sRGB;
|
||||
case TextureImportTypes.Linear:
|
||||
case TextureImportTypes.StandardMap:
|
||||
case TextureImportTypes.NormalMap:
|
||||
return RenderTextureReadWrite.Linear;
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user