public async Task LoadTexturesAsync(IAwaitCaller awaitCaller = null) { awaitCaller = awaitCaller ?? new ImmediateCaller(); var textures = TextureDescriptorGenerator.Get().GetEnumerable(); foreach (var param in textures) { var tex = await TextureFactory.GetTextureAsync(param, awaitCaller); } }
/// <summary> /// Root ヒエラルキーで使っているリソース /// </summary> /// <returns></returns> public virtual void TransferOwnership(TakeResponsibilityForDestroyObjectFunc take) { foreach (var mesh in Meshes.ToArray()) { take(SubAssetKey.Create(mesh.Mesh), mesh.Mesh); Meshes.Remove(mesh); } AnimationClipFactory.TransferOwnership(take); TextureFactory.TransferOwnership(take); MaterialFactory.TransferOwnership(take); }
/// <summary> /// ImporterContextが所有する UnityEngine.Object を破棄する /// </summary> public virtual void Dispose() { foreach (var x in Meshes) { UnityObjectDestoyer.DestroyRuntimeOrEditor(x.Mesh); } Meshes.Clear(); AnimationClipFactory?.Dispose(); MaterialFactory?.Dispose(); TextureFactory?.Dispose(); }
public async Task LoadTexturesAsync(IAwaitCaller awaitCaller) { if (awaitCaller == null) { throw new ArgumentNullException(); } var textures = TextureDescriptorGenerator.Get().GetEnumerable(); foreach (var param in textures) { var tex = await TextureFactory.GetTextureAsync(param, awaitCaller); } }
public static TextureImportParam StandardTexture(GltfParser parser, glTFMaterial src) { var metallicFactor = 1.0f; var roughnessFactor = 1.0f; if (src.pbrMetallicRoughness != null) { metallicFactor = src.pbrMetallicRoughness.metallicFactor; roughnessFactor = src.pbrMetallicRoughness.roughnessFactor; } return(TextureFactory.CreateStandard(parser, src.pbrMetallicRoughness?.metallicRoughnessTexture?.index, src.occlusionTexture?.index, metallicFactor, roughnessFactor)); }
public static IEnumerable <TextureImportParam> EnumerateTextures(GltfParser parser, glTFMaterial m) { int?metallicRoughnessTexture = default; if (m.pbrMetallicRoughness != null) { // base color if (m.pbrMetallicRoughness?.baseColorTexture != null) { yield return(PBRMaterialItem.BaseColorTexture(parser, m)); } // metallic roughness if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null && m.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { metallicRoughnessTexture = m.pbrMetallicRoughness?.metallicRoughnessTexture?.index; } } // emission if (m.emissiveTexture != null) { var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(m.emissiveTexture); yield return(TextureFactory.CreateSRGB(parser, m.emissiveTexture.index, offset, scale)); } // normal if (m.normalTexture != null) { yield return(PBRMaterialItem.NormalTexture(parser, m)); } // occlusion int?occlusionTexture = default; if (m.occlusionTexture != null && m.occlusionTexture.index != -1) { occlusionTexture = m.occlusionTexture.index; } // metallicSmooth and occlusion if (metallicRoughnessTexture.HasValue || occlusionTexture.HasValue) { yield return(PBRMaterialItem.StandardTexture(parser, m)); } }
public ImporterContext( GltfData data, IReadOnlyDictionary <SubAssetKey, UnityEngine.Object> externalObjectMap = null, ITextureDeserializer textureDeserializer = null) { Data = data; TextureDescriptorGenerator = new GltfTextureDescriptorGenerator(Data); MaterialDescriptorGenerator = new GltfMaterialDescriptorGenerator(); ExternalObjectMap = externalObjectMap ?? new Dictionary <SubAssetKey, UnityEngine.Object>(); textureDeserializer = textureDeserializer ?? new UnityTextureDeserializer(); TextureFactory = new TextureFactory(textureDeserializer, ExternalObjectMap .Where(x => x.Value is Texture) .ToDictionary(x => x.Key, x => (Texture)x.Value), Data.MigrationFlags.IsRoughnessTextureValueSquared); MaterialFactory = new MaterialFactory(ExternalObjectMap .Where(x => x.Value is Material) .ToDictionary(x => x.Key, x => (Material)x.Value)); AnimationClipFactory = new AnimationClipFactory(ExternalObjectMap .Where(x => x.Value is AnimationClip) .ToDictionary(x => x.Key, x => (AnimationClip)x.Value)); }
public static async Task <Material> CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture) { if (getTexture == null) { getTexture = (IAwaitCaller _awaitCaller, glTF _gltf, TextureImportParam _param) => Task.FromResult <Texture2D>(null); } var material = new Material(Shader.Find(ShaderName)); if (i < 0 || i >= parser.GLTF.materials.Count) { material.name = MaterialFactory.MaterialName(i, null); return(material); } var src = parser.GLTF.materials[i]; material.name = MaterialFactory.MaterialName(i, src); var standardParam = default(TextureImportParam); if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) { if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null) { standardParam = StandardTexture(parser, src); } if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) { var color = src.pbrMetallicRoughness.baseColorFactor; material.color = (new Color(color[0], color[1], color[2], color[3])).gamma; } if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1) { var param = BaseColorTexture(parser, src); material.mainTexture = await getTexture(awaitCaller, parser.GLTF, param); // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, "_MainTex", param.Offset, param.Scale); } if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { material.EnableKeyword("_METALLICGLOSSMAP"); var texture = await getTexture(awaitCaller, parser.GLTF, standardParam); if (texture != null) { material.SetTexture(TextureImportParam.METALLIC_GLOSS_PROP, texture); } material.SetFloat("_Metallic", 1.0f); // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. material.SetFloat("_GlossMapScale", 1.0f); // Texture Offset and Scale var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture); MaterialFactory.SetTextureOffsetAndScale(material, "_MetallicGlossMap", offset, scale); } else { material.SetFloat("_Metallic", src.pbrMetallicRoughness.metallicFactor); material.SetFloat("_Glossiness", 1.0f - src.pbrMetallicRoughness.roughnessFactor); } } if (src.normalTexture != null && src.normalTexture.index != -1) { material.EnableKeyword("_NORMALMAP"); var param = NormalTexture(parser, src); var texture = await getTexture(awaitCaller, parser.GLTF, param); if (texture != null) { material.SetTexture(TextureImportParam.NORMAL_PROP, texture); material.SetFloat("_BumpScale", src.normalTexture.scale); } // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, "_BumpMap", param.Offset, param.Scale); } if (src.occlusionTexture != null && src.occlusionTexture.index != -1) { var texture = await getTexture(awaitCaller, parser.GLTF, standardParam); if (texture != null) { material.SetTexture(TextureImportParam.OCCLUSION_PROP, texture); material.SetFloat("_OcclusionStrength", src.occlusionTexture.strength); } // Texture Offset and Scale var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.occlusionTexture); MaterialFactory.SetTextureOffsetAndScale(material, "_OcclusionMap", offset, scale); } if (src.emissiveFactor != null || (src.emissiveTexture != null && src.emissiveTexture.index != -1)) { material.EnableKeyword("_EMISSION"); material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack; if (src.emissiveFactor != null && src.emissiveFactor.Length == 3) { material.SetColor("_EmissionColor", new Color(src.emissiveFactor[0], src.emissiveFactor[1], src.emissiveFactor[2])); } if (src.emissiveTexture != null && src.emissiveTexture.index != -1) { var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.emissiveTexture); var param = TextureFactory.CreateSRGB(parser, src.emissiveTexture.index, offset, scale); var texture = await getTexture(awaitCaller, parser.GLTF, param); if (texture != null) { material.SetTexture("_EmissionMap", texture); } // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, "_EmissionMap", param.Offset, param.Scale); } } BlendMode blendMode = BlendMode.Opaque; // https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980 switch (src.alphaMode) { case "BLEND": blendMode = BlendMode.Fade; material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; break; case "MASK": blendMode = BlendMode.Cutout; material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.SetFloat("_Cutoff", src.alphaCutoff); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 2450; break; default: // OPAQUE blendMode = BlendMode.Opaque; material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; break; } material.SetFloat("_Mode", (float)blendMode); return(material); }
public static TextureImportParam NormalTexture(GltfParser parser, glTFMaterial src) { var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.normalTexture); return(TextureFactory.CreateNormal(parser, src.normalTexture.index, offset, scale)); }
public static TextureImportParam BaseColorTexture(GltfParser parser, glTFMaterial src) { var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture); return(TextureFactory.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale)); }
public static TextureImportParam NormalTexture(GltfParser parser, glTFMaterial src) { return(TextureFactory.CreateNormal(parser, src.normalTexture.index)); }
public static TextureImportParam BaseColorTexture(GltfParser parser, glTFMaterial src) { return(TextureFactory.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index)); }