static VrmLib.PBRMaterial ExportStandard(Material src, GetOrCreateTextureDelegate map) { var material = new VrmLib.PBRMaterial(src.name) { }; switch (src.GetTag("RenderType", true)) { case "Transparent": material.AlphaMode = VrmLib.AlphaModeType.BLEND; break; case "TransparentCutout": material.AlphaMode = VrmLib.AlphaModeType.MASK; material.AlphaCutoff = src.GetFloat("_Cutoff"); break; default: material.AlphaMode = VrmLib.AlphaModeType.OPAQUE; break; } if (src.HasProperty("_Color")) { material.BaseColorFactor = src.color.linear.FromUnitySrgbToLinear(); } if (src.HasProperty("_MainTex")) { material.BaseColorTexture = map(src, src.GetTexture("_MainTex"), VrmLib.Texture.ColorSpaceTypes.Srgb, VrmLib.Texture.TextureTypes.Default); } if (src.HasProperty("_MetallicGlossMap")) { // float smoothness = 0.0f; // if (m.HasProperty("_GlossMapScale")) // { // smoothness = m.GetFloat("_GlossMapScale"); // } material.MetallicRoughnessTexture = map( src, src.GetTexture("_MetallicGlossMap"), VrmLib.Texture.ColorSpaceTypes.Linear, VrmLib.Texture.TextureTypes.MetallicRoughness)?.Texture; if (material.MetallicRoughnessTexture != null) { material.MetallicFactor = 1.0f; // Set 1.0f as hard-coded. See: https://github.com/vrm-c/UniVRM/issues/212. material.RoughnessFactor = 1.0f; } } if (material.MetallicRoughnessTexture == null) { if (src.HasProperty("_Metallic")) { material.MetallicFactor = src.GetFloat("_Metallic"); } if (src.HasProperty("_Glossiness")) { material.RoughnessFactor = 1.0f - src.GetFloat("_Glossiness"); } } if (src.HasProperty("_BumpMap")) { material.NormalTexture = map(src, src.GetTexture("_BumpMap"), VrmLib.Texture.ColorSpaceTypes.Linear, VrmLib.Texture.TextureTypes.NormalMap)?.Texture; if (src.HasProperty("_BumpScale")) { material.NormalTextureScale = src.GetFloat("_BumpScale"); } } if (src.HasProperty("_OcclusionMap")) { material.OcclusionTexture = map(src, src.GetTexture("_OcclusionMap"), VrmLib.Texture.ColorSpaceTypes.Linear, VrmLib.Texture.TextureTypes.Occlusion)?.Texture; if (src.HasProperty("_OcclusionStrength")) { material.OcclusionTextureStrength = src.GetFloat("_OcclusionStrength"); } } if (src.IsKeywordEnabled("_EMISSION")) { if (src.HasProperty("_EmissionColor")) { var color = src.GetColor("_EmissionColor"); if (color.maxColorComponent > 1) { color /= color.maxColorComponent; } material.EmissiveFactor = new System.Numerics.Vector3(color.r, color.g, color.b); } if (src.HasProperty("_EmissionMap")) { material.EmissiveTexture = map(src, src.GetTexture("_EmissionMap"), VrmLib.Texture.ColorSpaceTypes.Srgb, VrmLib.Texture.TextureTypes.Emissive)?.Texture; } } return(material); }
/// <summary> /// MToon.MToonDefinition(Unity) を VrmLib.MToon.MToonDefinition に変換する /// <summary> public static VrmLib.MToon.MToonDefinition ToVrmLib(this global::MToon.MToonDefinition unity, Material material, GetOrCreateTextureDelegate getOrCreateTexture) { return(new VrmLib.MToon.MToonDefinition { Color = new VrmLib.MToon.ColorDefinition { CutoutThresholdValue = unity.Color.CutoutThresholdValue, LitColor = unity.Color.LitColor.FromUnitySrgbToLinear(), LitMultiplyTexture = unity.Color.LitMultiplyTexture.ToVrmLib(getOrCreateTexture, material, VrmLib.Texture.ColorSpaceTypes.Srgb), ShadeColor = unity.Color.ShadeColor.FromUnitySrgbToLinear(), ShadeMultiplyTexture = unity.Color.ShadeMultiplyTexture.ToVrmLib(getOrCreateTexture, material, VrmLib.Texture.ColorSpaceTypes.Srgb), }, Emission = new VrmLib.MToon.EmissionDefinition { EmissionColor = unity.Emission.EmissionColor.FromUnityLinear(), EmissionMultiplyTexture = unity.Emission.EmissionMultiplyTexture.ToVrmLib(getOrCreateTexture, material, VrmLib.Texture.ColorSpaceTypes.Srgb), }, Lighting = new VrmLib.MToon.LightingDefinition { LightingInfluence = new VrmLib.MToon.LightingInfluenceDefinition { GiIntensityValue = unity.Lighting.LightingInfluence.GiIntensityValue, LightColorAttenuationValue = unity.Lighting.LightingInfluence.LightColorAttenuationValue, }, LitAndShadeMixing = new VrmLib.MToon.LitAndShadeMixingDefinition { ShadingShiftValue = unity.Lighting.LitAndShadeMixing.ShadingShiftValue, ShadingToonyValue = unity.Lighting.LitAndShadeMixing.ShadingToonyValue, }, Normal = new VrmLib.MToon.NormalDefinition { NormalScaleValue = unity.Lighting.Normal.NormalScaleValue, NormalTexture = unity.Lighting.Normal.NormalTexture.ToVrmLib(getOrCreateTexture, material, VrmLib.Texture.ColorSpaceTypes.Linear, VrmLib.Texture.TextureTypes.NormalMap), }, }, MatCap = new VrmLib.MToon.MatCapDefinition { AdditiveTexture = unity.MatCap.AdditiveTexture.ToVrmLib(getOrCreateTexture, material, VrmLib.Texture.ColorSpaceTypes.Srgb), }, Meta = new VrmLib.MToon.MetaDefinition { Implementation = unity.Meta.Implementation, VersionNumber = unity.Meta.VersionNumber, }, Outline = new VrmLib.MToon.OutlineDefinition { OutlineColor = unity.Outline.OutlineColor.FromUnitySrgbToLinear(), OutlineColorMode = (VrmLib.MToon.OutlineColorMode)unity.Outline.OutlineColorMode, OutlineLightingMixValue = unity.Outline.OutlineLightingMixValue, OutlineScaledMaxDistanceValue = unity.Outline.OutlineScaledMaxDistanceValue, OutlineWidthMode = (VrmLib.MToon.OutlineWidthMode)unity.Outline.OutlineWidthMode, OutlineWidthMultiplyTexture = unity.Outline.OutlineWidthMultiplyTexture.ToVrmLib(getOrCreateTexture, material, VrmLib.Texture.ColorSpaceTypes.Linear), OutlineWidthValue = unity.Outline.OutlineWidthValue, }, Rendering = new VrmLib.MToon.RenderingDefinition { CullMode = (VrmLib.MToon.CullMode)unity.Rendering.CullMode, RenderMode = (VrmLib.MToon.RenderMode)unity.Rendering.RenderMode, RenderQueueOffsetNumber = unity.Rendering.RenderQueueOffsetNumber, }, Rim = new VrmLib.MToon.RimDefinition { RimColor = unity.Rim.RimColor.FromUnityLinear(), RimFresnelPowerValue = unity.Rim.RimFresnelPowerValue, RimLiftValue = unity.Rim.RimLiftValue, RimLightingMixValue = unity.Rim.RimLightingMixValue, RimMultiplyTexture = unity.Rim.RimMultiplyTexture.ToVrmLib(getOrCreateTexture, material, VrmLib.Texture.ColorSpaceTypes.Srgb), }, TextureOption = new VrmLib.MToon.TextureUvCoordsDefinition { MainTextureLeftBottomOriginOffset = unity.TextureOption.MainTextureLeftBottomOriginOffset.ToNumericsVector2(), MainTextureLeftBottomOriginScale = unity.TextureOption.MainTextureLeftBottomOriginScale.ToNumericsVector2(), UvAnimationMaskTexture = unity.TextureOption.UvAnimationMaskTexture.ToVrmLib(getOrCreateTexture, material, VrmLib.Texture.ColorSpaceTypes.Linear), UvAnimationRotationSpeedValue = unity.TextureOption.UvAnimationRotationSpeedValue, UvAnimationScrollXSpeedValue = unity.TextureOption.UvAnimationScrollXSpeedValue, UvAnimationScrollYSpeedValue = unity.TextureOption.UvAnimationScrollYSpeedValue, }, }); }
public VrmLib.Material Export10(Material src, GetOrCreateTextureDelegate map) { switch (src.shader.name) { case "VRM/MToon": { var def = MToon.Utils.GetMToonParametersFromMaterial(src); return(new VrmLib.MToonMaterial(src.name) { Definition = def.ToVrmLib(src, map), }); } case "Unlit/Color": return(new VrmLib.UnlitMaterial(src.name) { BaseColorFactor = src.color.FromUnitySrgbToLinear(), }); case "Unlit/Texture": return(new VrmLib.UnlitMaterial(src.name) { BaseColorTexture = map(src, src.mainTexture as Texture2D, VrmLib.Texture.ColorSpaceTypes.Srgb, VrmLib.Texture.TextureTypes.Default), }); case "Unlit/Transparent": return(new VrmLib.UnlitMaterial(src.name) { BaseColorTexture = map(src, src.mainTexture as Texture2D, VrmLib.Texture.ColorSpaceTypes.Srgb, VrmLib.Texture.TextureTypes.Default), AlphaMode = VrmLib.AlphaModeType.BLEND, }); case "Unlit/Transparent Cutout": return(new VrmLib.UnlitMaterial(src.name) { BaseColorTexture = map(src, src.mainTexture as Texture2D, VrmLib.Texture.ColorSpaceTypes.Srgb, VrmLib.Texture.TextureTypes.Default), AlphaMode = VrmLib.AlphaModeType.MASK, AlphaCutoff = src.GetFloat("_Cutoff"), }); case "UniGLTF/UniUnlit": case "VRM/UniUnlit": { var material = new VrmLib.UnlitMaterial(src.name) { BaseColorFactor = src.color.FromUnitySrgbToLinear(), BaseColorTexture = map(src, src.mainTexture as Texture2D, VrmLib.Texture.ColorSpaceTypes.Srgb, VrmLib.Texture.TextureTypes.Default), AlphaMode = GetAlphaMode(src), DoubleSided = UniGLTF.UniUnlit.Utils.GetCullMode(src) == UniGLTF.UniUnlit.UniUnlitCullMode.Off, }; if (material.AlphaMode == VrmLib.AlphaModeType.MASK) { material.AlphaCutoff = src.GetFloat("_Cutoff"); } // TODO: VertexColorMode return(material); } default: return(ExportStandard(src, map)); } }
static VrmLib.TextureInfo ToVrmLib(this Texture2D src, GetOrCreateTextureDelegate map, Material material, VrmLib.Texture.ColorSpaceTypes colorSpace, VrmLib.Texture.TextureTypes textureType = VrmLib.Texture.TextureTypes.Default) { return(map(material, src, colorSpace, textureType)); }