Beispiel #1
0
        public static glTF_VRM_Material CreateFromMaterial(Material m, TextureExporter textureExporter)
        {
            var material = new glTF_VRM_Material
            {
                name        = m.name,
                shader      = m.shader.name,
                renderQueue = m.renderQueue,
            };

            if (!PreShaderPropExporter.VRMExtensionShaders.Contains(m.shader.name))
            {
                material.shader = glTF_VRM_Material.VRM_USE_GLTFSHADER;
                return(material);
            }

            var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);

            if (prop == null)
            {
                Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name);
            }
            else
            {
                foreach (var keyword in m.shaderKeywords)
                {
                    material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
                }

                // get properties
                //material.SetProp(prop);
                foreach (var kv in prop.Properties)
                {
                    switch (kv.ShaderPropertyType)
                    {
                    case ShaderPropertyType.Color:
                    {
                        var value = m.GetColor(kv.Key).ToArray();
                        material.vectorProperties.Add(kv.Key, value);
                    }
                    break;

                    case ShaderPropertyType.Range:
                    case ShaderPropertyType.Float:
                    {
                        var value = m.GetFloat(kv.Key);
                        material.floatProperties.Add(kv.Key, value);
                    }
                    break;

                    case ShaderPropertyType.TexEnv:
                    {
                        var texture = m.GetTexture(kv.Key);
                        if (texture != null)
                        {
                            var value = kv.Key == "_BumpMap"
                                        ? textureExporter.ExportNormal(texture)
                                        : textureExporter.ExportSRGB(texture)
                            ;
                            if (value == -1)
                            {
                                Debug.LogFormat("not found {0}", texture.name);
                            }
                            else
                            {
                                material.textureProperties.Add(kv.Key, value);
                            }
                        }

                        // offset & scaling
                        var offset  = m.GetTextureOffset(kv.Key);
                        var scaling = m.GetTextureScale(kv.Key);
                        material.vectorProperties.Add(kv.Key,
                                                      new float[] { offset.x, offset.y, scaling.x, scaling.y });
                    }
                    break;

                    case ShaderPropertyType.Vector:
                    {
                        var value = m.GetVector(kv.Key).ToArray();
                        material.vectorProperties.Add(kv.Key, value);
                    }
                    break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }

            foreach (var tag in TAGS)
            {
                var value = m.GetTag(tag, false);
                if (!String.IsNullOrEmpty(value))
                {
                    material.tagMap.Add(tag, value);
                }
            }

            return(material);
        }
Beispiel #2
0
        public override void ExportExtensions(ITextureSerializer textureSerializer)
        {
            // avatar
            var animator = Copy.GetComponent <Animator>();

            if (animator != null)
            {
                var humanoid = Copy.GetComponent <VRMHumanoidDescription>();
                UniHumanoid.AvatarDescription description = null;
                var nodes = Copy.transform.Traverse().Skip(1).ToList();
                {
                    var isCreated = false;
                    if (humanoid != null)
                    {
                        description = humanoid.GetDescription(out isCreated);
                    }

                    if (description != null)
                    {
                        // use description
                        VRM.humanoid.Apply(description, nodes);
                    }

                    if (isCreated)
                    {
                        GameObject.DestroyImmediate(description);
                    }
                }

                {
                    // set humanoid bone mapping
                    var avatar = animator.avatar;
                    foreach (HumanBodyBones key in Enum.GetValues(typeof(HumanBodyBones)))
                    {
                        if (key == HumanBodyBones.LastBone)
                        {
                            break;
                        }

                        var transform = animator.GetBoneTransform(key);
                        if (transform != null)
                        {
                            VRM.humanoid.SetNodeIndex(key, nodes.IndexOf(transform));
                        }
                    }
                }
            }

            // morph
            var master = Copy.GetComponent <VRMBlendShapeProxy>();

            if (master != null)
            {
                var avatar = master.BlendShapeAvatar;
                if (avatar != null)
                {
                    foreach (var x in avatar.Clips)
                    {
                        VRM.blendShapeMaster.Add(x, this);
                    }
                }
            }

            // secondary
            VRMSpringUtility.ExportSecondary(Copy.transform, Nodes,
                                             x => VRM.secondaryAnimation.colliderGroups.Add(x),
                                             x => VRM.secondaryAnimation.boneGroups.Add(x)
                                             );

#pragma warning disable 0618
            // meta(obsolete)
            {
                var meta = Copy.GetComponent <VRMMetaInformation>();
                if (meta != null)
                {
                    VRM.meta.author             = meta.Author;
                    VRM.meta.contactInformation = meta.ContactInformation;
                    VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        VRM.meta.texture = TextureExporter.RegisterExportingAsSRgb(meta.Thumbnail, needsAlpha: true);
                    }

                    VRM.meta.licenseType     = meta.LicenseType;
                    VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    VRM.meta.reference       = meta.Reference;
                }
            }
#pragma warning restore 0618

            // meta
            {
                var _meta = Copy.GetComponent <VRMMeta>();
                if (_meta != null && _meta.Meta != null)
                {
                    var meta = _meta.Meta;

                    // info
                    VRM.meta.version            = meta.Version;
                    VRM.meta.author             = meta.Author;
                    VRM.meta.contactInformation = meta.ContactInformation;
                    VRM.meta.reference          = meta.Reference;
                    VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        VRM.meta.texture = TextureExporter.RegisterExportingAsSRgb(meta.Thumbnail, needsAlpha: true);
                    }

                    // ussage permission
                    VRM.meta.allowedUser        = meta.AllowedUser;
                    VRM.meta.violentUssage      = meta.ViolentUssage;
                    VRM.meta.sexualUssage       = meta.SexualUssage;
                    VRM.meta.commercialUssage   = meta.CommercialUssage;
                    VRM.meta.otherPermissionUrl = meta.OtherPermissionUrl;

                    // distribution license
                    VRM.meta.licenseType = meta.LicenseType;
                    if (meta.LicenseType == LicenseType.Other)
                    {
                        VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    }
                }
            }

            // firstPerson
            var firstPerson = Copy.GetComponent <VRMFirstPerson>();
            if (firstPerson != null)
            {
                if (firstPerson.FirstPersonBone != null)
                {
                    VRM.firstPerson.firstPersonBone       = Nodes.IndexOf(firstPerson.FirstPersonBone);
                    VRM.firstPerson.firstPersonBoneOffset = firstPerson.FirstPersonOffset;
                    VRM.firstPerson.meshAnnotations       = firstPerson.Renderers.Select(x => new glTF_VRM_MeshAnnotation
                    {
                        mesh            = Meshes.IndexOf(x.SharedMesh),
                        firstPersonFlag = x.FirstPersonFlag.ToString(),
                    }).ToList();
                }

                // lookAt
                {
                    var lookAtHead = Copy.GetComponent <VRMLookAtHead>();
                    if (lookAtHead != null)
                    {
                        var boneApplyer       = Copy.GetComponent <VRMLookAtBoneApplyer>();
                        var blendShapeApplyer = Copy.GetComponent <VRMLookAtBlendShapeApplyer>();
                        if (boneApplyer != null)
                        {
                            VRM.firstPerson.lookAtType = LookAtType.Bone;
                            VRM.firstPerson.lookAtHorizontalInner.Apply(boneApplyer.HorizontalInner);
                            VRM.firstPerson.lookAtHorizontalOuter.Apply(boneApplyer.HorizontalOuter);
                            VRM.firstPerson.lookAtVerticalDown.Apply(boneApplyer.VerticalDown);
                            VRM.firstPerson.lookAtVerticalUp.Apply(boneApplyer.VerticalUp);
                        }
                        else if (blendShapeApplyer != null)
                        {
                            VRM.firstPerson.lookAtType = LookAtType.BlendShape;
                            VRM.firstPerson.lookAtHorizontalOuter.Apply(blendShapeApplyer.Horizontal);
                            VRM.firstPerson.lookAtVerticalDown.Apply(blendShapeApplyer.VerticalDown);
                            VRM.firstPerson.lookAtVerticalUp.Apply(blendShapeApplyer.VerticalUp);
                        }
                    }
                }
            }

            // materials
            foreach (var m in Materials)
            {
                VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureExporter));
            }

            // Serialize VRM
            var f = new JsonFormatter();
            VRMSerializer.Serialize(f, VRM);
            var bytes = f.GetStoreBytes();
            glTFExtensionExport.GetOrCreate(ref _gltf.extensions).Add("VRM", bytes);
        }
