3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ランタイム(exe)でVRM(MToon)をHDRP(Lit)表示

Posted at

#概要
ランタイム(exe)でVRM(MToon)をHDRP(Lit)表示します。

#環境
Unity 2020.3.11f1
UniVRM 0.66.0

#実装

using System.IO;
using System.Collections.Generic;
using UnityEngine;
using VRM;

public class MToonToLit : MonoBehaviour
{
    [SerializeField] string FilePath;
    [SerializeField] Material Opaque;
    [SerializeField] Material Transparent;

    void Start()
    {
        // VRM読み込み
        var bytes = File.ReadAllBytes(FilePath);
        var context = new VRMImporterContext();
        context.ParseGlb(bytes);
        context.Load();
        context.ShowMeshes();

        // MToonをLitに置き換え
        var renderers = context.Root.GetComponentsInChildren<Renderer>();
        for (int i = 0; i < renderers.Length; i++)
        {
            var materials = new List<Material>(renderers[i].materials);
            for (int j = 0; j < renderers[i].materials.Length; j++)
            {
                if (renderers[i].materials[j].GetFloat("_BlendMode") != 2)
                {
                    materials[j] = new Material(Opaque);
                }
                else
                {
                    materials[j] = new Material(Transparent);
                }
                // 通常と透過を使い分けて色とテクスチャを反映
                materials[j].color = renderers[i].materials[j].color;
                materials[j].mainTexture = renderers[i].materials[j].mainTexture;
            }
            renderers[i].materials = materials.ToArray();
        }
    }
}

#補足
HDRPで作成したプロジェクトのシーンのどこかへ前述のスクリプトを張り付けて、FilePathに読み込みたいVRMファイルのパス、Opaqueに新規作成したHDRP/Litのマテリアルを設定、Transparentには新規作成したHDRP/LitのマテリアルのSurface TypeをTransparentにしてから設定すれば動くと思います。
image.png
既知問題として、このままだとUnlitが来た時に失敗して真っ黒になるので、_BlendModeの判定辺りをちゃんとやる必要があります。どうやれば良いかは調べれてません。
#参考
左がHDRP、右がMToonです。
image.png
モデル:リンツちゃん改変
https://booth.pm/ja/items/1255264
サンプルアプリ
https://120byte.booth.pm/items/3062310

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?