Beispiel #1
0
    private void createGameObjects(List <LabeledData> tmpSet)
    {
        Vector3    pos     = currentDataSet.transform.position;
        Quaternion rot     = currentDataSet.transform.rotation;
        Vector3    colSize = currentDataSet.GetComponent <BoxCollider>().size;

        if (currentDataSet)
        {
            loadedDataSets.Remove(currentDataSet);
            Destroy(currentDataSet);
        }

        currentDataSet = CreateDataGameObject("PointCloud GameObjs");
        GradientManager gradMan   = spaceManager.GetComponent <GradientManager>();
        int             featIndex = spaceManager.GetComponent <SpaceUtilities>().currentVariableForGraph;

        foreach (LabeledData d in tmpSet)
        {
            GameObject tmp = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            //Stell mass divided with max to normalize and then divided with 10 to get better shapes
            tmp.transform.localScale = (new Vector3(d.features[0], d.features[0], d.features[0]) / maxSizeTotal[0]) / 50; //Stellar mass
            if (tmp.transform.localScale.x > 0.1)
            {
                tmp.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
            }
            tmp.GetComponent <Renderer>().material.color = gradMan.getColor(d.features[featIndex] / maxData[featIndex]);
            tmp.transform.position = d.Position;
            tmp.transform.parent   = currentDataSet.transform;
        }
        currentDataSet.transform.position = pos;
        currentDataSet.transform.rotation = rot;
    }
Beispiel #2
0
        private void GenerateAndSaveTexture(string path, bool is2dRamp)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (is2dRamp)
            {
                GradientManager.SaveGradientTexture2D(m2dGradients, textureWidth, textureHeight, path);
            }
            else
            {
                GradientManager.SaveGradientTexture(mGradient, textureWidth, path);
            }
        }
Beispiel #3
0
        private void SaveEditedTexture()
        {
            if (textureIsDirty)
            {
                //Save data to file
                File.WriteAllBytes(Application.dataPath + AssetDatabase.GetAssetPath(linkedTexture).Substring(6), linkedTexture.EncodeToPNG());

                //Update linked texture userData
                if (editedTextureIs2d)
                {
                    linkedImporter.userData = GradientManager.GradientToUserData(m2dGradients);
                }
                else
                {
                    linkedImporter.userData = GradientManager.GradientToUserData(mGradient);
                }
            }
            textureIsDirty = false;
        }
Beispiel #4
0
        void InitEditMode(Texture2D texture, Material[] materials)
        {
            textureIsDirty  = false;
            editMode        = true;
            linkedTexture   = texture;
            linkedImporter  = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture));
            linkedMaterials = materials;
            var gradientList = GradientManager.GetGradientsFromUserData(linkedImporter.userData);

            if (gradientList.Count == 1)
            {
                mGradient         = gradientList[0];
                editedTextureIs2d = false;
            }
            else
            {
                m2dGradients      = gradientList.ToArray();
                editedTextureIs2d = true;
            }

            UpdateGradientPreview();
        }
