public void AddControlPoint(TFAlphaControlPoint ctrlPoint)
 {
     alphaControlPoints.Add(ctrlPoint);
 }
    public void GenerateTexture()  // "Texture" of Window Content -> Visualisation of CP and lines etc.
    {
        List <TFColourControlPoint> cols   = new List <TFColourControlPoint>(colourControlPoints);
        List <TFAlphaControlPoint>  alphas = new List <TFAlphaControlPoint>(alphaControlPoints);

        // Sort lists of control points
        cols.Sort((a, b) => (a.dataValue.CompareTo(b.dataValue)));
        alphas.Sort((a, b) => (a.dataValue.CompareTo(b.dataValue)));

        // Add colour points at beginning and end
        if (cols.Count == 0 || cols[cols.Count - 1].dataValue < 1.0f)
        {
            cols.Add(new TFColourControlPoint(1.0f, Color.white));
        }
        if (cols[0].dataValue > 0.0f)
        {
            cols.Insert(0, new TFColourControlPoint(0.0f, Color.white));
        }

        // Add alpha points at beginning and end
        if (alphas.Count == 0 || alphas[alphas.Count - 1].dataValue < 1.0f)
        {
            alphas.Add(new TFAlphaControlPoint(1.0f, 1.0f));
        }
        if (alphas[0].dataValue > 0.0f)
        {
            alphas.Insert(0, new TFAlphaControlPoint(0.0f, 0.0f));
        }

        int numColours  = cols.Count;
        int numAlphas   = alphas.Count;
        int iCurrColour = 0;
        int iCurrAlpha  = 0;

        for (int iX = 0; iX < TEXTURE_WIDTH; iX++)
        {
            float t = iX / (float)(TEXTURE_WIDTH - 1);
            while (iCurrColour < numColours - 2 && cols[iCurrColour + 1].dataValue < t)
            {
                iCurrColour++;
            }
            while (iCurrAlpha < numAlphas - 2 && alphas[iCurrAlpha + 1].dataValue < t)
            {
                iCurrAlpha++;
            }

            TFColourControlPoint leftCol    = cols[iCurrColour];
            TFColourControlPoint rightCol   = cols[iCurrColour + 1];
            TFAlphaControlPoint  leftAlpha  = alphas[iCurrAlpha];
            TFAlphaControlPoint  rightAlpha = alphas[iCurrAlpha + 1];

            float tCol   = (Mathf.Clamp(t, leftCol.dataValue, rightCol.dataValue) - leftCol.dataValue) / (rightCol.dataValue - leftCol.dataValue);
            float tAlpha = (Mathf.Clamp(t, leftAlpha.dataValue, rightAlpha.dataValue) - leftAlpha.dataValue) / (rightAlpha.dataValue - leftAlpha.dataValue);

            Color pixCol = rightCol.colourValue * tCol + leftCol.colourValue * (1.0f - tCol);
            pixCol.a = rightAlpha.alphaValue * tAlpha + leftAlpha.alphaValue * (1.0f - tAlpha);

            for (int iY = 0; iY < TEXTURE_HEIGHT; iY++)
            {
                tfCols[iX + iY * TEXTURE_WIDTH] = pixCol;
            }
        }
        texture.wrapMode = TextureWrapMode.Clamp;
        texture.SetPixels(tfCols);
        texture.Apply();
    }
    private void OnGUI()
    {
        volRend = FindObjectOfType <VolumeController>();
        if (volRend == null)
        {
            return;
        }
        tf = volRend.getTransferFunction();

        Color oldColour = GUI.color;
        float bgWidth   = Mathf.Min(this.position.width - 40.0f, (this.position.height - 50.0f) * 2.0f);
        Rect  bgRect    = new Rect(leftOff, 0.0f, bgWidth, bgWidth * 0.5f);

        tf.GenerateTexture();

        tfGUIMat.SetTexture("_TFTex", tf.GetTexture());
        tfGUIMat.SetTexture("_HistTex", tf.getHistogramTexture());
        Graphics.DrawTexture(bgRect, tf.GetTexture(), tfGUIMat);

        Texture2D tfTexture = tf.GetTexture();

        tfPaletteGUIMat.SetTexture("_TFTex", tf.GetTexture());
        Graphics.DrawTexture(new Rect(bgRect.x, bgRect.y + bgRect.height + 20, bgRect.width, 20.0f), tfTexture, tfPaletteGUIMat);

        // Colour control points
        for (int iCol = 0; iCol < tf.colourControlPoints.Count; iCol++)
        {
            TFColourControlPoint colPoint = tf.colourControlPoints[iCol];
            Rect ctrlBox = new Rect(bgRect.x + bgRect.width * colPoint.dataValue, bgRect.y + bgRect.height + 20, 10, 20);
            GUI.color             = Color.red;
            GUI.skin.box.fontSize = 8;
            GUI.Box(ctrlBox, "|");
            if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && ctrlBox.Contains(new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y)))
            {
                movingColPointIndex   = iCol;
                selectedColPointIndex = iCol;
            }
            else if (movingColPointIndex == iCol)
            {
                colPoint.dataValue = Mathf.Clamp((Event.current.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f);
            }
            tf.colourControlPoints[iCol] = colPoint;
        }

        // Alpha control points
        for (int iAlpha = 0; iAlpha < tf.alphaControlPoints.Count; iAlpha++)
        {
            TFAlphaControlPoint alphaPoint = tf.alphaControlPoints[iAlpha];
            Rect ctrlBox = new Rect(bgRect.x + bgRect.width * alphaPoint.dataValue, bgRect.y + (1.0f - alphaPoint.alphaValue) * bgRect.height, 10, 10);
            GUI.color             = oldColour;
            GUI.skin.box.fontSize = 6;
            GUI.Box(ctrlBox, "a");
            if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && ctrlBox.Contains(new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y)))
            {
                movingAlphaPointIndex = iAlpha;
            }
            else if (movingAlphaPointIndex == iAlpha)
            {
                alphaPoint.dataValue  = Mathf.Clamp((Event.current.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f);
                alphaPoint.alphaValue = Mathf.Clamp(1.0f - (Event.current.mousePosition.y - bgRect.y) / bgRect.height, 0.0f, 1.0f);
            }
            tf.alphaControlPoints[iAlpha] = alphaPoint;
        }

        if (Event.current.type == EventType.MouseUp)
        {
            movingColPointIndex   = -1;
            movingAlphaPointIndex = -1;
        }

        if (Event.current.type == EventType.MouseDown && Event.current.button == 1)
        {
            if (bgRect.Contains(new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y)))
            {
                tf.alphaControlPoints.Add(new TFAlphaControlPoint(Mathf.Clamp((Event.current.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f), Mathf.Clamp(1.0f - (Event.current.mousePosition.y - bgRect.y) / bgRect.height, 0.0f, 1.0f)));
            }
            else
            {
                tf.colourControlPoints.Add(new TFColourControlPoint(Mathf.Clamp((Event.current.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f), Random.ColorHSV()));
            }
            selectedColPointIndex = -1;
        }

        if (selectedColPointIndex != -1)
        {
            TFColourControlPoint colPoint = tf.colourControlPoints[selectedColPointIndex];
            colPoint.colourValue = EditorGUI.ColorField(new Rect(bgRect.x, bgRect.y + bgRect.height + 50, 100.0f, 40.0f), colPoint.colourValue);
            tf.colourControlPoints[selectedColPointIndex] = colPoint;
        }

        updateController();
        GUI.color = oldColour;
    }