示例#1
0
        ///
        /// BlendShape List のElement描画
        ///
        public static bool DrawBlendShapeBinding(Rect position, SerializedProperty property,
                                                 PreviewSceneManager scene)
        {
            bool changed = false;

            if (scene != null)
            {
                var height = 16;

                var y    = position.y;
                var rect = new Rect(position.x, y, position.width, height);
                int pathIndex;
                if (StringPopup(rect, property.FindPropertyRelative("RelativePath"), scene.SkinnedMeshRendererPathList, out pathIndex))
                {
                    changed = true;
                }

                y   += height;
                rect = new Rect(position.x, y, position.width, height);
                int blendShapeIndex;
                if (IntPopup(rect, property.FindPropertyRelative("Index"), scene.GetBlendShapeNames(pathIndex), out blendShapeIndex))
                {
                    changed = true;
                }

                y   += height;
                rect = new Rect(position.x, y, position.width, height);
                if (FloatSlider(rect, property.FindPropertyRelative("Weight"), 100))
                {
                    changed = true;
                }
            }
            return(changed);
        }
示例#2
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            m_changed = false;

            EditorGUILayout.Space();
            var previewSlider = EditorGUILayout.Slider("Preview Weight", m_previewSlider, 0, 1.0f);

            if (previewSlider != m_previewSlider)
            {
                m_previewSlider = previewSlider;
                m_changed       = true;
            }

            EditorGUILayout.Space();

            serializedObject.Update();

            EditorGUILayout.PropertyField(m_BlendShapeNameProp, true);
            EditorGUILayout.PropertyField(m_PresetProp, true);

            EditorGUILayout.LabelField("BlendShapeBindings", EditorStyles.boldLabel);
            m_ValuesList.DoLayoutList();

            EditorGUILayout.LabelField("MaterialValueBindings", EditorStyles.boldLabel);
            m_MaterialValuesList.DoLayoutList();

            serializedObject.ApplyModifiedProperties();

            if (m_changed && PreviewSceneManager != null)
            {
                PreviewSceneManager.Bake(m_target.Values, m_target.MaterialValues, m_previewSlider);
            }
        }
示例#3
0
        public static bool DrawMaterialValueBinding(Rect position, SerializedProperty property,
                                                    PreviewSceneManager scene)
        {
            bool changed = false;

            if (scene != null)
            {
                var height = 16;

                var y    = position.y;
                var rect = new Rect(position.x, y, position.width, height);
                int materialIndex;
                if (StringPopup(rect, property.FindPropertyRelative("MaterialName"), scene.MaterialNames, out materialIndex))
                {
                    changed = true;
                }

                if (materialIndex >= 0)
                {
                    var materialItem = scene.GetMaterialItem(scene.MaterialNames[materialIndex]);
                    if (materialItem != null)
                    {
                        y   += height;
                        rect = new Rect(position.x, y, position.width, height);
                        int propIndex;
                        if (StringPopup(rect, property.FindPropertyRelative("ValueName"), materialItem.PropNames, out propIndex))
                        {
                            changed = true;
                        }

                        if (propIndex >= 0)
                        {
                            var propItem = materialItem.PropMap[materialItem.PropNames[propIndex]];
                            {
                                switch (propItem.PropertyType)
                                {
                                case ShaderUtil.ShaderPropertyType.Color:
                                {
                                    property.FindPropertyRelative("BaseValue").vector4Value = propItem.DefaultValues;

                                    // max
                                    y   += height;
                                    rect = new Rect(position.x, y, position.width, height);
                                    if (ColorProp(rect, property.FindPropertyRelative("TargetValue")))
                                    {
                                        changed = true;
                                    }
                                }
                                break;
                                }
                            }
                        }
                    }
                }
            }
            return(changed);
        }
示例#4
0
 protected virtual void OnDestroy()
 {
     if (m_scene != null)
     {
         //Debug.LogFormat("OnDestroy");
         m_scene.Clean();
         GameObject.DestroyImmediate(m_scene.gameObject);
         m_scene = null;
     }
 }
        public static PreviewSceneManager GetOrCreate(GameObject prefab)
        {
            if (prefab == null)
            {
                return(null);
            }

            PreviewSceneManager manager = null;

            // if we already instantiated a PreviewInstance previously but just lost the reference, then use that same instance instead of making a new one
            var managers = GameObject.FindObjectsOfType <PreviewSceneManager>();

            foreach (var x in managers)
            {
                if (x.Prefab == prefab)
                {
                    Debug.LogFormat("find {0}", manager);
                    return(manager);
                }
                Debug.LogFormat("destroy {0}", x);
                GameObject.DestroyImmediate(x.gameObject);
            }

            Debug.Log("new prefab. instanciate");
            // no previous instance detected, so now let's make a fresh one
            // very important: this loads the PreviewInstance prefab and temporarily instantiates it into PreviewInstance
            var go = GameObject.Instantiate(prefab,
                                            prefab.transform.position,
                                            prefab.transform.rotation
                                            );

            go.name = "__PREVIEW_SCENE_MANGER__";
            manager = go.AddComponent <PreviewSceneManager>();
            manager.Initialize(prefab);

            // HideFlags are special editor-only settings that let you have *secret* GameObjects in a scene, or to tell Unity not to save that temporary GameObject as part of the scene
            foreach (var x in go.transform.Traverse())
            {
                x.gameObject.hideFlags = HideFlags.None
                                         | HideFlags.DontSave
                                         //| HideFlags.DontSaveInBuild
#if VRM_DEVELOP
#else
                                         | HideFlags.HideAndDontSave
#endif
                ;
            }

            return(manager);
        }
示例#6
0
        public SerializedBlendShapeEditor(SerializedObject serializedObject, BlendShapeClip targetObject,
                                          PreviewSceneManager previewSceneManager)
        {
            this.m_serializedObject = serializedObject;
            this.m_targetObject     = targetObject;

            //m_thumbnail = serializedObject.FindProperty("Thumbnail");
            m_blendShapeNameProp = serializedObject.FindProperty("BlendShapeName");
            m_presetProp         = serializedObject.FindProperty("Preset");
            m_isBinaryProp       = serializedObject.FindProperty("IsBinary");

            m_valuesProp = serializedObject.FindProperty("Values");

            m_ValuesList = new ReorderableList(serializedObject, m_valuesProp);
            m_ValuesList.elementHeight       = BlendShapeBindingHeight;
            m_ValuesList.drawElementCallback =
                (rect, index, isActive, isFocused) =>
            {
                var element = m_valuesProp.GetArrayElementAtIndex(index);
                rect.height -= 4;
                rect.y      += 2;
                if (BlendShapeClipEditorHelper.DrawBlendShapeBinding(rect, element, previewSceneManager))
                {
                    m_changed = true;
                }
            };

            m_materialsProp      = serializedObject.FindProperty("MaterialValues");
            m_MaterialValuesList = new ReorderableList(serializedObject, m_materialsProp);
            m_MaterialValuesList.elementHeight       = MaterialValueBindingHeight;
            m_MaterialValuesList.drawElementCallback =
                (rect, index, isActive, isFocused) =>
            {
                var element = m_materialsProp.GetArrayElementAtIndex(index);
                rect.height -= 4;
                rect.y      += 2;
                if (BlendShapeClipEditorHelper.DrawMaterialValueBinding(rect, element, previewSceneManager))
                {
                    m_changed = true;
                }
            };

            m_items = previewSceneManager.EnumRenderItems
                      .Where(x => x.SkinnedMeshRenderer != null)
                      .ToArray();
        }
        ///
        /// BlendShape List のElement描画
        ///
        public static bool DrawBlendShapeBinding(Rect position, SerializedProperty property,
                                                 PreviewSceneManager scene)
        {
            bool changed = false;

            if (scene != null)
            {
                var height = 16;

                var y    = position.y;
                var rect = new Rect(position.x, y, position.width, height);
                int pathIndex;
                var(relChanged, oldIndex) = StringPopup(rect, property.FindPropertyRelative("RelativePath"), scene.SkinnedMeshRendererPathList, out pathIndex);
                if (relChanged)
                {
                    changed = true;
                }

                y   += height;
                rect = new Rect(position.x, y, position.width, height);
                if (!relChanged && oldIndex == -1)
                {
                    EditorGUI.HelpBox(rect, "renderer not found ! please fix", MessageType.Error);
                }
                else
                {
                    int blendShapeIndex;
                    if (IntPopup(rect, property.FindPropertyRelative("Index"), scene.GetBlendShapeNames(pathIndex), out blendShapeIndex))
                    {
                        changed = true;
                    }
                }

                y   += height;
                rect = new Rect(position.x, y, position.width, height);
                if (FloatSlider(rect, property.FindPropertyRelative("Weight"), 100))
                {
                    changed = true;
                }
            }
            return(changed);
        }
