Exemple #1
0
    public static void CloseAnimationEditor(NPVoxAnimationEditorSession session)
    {
        NPVoxAnimation animation         = session.animation;
        string         previousScenePath = session.previousScenePath;
        string         editorScenePath   = session.editorScenePath;

        UnityEngine.SceneManagement.Scene editorScene = UnityEngine.SceneManagement.SceneManager.GetSceneByPath(editorScenePath);

        GameObject.DestroyImmediate(session.gameObject);

        if (previousScenePath != null)
        {
            EditorSceneManager.OpenScene(previousScenePath, OpenSceneMode.Additive);
        }
        else
        {
            EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
        }
        EditorSceneManager.CloseScene(editorScene, true);

        File.Delete(editorScenePath);

        animation.EnsureAllMeshesLoaded();

        Selection.objects = new Object[] { animation };
    }
    public static void OpenAnimationScene(NPVoxAnimation animation)
    {
        bool proceed = true;

        UnityEngine.SceneManagement.Scene previousScene = EditorSceneManager.GetActiveScene();
        if (previousScene.isDirty)
        {
            proceed = false;
            if (EditorUtility.DisplayDialog("Unsaved Changes", "You need to save any changes to your active scene before you can edit NPVox Animations", "Save Now", "Abort"))
            {
                proceed = true;
                EditorSceneManager.SaveScene(previousScene);
            }
        }

        if (proceed)
        {
            UnityEngine.SceneManagement.Scene editorScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
            GameObject gameObject = new GameObject();
            gameObject.name = "Edit " + animation.name;
            NPVoxAnimationEditorSession session = gameObject.AddComponent <NPVoxAnimationEditorSession>();
            session.animation         = animation;
            session.previousScene     = previousScene;
            session.previousScenePath = previousScene.path;
            EditorSceneManager.CloseScene(previousScene, false);
            EditorSceneManager.SaveScene(editorScene, "_NPVOX_TMP.scene", false);
            session.editorScene           = editorScene;
            gameObject.transform.position = Vector3.zero;
            Selection.objects             = new Object[] { gameObject };
        }
    }
Exemple #3
0
    private void UpdateMeshes()
    {
        NPVoxAnimationEditorSession session = ((NPVoxAnimationEditorSession)target);

        if (session != null)
        {
            session.previewFilter.sharedMesh = viewModel.Mesh;

            foreach (string socketName in viewModel.GetSocketPreviewTargetNames())
            {
                if (!viewModel.GetPreviewSocketEnabled(socketName))
                {
                    session.GetSocketPreviewFilter(socketName).gameObject.SetActive(false);
                }
                else
                {
                    var factory = viewModel.GetSocketPreviewMeshFactoryForCurrentFrame(socketName);
                    if (factory != null)
                    {
                        session.GetSocketPreviewFilter(socketName).gameObject.SetActive(true);
                        session.GetSocketPreviewFilter(socketName).sharedMesh = factory.GetProduct();
                    }
                    else
                    {
                        session.GetSocketPreviewFilter(socketName).gameObject.SetActive(false);
                    }
                }
            }
        }
    }
Exemple #4
0
    public void OnDisable()
    {
        Tools.current = previousUnityTool;
        viewModel.SelectAnimation(null);
        NPVoxAnimationEditorSession session = ((NPVoxAnimationEditorSession)target);

        if (session)
        {
            session.WipeSocketPreviewFilters();
        }
        Undo.undoRedoPerformed           -= MyUndoCallback;
        viewModel.OnMeshChange           -= OnMeshChange;
        viewModel.OnCheckForInvalidation -= OnCheckForInvalidation;
    }
