public CubemapTextureExporter(ITextureExporter exporter, glTF gltf) { _exporter = exporter; _gltf = gltf; _exportAsDLdrShader = Shader.Find("Hidden/UniVCI/CubemapConversion/ExportAsDLdr"); _exportAsRgbmShader = Shader.Find("Hidden/UniVCI/CubemapConversion/ExportAsRgbm"); }
static void Export_Emission(Material m, ITextureExporter textureExporter, glTFMaterial material) { if (m.IsKeywordEnabled("_EMISSION") == false) { return; } if (m.HasProperty("_EmissionColor")) { var color = m.GetColor("_EmissionColor"); if (color.maxColorComponent > 1) { color /= color.maxColorComponent; } material.emissiveFactor = color.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); } if (m.HasProperty("_EmissionMap")) { var index = textureExporter.ExportAsSRgb(m.GetTexture("_EmissionMap")); if (index != -1) { material.emissiveTexture = new glTFMaterialEmissiveTextureInfo() { index = index, }; Export_MainTextureTransform(m, material.emissiveTexture); } } }
static void Export_Emission(Material m, ITextureExporter textureExporter, glTFMaterial material) { if (m.IsKeywordEnabled("_EMISSION") == false) { return; } if (m.HasProperty("_EmissionColor")) { var color = m.GetColor("_EmissionColor"); if (color.maxColorComponent > 1) { var maxColorComponent = color.maxColorComponent; color /= maxColorComponent; UniGLTF.glTF_KHR_materials_emissive_strength.Serialize(ref material.extensions, maxColorComponent); } material.emissiveFactor = color.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); } if (m.HasProperty(EMISSION_TEX_PROP)) { var index = textureExporter.RegisterExportingAsSRgb(m.GetTexture(EMISSION_TEX_PROP), needsAlpha: false); if (index != -1) { material.emissiveTexture = new glTFMaterialEmissiveTextureInfo() { index = index, }; Export_MainTextureTransform(m, material.emissiveTexture); } } }
static IEnumerable <glTFMaterial> ExportMaterials(Model model, ITextureExporter textureExporter, GltfExportSettings settings) { var materialExporter = new Vrm10MaterialExporter(); foreach (Material material in model.Materials) { yield return(materialExporter.ExportMaterial(material, textureExporter, settings)); } }
public override glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter, GltfExportSettings settings) { if (Vrm10MToonMaterialExporter.TryExportMaterialAsMToon(m, textureExporter, out var dst)) { return(dst); } else { return(base.ExportMaterial(m, textureExporter, settings)); } }
public virtual glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter) { var material = CreateMaterial(m); // common params material.name = m.name; Export_Color(m, textureExporter, material); Export_Emission(m, textureExporter, material); Export_Normal(m, textureExporter, material); Export_OcclusionMetallicRoughness(m, textureExporter, material); return(material); }
public virtual glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter, GltfExportSettings settings) { var material = CreateMaterial(m); // common params material.name = m.name; Export_Color(m, textureExporter, material); Export_Emission(m, textureExporter, material, settings.UseEmissiveMultiplier); Export_Normal(m, textureExporter, material); Export_OcclusionMetallicRoughness(m, textureExporter, material); return(material); }
static void Export_Color(Material m, ITextureExporter textureManager, glTFMaterial material) { if (m.HasProperty("_Color")) { material.pbrMetallicRoughness.baseColorFactor = m.GetColor("_Color").ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); } if (m.HasProperty("_MainTex")) { var index = textureManager.ExportAsSRgb(m.GetTexture("_MainTex")); if (index != -1) { material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo() { index = index, }; Export_MainTextureTransform(m, material.pbrMetallicRoughness.baseColorTexture); } } }
static void Export_Normal(Material m, ITextureExporter textureExporter, glTFMaterial material) { if (m.HasProperty("_BumpMap")) { var index = textureExporter.ExportAsNormal(m.GetTexture("_BumpMap")); if (index != -1) { material.normalTexture = new glTFMaterialNormalTextureInfo() { index = index, }; Export_MainTextureTransform(m, material.normalTexture); } if (index != -1 && m.HasProperty("_BumpScale")) { material.normalTexture.scale = m.GetFloat("_BumpScale"); } } }
static void Export_Emission(Material m, ITextureExporter textureExporter, glTFMaterial material, bool useEmissiveMultiplier) { if (m.IsKeywordEnabled("_EMISSION") == false) { return; } if (m.HasProperty("_EmissionColor")) { var color = m.GetColor("_EmissionColor"); if (color.maxColorComponent > 1) { var maxColorComponent = color.maxColorComponent; color /= maxColorComponent; if (useEmissiveMultiplier) { UniGLTF.Extensions.VRMC_materials_hdr_emissiveMultiplier.GltfSerializer.SerializeTo(ref material.extensions, new Extensions.VRMC_materials_hdr_emissiveMultiplier.VRMC_materials_hdr_emissiveMultiplier { EmissiveMultiplier = maxColorComponent, }); } } material.emissiveFactor = color.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); } if (m.HasProperty(EMISSION_TEX_PROP)) { var index = textureExporter.RegisterExportingAsSRgb(m.GetTexture(EMISSION_TEX_PROP), needsAlpha: false); if (index != -1) { material.emissiveTexture = new glTFMaterialEmissiveTextureInfo() { index = index, }; Export_MainTextureTransform(m, material.emissiveTexture); } } }
static void Export_Color(Material m, ITextureExporter textureManager, glTFMaterial material) { if (m.HasProperty("_Color")) { material.pbrMetallicRoughness.baseColorFactor = m.GetColor("_Color").ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); } if (m.HasProperty(COLOR_TEXTURE_PROP)) { // Don't export alpha channel if material was OPAQUE var unnecessaryAlpha = string.Equals(material.alphaMode, "OPAQUE", StringComparison.Ordinal); var index = textureManager.RegisterExportingAsSRgb(m.GetTexture(COLOR_TEXTURE_PROP), !unnecessaryAlpha); if (index != -1) { material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo() { index = index, }; Export_MainTextureTransform(m, material.pbrMetallicRoughness.baseColorTexture); } } }
/// <summary> /// Occlusion, Metallic, Roughness /// </summary> /// <param name="m"></param> /// <param name="textureExporter"></param> /// <param name="material"></param> static void Export_OcclusionMetallicRoughness(Material m, ITextureExporter textureExporter, glTFMaterial material) { Texture metallicSmoothTexture = default; float smoothness = 1.0f; var textuerNames = m.GetTexturePropertyNames(); if (textuerNames.Contains("_MetallicGlossMap")) { if (m.HasProperty("_GlossMapScale")) { smoothness = m.GetFloat("_GlossMapScale"); } metallicSmoothTexture = m.GetTexture("_MetallicGlossMap"); } Texture occlusionTexture = default; var occlusionStrength = 1.0f; if (textuerNames.Contains("_OcclusionMap")) { occlusionTexture = m.GetTexture("_OcclusionMap"); if (occlusionTexture != null && m.HasProperty("_OcclusionStrength")) { occlusionStrength = m.GetFloat("_OcclusionStrength"); } } int index = textureExporter.ExportAsGltfMetallicSmoothnessOcclusionCombined(metallicSmoothTexture, smoothness, occlusionTexture); if (index != -1 && metallicSmoothTexture != null) { material.pbrMetallicRoughness.metallicRoughnessTexture = new glTFMaterialMetallicRoughnessTextureInfo() { index = index, }; Export_MainTextureTransform(m, material.pbrMetallicRoughness.metallicRoughnessTexture); // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. material.pbrMetallicRoughness.metallicFactor = 1.0f; material.pbrMetallicRoughness.roughnessFactor = 1.0f; } else { if (m.HasProperty("_Metallic")) { material.pbrMetallicRoughness.metallicFactor = m.GetFloat("_Metallic"); } if (m.HasProperty("_Glossiness")) { material.pbrMetallicRoughness.roughnessFactor = 1.0f - m.GetFloat("_Glossiness"); } } if (index != -1 && occlusionTexture != null) { material.occlusionTexture = new glTFMaterialOcclusionTextureInfo() { index = index, strength = occlusionStrength, }; Export_MainTextureTransform(m, material.occlusionTexture); } }
public static bool TryExportMaterialAsMToon(Material src, ITextureExporter textureExporter, out glTFMaterial dst) { try { if (src.shader.name != MToon10Meta.UnityShaderName) { dst = null; return(false); } // Get MToon10 Context var context = new MToon10Context(src); context.Validate(); // base material dst = glTF_KHR_materials_unlit.CreateDefault(); dst.name = src.name; // vrmc_materials_mtoon ext var mtoon = new UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon(); mtoon.SpecVersion = MTOON_SPEC_VERSION; // Rendering dst.alphaMode = ExportAlphaMode(context.AlphaMode); mtoon.TransparentWithZWrite = context.TransparentWithZWriteMode == MToon10TransparentWithZWriteMode.On; dst.alphaCutoff = Mathf.Max(0, context.AlphaCutoff); mtoon.RenderQueueOffsetNumber = context.RenderQueueOffsetNumber; dst.doubleSided = context.DoubleSidedMode == MToon10DoubleSidedMode.On; // Lighting dst.pbrMetallicRoughness = new glTFPbrMetallicRoughness(); dst.pbrMetallicRoughness.baseColorFactor = context.BaseColorFactorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); var baseColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.BaseColorTexture, context.AlphaMode != MToon10AlphaMode.Opaque); if (baseColorTextureIndex != -1) { dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo { index = baseColorTextureIndex, }; } mtoon.ShadeColorFactor = context.ShadeColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); var shadeColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.ShadeColorTexture, needsAlpha: false); if (shadeColorTextureIndex != -1) { mtoon.ShadeMultiplyTexture = new TextureInfo { Index = shadeColorTextureIndex, }; } var normalTextureIndex = textureExporter.RegisterExportingAsNormal(context.NormalTexture); if (normalTextureIndex != -1) { dst.normalTexture = new glTFMaterialNormalTextureInfo { index = normalTextureIndex, scale = context.NormalTextureScale, }; } mtoon.ShadingShiftFactor = context.ShadingShiftFactor; var shadingShiftTextureIndex = textureExporter.RegisterExportingAsLinear(context.ShadingShiftTexture, needsAlpha: false); if (shadingShiftTextureIndex != -1) { mtoon.ShadingShiftTexture = new ShadingShiftTextureInfo { Index = shadingShiftTextureIndex, Scale = context.ShadingShiftTextureScale, }; } mtoon.ShadingToonyFactor = context.ShadingToonyFactor; // GI // TODO: update schema mtoon.GiIntensityFactor = context.GiEqualizationFactor; // Emission dst.emissiveFactor = context.EmissiveFactorLinear.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); var emissiveTextureIndex = textureExporter.RegisterExportingAsSRgb(context.EmissiveTexture, needsAlpha: false); if (emissiveTextureIndex != -1) { dst.emissiveTexture = new glTFMaterialEmissiveTextureInfo { index = emissiveTextureIndex, }; } // Rim Lighting var matcapTextureIndex = textureExporter.RegisterExportingAsSRgb(context.MatcapTexture, needsAlpha: false); if (matcapTextureIndex != -1) { mtoon.MatcapTexture = new TextureInfo { Index = matcapTextureIndex, }; } mtoon.ParametricRimColorFactor = context.ParametricRimColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); mtoon.ParametricRimFresnelPowerFactor = context.ParametricRimFresnelPowerFactor; mtoon.ParametricRimLiftFactor = context.ParametricRimLiftFactor; var rimMultiplyTextureIndex = textureExporter.RegisterExportingAsSRgb(context.RimMultiplyTexture, needsAlpha: false); if (rimMultiplyTextureIndex != -1) { mtoon.RimMultiplyTexture = new TextureInfo { Index = rimMultiplyTextureIndex, }; } mtoon.RimLightingMixFactor = context.RimLightingMixFactor; // Outline mtoon.OutlineWidthMode = ExportOutlineWidthMode(context.OutlineWidthMode); mtoon.OutlineWidthFactor = context.OutlineWidthFactor; var outlineWidthMultiplyTextureIndex = textureExporter.RegisterExportingAsLinear(context.OutlineWidthMultiplyTexture, needsAlpha: false); if (outlineWidthMultiplyTextureIndex != -1) { mtoon.OutlineWidthMultiplyTexture = new TextureInfo { Index = outlineWidthMultiplyTextureIndex, }; } mtoon.OutlineColorFactor = context.OutlineColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); mtoon.OutlineLightingMixFactor = context.OutlineLightingMixFactor; // UV Animation var uvAnimationMaskTextureIndex = textureExporter.RegisterExportingAsLinear(context.UvAnimationMaskTexture, needsAlpha: false); if (uvAnimationMaskTextureIndex != -1) { mtoon.UvAnimationMaskTexture = new TextureInfo { Index = uvAnimationMaskTextureIndex, }; } mtoon.UvAnimationScrollXSpeedFactor = context.UvAnimationScrollXSpeedFactor; { // Coordinate Conversion const float invertY = -1f; mtoon.UvAnimationScrollYSpeedFactor = context.UvAnimationScrollYSpeedFactor * invertY; } mtoon.UvAnimationRotationSpeedFactor = context.UvAnimationRotationSpeedFactor; // Texture Transforms var scale = context.TextureScale; var offset = context.TextureOffset; ExportTextureTransform(dst.pbrMetallicRoughness.baseColorTexture, scale, offset); ExportTextureTransform(dst.emissiveTexture, scale, offset); ExportTextureTransform(dst.normalTexture, scale, offset); ExportTextureTransform(mtoon.ShadeMultiplyTexture, scale, offset); ExportTextureTransform(mtoon.ShadingShiftTexture, scale, offset); ExportTextureTransform(mtoon.MatcapTexture, scale, offset); ExportTextureTransform(mtoon.RimMultiplyTexture, scale, offset); ExportTextureTransform(mtoon.OutlineWidthMultiplyTexture, scale, offset); ExportTextureTransform(mtoon.UvAnimationMaskTexture, scale, offset); UniGLTF.Extensions.VRMC_materials_mtoon.GltfSerializer.SerializeTo(ref dst.extensions, mtoon); return(true); } catch (Exception) { dst = null; return(false); } }
public static bool TryExportMaterialAsMToon(Material src, ITextureExporter textureExporter, out glTFMaterial dst) { try { if (src.shader.name != MToon.Utils.ShaderName) { dst = null; return(false); } // convert MToon intermediate value from UnityEngine.Material var def = MToon.Utils.GetMToonParametersFromMaterial(src); // gltfMaterial dst = new glTFMaterial { name = src.name, // Rendering doubleSided = def.Rendering.CullMode == CullMode.Off, alphaMode = ExportAlphaMode(def.Rendering.RenderMode), alphaCutoff = Mathf.Max(def.Color.CutoutThresholdValue, 0), // Lighting pbrMetallicRoughness = new glTFPbrMetallicRoughness { baseColorFactor = def.Color.LitColor.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear), baseColorTexture = new glTFMaterialBaseColorTextureInfo { index = textureExporter.ExportAsSRgb(def.Color.LitMultiplyTexture), }, }, normalTexture = new glTFMaterialNormalTextureInfo { index = textureExporter.ExportAsNormal(def.Lighting.Normal.NormalTexture), scale = def.Lighting.Normal.NormalScaleValue, }, // Emission emissiveFactor = def.Emission.EmissionColor.ToFloat3(ColorSpace.Linear, ColorSpace.Linear), emissiveTexture = new glTFMaterialEmissiveTextureInfo { index = textureExporter.ExportAsSRgb(def.Emission.EmissionMultiplyTexture), }, }; const float centimeterToMeter = 0.01f; const float invertY = -1f; // VRMC_materials_mtoon var mtoon = new UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon { Version = "", // Rendering TransparentWithZWrite = def.Rendering.RenderMode == RenderMode.TransparentWithZWrite, RenderQueueOffsetNumber = ExportRenderQueueOffset(def.Rendering.RenderMode, def.Rendering.RenderQueueOffsetNumber), // Lighting ShadeColorFactor = def.Color.ShadeColor.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear), ShadeMultiplyTexture = new TextureInfo { Index = textureExporter.ExportAsSRgb(def.Color.ShadeMultiplyTexture), }, ShadingToonyFactor = def.Lighting.LitAndShadeMixing.ShadingToonyValue, ShadingShiftFactor = def.Lighting.LitAndShadeMixing.ShadingShiftValue, ShadingShiftTexture = null, // Global Illumination GiIntensityFactor = def.Lighting.LightingInfluence.GiIntensityValue, // Rim Lighting MatcapTexture = new TextureInfo { Index = textureExporter.ExportAsSRgb(def.MatCap.AdditiveTexture), }, ParametricRimColorFactor = def.Rim.RimColor.ToFloat3(ColorSpace.Linear, ColorSpace.Linear), ParametricRimFresnelPowerFactor = def.Rim.RimFresnelPowerValue, ParametricRimLiftFactor = def.Rim.RimLiftValue, RimMultiplyTexture = new TextureInfo { Index = textureExporter.ExportAsSRgb(def.Rim.RimMultiplyTexture), }, RimLightingMixFactor = def.Rim.RimLightingMixValue, // Outline OutlineWidthMode = ExportOutlineWidthMode(def.Outline.OutlineWidthMode), OutlineWidthFactor = def.Outline.OutlineWidthValue * centimeterToMeter, OutlineWidthMultiplyTexture = new TextureInfo { Index = textureExporter.ExportAsLinear(def.Outline.OutlineWidthMultiplyTexture), }, OutlineColorFactor = def.Outline.OutlineColor.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear), OutlineLightingMixFactor = ExportOutlineLightingMixFactor(def.Outline.OutlineColorMode, def.Outline.OutlineLightingMixValue), // UV Anim UvAnimationMaskTexture = new TextureInfo { Index = textureExporter.ExportAsLinear(def.TextureOption.UvAnimationMaskTexture), }, UvAnimationScrollXSpeedFactor = def.TextureOption.UvAnimationScrollXSpeedValue, UvAnimationScrollYSpeedFactor = def.TextureOption.UvAnimationScrollYSpeedValue * invertY, UvAnimationRotationSpeedFactor = def.TextureOption.UvAnimationRotationSpeedValue, }; // Texture Transforms var scale = def.TextureOption.MainTextureLeftBottomOriginScale; var offset = def.TextureOption.MainTextureLeftBottomOriginOffset; ExportTextureTransform(dst.pbrMetallicRoughness.baseColorTexture, scale, offset); ExportTextureTransform(dst.emissiveTexture, scale, offset); ExportTextureTransform(dst.normalTexture, scale, offset); ExportTextureTransform(mtoon.ShadeMultiplyTexture, scale, offset); ExportTextureTransform(mtoon.MatcapTexture, scale, offset); ExportTextureTransform(mtoon.RimMultiplyTexture, scale, offset); ExportTextureTransform(mtoon.OutlineWidthMultiplyTexture, scale, offset); ExportTextureTransform(mtoon.UvAnimationMaskTexture, scale, offset); UniGLTF.Extensions.VRMC_materials_mtoon.GltfSerializer.SerializeTo(ref dst.extensions, mtoon); return(true); } catch (Exception e) { dst = null; return(false); } }
public static glTF_VRM_Material CreateFromMaterial(Material m, ITextureExporter textureExporter) { var material = new glTF_VRM_Material { name = m.name, shader = m.shader.name, renderQueue = m.renderQueue, }; if (m.shader.name != MToon.Utils.ShaderName) { material.shader = glTF_VRM_Material.VRM_USE_GLTFSHADER; return(material); } var prop = PreShaderPropExporter.GetPropsForMToon(); if (prop == null) { throw new Exception("arienai"); } else { foreach (var keyword in m.shaderKeywords) { material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword)); } // get properties //material.SetProp(prop); foreach (var kv in prop.Properties) { switch (kv.ShaderPropertyType) { case ShaderPropertyType.Color: { // No color conversion. Because color property is serialized to raw float array. var value = m.GetColor(kv.Key).ToFloat4(ColorSpace.Linear, ColorSpace.Linear); material.vectorProperties.Add(kv.Key, value); } break; case ShaderPropertyType.Range: case ShaderPropertyType.Float: { var value = m.GetFloat(kv.Key); material.floatProperties.Add(kv.Key, value); } break; case ShaderPropertyType.TexEnv: { var texture = m.GetTexture(kv.Key); if (texture != null) { var value = -1; var isNormalMap = kv.Key == "_BumpMap"; if (isNormalMap) { value = textureExporter.RegisterExportingAsNormal(texture); } else { var needsAlpha = kv.Key == "_MainTex"; value = textureExporter.RegisterExportingAsSRgb(texture, needsAlpha); } if (value == -1) { Debug.LogFormat("not found {0}", texture.name); } else { material.textureProperties.Add(kv.Key, value); } } // offset & scaling var offset = m.GetTextureOffset(kv.Key); var scaling = m.GetTextureScale(kv.Key); material.vectorProperties.Add(kv.Key, new float[] { offset.x, offset.y, scaling.x, scaling.y }); } break; case ShaderPropertyType.Vector: { var value = m.GetVector(kv.Key).ToArray(); material.vectorProperties.Add(kv.Key, value); } break; default: throw new NotImplementedException(); } } } foreach (var tag in TAGS) { var value = m.GetTag(tag, false); if (!String.IsNullOrEmpty(value)) { material.tagMap.Add(tag, value); } } return(material); }