示例#8
0
 void OnSelected(BlendShapeClip clip)
 {
     if (PreviewSceneManager == null)
     {
         m_clipEditor = null;
     }
     else if (clip != null)
     {
         m_clipEditor = new SerializedBlendShapeEditor(clip, PreviewSceneManager);
         PreviewSceneManager.Bake(new PreviewSceneManager.BakeValue
         {
             BlendShapeBindings    = clip.Values,
             MaterialValueBindings = clip.MaterialValues,
             Weight = 1.0f
         });
     }
     else
     {
         m_clipEditor = null;
         PreviewSceneManager.Bake(new PreviewSceneManager.BakeValue());
     }
 }
        //const float FACTOR = 0.1f;

        public Texture Render(Rect r, GUIStyle background, PreviewSceneManager scene,
                              float yaw, float pitch, Vector3 position)
        {
            if (scene == null)
            {
                return(null);
            }

            using (var fog = new FogScope())
            {
                m_previewUtility.BeginPreview(r, background); // set up the PreviewRenderUtility's mini internal scene

                // setup the ObjectPreview's camera
                scene.SetupCamera(PreviewCamera, scene.TargetPosition, yaw, pitch, position);

                foreach (var item in scene.EnumRenderItems)
                {
                    // now, actually render out the RenderTexture
                    //RenderMeshPreview(previewMesh, skinMeshRender.sharedMaterials);
                    // submesh support, in case the mesh is made of multiple parts
                    int subMeshCount = item.Mesh.subMeshCount;
                    for (int i = 0; i < subMeshCount; i++)
                    {
                        m_previewUtility.DrawMesh(item.Mesh,
                                                  item.Position, item.Rotation,
                                                  item.Materials[i], i);
                    }
                }

                // VERY IMPORTANT: this manually tells the camera to render and produce the render texture
                PreviewCamera.Render();
                //m_previewUtility.Render(false, false);

                // reset the scene's fog from before
                return(m_previewUtility.EndPreview());
            }
        }
示例#10
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            base.OnInspectorGUI();

            m_mode = GUILayout.Toolbar(m_mode, MODES);
            switch (m_mode)
            {
            case 0:
                m_selector.SelectGUI();
                if (m_clipEditor != null)
                {
                    Separator();
                    var result = m_clipEditor.Draw();
                    if (result.Changed)
                    {
                        PreviewSceneManager.Bake(new PreviewSceneManager.BakeValue
                        {
                            BlendShapeBindings    = result.BlendShapeBindings,
                            MaterialValueBindings = result.MaterialValueBindings,
                            Weight = 1.0f,
                        });
                    }
                }
                break;

            case 1:
                m_clipList.DoLayoutList();
                break;

            default:
                throw new NotImplementedException();
            }

            serializedObject.ApplyModifiedProperties();
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            // buttons
            if (m_target.Clips != null)
            {
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Select BlendShapeClip", EditorStyles.boldLabel);
                var array = m_target.Clips
                            .Select(x => x != null
                        ? BlendShapeKey.CreateFrom(x).ToString()
                        : "null"
                                    ).ToArray();
                var preset = GUILayout.SelectionGrid(m_preset, array, 4);
                if (preset != m_preset)
                {
                    CurrentClip = m_target.Clips[preset];
                    m_preset    = preset;
                }
            }

            // Add
            if (GUILayout.Button("Add BlendShapeClip"))
            {
                AddBlendShapeClip();
            }

            if (CurrentClip != null)
            {
                // clip
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("CurrentClip", EditorStyles.boldLabel);

                /*var loadClip = (BlendShapeClip)*/
                GUI.enabled = false;
                EditorGUILayout.ObjectField("Current clip",
                                            CurrentClip, typeof(BlendShapeClip), false);
                GUI.enabled = true;

                CurrentClip.Preset = (BlendShapePreset)EditorGUILayout.Popup("Preset", (int)CurrentClip.Preset, Presets);

                GUI.enabled = false;
                CurrentClip.BlendShapeName = EditorGUILayout.TextField("BlendShapeName", CurrentClip.BlendShapeName);
                GUI.enabled = true;

                var key = BlendShapeKey.CreateFrom(CurrentClip);
                if (m_target.Clips.Where(x => key.Match(x)).Count() > 1)
                {
                    EditorGUILayout.HelpBox("duplicate clip", MessageType.Error);
                }

                EditorGUILayout.Space();
                EditorGUILayout.LabelField("BlendShapeValues", EditorStyles.boldLabel);

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Clear"))
                {
                    ClearBlendShape();
                }

                if (CurrentClip != null && GUILayout.Button("Apply"))
                {
                    string maxWeightString;
                    CurrentClip.Values = GetBindings(out maxWeightString);
                    EditorUtility.SetDirty(CurrentClip);
                }
                EditorGUILayout.EndHorizontal();

                // sliders
                bool changed   = false;
                int  foldIndex = 0;
                if (PreviewSceneManager != null)
                {
                    foreach (var item in PreviewSceneManager.EnumRenderItems.Where(x => x.SkinnedMeshRenderer != null))
                    {
                        var mesh = item.SkinnedMeshRenderer.sharedMesh;
                        if (mesh != null && mesh.blendShapeCount > 0)
                        {
                            //var relativePath = UniGLTF.UnityExtensions.RelativePathFrom(renderer.transform, m_target.transform);
                            //EditorGUILayout.LabelField(m_target.name + "/" + item.Path);

                            if (foldIndex >= m_meshFolds.Count)
                            {
                                m_meshFolds.Add(false);
                            }
                            m_meshFolds[foldIndex] = EditorGUILayout.Foldout(m_meshFolds[foldIndex], item.SkinnedMeshRenderer.name);
                            if (m_meshFolds[foldIndex])
                            {
                                //EditorGUI.indentLevel += 1;
                                for (int i = 0; i < mesh.blendShapeCount; ++i)
                                {
                                    var src = item.SkinnedMeshRenderer.GetBlendShapeWeight(i);
                                    var dst = EditorGUILayout.Slider(mesh.GetBlendShapeName(i), src, 0, 100.0f);
                                    if (dst != src)
                                    {
                                        item.SkinnedMeshRenderer.SetBlendShapeWeight(i, dst);
                                        changed = true;
                                    }
                                }
                                //EditorGUI.indentLevel -= 1;
                            }
                            ++foldIndex;
                        }
                    }

                    if (changed)
                    {
                        PreviewSceneManager.Bake();
                    }
                }
            }
        }
