diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs new file mode 100644 index 000000000..038251f16 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs @@ -0,0 +1,44 @@ +using UnityEngine; + + +namespace UniGLTF +{ + public abstract class MaterialItemBase + { + protected int m_index; + protected glTFMaterial m_src; + + public string Name { get; set; } + + public MaterialItemBase(int i, glTFMaterial src) + { + m_index = i; + m_src = src; + Name = src != null ? m_src.name : ""; + } + + public abstract Material GetOrCreate(GetTextureItemFunc getTexture); + + protected static void SetTextureOffsetAndScale(Material material, glTFTextureInfo textureInfo, string propertyName) + { + if (glTF_KHR_texture_transform.TryGet(textureInfo, out glTF_KHR_texture_transform textureTransform)) + { + Vector2 offset = new Vector2(0, 0); + Vector2 scale = new Vector2(1, 1); + if (textureTransform.offset != null && textureTransform.offset.Length == 2) + { + offset = new Vector2(textureTransform.offset[0], textureTransform.offset[1]); + } + if (textureTransform.scale != null && textureTransform.scale.Length == 2) + { + scale = new Vector2(textureTransform.scale[0], textureTransform.scale[1]); + } + + offset.y = (offset.y + scale.y - 1.0f) * -1.0f; + + material.SetTextureOffset(propertyName, offset); + material.SetTextureScale(propertyName, scale); + } + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItem.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItem.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs similarity index 66% rename from Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItem.cs rename to Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs index 65ffdedbb..ca33fd8ee 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs @@ -1,138 +1,7 @@ -using System; -using UnityEngine; -using UnityEngine.Rendering; +using UnityEngine; namespace UniGLTF { - public abstract class MaterialItemBase - { - protected int m_index; - protected glTFMaterial m_src; - - public string Name { get; set; } - - public MaterialItemBase(int i, glTFMaterial src) - { - m_index = i; - m_src = src; - Name = src != null ? m_src.name : ""; - } - - public abstract Material GetOrCreate(GetTextureItemFunc getTexture); - - protected static void SetTextureOffsetAndScale(Material material, glTFTextureInfo textureInfo, string propertyName) - { - if (glTF_KHR_texture_transform.TryGet(textureInfo, out glTF_KHR_texture_transform textureTransform)) - { - Vector2 offset = new Vector2(0, 0); - Vector2 scale = new Vector2(1, 1); - if (textureTransform.offset != null && textureTransform.offset.Length == 2) - { - offset = new Vector2(textureTransform.offset[0], textureTransform.offset[1]); - } - if (textureTransform.scale != null && textureTransform.scale.Length == 2) - { - scale = new Vector2(textureTransform.scale[0], textureTransform.scale[1]); - } - - offset.y = (offset.y + scale.y - 1.0f) * -1.0f; - - material.SetTextureOffset(propertyName, offset); - material.SetTextureScale(propertyName, scale); - } - } - } - - public class UnlitMaterialItem : MaterialItemBase - { - public const string UniUnlitShaderName = "UniGLTF/UniUnlit"; - - bool m_hasVertexColor; - - public UnlitMaterialItem(int i, glTFMaterial src, bool hasVertexColor) : base(i, src) - { - m_hasVertexColor = hasVertexColor; - } - - public override Material GetOrCreate(GetTextureItemFunc getTexture) - { - if (getTexture == null) - { - getTexture = _ => null; - } - - var material = new Material(Shader.Find(UniUnlitShaderName)); -#if UNITY_EDITOR - // textureImporter.SaveAndReimport(); may destroy this material - material.hideFlags = HideFlags.DontUnloadUnusedAsset; -#endif - material.name = (m_src == null || string.IsNullOrEmpty(m_src.name)) - ? string.Format("material_{0:00}", m_index) - : m_src.name - ; - - // texture - if (m_src.pbrMetallicRoughness.baseColorTexture != null) - { - var texture = getTexture(m_src.pbrMetallicRoughness.baseColorTexture.index); - if (texture != null) - { - material.mainTexture = texture.Texture; - } - - // Texture Offset and Scale - SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); - } - - // color - if (m_src.pbrMetallicRoughness.baseColorFactor != null && m_src.pbrMetallicRoughness.baseColorFactor.Length == 4) - { - var color = m_src.pbrMetallicRoughness.baseColorFactor; - material.color = (new Color(color[0], color[1], color[2], color[3])).gamma; - } - - //renderMode - if (m_src.alphaMode == "OPAQUE") - { - UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); - } - else if (m_src.alphaMode == "BLEND") - { - UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent); - } - else if (m_src.alphaMode == "MASK") - { - UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout); - material.SetFloat("_Cutoff", m_src.alphaCutoff); - } - else - { - // default OPAQUE - UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); - } - - // culling - if (m_src.doubleSided) - { - UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off); - } - else - { - UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back); - } - - // VColor - if (m_hasVertexColor) - { - UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply); - } - - UniUnlit.Utils.ValidateProperties(material, true); - - return material; - } - } - /// StandardShader variables /// /// _Color diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs.meta new file mode 100644 index 000000000..769570a27 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 96c0d7434bd69ba478e2d77e1b3e4bd2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs new file mode 100644 index 000000000..1eb4a5113 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs @@ -0,0 +1,94 @@ +using UnityEngine; + +namespace UniGLTF +{ + public class UnlitMaterialItem : MaterialItemBase + { + public const string UniUnlitShaderName = "UniGLTF/UniUnlit"; + + bool m_hasVertexColor; + + public UnlitMaterialItem(int i, glTFMaterial src, bool hasVertexColor) : base(i, src) + { + m_hasVertexColor = hasVertexColor; + } + + public override Material GetOrCreate(GetTextureItemFunc getTexture) + { + if (getTexture == null) + { + getTexture = _ => null; + } + + var material = new Material(Shader.Find(UniUnlitShaderName)); +#if UNITY_EDITOR + // textureImporter.SaveAndReimport(); may destroy this material + material.hideFlags = HideFlags.DontUnloadUnusedAsset; +#endif + material.name = (m_src == null || string.IsNullOrEmpty(m_src.name)) + ? string.Format("material_{0:00}", m_index) + : m_src.name + ; + + // texture + if (m_src.pbrMetallicRoughness.baseColorTexture != null) + { + var texture = getTexture(m_src.pbrMetallicRoughness.baseColorTexture.index); + if (texture != null) + { + material.mainTexture = texture.Texture; + } + + // Texture Offset and Scale + SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); + } + + // color + if (m_src.pbrMetallicRoughness.baseColorFactor != null && m_src.pbrMetallicRoughness.baseColorFactor.Length == 4) + { + var color = m_src.pbrMetallicRoughness.baseColorFactor; + material.color = (new Color(color[0], color[1], color[2], color[3])).gamma; + } + + //renderMode + if (m_src.alphaMode == "OPAQUE") + { + UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); + } + else if (m_src.alphaMode == "BLEND") + { + UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent); + } + else if (m_src.alphaMode == "MASK") + { + UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout); + material.SetFloat("_Cutoff", m_src.alphaCutoff); + } + else + { + // default OPAQUE + UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); + } + + // culling + if (m_src.doubleSided) + { + UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off); + } + else + { + UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back); + } + + // VColor + if (m_hasVertexColor) + { + UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply); + } + + UniUnlit.Utils.ValidateProperties(material, true); + + return material; + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs.meta new file mode 100644 index 000000000..51b23e00a --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 14bc3a4ce3e24924a9ae8bb1654a1b7b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: