Beispiel #1
0
 private void OnLoadTransferFunction(RuntimeFileBrowser.DialogResult result)
 {
     if (!result.cancelled)
     {
         string extension = Path.GetExtension(result.path);
         if (extension == ".tf")
         {
             TransferFunction tf = TransferFunctionDatabase.LoadTransferFunction(result.path);
             if (tf != null)
             {
                 targetObject.transferFunction = tf;
                 targetObject.SetTransferFunctionMode(TFRenderMode.TF1D);
             }
         }
         if (extension == ".tf2d")
         {
             TransferFunction2D tf = TransferFunctionDatabase.LoadTransferFunction2D(result.path);
             if (tf != null)
             {
                 targetObject.transferFunction2D = tf;
                 targetObject.SetTransferFunctionMode(TFRenderMode.TF2D);
             }
         }
     }
 }
Beispiel #2
0
        private void OnGUI()
        {
            // Update selected object
            if (volRendObject == null)
            {
                volRendObject = SelectionHelper.GetSelectedVolumeObject();
            }

            if (volRendObject == null)
            {
                return;
            }

            if (hist2DTex == null)
            {
                hist2DTex = HistogramTextureGenerator.Generate2DHistogramTexture(volRendObject.dataset);
            }

            TransferFunction2D tf2d = volRendObject.transferFunction2D;

            // Calculate GUI width (minimum of window width and window height * 2)
            float bgWidth = Mathf.Min(this.position.width - 20.0f, (this.position.height - 250.0f) * 2.0f);
            // Draw the histogram
            Rect histRect = new Rect(0.0f, 0.0f, bgWidth, bgWidth * 0.5f);

            Graphics.DrawTexture(histRect, hist2DTex);
            // Draw the TF texture (showing the rectangles)
            tfGUIMat.SetTexture("_TFTex", tf2d.GetTexture());
            Graphics.DrawTexture(histRect, tf2d.GetTexture(), tfGUIMat);

            // Handle mouse click in box
            for (int iBox = 0; iBox < tf2d.boxes.Count; iBox++)
            {
                TransferFunction2D.TF2DBox box = tf2d.boxes[iBox];
                Rect boxRect = new Rect(histRect.x + box.rect.x * histRect.width, histRect.y + (1.0f - box.rect.height - box.rect.y) * histRect.height, box.rect.width * histRect.width, box.rect.height * histRect.height);
                if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && boxRect.Contains(new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y)))
                {
                    selectedBoxIndex = iBox;
                }
            }

            float startX = histRect.x;
            float startY = histRect.y + histRect.height + 10;

            // Show GUI for editing selected rectangle
            if (selectedBoxIndex != -1)
            {
                EditorGUI.BeginChangeCheck();
                TransferFunction2D.TF2DBox box = tf2d.boxes[selectedBoxIndex];
                float oldX = box.rect.x;
                float oldY = box.rect.y;
                box.rect.x      = EditorGUI.Slider(new Rect(startX, startY, 200.0f, 20.0f), "x", box.rect.x, 0.0f, 0.99f);
                box.rect.width  = EditorGUI.Slider(new Rect(startX + 220.0f, startY, 200.0f, 20.0f), "width", oldX + box.rect.width, box.rect.x + 0.01f, 1.0f) - box.rect.x;
                box.rect.y      = EditorGUI.Slider(new Rect(startX, startY + 50, 200.0f, 20.0f), "y", box.rect.y, 0.0f, 1.0f);
                box.rect.height = EditorGUI.Slider(new Rect(startX + 220.0f, startY + 50, 200.0f, 20.0f), "height", oldY + box.rect.height, box.rect.y + 0.01f, 1.0f) - box.rect.y;
                box.colour      = EditorGUI.ColorField(new Rect(startX + 450.0f, startY + 10, 100.0f, 20.0f), box.colour);
                box.minAlpha    = EditorGUI.Slider(new Rect(startX + 450.0f, startY + 30, 200.0f, 20.0f), "min alpha", box.minAlpha, 0.0f, 1.0f);
                box.alpha       = EditorGUI.Slider(new Rect(startX + 450.0f, startY + 60, 200.0f, 20.0f), "max alpha", box.alpha, 0.0f, 1.0f);

                tf2d.boxes[selectedBoxIndex] = box;
                needsRegenTexture           |= EditorGUI.EndChangeCheck();
            }
            else
            {
                EditorGUI.LabelField(new Rect(startX, startY, this.position.width - startX, 50.0f), "Select a rectangle in the above view, or add a new one.");
            }

            // Add new rectangle
            if (GUI.Button(new Rect(startX, startY + 100, 110.0f, 30.0f), "Add rectangle"))
            {
                tf2d.AddBox(0.1f, 0.1f, 0.8f, 0.8f, Color.white, 0.5f);
                needsRegenTexture = true;
            }
            // Remove selected shape
            if (selectedBoxIndex != -1)
            {
                if (GUI.Button(new Rect(startX, startY + 140, 110.0f, 30.0f), "Remove selected shape"))
                {
                    tf2d.boxes.RemoveAt(selectedBoxIndex);
                    needsRegenTexture = true;
                }
            }

            if (GUI.Button(new Rect(startX, startY + 180, 110.0f, 30.0f), "Save"))
            {
                string filepath = EditorUtility.SaveFilePanel("Save transfer function", "", "default.tf2d", "tf2d");
                if (filepath != "")
                {
                    TransferFunctionDatabase.SaveTransferFunction2D(tf2d, filepath);
                }
            }
            if (GUI.Button(new Rect(startX, startY + 220, 110.0f, 30.0f), "Load"))
            {
                string filepath = EditorUtility.OpenFilePanel("Save transfer function", "", "tf2d");
                if (filepath != "")
                {
                    TransferFunction2D newTF = TransferFunctionDatabase.LoadTransferFunction2D(filepath);
                    if (newTF != null)
                    {
                        volRendObject.transferFunction2D = tf2d = newTF;
                        needsRegenTexture = true;
                    }
                }
            }

            // TODO: regenerate on add/remove/modify (and do it async)
            if (needsRegenTexture)
            {
                tf2d.GenerateTexture();
                needsRegenTexture = false;
            }

            volRendObject.SetTransferFunctionMode(TFRenderMode.TF2D);

            return;
        }