Exemplo n.º 1
0
        public static PreviewMaterialItem CreateForPreview(Material material)
        {
            var item = new PreviewMaterialItem(material);

            var propNames = new List <string>();

            for (int i = 0; i < ShaderUtil.GetPropertyCount(material.shader); ++i)
            {
                var propType = ShaderUtil.GetPropertyType(material.shader, i);
                var name     = ShaderUtil.GetPropertyName(material.shader, i);

                switch (propType)
                {
                case ShaderUtil.ShaderPropertyType.Color:
                    // 色
                {
                    var bindType = PreviewMaterialItem.GetBindType(name);
                    item.PropMap.Add(bindType, new PropItem
                        {
                            Name          = name,
                            PropertyType  = (UniVRM10.ShaderPropertyType)Enum.Parse(typeof(UniVRM10.ShaderPropertyType), propType.ToString(), true),
                            DefaultValues = material.GetColor(name),
                        });
                    propNames.Add(name);
                }
                break;

                case ShaderUtil.ShaderPropertyType.TexEnv:
                    break;
                }
            }
            item.PropNames = propNames.ToArray();
            return(item);
        }
Exemplo n.º 2
0
        void InitializeMaterialMap(Dictionary <ExpressionKey, VRM10Expression> clipMap, Transform root)
        {
            Dictionary <string, Material> materialNameMap = new Dictionary <string, Material>();

            foreach (var renderer in root.GetComponentsInChildren <Renderer>())
            {
                foreach (var material in renderer.sharedMaterials)
                {
                    if (material != null && !materialNameMap.ContainsKey(material.name))
                    {
                        materialNameMap.Add(material.name, material);
                    }
                }
            }

            foreach (var kv in clipMap)
            {
                foreach (var binding in kv.Value.MaterialColorBindings)
                {
                    PreviewMaterialItem item;
                    if (!m_materialMap.TryGetValue(binding.MaterialName, out item))
                    {
                        if (!materialNameMap.TryGetValue(binding.MaterialName, out Material material))
                        {
                            // not found skip
                            continue;
                        }
                        item = new PreviewMaterialItem(material);
                        m_materialMap.Add(binding.MaterialName, item);
                    }
                    // color default value
                    var propName = GetProperty(binding.BindType);
                    if (!item.PropMap.ContainsKey(binding.BindType))
                    {
                        item.PropMap.Add(binding.BindType, new PropItem
                        {
                            Name          = propName,
                            DefaultValues = item.Material.GetVector(propName),
                        });
                    }
                }

                foreach (var binding in kv.Value.MaterialUVBindings)
                {
                    PreviewMaterialItem item;
                    if (!m_materialMap.TryGetValue(binding.MaterialName, out item))
                    {
                        if (!materialNameMap.TryGetValue(binding.MaterialName, out Material material))
                        {
                            // not found skip
                            continue;
                        }
                        item = new PreviewMaterialItem(material);
                        m_materialMap.Add(binding.MaterialName, item);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static PreviewMaterialItem CreateForPreview(Material material)
        {
            var item = new PreviewMaterialItem(material);

            var propNames = new List <string>();

            for (int i = 0; i < ShaderUtil.GetPropertyCount(material.shader); ++i)
            {
                var propType = ShaderUtil.GetPropertyType(material.shader, i);
                var name     = ShaderUtil.GetPropertyName(material.shader, i);

                switch (propType)
                {
                case ShaderUtil.ShaderPropertyType.Color:
                    // 色
                {
                    var bindType = VrmLib.MaterialBindTypeExtensions.GetBindType(name);
                    item.PropMap.Add(bindType, new PropItem
                        {
                            Name          = name,
                            PropertyType  = propType,
                            DefaultValues = material.GetColor(name),
                        });
                    propNames.Add(name);
                }
                break;

                case ShaderUtil.ShaderPropertyType.TexEnv:
                    // テクスチャ
                    // {
                    //     name += "_ST";
                    //     item.PropMap.Add(name, new PropItem
                    //     {
                    //         PropertyType = propType,
                    //         DefaultValues = material.GetVector(name),
                    //     });
                    //     propNames.Add(name);
                    // }
                    // // 縦横分離用
                    // {
                    //     var st_name = name + "_S";
                    //     item.PropMap.Add(st_name, new PropItem
                    //     {
                    //         PropertyType = propType,
                    //         DefaultValues = material.GetVector(name),
                    //     });
                    //     propNames.Add(st_name);
                    // }
                    // {
                    //     var st_name = name + "_T";
                    //     item.PropMap.Add(st_name, new PropItem
                    //     {
                    //         PropertyType = propType,
                    //         DefaultValues = material.GetVector(name),
                    //     });
                    //     propNames.Add(st_name);
                    // }
                    break;
                }
            }
            item.PropNames = propNames.ToArray();
            return(item);
        }
Exemplo n.º 4
0
        private void Initialize(GameObject prefab)
        {
            //Debug.LogFormat("[PreviewSceneManager.Initialize] {0}", prefab);
            Prefab = prefab;

            var materialNames = new List <string>();

            // preview シーン用に Material を複製する。
            // Expression のカスタムエディタのマテリアル変更は、
            // 複製したものに適用される。
            var map = new Dictionary <Material, Material>();
            Func <Material, Material> getOrCreateMaterial = src =>
            {
                if (src == null)
                {
                    return(null);
                }
                if (string.IsNullOrEmpty(src.name))
                {
                    return(null);                                // !
                }
                Material dst;
                if (!map.TryGetValue(src, out dst))
                {
                    dst = new Material(src);
                    map.Add(src, dst);

                    //Debug.LogFormat("add material {0}", src.name);
                    materialNames.Add(src.name);
                    m_materialMap.Add(src.name, PreviewMaterialItem.CreateForPreview(dst));
                }
                return(dst);
            };

            m_meshes = transform.Traverse()
                       .Select(x => PreviewMeshItem.Create(x, transform, getOrCreateMaterial))
                       .Where(x => x != null)
                       .ToArray()
            ;
            MaterialNames = materialNames.ToArray();

            m_blendShapeMeshes = m_meshes
                                 .Where(x => x.SkinnedMeshRenderer != null &&
                                        x.SkinnedMeshRenderer.sharedMesh.blendShapeCount > 0)
                                 .ToArray();

            m_rendererPathList            = m_meshes.Select(x => x.Path).ToArray();
            m_skinnedMeshRendererPathList = m_meshes
                                            .Where(x => x.SkinnedMeshRenderer != null)
                                            .Select(x => x.Path)
                                            .ToArray();

            var animator = GetComponent <Animator>();

            if (animator != null)
            {
                var head = animator.GetBoneTransform(HumanBodyBones.Head);
                if (head != null)
                {
                    m_target = head;
                }
            }
        }