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);
            }
        }
Exemplo n.º 2
0
        private static glTFMaterial Migrate(JsonNode vrm0XMaterial, string materialName)
        {
            var unlitMaterial = new glTFMaterial
            {
                name = materialName,
                pbrMetallicRoughness = new glTFPbrMetallicRoughness
                {
                    metallicFactor  = 0f,
                    roughnessFactor = 1f,
                },
                extensions = new glTFExtensionExport()
                             .Add(glTF_KHR_materials_unlit.ExtensionName, new ArraySegment <byte>(glTF_KHR_materials_unlit.Raw)),
            };

            switch (MigrationMaterialUtil.GetShaderName(vrm0XMaterial))
            {
            case "Unlit/Color":
                unlitMaterial.pbrMetallicRoughness.baseColorFactor  = MigrationMaterialUtil.GetBaseColorFactor(vrm0XMaterial);
                unlitMaterial.pbrMetallicRoughness.baseColorTexture = null;
                return(unlitMaterial);

            case "Unlit/Texture":
                unlitMaterial.pbrMetallicRoughness.baseColorFactor  = new float[] { 1, 1, 1, 1 };
                unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
                return(unlitMaterial);

            case "Unlit/Transparent":
                unlitMaterial.pbrMetallicRoughness.baseColorFactor  = new float[] { 1, 1, 1, 1 };
                unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
                unlitMaterial.alphaMode = "BLEND";
                return(unlitMaterial);

            case "Unlit/Transparent Cutout":
                unlitMaterial.pbrMetallicRoughness.baseColorFactor  = new float[] { 1, 1, 1, 1 };
                unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
                unlitMaterial.alphaMode   = "MASK";
                unlitMaterial.alphaCutoff = MigrationMaterialUtil.GetCutoff(vrm0XMaterial);
                return(unlitMaterial);

            case "VRM/UnlitTexture":
                unlitMaterial.pbrMetallicRoughness.baseColorFactor  = new float[] { 1, 1, 1, 1 };
                unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
                return(unlitMaterial);

            case "VRM/UnlitTransparent":
                unlitMaterial.pbrMetallicRoughness.baseColorFactor  = new float[] { 1, 1, 1, 1 };
                unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
                unlitMaterial.alphaMode = "BLEND";
                return(unlitMaterial);

            case "VRM/UnlitCutout":
                unlitMaterial.pbrMetallicRoughness.baseColorFactor  = new float[] { 1, 1, 1, 1 };
                unlitMaterial.pbrMetallicRoughness.baseColorTexture = MigrationMaterialUtil.GetBaseColorTexture(vrm0XMaterial);
                unlitMaterial.alphaMode   = "MASK";
                unlitMaterial.alphaCutoff = MigrationMaterialUtil.GetCutoff(vrm0XMaterial);
                return(unlitMaterial);

            case "VRM/UnlitTransparentZWrite":
                // NOTE: ZWrite マテリアルのみ、MToon にマイグレーションするため、別処理.
                return(null);

            default:
                return(null);
            }
        }