示例#12
0
        public override void OnInspectorGUI()
        {
            if (PreviewSceneManager == null)
            {
                return;
            }
            serializedObject.Update();

            if (m_serializedEditor == null)
            {
                m_serializedEditor = new SerializedBlendShapeEditor(serializedObject, PreviewSceneManager);
                //m_thumbnailProp = serializedObject.FindProperty("Thumbnail");
                m_isBinaryProp = serializedObject.FindProperty("IsBinary");
            }

            int thumbnailSize = 96;

            EditorGUILayout.BeginHorizontal();

            /*
             * var objectReferenceValue = EditorGUILayout.ObjectField(m_thumbnailProp.objectReferenceValue, typeof(Texture), false,
             *  GUILayout.Width(thumbnailSize), GUILayout.Height(thumbnailSize));
             * if (m_thumbnailProp.objectReferenceValue != objectReferenceValue)
             * {
             *  m_thumbnailProp.objectReferenceValue = objectReferenceValue;
             *  serializedObject.ApplyModifiedProperties();
             * }
             */

            var changed = false;

            EditorGUILayout.BeginVertical();
            base.OnInspectorGUI();
            EditorGUILayout.LabelField("Preview Weight");
            var previewSlider = EditorGUILayout.Slider(m_previewSlider, 0, 1.0f);

            GUI.enabled = PreviewTexture != null;

            /*
             * if (GUILayout.Button("save thumbnail"))
             * {
             *  //var ext = "jpg";
             *  var ext = "png";
             *  var asset = UnityPath.FromAsset(target);
             *  var path = EditorUtility.SaveFilePanel(
             *                 "save thumbnail",
             *                 asset.Parent.FullPath,
             *                 string.Format("{0}.{1}", asset.FileNameWithoutExtension, ext),
             *                 ext);
             *  if (!string.IsNullOrEmpty(path))
             *  {
             *      var thumbnail = SaveResizedImage(PreviewTexture, UnityPath.FromFullpath(path),
             *      BlendShapeClipDrawer.ThumbnailSize);
             *      m_thumbnailProp.objectReferenceValue = thumbnail;
             *      serializedObject.ApplyModifiedProperties();
             *  }
             * }
             */
            GUI.enabled = true;
            EditorGUILayout.EndVertical();

            if (m_isBinaryProp.boolValue)
            {
                previewSlider = Mathf.Round(previewSlider);
            }
            if (previewSlider != m_previewSlider)
            {
                m_previewSlider = previewSlider;
                changed         = true;
            }

            EditorGUILayout.EndHorizontal();
            Separator();
            EditorGUILayout.Space();

            var result = m_serializedEditor.Draw();

            if ((changed || result.Changed) && PreviewSceneManager != null)
            {
                PreviewSceneManager.Bake(new PreviewSceneManager.BakeValue
                {
                    BlendShapeBindings    = result.BlendShapeBindings,
                    MaterialValueBindings = result.MaterialValueBindings,
                    Weight = m_previewSlider
                });
            }
        }
示例#13
0
 public SerializedBlendShapeEditor(BlendShapeClip blendShapeClip,
                                   PreviewSceneManager previewSceneManager) : this(
         new SerializedObject(blendShapeClip), blendShapeClip, previewSceneManager)
 {
 }
示例#14
0
 public SerializedBlendShapeEditor(SerializedObject serializedObject,
                                   PreviewSceneManager previewSceneManager) : this(
         serializedObject, (BlendShapeClip)serializedObject.targetObject, previewSceneManager)
 {
 }