private static Dictionary <int, int> GetRenderQueueMapper(IReadOnlyList <JsonNode> vrm0XMaterials) { try { var renderQueueSet = new SortedSet <int>(); foreach (var vrm0XMaterial in vrm0XMaterials) { var renderQueue = MigrationMaterialUtil.GetRenderQueue(vrm0XMaterial); if (renderQueue.HasValue && renderQueue.Value != -1) { renderQueueSet.Add(renderQueue.Value); } else { renderQueueSet.Add(Unity0XDefaultRenderQueue); } } var mapper = new Dictionary <int, int>(); var currentQueueOffset = 0; foreach (var queue in renderQueueSet) { mapper.Add(queue, currentQueueOffset); currentQueueOffset = Mathf.Min(currentQueueOffset + 1, MaxRenderQueueOffset); } return(mapper); } catch (Exception ex) { Debug.LogException(ex); return(new Dictionary <int, int>()); } }
private static glTFMaterial Migrate(JsonNode vrm0XMaterial, string materialName, Dictionary <int, int> renderQueueMapper) { try { if (MigrationMaterialUtil.GetShaderName(vrm0XMaterial) != Unity0XShaderName) { return(null); } var baseColorFactor = MigrationMaterialUtil.GetBaseColorFactor(vrm0XMaterial); var baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial); var emissiveTexture = new glTFMaterialEmissiveTextureInfo { index = baseColorTexture.index, extensions = baseColorTexture.extensions, }; var renderQueue = MigrationMaterialUtil.GetRenderQueue(vrm0XMaterial) ?? Unity0XDefaultRenderQueue; var renderQueueOffset = renderQueueMapper.ContainsKey(renderQueue) ? renderQueueMapper[renderQueue] : 0; var mtoonMaterial = new glTFMaterial { name = materialName, extensions = new glTFExtensionExport().Add( glTF_KHR_materials_unlit.ExtensionName, new ArraySegment <byte>(glTF_KHR_materials_unlit.Raw) ), pbrMetallicRoughness = new glTFPbrMetallicRoughness { baseColorFactor = new[] { 0f, 0f, 0f, baseColorFactor[3] }, // black + _Color.a baseColorTexture = baseColorTexture, // _MainTex metallicFactor = 0f, roughnessFactor = 1f, }, alphaMode = "BLEND", alphaCutoff = 0.5f, doubleSided = false, emissiveFactor = new[] { baseColorFactor[0], baseColorFactor[1], baseColorFactor[2] }, // _Color.rgb emissiveTexture = emissiveTexture, }; var mtoon10 = new VRMC_materials_mtoon { SpecVersion = Vrm10Exporter.MTOON_SPEC_VERSION, TransparentWithZWrite = true, // transparent with zWrite RenderQueueOffsetNumber = renderQueueOffset, ShadeColorFactor = new[] { 0f, 0f, 0f }, // black OutlineWidthMode = OutlineWidthMode.none // disable outline }; UniGLTF.Extensions.VRMC_materials_mtoon.GltfSerializer.SerializeTo(ref mtoonMaterial.extensions, mtoon10); return(mtoonMaterial); } catch (Exception) { Debug.LogWarning($"Migration failed in VRM/UnlitTransparentZWrite material: {materialName}"); return(null); } }