Beispiel #5
0
        void OnGUI()
        {
            EditorGUILayout.BeginHorizontal();
            TCP2_GUI.HeaderBig(editMode ? "TCP 2 - RAMP EDITOR" : "TCP 2 - RAMP GENERATOR");
            TCP2_GUI.HelpButton("Ramp Generator");
            EditorGUILayout.EndHorizontal();
            TCP2_GUI.Separator();

            serializedObject.Update();

            if (editMode)
            {
                if (!isNewTexture)
                {
                    var msg = "This will affect <b>all materials</b> that use this texture!" +
                              (editModeFromMaterial ? "\n\nSave as a new texture first if you want to affect this material only." : "\n\nSave as a new texture if you want to keep the original ramp.");
                    EditorGUILayout.LabelField(GUIContent.none, new GUIContent(msg, TCP2_GUI.GetHelpBoxIcon(MessageType.Warning)), TCP2_GUI.HelpBoxRichTextStyle);
                }

                var rect = EditorGUILayout.GetControlRect(GUILayout.Height(16f));
                var lw   = EditorGUIUtility.labelWidth;
                EditorGUIUtility.labelWidth = 50f;
                var enabled = GUI.enabled;
                GUI.enabled = false;
                EditorGUI.ObjectField(rect, "Editing: ", linkedTexture, typeof(Texture2D), false);
                EditorGUIUtility.labelWidth = lw;
                GUI.enabled = enabled;
            }
            else
            {
                /*
                 * EditorGUILayout.BeginHorizontal();
                 * if (GUILayout.Toggle(tabIndex == 0, "1D RAMP", TCP2_GUI.Tab))
                 *      tabIndex = 0;
                 * if (GUILayout.Toggle(tabIndex == 1, "2D RAMP", TCP2_GUI.Tab))
                 *      tabIndex = 1;
                 * GUILayout.FlexibleSpace();
                 * EditorGUILayout.EndHorizontal();
                 * TCP2_GUI.SeparatorSimple();
                 */
            }

            if (isRamp1d)
            {
                GUILayout.Label("Click on the gradient to edit it:");
                EditorGUILayout.PropertyField(gradientProperty, GUIContent.none);
            }
            else
            {
                scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
                gradients2dList.DoLayoutList();
                EditorGUILayout.EndScrollView();
            }

            if (!editMode)
            {
                if (isRamp1d)
                {
                    textureWidth = EditorGUILayout.IntField("TEXTURE SIZE:", textureWidth);
                    EditorGUILayout.BeginHorizontal();
                    if (GUILayout.Button("64", EditorStyles.miniButtonLeft))
                    {
                        textureWidth = 64;
                    }
                    if (GUILayout.Button("128", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 128;
                    }
                    if (GUILayout.Button("256", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 256;
                    }
                    if (GUILayout.Button("512", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 512;
                    }
                    if (GUILayout.Button("1024", EditorStyles.miniButtonRight))
                    {
                        textureWidth = 1024;
                    }
                    EditorGUILayout.EndHorizontal();
                }
                else if (isRamp2d)
                {
                    GUILayout.BeginHorizontal();
                    textureWidth = EditorGUILayout.IntField("TEXTURE SIZE:", textureWidth);
                    GUILayout.Label("x");
                    textureHeight = EditorGUILayout.IntField(GUIContent.none, textureHeight);
                    GUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal();
                    if (GUILayout.Button("64", EditorStyles.miniButtonLeft))
                    {
                        textureWidth = 64;
                    }
                    if (GUILayout.Button("128", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 128;
                    }
                    if (GUILayout.Button("256", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 256;
                    }
                    if (GUILayout.Button("512", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 512;
                    }
                    if (GUILayout.Button("1024", EditorStyles.miniButtonRight))
                    {
                        textureWidth = 1024;
                    }
                    GUILayout.Space(8);
                    if (GUILayout.Button("64", EditorStyles.miniButtonLeft))
                    {
                        textureHeight = 64;
                    }
                    if (GUILayout.Button("128", EditorStyles.miniButtonMid))
                    {
                        textureHeight = 128;
                    }
                    if (GUILayout.Button("256", EditorStyles.miniButtonMid))
                    {
                        textureHeight = 256;
                    }
                    if (GUILayout.Button("512", EditorStyles.miniButtonMid))
                    {
                        textureHeight = 512;
                    }
                    if (GUILayout.Button("1024", EditorStyles.miniButtonRight))
                    {
                        textureHeight = 1024;
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }

            if (GUI.changed)
            {
                serializedObject.ApplyModifiedProperties();

                mGradient.alphaKeys = new[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f) };

                if (editMode)
                {
                    textureIsDirty = true;

                    //Update linked texture
                    if (editedTextureIs2d)
                    {
                        GradientManager.SetPixelsFromGradients(linkedTexture, m2dGradients, linkedTexture.width, linkedTexture.height);
                    }
                    else
                    {
                        var pixels = GradientManager.GetPixelsFromGradient(mGradient, linkedTexture.width, linkedTexture.height);
                        linkedTexture.SetPixels(pixels);
                        linkedTexture.Apply(true, false);
                    }
                }
            }

            GUILayout.Space(8f);
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (editMode)
            {
                if (GUILayout.Button("Discard", GUILayout.Width(90f), GUILayout.Height(20f)))
                {
                    DiscardEditedTexture();
                    if (editModeFromMaterial)
                    {
                        Close();
                    }
                    else
                    {
                        OpenTool();
                    }
                }
                if (GUILayout.Button("Apply", GUILayout.Width(90f), GUILayout.Height(20f)))
                {
                    SaveEditedTexture();
                    if (editModeFromMaterial)
                    {
                        Close();
                    }
                    else
                    {
                        OpenTool();
                    }
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
            }

            var saveButton = false;

            EditorGUI.BeginDisabledGroup(isRamp2d && (m2dGradients == null || m2dGradients.Length < 2));
            if (editMode)
            {
                saveButton = GUILayout.Button("Save as...", EditorStyles.miniButton, GUILayout.Width(120f), GUILayout.Height(16f));
            }
            else
            {
                saveButton = GUILayout.Button("GENERATE", GUILayout.Width(120f), GUILayout.Height(34f));
            }
            EditorGUI.EndDisabledGroup();
            if (saveButton)
            {
                var path = EditorUtility.SaveFilePanel("Save Generated Ramp", GradientManager.LAST_SAVE_PATH, editMode ? linkedTexture.name : "TCP2_CustomRamp", "png");
                if (!string.IsNullOrEmpty(path))
                {
                    GradientManager.LAST_SAVE_PATH = Path.GetDirectoryName(path);
                    var projectPath = path.Replace(Application.dataPath, "Assets");
                    GenerateAndSaveTexture(projectPath, isRamp2d);

                    if (editMode)
                    {
                        var newTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(projectPath);
                        if (newTexture != null)
                        {
                            foreach (var mat in linkedMaterials)
                            {
                                mat.SetTexture("_Ramp", newTexture);
                                EditorUtility.SetDirty(mat);
                            }
                        }

                        //Reinitialize edit mode
                        InitEditMode(newTexture, linkedMaterials);
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

            if (!editMode)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Load Texture", EditorStyles.miniButton, GUILayout.Width(120f)))
                {
                    LoadTexture();
                }
                EditorGUILayout.EndHorizontal();
            }
        }
Beispiel #6
0
        public static void RampProperty(MaterialProperty prop, string label, MaterialEditor editor, Gradient gradient, AssetImporter assetImporter, string defaultFileName = "JTRP_RampMap")
        {
            if (_iconAdd == null || _iconEdit == null)
            {
                _iconAdd = EditorGUIUtility.IconContent("d_Toolbar Plus");
                _iconEdit = EditorGUIUtility.IconContent("editicon.sml");
            }

            //Label
            var position = EditorGUILayout.GetControlRect();
            var labelRect = position;
            labelRect.height = EditorGUIUtility.singleLineHeight;
            var space = labelRect.height + 4;
            position.y += space - 3;
            position.height -= space;
            EditorGUI.PrefixLabel(labelRect, new GUIContent(label));

            //Texture object field
            var w = EditorGUIUtility.labelWidth;
            var indentLevel = EditorGUI.indentLevel;
            editor.SetDefaultGUIWidths();
            var buttonRect = MaterialEditor.GetRectAfterLabelWidth(labelRect);
            
            EditorGUIUtility.labelWidth = 0;
            EditorGUI.indentLevel = 0;
            var textureRect = MaterialEditor.GetRectAfterLabelWidth(labelRect);
            textureRect.xMax -= buttonRect.width;
            var newTexture = (Texture)EditorGUI.ObjectField(textureRect, prop.textureValue, typeof(Texture2D), false);
            EditorGUIUtility.labelWidth = w;
            EditorGUI.indentLevel = indentLevel;
            if (newTexture != prop.textureValue)
            {
                prop.textureValue = newTexture;
                assetImporter = null;
            }

            //Preview texture override (larger preview, hides texture name)
            var previewRect = new Rect(textureRect.x + 1, textureRect.y + 1, textureRect.width - 19, textureRect.height - 2);
            if (prop.hasMixedValue)
            {
                EditorGUI.DrawPreviewTexture(previewRect, Texture2D.grayTexture);
                GUI.Label(new Rect(previewRect.x + previewRect.width * 0.5f - 10, previewRect.y, previewRect.width * 0.5f, previewRect.height), "―");
            }
            else if (prop.textureValue != null)
                EditorGUI.DrawPreviewTexture(previewRect, prop.textureValue);

            if (prop.textureValue != null)
            {
                assetImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(prop.textureValue));
            }

            var buttonRectL = new Rect(buttonRect.x, buttonRect.y, buttonRect.width * 0.5f, buttonRect.height);
            var buttonRectR = new Rect(buttonRectL.xMax, buttonRect.y, buttonRect.width * 0.5f, buttonRect.height);
            bool needCreat = false;
            if (GUI.Button(buttonRectL, _iconEdit))
            {
                if ((assetImporter != null) && (assetImporter.userData.StartsWith("GRADIENT") || assetImporter.userData.StartsWith("gradient:")) && !prop.hasMixedValue)
                {
                    TCP2_RampGenerator.OpenForEditing((Texture2D)prop.textureValue, editor.targets, true, false);
                }
                else
                {
                    needCreat = true;
                }
            }
            if (GUI.Button(buttonRectR, _iconAdd) || needCreat)
            {
                var lastSavePath = GradientManager.LAST_SAVE_PATH;
                if (!lastSavePath.Contains(Application.dataPath))
                    lastSavePath = Application.dataPath;

                var path = EditorUtility.SaveFilePanel("Create New Ramp Texture", lastSavePath, defaultFileName, "png");
                if (!string.IsNullOrEmpty(path))
                {
                    bool overwriteExistingFile = File.Exists(path);

                    GradientManager.LAST_SAVE_PATH = Path.GetDirectoryName(path);

                    //Create texture and save PNG
                    var projectPath = path.Replace(Application.dataPath, "Assets");
                    GradientManager.CreateAndSaveNewGradientTexture(256, projectPath);

                    //Load created texture
                    var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(projectPath);
                    assetImporter = AssetImporter.GetAtPath(projectPath);

                    //Assign to material(s)
                    foreach (var item in prop.targets)
                    {
                        ((Material)item).SetTexture(prop.name, texture);
                    }

                    //Open for editing
                    TCP2_RampGenerator.OpenForEditing(texture, editor.targets, true, !overwriteExistingFile);
                }
            }

        }
Beispiel #7
0
 public GradientViewModel(GradientManager gradientManager)
 {
     _gradientManager             = gradientManager;
     _gradientManager.FirstColor  = Colors.Yellow;
     _gradientManager.SecondColor = Colors.Purple;
 }