Exemple #5
0
    // ===================================================================================================
    // Material Preview
    // ===================================================================================================

    private void DrawMaterialSelection()
    {
        NPVoxAnimationEditorSession session = ((NPVoxAnimationEditorSession)target);

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Preview Materials:");
        if (session.animation && session.animation.PreviewMaterials != null && session.animation.PreviewMaterials.Length < 3)
        {
            session.animation.PreviewMaterials = new Material[3];
        }
        session.animation.PreviewMaterials[0] = EditorGUILayout.ObjectField(session.animation.PreviewMaterials[0], typeof(Material), false, null) as Material;
        session.animation.PreviewMaterials[1] = EditorGUILayout.ObjectField(session.animation.PreviewMaterials[1], typeof(Material), false, null) as Material;
        session.animation.PreviewMaterials[2] = EditorGUILayout.ObjectField(session.animation.PreviewMaterials[2], typeof(Material), false, null) as Material;
        EditorGUILayout.EndHorizontal();

        if (!session.previewFilter)
        {
            return;
        }
        MeshRenderer renderer = session.previewFilter.GetComponent <MeshRenderer>();

        Material[] materials        = renderer.sharedMaterials;
        bool       somethingChanged = false;

        for (int i = 0; i < 3; i++)
        {
            if (session.animation.PreviewMaterials[i])
            {
                if (materials.Length > i)
                {
                    if (materials[i] != session.animation.PreviewMaterials[i])
                    {
                        materials[i]     = session.animation.PreviewMaterials[i];
                        somethingChanged = true;
                    }
                }
                else
                {
                    somethingChanged = true;
                    ArrayUtility.Add(ref materials, session.animation.PreviewMaterials[i]);
                }
            }
        }
        if (somethingChanged)
        {
            renderer.sharedMaterials = materials;
        }
    }
Exemple #6
0
    public static void OpenAnimationScene(NPVoxAnimation animation)
    {
        bool proceed = true;

        UnityEngine.SceneManagement.Scene previousScene = EditorSceneManager.GetActiveScene();

//        GameObject activeSession = GameObject.Find("~NPVoxAnimEditorSession");
//        if(activeSession)
//        {
//            CloseAnimationEditor(activeSession.GetComponent<NPVoxAnimationEditorSession>());
//        }

        if (previousScene.isDirty)
        {
            proceed = false;
            if (EditorUtility.DisplayDialog("Unsaved Changes", "You need to save any changes to your active scene before you can edit NPVox Animations", "Save Now", "Abort"))
            {
                proceed = true;
                EditorSceneManager.SaveScene(previousScene);
            }
        }

        if (proceed)
        {
            UnityEngine.SceneManagement.Scene editorScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
            GameObject gameObject = new GameObject();
            gameObject.name = "~NPVoxAnimEditorSession";

            NPVoxAnimationEditorSession session = gameObject.AddComponent <NPVoxAnimationEditorSession>();
            session.animation         = animation;
            session.previousScene     = previousScene;
            session.previousScenePath = previousScene.path;
            EditorSceneManager.CloseScene(previousScene, false);

            session.editorScenePath = FileUtil.GetUniqueTempPathInProject() + ".unity";
            EditorSceneManager.SaveScene(editorScene, session.editorScenePath, false);
            session.editorScene           = editorScene;
            gameObject.transform.position = Vector3.zero;
            Selection.objects             = new Object[] { gameObject };

            RenderSettings.skybox = null;

            // add light
            GameObject lightGameObject = new GameObject("The Light");
            Light      lightComp       = lightGameObject.AddComponent <Light>();
            lightComp.color     = new Color(1.0f, 1.0f, 0.9f);
            lightComp.type      = LightType.Directional;
            lightComp.intensity = 0.5f;
            lightGameObject.transform.rotation = Quaternion.Euler(300, 0, 235);
            lightGameObject.transform.position = Vector3.one * 1000f;

            // add preview
            gameObject            = new GameObject();
            gameObject.name       = "Preview";
            session.previewFilter = gameObject.AddComponent <MeshFilter>();
            MeshRenderer renderer = gameObject.AddComponent <MeshRenderer>();

            // TODO allow arbitrary number of materials
            Material mat = (Material)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath("45ff6773d76864cb0b24978a7bced992"), typeof(Material));
            renderer.sharedMaterials = new Material[] {
                mat, mat, mat, mat, mat, mat
            };

            // Add ground plate
            gameObject      = GameObject.CreatePrimitive(PrimitiveType.Cube);
            gameObject.name = "GroundPlate";
            gameObject.transform.localScale = new Vector3(10, 10, 0.125f);
            gameObject.transform.position   = new Vector3(0, 0, 2.335f);
            session.groundFilter            = gameObject.GetComponent <MeshFilter>();
            renderer = gameObject.GetComponent <MeshRenderer>();
            renderer.sharedMaterial = (Material)AssetDatabase.LoadAssetAtPath <Material>(AssetDatabase.GUIDToAssetPath("8ad74ef8a5b1547aba7e742c4650e508"));
//            renderer.material.SetColor("_Albedo", Color.red);
        }
    }
