ファイル分割

This commit is contained in:
ousttrue
2021-02-10 20:19:25 +09:00
parent e2b2495e1c
commit 8bd8c461d2
6 changed files with 161 additions and 132 deletions

View File

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

View File

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

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 96c0d7434bd69ba478e2d77e1b3e4bd2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

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

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 14bc3a4ce3e24924a9ae8bb1654a1b7b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: