Beispiel #1
0
        public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx,
                                          out MaterialDescriptor matDesc)
        {
            if (vrm?.materialProperties == null || vrm.materialProperties.Count == 0)
            {
                matDesc = default;
                return(false);
            }

            if (materialIdx < 0 || materialIdx >= vrm.materialProperties.Count)
            {
                matDesc = default;
                return(false);
            }

            var vrmMaterial = vrm.materialProperties[materialIdx];

            if (vrmMaterial.shader == glTF_VRM_Material.VRM_USE_GLTFSHADER)
            {
                // fallback to gltf
                matDesc = default;
                return(false);
            }

            //
            // restore VRM material
            //
            // use material.name, because material name may renamed in GltfParser.
            var name = data.GLTF.materials[materialIdx].name;

            var textureSlots = new Dictionary <string, TextureDescriptor>();
            var floatValues  = new Dictionary <string, float>();
            var colors       = new Dictionary <string, Color>();
            var vectors      = new Dictionary <string, Vector4>();
            var actions      = new List <Action <Material> >();

            matDesc = new MaterialDescriptor(
                name,
                vrmMaterial.shader,
                vrmMaterial.renderQueue,
                textureSlots,
                floatValues,
                colors,
                vectors,
                actions);

            foreach (var kv in vrmMaterial.floatProperties)
            {
                floatValues.Add(kv.Key, kv.Value);
            }

            foreach (var kv in vrmMaterial.vectorProperties)
            {
                // vector4 exclude TextureOffsetScale
                if (vrmMaterial.textureProperties.ContainsKey(kv.Key))
                {
                    continue;
                }
                var v = new Vector4(kv.Value[0], kv.Value[1], kv.Value[2], kv.Value[3]);
                vectors.Add(kv.Key, v);
            }

            foreach (var kv in vrmMaterial.textureProperties)
            {
                if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, kv.Key,
                                                                              out var texture))
                {
                    textureSlots.Add(kv.Key, texture.Item2);
                }
            }

            foreach (var kv in vrmMaterial.keywordMap)
            {
                if (kv.Value)
                {
                    actions.Add(material => material.EnableKeyword(kv.Key));
                }
                else
                {
                    actions.Add(material => material.DisableKeyword(kv.Key));
                }
            }

            foreach (var kv in vrmMaterial.tagMap)
            {
                actions.Add(material => material.SetOverrideTag(kv.Key, kv.Value));
            }

            if (vrmMaterial.shader == MToon.Utils.ShaderName)
            {
                // TODO: Material拡張にMToonの項目が追加されたら旧バージョンのshaderPropから変換をかける
                // インポート時にUniVRMに含まれるMToonのバージョンに上書きする
                floatValues[MToon.Utils.PropVersion] = MToon.Utils.VersionNumber;
            }

            return(true);
        }
        public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx,
                                          out MaterialDescriptor matDesc)
        {
            if (vrm?.materialProperties == null || vrm.materialProperties.Count == 0)
            {
                matDesc = default;
                return(false);
            }

            if (materialIdx < 0 || materialIdx >= vrm.materialProperties.Count)
            {
                matDesc = default;
                return(false);
            }

            var vrmMaterial = vrm.materialProperties[materialIdx];

            if (vrmMaterial.shader != UnlitTransparentZWriteShaderName)
            {
                matDesc = default;
                return(false);
            }

            // use material.name, because material name may renamed in GltfParser.
            var name = data.GLTF.materials[materialIdx].name;

            var textureSlots = new Dictionary <string, TextureDescriptor>();
            var floatValues  = new Dictionary <string, float>();
            var colors       = new Dictionary <string, Color>();
            var vectors      = new Dictionary <string, Vector4>();
            var actions      = new Collection <Action <Material> >();

            if (vrmMaterial.textureProperties.ContainsKey(UnlitTransparentZWriteMainTexturePropName))
            {
                if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial,
                                                                              UnlitTransparentZWriteMainTexturePropName, out var texture))
                {
                    textureSlots.Add(MToon.Utils.PropMainTex, texture.Item2);
                }
            }

            actions.Add(unityMaterial =>
            {
                var mainTexture = (Texture2D)unityMaterial.GetTexture(MToon.Utils.PropMainTex);

                // NOTE: Unlit のフォールバックなので
                // Lit/Shade 色は黒として、Alpha のために Lit にテクスチャを設定する.
                // Emissive 色は白として、テクスチャを設定する.
                // また、元のシェーダのうちユーザが設定できるプロパティは Texture のみ.
                MToon.Utils.SetMToonParametersToMaterial(unityMaterial, new MToonDefinition
                {
                    Meta = new MetaDefinition
                    {
                        Implementation = MToon.Utils.Implementation,
                        VersionNumber  = MToon.Utils.VersionNumber,
                    },
                    Rendering = new RenderingDefinition
                    {
                        // NOTE: Transparent ZWrite
                        RenderMode = RenderMode.TransparentWithZWrite,
                        CullMode   = CullMode.Back,
                        RenderQueueOffsetNumber = 0,
                    },
                    Color = new ColorDefinition
                    {
                        // NOTE: Unlit なので、RGB 値は黒とする。
                        // NOTE: Alpha は使うので、テクスチャを設定する.
                        LitColor             = new Color(0, 0, 0, 1),
                        LitMultiplyTexture   = mainTexture,
                        ShadeColor           = new Color(0, 0, 0, 1),
                        ShadeMultiplyTexture = default,