public Texture2D GetRenderTexture(bool doRender = false)
    {
        if (PreviewGenerator != null)
        {
            PreviewGenerator.Initialize();
            if (doRender)
            {
                PreviewGenerator.RenderPreviewTexture();
                renderTextureChanged = false;
            }

            return(PreviewGenerator.PreviewTexture);
        }

        return(null);
    }
Exemple #2
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        PreviewGenerator previewGenerator =
            (PreviewGenerator)fieldInfo.GetValue(property.serializedObject.targetObject);

        // need to updated values with Serialization for Undo and Changes
        SerializedProperty PanOffsetProperty     = property.FindPropertyRelative("PanOffset");
        SerializedProperty ZoomLevelProperty     = property.FindPropertyRelative("ZoomLevel");
        SerializedProperty ViewDirectionProperty = property.FindPropertyRelative("ViewDirection");
        SerializedProperty ViewRightProperty     = property.FindPropertyRelative("ViewRightDirection");
        SerializedProperty ViewUpProperty        = property.FindPropertyRelative("ViewUpDirection");
        SerializedProperty RenderWidthProperty   = property.FindPropertyRelative("RenderWidth");
        SerializedProperty RenderHeightProperty  = property.FindPropertyRelative("RenderHeight");

        mouseOverTexture = texRect.Contains(Event.current.mousePosition);
        if (mouseOverTexture && (Event.current.type == EventType.MouseDrag))
        {
            // do pan if the control key is pressed
            if (Event.current.control)
            {
                Vector2 panOffset = PanOffsetProperty.vector2Value;
                panOffset.x += Event.current.delta.x * panSpeed;
                panOffset.y += Event.current.delta.y * panSpeed;
                PanOffsetProperty.vector2Value = panOffset;
            }
            else if (Event.current.shift)
            {
                float newZoom = previewGenerator.ZoomLevel - Event.current.delta.y * zoomSpeed;

                if (previewGenerator.OrthographicCamera)
                {
                    newZoom = Mathf.Min(.999999f, newZoom);
                }

                ZoomLevelProperty.floatValue = newZoom;
            }
            else
            {
                Vector3 viewDirection = ViewDirectionProperty.vector3Value;
                viewDirection += ViewRightProperty.vector3Value * (-Event.current.delta.x * rotSpeed * Mathf.Deg2Rad) +
                                 ViewUpProperty.vector3Value * (-Event.current.delta.y * rotSpeed * Mathf.Deg2Rad);

                viewDirection.Normalize();
                ViewRightProperty.vector3Value = Vector3.Cross(ViewUpProperty.vector3Value, viewDirection).normalized;
                // and new up Vector
                ViewUpProperty.vector3Value        = Vector3.Cross(viewDirection, ViewRightProperty.vector3Value).normalized;
                ViewDirectionProperty.vector3Value = viewDirection;
            }

            if ((previewGenerator != null) && (previewGenerator.GameObjectToRender != null))
            {
                // allows high frame rate in inspector
                previewGenerator.bRepaintNeeded = true;
            }

            return;
        }

        if ((Event.current.type == EventType.ValidateCommand) &&
            (Event.current.commandName == "UndoRedoPerformed"))
        {
            if ((previewGenerator != null) && (previewGenerator.GameObjectToRender != null))
            {
                previewGenerator.bRepaintNeeded = true;
            }

            return;
        }

        // should we only do this during layout and paint events?
        EditorGUI.BeginChangeCheck();
        {
            if (property.hasVisibleChildren)
            {
                property.NextVisible(true);
                do
                {
                    EditorGUILayout.PropertyField(property, true);
                } while (property.NextVisible(false));
            }
        }
        if (EditorGUI.EndChangeCheck())
        {
            // do these outside the drag, since the user may have added new entries manually
            ViewDirectionProperty.vector3Value.Normalize();
            // get new right Vector
            ViewRightProperty.vector3Value =
                Vector3.Cross(ViewUpProperty.vector3Value.normalized, ViewDirectionProperty.vector3Value);
            // and new up Vector
            ViewUpProperty.vector3Value =
                Vector3.Cross(ViewDirectionProperty.vector3Value, ViewRightProperty.vector3Value.normalized);

            // This can happen when the user is editing the View Direction with keyboard.
            if (ViewUpProperty.vector3Value == Vector3.zero)
            {
                ViewUpProperty.vector3Value = Vector3.up;
            }

            if (previewGenerator.OrthographicCamera)
            {
                ZoomLevelProperty.floatValue = Mathf.Min(.999999f, ZoomLevelProperty.floatValue);
            }

            RenderWidthProperty.intValue  = Mathf.Min(Mathf.Max(RenderWidthProperty.intValue, MinTextureSize), MaxTextureSize);
            RenderHeightProperty.intValue = Mathf.Min(Mathf.Max(RenderHeightProperty.intValue, MinTextureSize), MaxTextureSize);

            previewGenerator.bRepaintNeeded = true;
        }

        if ((previewGenerator != null) && (previewGenerator.GameObjectToRender != null) &&
            (Event.current.type == EventType.Repaint) && previewGenerator.bRepaintNeeded)
        {
            previewGenerator.RenderPreviewTexture();
            previewGenerator.bRepaintNeeded = false;
        }

        Texture2D previewTexture = previewGenerator.PreviewTexture;

        EditorGUILayout.Space();
        if (GUILayout.Button(" Save PNG... ", GUILayout.ExpandWidth(false)))
        {
            string path     = Path.GetDirectoryName(previewGenerator.LastPNGPathName);
            string filename = Path.GetFileName(previewGenerator.LastPNGPathName);
            path = EditorUtility.SaveFilePanel("Save Texture as PNG", path, filename, "png");
            if (path.Length != 0)
            {
                previewGenerator.SavePNG(path);
            }
        }

        EditorGUILayout.Space();
        float boxHeight = Mathf.Min(EditorGUIUtility.currentViewWidth - 36, previewGenerator.RenderHeight);

        GUILayout.Box(previewTexture,
                      GUILayout.Height(boxHeight), GUILayout.Width(EditorGUIUtility.currentViewWidth - 36));
        if (Event.current.type == EventType.Repaint)
        {
            texRect = GUILayoutUtility.GetLastRect();
        }


        EditorGUILayout.Space();
    }