public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_Material vrmMaterial, string textureKey, out (SubAssetKey, TextureDescriptor) texture) { // 任意の shader の import を許容する if (/*vrmMaterial.shader == MToon.Utils.ShaderName &&*/ vrmMaterial.textureProperties.TryGetValue(textureKey, out var textureIdx)) { var(offset, scale) = (new Vector2(0, 0), new Vector2(1, 1)); if (TryGetTextureOffsetAndScale(vrmMaterial, textureKey, out var os)) { offset = os.offset; scale = os.scale; } switch (textureKey) { case MToon.Utils.PropBumpMap: texture = GltfTextureImporter.CreateNormal(data, textureIdx, offset, scale); break; default: texture = GltfTextureImporter.CreateSrgb(data, textureIdx, offset, scale); break; } return(true); } texture = default; return(false); }
public async Task <VRMMetaObject> ReadMetaAsync(IAwaitCaller awaitCaller = null, bool createThumbnail = false) { awaitCaller = awaitCaller ?? new ImmediateCaller(); var meta = ScriptableObject.CreateInstance <VRMMetaObject>(); meta.name = "Meta"; meta.ExporterVersion = VRM.exporterVersion; var gltfMeta = VRM.meta; meta.Version = gltfMeta.version; // model version meta.Author = gltfMeta.author; meta.ContactInformation = gltfMeta.contactInformation; meta.Reference = gltfMeta.reference; meta.Title = gltfMeta.title; if (gltfMeta.texture >= 0) { var(key, param) = GltfTextureImporter.CreateSRGB(Data, gltfMeta.texture, Vector2.zero, Vector2.one); meta.Thumbnail = await TextureFactory.GetTextureAsync(param, awaitCaller) as Texture2D; } meta.AllowedUser = gltfMeta.allowedUser; meta.ViolentUssage = gltfMeta.violentUssage; meta.SexualUssage = gltfMeta.sexualUssage; meta.CommercialUssage = gltfMeta.commercialUssage; meta.OtherPermissionUrl = gltfMeta.otherPermissionUrl; meta.LicenseType = gltfMeta.licenseType; meta.OtherLicenseUrl = gltfMeta.otherLicenseUrl; return(meta); }
private static (Vector2, Vector2) GetTextureOffsetAndScale(Vrm10TextureInfo textureInfo) { if (glTF_KHR_texture_transform.TryGet(textureInfo, out var textureTransform)) { return(GltfTextureImporter.GetTextureOffsetAndScale(textureTransform)); } return(new Vector2(0, 0), new Vector2(1, 1)); }
private static bool TryGetThumbnailTexture(GltfData data, glTF_VRM_extensions vrm, out (SubAssetKey, TextureDescriptor) texture) { if (vrm.meta.texture > -1) { texture = GltfTextureImporter.CreateSRGB(data, vrm.meta.texture, Vector2.zero, Vector2.one); return(true); } texture = default; return(false); }
public static (SubAssetKey, TextureImportParam) Create(GltfParser parser, int index, Vector2 offset, Vector2 scale, string prop, float metallicFactor, float roughnessFactor) { switch (prop) { case TextureImportParam.NORMAL_PROP: return(GltfTextureImporter.CreateNormal(parser, index, offset, scale)); default: return(GltfTextureImporter.CreateSRGB(parser, index, offset, scale)); case TextureImportParam.OCCLUSION_PROP: case TextureImportParam.METALLIC_GLOSS_PROP: throw new NotImplementedException(); } }
/// <summary> /// VRM-1 の thumbnail テクスチャー。gltf.textures ではなく gltf.images の参照であることに注意(sampler等の設定が無い) /// </summary> public static bool TryGetMetaThumbnailTextureImportParam(GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, out (SubAssetKey, TextureImportParam) value) { if (vrm?.Meta?.ThumbnailImage == null) { value = default; return(false); } var imageIndex = vrm.Meta.ThumbnailImage.Value; var gltfImage = parser.GLTF.images[imageIndex]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfImage.name, gltfImage.uri); GetTextureBytesAsync getThumbnailImageBytesAsync = () => { var bytes = parser.GLTF.GetImageBytes(parser.Storage, imageIndex); return(Task.FromResult(GltfTextureImporter.ToArray(bytes))); }; var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, Vector2.zero, Vector2.one, default, TextureImportTypes.sRGB, default, default,
private static bool TryGetLinearTexture(GltfParser parser, Vrm10TextureInfo info, out (SubAssetKey, TextureImportParam) pair) { try { var(offset, scale) = GetTextureOffsetAndScale(info); pair = GltfTextureImporter.CreateLinear(parser, info.index, offset, scale); return(true); } catch (NullReferenceException) { pair = default; return(false); } catch (ArgumentOutOfRangeException) { pair = default; return(false); } }
private static bool TryGetSRGBTexture(GltfData data, Vrm10TextureInfo info, out (SubAssetKey, TextureDescriptor) pair) { try { var(offset, scale) = GetTextureOffsetAndScale(info); pair = GltfTextureImporter.CreateSrgb(data, info.index, offset, scale); return(true); } catch (NullReferenceException) { pair = default; return(false); } catch (ArgumentOutOfRangeException) { pair = default; return(false); } }
/// <summary> /// VRM-1 の thumbnail テクスチャー。gltf.textures ではなく gltf.images の参照であることに注意(sampler等の設定が無い) /// /// MToonとは無関係だがとりあえずここに /// </summary> /// <param name="parser"></param> /// <param name="vrm"></param> /// <param name="value"></param> /// <returns></returns> public static bool TryGetMetaThumbnailTextureImportParam(GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, out TextureImportParam value) { if (!vrm.Meta.ThumbnailImage.HasValue) { value = default; return(false); } // thumbnail var imageIndex = vrm.Meta.ThumbnailImage.Value; var gltfImage = parser.GLTF.images[imageIndex]; var name = new TextureImportName(TextureImportTypes.sRGB, gltfImage.name, gltfImage.GetExt(), ""); GetTextureBytesAsync getBytesAsync = () => { var bytes = parser.GLTF.GetImageBytes(parser.Storage, imageIndex); return(Task.FromResult(GltfTextureImporter.ToArray(bytes))); }; value = new TextureImportParam(name, Vector2.zero, Vector2.one, default, TextureImportTypes.sRGB, default, default,
/// <summary> /// VRM-1 の thumbnail テクスチャー。gltf.textures ではなく gltf.images の参照であることに注意(sampler等の設定が無い) /// </summary> public static bool TryGetMetaThumbnailTextureImportParam(GltfData data, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, out (SubAssetKey, TextureDescriptor) value) { if (vrm?.Meta?.ThumbnailImage == null) { value = default; return(false); } var thumbnailImage = vrm.Meta.ThumbnailImage; if (!thumbnailImage.HasValue) { value = default; return(false); } var imageIndex = thumbnailImage.Value; if (imageIndex < 0 || imageIndex >= data.GLTF.images.Count) { value = default; return(false); } var gltfImage = data.GLTF.images[imageIndex]; // data.GLTF.textures は前処理によりユニーク性がある // unique な名前を振り出す var used = new HashSet <string>(data.GLTF.textures.Select(x => x.name)); var imageName = gltfImage.name; if (string.IsNullOrEmpty(imageName)) { imageName = THUMBNAIL_NAME; } var uniqueName = GlbLowLevelParser.FixNameUnique(used, imageName); value = GltfTextureImporter.CreateSrgbFromOnlyImage(data, imageIndex, uniqueName, gltfImage.uri); return(true); }
/// <summary> /// VMRC_materials_mtoon の場合にマテリアル生成情報を作成する /// </summary> /// <param name="parser"></param> /// <param name="i"></param> /// <param name="param"></param> /// <returns></returns> public static bool TryCreateParam(GltfParser parser, int i, out MaterialImportParam param) { var m = parser.GLTF.materials[i]; if (!UniGLTF.Extensions.VRMC_materials_mtoon.GltfDeserializer.TryGet(m.extensions, out UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon mtoon)) { // fallback to gltf param = default; return(false); } // use material.name, because material name may renamed in GltfParser. param = new MaterialImportParam(m.name, MToon.Utils.ShaderName); param.Actions.Add(material => { // Texture 以外をここで設定。Texture は TextureSlots へ { // material.SetFloat(PropVersion, mtoon.Version); } { // var rendering = mtoon.Rendering; // SetRenderMode(material, rendering.RenderMode, rendering.RenderQueueOffsetNumber, // useDefaultRenderQueue: false); // SetCullMode(material, rendering.CullMode); } { // var color = mtoon.Color; material.SetColor(MToon.Utils.PropColor, m.pbrMetallicRoughness.baseColorFactor.ToColor4()); material.SetColor(MToon.Utils.PropShadeColor, mtoon.ShadeFactor.ToColor3()); material.SetFloat(MToon.Utils.PropCutoff, m.alphaCutoff); } { { material.SetFloat(MToon.Utils.PropShadeShift, mtoon.ShadingShiftFactor.Value); material.SetFloat(MToon.Utils.PropShadeToony, mtoon.ShadingToonyFactor.Value); // material.SetFloat(PropReceiveShadowRate, mtoon.prop.ShadowReceiveMultiplierValue); // material.SetFloat(PropShadingGradeRate, mtoon.mix prop.LitAndShadeMixingMultiplierValue); } { material.SetFloat(MToon.Utils.PropLightColorAttenuation, mtoon.LightColorAttenuationFactor.Value); material.SetFloat(MToon.Utils.PropIndirectLightIntensity, mtoon.GiIntensityFactor.Value); } } { material.SetColor(MToon.Utils.PropEmissionColor, m.emissiveFactor.ToColor3()); } { material.SetColor(MToon.Utils.PropRimColor, mtoon.RimFactor.ToColor3()); material.SetFloat(MToon.Utils.PropRimLightingMix, mtoon.RimLightingMixFactor.Value); material.SetFloat(MToon.Utils.PropRimFresnelPower, mtoon.RimFresnelPowerFactor.Value); material.SetFloat(MToon.Utils.PropRimLift, mtoon.RimLiftFactor.Value); } { material.SetFloat(MToon.Utils.PropOutlineWidth, mtoon.OutlineWidthFactor.Value); material.SetFloat(MToon.Utils.PropOutlineScaledMaxDistance, mtoon.OutlineScaledMaxDistanceFactor.Value); material.SetColor(MToon.Utils.PropOutlineColor, mtoon.OutlineFactor.ToColor3()); material.SetFloat(MToon.Utils.PropOutlineLightingMix, mtoon.OutlineLightingMixFactor.Value); // private // MToon.Utils.SetOutlineMode(material, outline.OutlineWidthMode, outline.OutlineColorMode); } { // material.SetTextureScale(PropMainTex, mtoon.MainTextureLeftBottomOriginScale); // material.SetTextureOffset(PropMainTex, mtoon.MainTextureLeftBottomOriginOffset); material.SetFloat(MToon.Utils.PropUvAnimScrollX, mtoon.UvAnimationScrollXSpeedFactor.Value); material.SetFloat(MToon.Utils.PropUvAnimScrollY, mtoon.UvAnimationScrollYSpeedFactor.Value); material.SetFloat(MToon.Utils.PropUvAnimRotation, mtoon.UvAnimationRotationSpeedFactor.Value); } MToon.Utils.ValidateProperties(material, isBlendModeChangedByUser: false); }); // SetTexture(material, PropMainTex, color.LitMultiplyTexture); // SetNormalMapping(material, prop.NormalTexture, prop.NormalScaleValue); // SetTexture(material, PropEmissionMap, emission.EmissionMultiplyTexture); if (m.pbrMetallicRoughness != null) { // base color if (m.pbrMetallicRoughness?.baseColorTexture != null) { param.TextureSlots.Add("_MainTex", GltfPBRMaterial.BaseColorTexture(parser, m)); } } if (m.normalTexture != null && m.normalTexture.index != -1) { // normal map param.Actions.Add(material => material.EnableKeyword("_NORMALMAP")); var textureParam = GltfPBRMaterial.NormalTexture(parser, m); param.TextureSlots.Add("_BumpMap", textureParam); param.FloatValues.Add("_BumpScale", m.normalTexture.scale); } if (m.emissiveTexture != null && m.emissiveTexture.index != -1) { var(offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(m.emissiveTexture); var textureParam = GltfTextureImporter.CreateSRGB(parser, m.emissiveTexture.index, offset, scale); param.TextureSlots.Add("_EmissionMap", textureParam); } // TODO: if (mtoon.ShadeMultiplyTexture.HasValue) { var textureParam = GltfTextureImporter.CreateSRGB(parser, mtoon.ShadeMultiplyTexture.Value, Vector2.zero, Vector2.one); param.TextureSlots.Add("_ShadeTexture", textureParam); } if (mtoon.OutlineWidthMultiplyTexture.HasValue) { var textureParam = GltfTextureImporter.CreateSRGB(parser, mtoon.OutlineWidthMultiplyTexture.Value, Vector2.zero, Vector2.one); param.TextureSlots.Add("_OutlineWidthTexture", textureParam); } if (mtoon.AdditiveTexture.HasValue) { var textureParam = GltfTextureImporter.CreateSRGB(parser, mtoon.AdditiveTexture.Value, Vector2.zero, Vector2.one); param.TextureSlots.Add("_SphereAdd", textureParam); } if (mtoon.RimMultiplyTexture.HasValue) { var textureParam = GltfTextureImporter.CreateSRGB(parser, mtoon.RimMultiplyTexture.Value, Vector2.zero, Vector2.one); param.TextureSlots.Add("_RimTexture", textureParam);; } if (mtoon.UvAnimationMaskTexture.HasValue) { var textureParam = GltfTextureImporter.CreateSRGB(parser, mtoon.UvAnimationMaskTexture.Value, Vector2.zero, Vector2.one); param.TextureSlots.Add("_UvAnimMaskTexture", textureParam); } return(true); }