Exemple #7
0
    // ===================================================================================================
    // Inspector UI
    // ===================================================================================================

    public override void OnInspectorGUI()
    {
        if (!viewModel.IsPlaying && wasPreview)
        {
            // workaround for stupid "getting control 5" exception
            wasPreview = false;
            Repaint();
            return;
        }

        viewModel.ProcessInvalidations();

        NPVoxAnimationEditorSession session = ((NPVoxAnimationEditorSession)target);

        EditorGUILayout.BeginVertical();

        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("Close Editor") || !session.animation)
        {
            NPVoxAnimationEditor.CloseAnimationEditor(session);
        }
        if (GUILayout.Button("Debug"))
        {
            viewModel.DebugButton();
        }
        if (GUILayout.Button("Invalidate & Reimport All"))
        {
            NPipelineUtils.InvalidateAndReimportAll(session.animation);
        }
        if (GUILayout.Button("Invalidate & Reimport All Deep"))
        {
            NPipelineUtils.InvalidateAndReimportAllDeep(session.animation);
        }
        if (GUILayout.Button("Help"))
        {
            NPVoxAnimHelpWindow.ShowWindow();
        }
        EditorGUILayout.EndHorizontal();

        GUILayout.Label("Preview: ", EditorStyles.boldLabel);
        DrawPreview();

        if (!viewModel.IsPlaying)
        {
            GUILayout.Space(10);

            GUILayout.Label("Frames: ", EditorStyles.boldLabel);

            DrawFrameSelection();
            GUILayout.Space(10);
            DrawModelSelection();
            DrawTransformationSelector();
            GUILayout.Space(10);

            GUILayout.Label("Presentation: ", EditorStyles.boldLabel);
            DrawModeToolbar();
            DrawSocketTools();
        }
        else
        {
//            Debug.Log("playing");
            DrawFrameList();
        }
        DrawMaterialSelection();

        session.groundplateOffset = EditorGUILayout.FloatField("Ground Offset", session.groundplateOffset);
        EditorGUILayout.EndVertical();

        if (meshRefreshRequested)
        {
            meshRefreshRequested = false;
            UpdateMeshes();
            SceneView.RepaintAll();
        }

        // unfocus by pressing escape
        Event e = Event.current;

        if (e.isKey && e.keyCode == KeyCode.Escape)
        {
            unfocus();
        }

        // ground plate
        if (viewModel.SelectedFrame != null && viewModel.SelectedFrame.Source != null && session.groundFilter)
        {
            NPVoxModel   model = viewModel.SelectedFrame.Source.GetProduct();
            NPVoxToUnity n2u   = new NPVoxToUnity(model, viewModel.Animation.MeshFactory.VoxelSize);
            Vector3      pos   = n2u.ToUnityPosition(new Vector3(model.BoundingBox.SaveCenter.x, model.BoundingBox.SaveCenter.y, model.BoundingBox.Forward + 1));
            session.groundFilter.transform.position = pos + Vector3.forward * session.groundplateOffset;
        }

        KeyboardShortcuts();
    }