Beispiel #3
0
        public override glTFMaterial ExportMaterial(Material m, TextureExporter textureExporter)
        {
            if (m.shader.name != MToon.Utils.ShaderName)
            {
                return(base.ExportMaterial(m, textureExporter));
            }

            // convert MToon intermediate value from UnityEngine.Material
            var def = MToon.Utils.GetMToonParametersFromMaterial(m);

            // gltfMaterial
            var material = new glTFMaterial
            {
                name = m.name,

                pbrMetallicRoughness = new glTFPbrMetallicRoughness
                {
                    baseColorFactor  = def.Color.LitColor.ToArray(),
                    baseColorTexture = new glTFMaterialBaseColorTextureInfo
                    {
                        index = textureExporter.ExportSRGB(def.Color.LitMultiplyTexture),
                    },
                },

                emissiveFactor = def.Emission.EmissionColor.ToArray(),
            };

            // VRMC_materials_mtoon
            var mtoon = new UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon
            {
                Version = "",

                TransparentWithZWrite = false,

                RenderQueueOffsetNumber = 0,

                ShadeColorFactor = new float[] { 0, 0, 0 },

                ShadeMultiplyTexture = new UniGLTF.Extensions.VRMC_materials_mtoon.TextureInfo
                {
                    Index = textureExporter.ExportSRGB(def.Color.ShadeMultiplyTexture),
                },

                // Lighting
                ShadingShiftFactor = 0,

                ShadingToonyFactor = 0,

                GiIntensityFactor = 0,

                // MatCap
                // AdditiveTexture;

                // Rim
                ParametricRimColorFactor = new float[] { 0, 0, 0 },

                // public int? RimMultiplyTexture;

                RimLightingMixFactor = 0,

                ParametricRimFresnelPowerFactor = 0,

                ParametricRimLiftFactor = 0,

                // Outline
                OutlineWidthMode = UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode.none,

                OutlineWidthFactor = 0,

                // public int? OutlineWidthMultiplyTexture;

                OutlineColorFactor = new float[] { 0, 0, 0 },

                OutlineLightingMixFactor = 0,

                // public int? UvAnimationMaskTexture;

                UvAnimationScrollXSpeedFactor = 0,

                UvAnimationScrollYSpeedFactor = 0,

                UvAnimationRotationSpeedFactor = 0,
            };

            UniGLTF.Extensions.VRMC_materials_mtoon.GltfSerializer.SerializeTo(ref material.extensions, mtoon);

            return(material);
        }