public static void LoadCommonParams(this Material self, VrmProtobuf.Material material, List <Texture> textures) { var pbr = material.PbrMetallicRoughness; if (pbr.BaseColorFactor.Count > 0) { self.BaseColorFactor = LinearColor.FromLiner( pbr.BaseColorFactor[0], pbr.BaseColorFactor[1], pbr.BaseColorFactor[2], pbr.BaseColorFactor[3]); } var baseColorTexture = pbr.BaseColorTexture; if (baseColorTexture != null && baseColorTexture.Index.TryGetValidIndex(textures.Count, out int index)) { self.BaseColorTexture = new TextureInfo(textures[index]); } self.AlphaMode = EnumUtil.Cast <VrmLib.AlphaModeType>(material.AlphaMode); self.AlphaCutoff = material.AlphaCutoff.HasValue ? material.AlphaCutoff.Value : 0.5f // gltf default ; self.DoubleSided = material.DoubleSided.HasValue ? material.DoubleSided.Value : false // gltf default ; }
public static UnlitMaterial UnlitFromGltf(VrmProtobuf.Material material, List <Texture> textures) { var unlit = new UnlitMaterial(material.Name); unlit.LoadCommonParams(material, textures); return(unlit); }
static VrmProtobuf.Material ToGltf(this VrmLib.Material src, string name, List <Texture> textures) { var material = new VrmProtobuf.Material { Name = name, Extensions = new VrmProtobuf.Material.Types.Extensions { KHRMaterialsUnlit = new VrmProtobuf.KHR_materials_unlit(), }, PbrMetallicRoughness = new VrmProtobuf.MaterialPbrMetallicRoughness { }, AlphaMode = src.AlphaMode.ToString(), AlphaCutoff = src.AlphaCutoff, DoubleSided = src.DoubleSided, }; src.BaseColorFactor.ToProtobuf(material.PbrMetallicRoughness.BaseColorFactor.Add, true); if (src.BaseColorTexture != null) { material.PbrMetallicRoughness.BaseColorTexture = new VrmProtobuf.TextureInfo { Index = textures.IndexOfNullable(src.BaseColorTexture.Texture), }; } return(material); }
static VrmProtobuf.Image GetNormalImage(Vrm10Storage storage, VrmProtobuf.Material m) { if (m.NormalTexture == null) { return(null); } if (!m.NormalTexture.Index.TryGetValidIndex(storage.TextureCount, out int index)) { return(null); } return(GetTexture(storage, index)); }
static VrmProtobuf.Image GetColorImage(Vrm10Storage storage, VrmProtobuf.Material m) { if (m.PbrMetallicRoughness == null) { return(null); } if (m.PbrMetallicRoughness.BaseColorTexture == null) { return(null); } if (!m.PbrMetallicRoughness.BaseColorTexture.Index.TryGetValidIndex(storage.TextureCount, out int index)) { return(null); } return(GetTexture(storage, index)); }
public void MaterialTest() { var settings = Google.Protobuf.JsonFormatter.Settings.Default.WithPreserveProtoFieldNames(true); var q = "\""; { var data = new VrmProtobuf.Material { Name = "Some", }; var json = new Google.Protobuf.JsonFormatter(settings).Format(data); Assert.AreEqual($"{{ {q}name{q}: {q}Some{q} }}", json); } { var data = new VrmProtobuf.glTF(); data.Textures.Add(new VrmProtobuf.Texture { }); var json = new Google.Protobuf.JsonFormatter(settings).Format(data); // Assert.Equal($"{{ {q}name{q}: {q}Some{q} }}", json); } { var data = new VrmProtobuf.Material { Name = "Alicia_body", PbrMetallicRoughness = new VrmProtobuf.MaterialPbrMetallicRoughness { // BaseColorFactor = new[] { 1, 1, 1, 1 }, // BaseColorTexture= { }, MetallicFactor = 0, RoughnessFactor = 0.9f }, AlphaMode = "OPAQUE", AlphaCutoff = 0.5f, Extensions = new VrmProtobuf.Material.Types.Extensions { KHRMaterialsUnlit = { } } }; var json = new Google.Protobuf.JsonFormatter(settings).Format(data); // Assert.Equal($"{{ {q}name{q}: {q}Some{q} }}", json); } }
public static Material FromGltf(this VrmProtobuf.Material x, List <Texture> textures) { if (x.Extensions != null && x.Extensions.VRMCMaterialsMtoon != null) { // MToon return(MToonAdapter.MToonFromGltf(x, textures)); } else if (x.Extensions != null && x.Extensions.KHRMaterialsUnlit != null) { // unlit return(UnlitFromGltf(x, textures)); } else { // PBR return(PBRFromGltf(x, textures)); } }
static VrmProtobuf.Material ToGltf(this VrmLib.Material src, List <Texture> textures) { var material = new VrmProtobuf.Material { Name = src.Name, PbrMetallicRoughness = new VrmProtobuf.MaterialPBRMetallicRoughness { }, AlphaMode = EnumUtil.Cast <VrmProtobuf.Material.Types.alphaModeType>(src.AlphaMode), AlphaCutoff = src.AlphaCutoff, DoubleSided = src.DoubleSided, }; src.BaseColorFactor.ToProtobuf(material.PbrMetallicRoughness.BaseColorFactor.Add, true); if (src.BaseColorTexture != null) { material.PbrMetallicRoughness.BaseColorTexture = new VrmProtobuf.TextureInfo { Index = textures.IndexOfNullable(src.BaseColorTexture.Texture), }; } return(material); }
public static PBRMaterial PBRFromGltf(VrmProtobuf.Material material, List <Texture> textures) { var self = new PBRMaterial(material.Name); self.LoadCommonParams(material, textures); // // pbr // var pbr = material.PbrMetallicRoughness; // metallic roughness self.MetallicFactor = pbr.MetallicFactor.HasValue ? pbr.MetallicFactor.Value : 1.0f // gltf default ; self.RoughnessFactor = pbr.RoughnessFactor.HasValue ? pbr.RoughnessFactor.Value : 1.0f // gltf default ; var metallicRoughnessTexture = pbr.MetallicRoughnessTexture; if (metallicRoughnessTexture != null && metallicRoughnessTexture.Index.TryGetValidIndex(textures.Count, out int metallicRoughnessTextureIndex)) { self.MetallicRoughnessTexture = textures[metallicRoughnessTextureIndex]; } // // emissive // if (material.EmissiveFactor.Count > 0) { self.EmissiveFactor = new Vector3( material.EmissiveFactor[0], material.EmissiveFactor[1], material.EmissiveFactor[2]); } var emissiveTexture = material.EmissiveTexture; if (emissiveTexture != null && emissiveTexture.Index.TryGetValidIndex(textures.Count, out int emissiveTextureIndex)) { self.EmissiveTexture = textures[emissiveTextureIndex]; } // // normal // var normalTexture = material.NormalTexture; if (normalTexture != null && normalTexture.Index.TryGetValidIndex(textures.Count, out int normalTextureIndex)) { self.NormalTexture = textures[normalTextureIndex]; } // // occlusion // var occlusionTexture = material.OcclusionTexture; if (occlusionTexture != null && occlusionTexture.Index.TryGetValidIndex(textures.Count, out int occlusionTextureIndex)) { self.OcclusionTexture = textures[occlusionTextureIndex]; } return(self); }
public static MToonMaterial MToonFromGltf(VrmProtobuf.Material material, List <Texture> textures) { var mtoon = new MToonMaterial(material.Name); var extension = material.Extensions.VRMCMaterialsMtoon; mtoon.Definition = new MToonDefinition { Meta = new MetaDefinition { Implementation = "Santarh/MToon", }, Color = new ColorDefinition { LitColor = material.PbrMetallicRoughness.BaseColorFactor.ToLinearColor(Nan), ShadeColor = extension.ShadeFactor.ToLinearColor(Nan), ShadeMultiplyTexture = extension.ShadeMultiplyTexture.GetTexture(textures), CutoutThresholdValue = material.AlphaCutoff.Value, }, Outline = new OutlineDefinition { OutlineColorMode = (OutlineColorMode)extension.OutlineColorMode, OutlineColor = extension.OutlineFactor.ToLinearColor(Nan), OutlineLightingMixValue = extension.OutlineLightingMixFactor.Value, OutlineScaledMaxDistanceValue = extension.OutlineScaledMaxDistanceFactor.Value, OutlineWidthMode = (OutlineWidthMode)extension.OutlineWidthMode, OutlineWidthValue = extension.OutlineWidthFactor.Value, OutlineWidthMultiplyTexture = extension.OutlineWidthMultiplyTexture.GetTexture(textures), }, Emission = new EmissionDefinition { EmissionColor = material.EmissiveFactor.ToLinearColor(Nan), }, Lighting = new LightingDefinition { LightingInfluence = new LightingInfluenceDefinition { GiIntensityValue = extension.GiIntensityFactor.Value, LightColorAttenuationValue = extension.LightColorAttenuationFactor.Value, }, LitAndShadeMixing = new LitAndShadeMixingDefinition { ShadingShiftValue = extension.ShadingShiftFactor.Value, ShadingToonyValue = extension.ShadingToonyFactor.Value, }, Normal = new NormalDefinition { }, }, MatCap = new MatCapDefinition { AdditiveTexture = extension.AdditiveTexture.GetTexture(textures) }, Rendering = new RenderingDefinition { CullMode = material.DoubleSided.Value ? CullMode.Off : CullMode.Back, RenderMode = GetRenderMode(material.AlphaMode, extension.TransparentWithZWrite.Value), RenderQueueOffsetNumber = extension.RenderQueueOffsetNumber.Value, }, Rim = new RimDefinition { RimColor = extension.RimFactor.ToLinearColor(Nan), RimMultiplyTexture = extension.RimMultiplyTexture.GetTexture(textures), RimLiftValue = extension.RimLiftFactor.Value, RimFresnelPowerValue = extension.RimFresnelPowerFactor.Value, RimLightingMixValue = extension.RimLightingMixFactor.Value, }, TextureOption = new TextureUvCoordsDefinition { UvAnimationMaskTexture = extension.UvAnimationMaskTexture.GetTexture(textures), UvAnimationRotationSpeedValue = extension.UvAnimationRotationSpeedFactor.Value, UvAnimationScrollXSpeedValue = extension.UvAnimationScrollXSpeedFactor.Value, UvAnimationScrollYSpeedValue = extension.UvAnimationScrollYSpeedFactor.Value, }, }; if (material.PbrMetallicRoughness.BaseColorTexture != null) { mtoon.Definition.Color.LitMultiplyTexture = material.PbrMetallicRoughness.BaseColorTexture.Index.GetTexture(textures); if (material.PbrMetallicRoughness.BaseColorTexture.Extensions != null && material.PbrMetallicRoughness.BaseColorTexture.Extensions.KHRTextureTransform != null) { mtoon.Definition.TextureOption.MainTextureLeftBottomOriginOffset = material.PbrMetallicRoughness.BaseColorTexture.Extensions.KHRTextureTransform.Offset.ToVector2(); mtoon.Definition.TextureOption.MainTextureLeftBottomOriginScale = material.PbrMetallicRoughness.BaseColorTexture.Extensions.KHRTextureTransform.Scale.ToVector2(); } } if (material.EmissiveTexture != null) { mtoon.Definition.Emission.EmissionMultiplyTexture = material.EmissiveTexture.Index.GetTexture(textures); } if (material.NormalTexture != null) { mtoon.Definition.Lighting.Normal.NormalScaleValue = material.NormalTexture.Scale.Value; mtoon.Definition.Lighting.Normal.NormalTexture = material.NormalTexture.Index.GetTexture(textures); } return(mtoon); }
public static void SetMToonParametersToMaterial(Material material, MToonDefinition parameters, List <Texture> textures) { var mtoon = material.Extensions.VRMCMaterialsMtoon; { var meta = parameters.Meta; mtoon.Version = meta.VersionNumber.ToString(); } // TODO: // { // var rendering = parameters.Rendering; // ValidateBlendMode(material, rendering.RenderMode, isChangedByUser: true); // ValidateCullMode(material, rendering.CullMode); // ValidateRenderQueue(material, offset: rendering.RenderQueueOffsetNumber); // } { var color = parameters.Color; // SetColor(material, MToonUtils.PropColor, color.LitColor); color.LitColor.ToProtobuf(mtoon.LitFactor.Add, true); // SetTexture(material, MToonUtils.PropMainTex, color.LitMultiplyTexture, textures); mtoon.LitMultiplyTexture = textures.IndexOfNullable(color.LitMultiplyTexture.Texture); // SetColor(material, MToonUtils.PropShadeColor, color.ShadeColor); color.ShadeColor.ToProtobuf(mtoon.ShadeFactor.Add, false); // SetTexture(material, MToonUtils.PropShadeTexture, color.ShadeMultiplyTexture, textures); mtoon.ShadeMultiplyTexture = textures.IndexOfNullable(color.ShadeMultiplyTexture.Texture); // SetValue(material, MToonUtils.PropCutoff, color.CutoutThresholdValue); mtoon.CutoutThresholdFactor = color.CutoutThresholdValue; } { var lighting = parameters.Lighting; { var prop = lighting.LitAndShadeMixing; // SetValue(material, MToonUtils.PropShadeShift, prop.ShadingShiftValue); mtoon.ShadingShiftFactor = prop.ShadingShiftValue; // SetValue(material, MToonUtils.PropShadeToony, prop.ShadingToonyValue); mtoon.ShadingToonyFactor = prop.ShadingToonyValue; } { var prop = lighting.LightingInfluence; // SetValue(material, MToonUtils.PropLightColorAttenuation, prop.LightColorAttenuationValue); mtoon.LightColorAttenuationFactor = prop.LightColorAttenuationValue; // SetValue(material, MToonUtils.PropIndirectLightIntensity, prop.GiIntensityValue); mtoon.GiIntensityFactor = prop.GiIntensityValue; } { var prop = lighting.Normal; // SetTexture(material, MToonUtils.PropBumpMap, prop.NormalTexture, textures); mtoon.NormalTexture = textures.IndexOfNullable(prop.NormalTexture.Texture); // SetValue(material, MToonUtils.PropBumpScale, prop.NormalScaleValue); mtoon.NormalScaleFactor = prop.NormalScaleValue; } } { var emission = parameters.Emission; // SetColor(material, MToonUtils.PropEmissionColor, emission.EmissionColor); emission.EmissionColor.ToProtobuf(mtoon.EmissionFactor.Add, false); // SetTexture(material, MToonUtils.PropEmissionMap, emission.EmissionMultiplyTexture, textures); mtoon.EmissionMultiplyTexture = textures.IndexOfNullable(emission.EmissionMultiplyTexture.Texture); } { var matcap = parameters.MatCap; // SetTexture(material, MToonUtils.PropSphereAdd, matcap.AdditiveTexture, textures); mtoon.AdditiveTexture = textures.IndexOfNullable(matcap.AdditiveTexture.Texture); } { var rim = parameters.Rim; // SetColor(material, MToonUtils.PropRimColor, rim.RimColor); rim.RimColor.ToProtobuf(mtoon.RimFactor.Add, false); // SetTexture(material, MToonUtils.PropRimTexture, rim.RimMultiplyTexture, textures); mtoon.RimMultiplyTexture = textures.IndexOfNullable(rim.RimMultiplyTexture.Texture); // SetValue(material, MToonUtils.PropRimLightingMix, rim.RimLightingMixValue); mtoon.RimLightingMixFactor = rim.RimLightingMixValue; // SetValue(material, MToonUtils.PropRimFresnelPower, rim.RimFresnelPowerValue); mtoon.RimFresnelPowerFactor = rim.RimFresnelPowerValue; // SetValue(material, MToonUtils.PropRimLift, rim.RimLiftValue); mtoon.RimLiftFactor = rim.RimLiftValue; } { var outline = parameters.Outline; // SetValue(material, MToonUtils.PropOutlineWidth, outline.OutlineWidthValue); mtoon.OutlineWidthFactor = outline.OutlineWidthValue; // SetTexture(material, MToonUtils.PropOutlineWidthTexture, outline.OutlineWidthMultiplyTexture, textures); mtoon.OutlineWidthMultiplyTexture = textures.IndexOfNullable(outline.OutlineWidthMultiplyTexture.Texture); // SetValue(material, MToonUtils.PropOutlineScaledMaxDistance, outline.OutlineScaledMaxDistanceValue); mtoon.OutlineScaledMaxDistanceFactor = outline.OutlineScaledMaxDistanceValue; // SetColor(material, MToonUtils.PropOutlineColor, outline.OutlineColor); outline.OutlineColor.ToProtobuf(mtoon.OutlineFactor.Add, false); // SetValue(material, MToonUtils.PropOutlineLightingMix, outline.OutlineLightingMixValue); mtoon.OutlineLightingMixFactor = outline.OutlineLightingMixValue; // ValidateOutlineMode(material, outline.OutlineWidthMode, outline.OutlineColorMode); } { var textureOptions = parameters.TextureOption; // TODO: // material.SetTextureScale(MToonUtils.PropMainTex, textureOptions.MainTextureLeftBottomOriginScale); // material.SetTextureOffset(MToonUtils.PropMainTex, textureOptions.MainTextureLeftBottomOriginOffset); // material.SetTexture(MToonUtils.PropUvAnimMaskTexture, textureOptions.UvAnimationMaskTexture, textures); mtoon.UvAnimationMaskTexture = textures.IndexOfNullable(textureOptions.UvAnimationMaskTexture.Texture); // material.SetFloat(MToonUtils.PropUvAnimScrollX, textureOptions.UvAnimationScrollXSpeedValue); mtoon.UvAnimationScrollXSpeedFactor = textureOptions.UvAnimationScrollXSpeedValue; // material.SetFloat(MToonUtils.PropUvAnimScrollY, textureOptions.UvAnimationScrollYSpeedValue); mtoon.UvAnimationScrollYSpeedFactor = textureOptions.UvAnimationScrollYSpeedValue; // material.SetFloat(MToonUtils.PropUvAnimRotation, textureOptions.UvAnimationRotationSpeedValue); mtoon.UvAnimationRotationSpeedFactor = textureOptions.UvAnimationRotationSpeedValue; } }
public static MToonMaterial MToonFromGltf(VrmProtobuf.Material material, List <Texture> textures) { var mtoon = new MToonMaterial(material.Name); var extension = material.Extensions.VRMCMaterialsMtoon; mtoon.Definition = new MToonDefinition { Meta = new MetaDefinition { Implementation = "Santarh/MToon", }, Color = new ColorDefinition { LitColor = extension.LitFactor.ToLinearColor(Nan), LitMultiplyTexture = extension.LitMultiplyTexture.GetTexture(textures), ShadeColor = extension.ShadeFactor.ToLinearColor(Nan), ShadeMultiplyTexture = extension.ShadeMultiplyTexture.GetTexture(textures), CutoutThresholdValue = extension.CutoutThresholdFactor, }, Outline = new OutlineDefinition { OutlineColorMode = (OutlineColorMode)extension.OutlineColorMode, OutlineColor = extension.OutlineFactor.ToLinearColor(Nan), OutlineLightingMixValue = extension.OutlineLightingMixFactor, OutlineScaledMaxDistanceValue = extension.OutlineScaledMaxDistanceFactor, OutlineWidthMode = (OutlineWidthMode)extension.OutlineWidthMode, OutlineWidthValue = extension.OutlineWidthFactor, OutlineWidthMultiplyTexture = extension.OutlineWidthMultiplyTexture.GetTexture(textures), }, Emission = new EmissionDefinition { EmissionColor = extension.EmissionFactor.ToLinearColor(Nan), EmissionMultiplyTexture = extension.EmissionMultiplyTexture.GetTexture(textures), }, Lighting = new LightingDefinition { LightingInfluence = new LightingInfluenceDefinition { GiIntensityValue = extension.GiIntensityFactor, LightColorAttenuationValue = extension.LightColorAttenuationFactor, }, LitAndShadeMixing = new LitAndShadeMixingDefinition { ShadingShiftValue = extension.ShadingShiftFactor, ShadingToonyValue = extension.ShadingToonyFactor, }, Normal = new NormalDefinition { NormalScaleValue = extension.NormalScaleFactor, NormalTexture = extension.NormalTexture.GetTexture(textures), }, }, MatCap = new MatCapDefinition { AdditiveTexture = extension.AdditiveTexture.GetTexture(textures) }, Rendering = new RenderingDefinition { CullMode = (CullMode)extension.CullMode, RenderMode = (RenderMode)extension.RenderMode, RenderQueueOffsetNumber = extension.RenderQueueOffsetNumber, }, Rim = new RimDefinition { RimColor = extension.RimFactor.ToLinearColor(Nan), RimMultiplyTexture = extension.RimMultiplyTexture.GetTexture(textures), RimLiftValue = extension.RimLiftFactor, RimFresnelPowerValue = extension.RimFresnelPowerFactor, RimLightingMixValue = extension.RimLightingMixFactor, }, TextureOption = new TextureUvCoordsDefinition { UvAnimationMaskTexture = extension.UvAnimationMaskTexture.GetTexture(textures), UvAnimationRotationSpeedValue = extension.UvAnimationRotationSpeedFactor, UvAnimationScrollXSpeedValue = extension.UvAnimationScrollXSpeedFactor, UvAnimationScrollYSpeedValue = extension.UvAnimationScrollYSpeedFactor, MainTextureLeftBottomOriginOffset = extension.MainTextureLeftBottomOriginOffset.ToVector2(), MainTextureLeftBottomOriginScale = extension.MainTextureLeftBottomOriginScale.ToVector2(), }, }; return(mtoon); }