public static IEnumerable <GetTextureParam> EnumerateTextures(glTF gltf, glTFMaterial m) { if (m.pbrMetallicRoughness != null) { // base color if (m.pbrMetallicRoughness?.baseColorTexture != null) { yield return(PBRMaterialItem.BaseColorTexture(gltf, m)); } // metallic roughness if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null) { yield return(PBRMaterialItem.MetallicRoughnessTexture(gltf, m)); } } // emission if (m.emissiveTexture != null) { yield return(GetTextureParam.Create(gltf, m.emissiveTexture.index)); } // normal if (m.normalTexture != null) { yield return(PBRMaterialItem.NormalTexture(gltf, m)); } // occlusion if (m.occlusionTexture != null) { yield return(PBRMaterialItem.OcclusionTexture(gltf, m)); } }
public static void Configure(GetTextureParam textureInfo, IDictionary <string, Texture2D> ExternalMap) { switch (textureInfo.TextureType) { case GetTextureParam.TextureTypes.NormalMap: { if (ExternalMap.TryGetValue(textureInfo.GltflName, out Texture2D external)) { ConfigureNormalMap(external); } } break; case GetTextureParam.TextureTypes.StandardMap: { if (ExternalMap.TryGetValue(textureInfo.ConvertedName, out Texture2D external)) { ConfigureLinear(external); } } break; case GetTextureParam.TextureTypes.sRGB: break; default: throw new NotImplementedException(); } }
/// <summary> /// gltf に texture を足す /// /// * textures /// * samplers /// * images /// * bufferViews /// /// を更新し、textures の index を返す /// /// </summary> /// <param name="gltf"></param> /// <param name="bufferIndex"></param> /// <param name="texture"></param> /// <returns>gltf texture index</returns> public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture) { var bytesWithMime = GetBytesWithMime(texture); // add view var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); var viewIndex = gltf.AddBufferView(view); // add image var imageIndex = gltf.images.Count; gltf.images.Add(new glTFImage { name = GetTextureParam.RemoveSuffix(texture.name), bufferView = viewIndex, mimeType = bytesWithMime.mine, }); // add sampler var samplerIndex = gltf.samplers.Count; var sampler = TextureSamplerUtil.Export(texture); gltf.samplers.Add(sampler); // add texture var textureIndex = gltf.textures.Count; gltf.textures.Add(new glTFTexture { sampler = samplerIndex, source = imageIndex, }); return(textureIndex); }
public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external) { if (param.Index0.HasValue && m_externalMap != null) { if (m_externalMap.TryGetValue(param.Name, out external)) { // Debug.Log($"use external: {param.Name}"); m_textureCache.Add(param.Name, new TextureLoadInfo(external, used, true)); return(external); } } external = default; return(false); }
public static GetTextureParam StandardTexture(glTF gltf, glTFMaterial src) { var metallicFactor = 1.0f; var roughnessFactor = 1.0f; if (src.pbrMetallicRoughness != null) { metallicFactor = src.pbrMetallicRoughness.metallicFactor; roughnessFactor = src.pbrMetallicRoughness.roughnessFactor; } return(GetTextureParam.CreateStandard(gltf, src.pbrMetallicRoughness?.metallicRoughnessTexture?.index, src.occlusionTexture?.index, metallicFactor, roughnessFactor)); }
public void Extract(GetTextureParam param, bool hasUri) { if (Textures.Values.Contains(param)) { return; } var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName); string targetPath = ""; if (hasUri && !param.ExtractConverted) { var gltfTexture = GLTF.textures[param.Index0.Value]; var gltfImage = GLTF.images[gltfTexture.source]; var ext = GetExt(gltfImage.mimeType, gltfImage.uri); targetPath = $"{Path.GetDirectoryName(m_path)}/{param.GltflName}{ext}"; } else { switch (param.TextureType) { case GetTextureParam.METALLIC_GLOSS_PROP: case GetTextureParam.OCCLUSION_PROP: { // write converted texture targetPath = $"{m_path}/{param.ConvertedName}.png"; File.WriteAllBytes(targetPath, subAsset.EncodeToPNG().ToArray()); AssetDatabase.ImportAsset(targetPath); break; } default: { // write original bytes var gltfTexture = GLTF.textures[param.Index0.Value]; var gltfImage = GLTF.images[gltfTexture.source]; var ext = GetExt(gltfImage.mimeType, gltfImage.uri); targetPath = $"{m_path}/{param.GltflName}{ext}"; File.WriteAllBytes(targetPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray()); AssetDatabase.ImportAsset(targetPath); break; } } } Textures.Add(targetPath, param); }
public void Extract(GetTextureParam param, bool hasUri) { if (Textures.Values.Contains(param)) { return; } var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName); UnityPath targetPath = default; if (hasUri && !param.ExtractConverted) { var gltfTexture = GLTF.textures[param.Index0.Value]; var gltfImage = GLTF.images[gltfTexture.source]; var ext = GetExt(gltfImage.mimeType, gltfImage.uri); targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}"); } else { switch (param.TextureType) { case GetTextureParam.TextureTypes.StandardMap: { // write converted texture targetPath = m_textureDirectory.Child($"{param.ConvertedName}.png"); File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray()); targetPath.ImportAsset(); break; } default: { // write original bytes var gltfTexture = GLTF.textures[param.Index0.Value]; var gltfImage = GLTF.images[gltfTexture.source]; var ext = GetExt(gltfImage.mimeType, gltfImage.uri); targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}"); File.WriteAllBytes(targetPath.FullPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray()); targetPath.ImportAsset(); break; } } } Textures.Add(targetPath, param); }
public static IEnumerable <GetTextureParam> EnumerateTextures(glTF gltf, glTFMaterial m) { int?metallicRoughnessTexture = default; if (m.pbrMetallicRoughness != null) { // base color if (m.pbrMetallicRoughness?.baseColorTexture != null) { yield return(PBRMaterialItem.BaseColorTexture(gltf, m)); } // metallic roughness if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null && m.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { metallicRoughnessTexture = m.pbrMetallicRoughness?.metallicRoughnessTexture?.index; } } // emission if (m.emissiveTexture != null) { yield return(GetTextureParam.CreateSRGB(gltf, m.emissiveTexture.index)); } // normal if (m.normalTexture != null) { yield return(PBRMaterialItem.NormalTexture(gltf, 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(gltf, m)); } }
public void Extract(GetTextureParam param) { var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.Name); var targetPath = string.Format("{0}/{1}{2}", m_path, param.Name, ".png" ); File.WriteAllBytes(targetPath, subAsset.EncodeToPNG().ToArray()); AssetDatabase.ImportAsset(targetPath); Textures.Add(new TextureInfo { Path = targetPath, sRGB = true, IsNormalMap = param.TextureType == GetTextureParam.NORMAL_PROP, }); }
public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external) { if (param.Index0.HasValue && m_externalMap != null) { var cacheName = param.ConvertedName; if (param.TextureType == GetTextureParam.NORMAL_PROP) { cacheName = param.GltflName; } if (m_externalMap.TryGetValue(cacheName, out external)) { if (!m_textureCache.ContainsKey(cacheName)) { m_textureCache.Add(cacheName, new TextureLoadInfo(external, used, true)); } return(external); } } external = default; return(false); }
public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external) { if (param.Index0.HasValue && ExternalMap != null) { var cacheName = param.ConvertedName; if (param.TextureType == GetTextureParam.TextureTypes.NormalMap) { cacheName = param.GltflName; if (m_textureCache.TryGetValue(cacheName, out TextureLoadInfo normalInfo)) { external = normalInfo.Texture; return(true); } } if (ExternalMap.TryGetValue(cacheName, out external)) { m_textureCache.Add(cacheName, new TextureLoadInfo(external, used, true)); return(true); } } external = default; return(false); }
public void Extract(GetTextureParam param) { var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.Name); string targetPath = ""; switch (param.TextureType) { case GetTextureParam.METALLIC_GLOSS_PROP: case GetTextureParam.OCCLUSION_PROP: { // write converted texture targetPath = string.Format("{0}/{1}{2}", m_path, param.Name, ".png" ); File.WriteAllBytes(targetPath, subAsset.EncodeToPNG().ToArray()); break; } default: { // write original bytes targetPath = string.Format("{0}/{1}{2}", m_path, param.Name, ".png" ); var gltfTexture = GLTF.textures[param.Index0.Value]; File.WriteAllBytes(targetPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray()); break; } } AssetDatabase.ImportAsset(targetPath); Textures.Add(targetPath, param); }
public static GetTextureParam MetallicRoughnessTexture(glTF gltf, glTFMaterial src) { return(GetTextureParam.CreateMetallic(gltf, src.pbrMetallicRoughness.metallicRoughnessTexture.index, src.pbrMetallicRoughness.metallicFactor)); }
public static async Task <Material> CreateAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture) { if (getTexture == null) { getTexture = (IAwaitCaller _awaitCaller, glTF _gltf, GetTextureParam _param) => Task.FromResult <Texture2D>(null); } if (i < 0 || i >= gltf.materials.Count) { return(MaterialFactory.CreateMaterial(i, null, ShaderName)); } var src = gltf.materials[i]; var material = MaterialFactory.CreateMaterial(i, src, ShaderName); var standardParam = default(GetTextureParam); if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) { if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null) { standardParam = StandardTexture(gltf, 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) { material.mainTexture = await getTexture(awaitCaller, gltf, BaseColorTexture(gltf, src)); // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); } if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { material.EnableKeyword("_METALLICGLOSSMAP"); var texture = await getTexture(awaitCaller, gltf, standardParam); if (texture != null) { material.SetTexture(GetTextureParam.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 MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap"); } 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 texture = await getTexture(awaitCaller, gltf, NormalTexture(gltf, src)); if (texture != null) { material.SetTexture(GetTextureParam.NORMAL_PROP, texture); material.SetFloat("_BumpScale", src.normalTexture.scale); } // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, src.normalTexture, "_BumpMap"); } if (src.occlusionTexture != null && src.occlusionTexture.index != -1) { var texture = await getTexture(awaitCaller, gltf, standardParam); if (texture != null) { material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture); material.SetFloat("_OcclusionStrength", src.occlusionTexture.strength); } // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, src.occlusionTexture, "_OcclusionMap"); } 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 texture = await getTexture(awaitCaller, gltf, GetTextureParam.CreateSRGB(gltf, src.emissiveTexture.index)); if (texture != null) { material.SetTexture("_EmissionMap", texture); } // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, src.emissiveTexture, "_EmissionMap"); } } 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 GetTextureParam NormalTexture(glTF gltf, glTFMaterial src) { return(GetTextureParam.CreateNormal(gltf, src.normalTexture.index)); }
public static GetTextureParam BaseColorTexture(glTF gltf, glTFMaterial src) { return(GetTextureParam.CreateSRGB(gltf, src.pbrMetallicRoughness.baseColorTexture.index)); }
public static GetTextureParam OcclusionTexture(glTF gltf, glTFMaterial src) { return(GetTextureParam.CreateOcclusion(gltf, src.occlusionTexture.index)); }