public ControlPointColor CreateControlPointColor(float dataValue, Color color) { ControlPointColor newControlPoint = ScriptableObject.CreateInstance("ControlPointColor") as ControlPointColor; newControlPoint.dataValue = dataValue; newControlPoint.colourValue = color; return(newControlPoint); }
public void AddControlPointColor(float dataValue, Color color) { ControlPointColor newControlPoint = CreateControlPointColor( dataValue, color ); colourControlPoints.Add(newControlPoint); }
private void OnGUI() { VolumeRenderedObject volRend = FindObjectOfType <VolumeRenderedObject>(); if (volRend == null) { return; } tf = volRend.transferFunction; Color oldColour = GUI.color; float bgWidth = Mathf.Min(this.position.width - 20.0f, (this.position.height - 50.0f) * 2.0f); Rect bgRect = new Rect(0.0f, 0.0f, bgWidth, bgWidth * 0.5f); tf.GenerateTexture(); histogramMaterial.SetTexture("_TFTex", tf.GetTexture()); histogramMaterial.SetTexture("_HistTex", tf.histogramTexture); Graphics.DrawTexture(bgRect, tf.GetTexture(), histogramMaterial); Texture2D tfTexture = tf.GetTexture(); colorBarMaterial.SetTexture("_TFTex", tf.GetTexture()); Graphics.DrawTexture(new Rect(bgRect.x, bgRect.y + bgRect.height + 20, bgRect.width, 20.0f), tfTexture, colorBarMaterial); // Colour control points for (int iCol = 0; iCol < tf.colourControlPoints.Count; iCol++) { ControlPointColor 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++) { ControlPointAlpha 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( tf.CreateControlPointAlpha( 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(tf.CreateControlPointColor(Mathf.Clamp((Event.current.mousePosition.x - bgRect.x) / bgRect.width, 0.0f, 1.0f), Random.ColorHSV())); } selectedColPointIndex = -1; } if (selectedColPointIndex != -1) { ControlPointColor 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; } // TEST!!! TODO volRend.GetComponent <MeshRenderer>().sharedMaterial.SetTexture("_TFTex", tfTexture); volRend.GetComponent <MeshRenderer>().sharedMaterial.DisableKeyword("TF2D_ON"); GUI.color = oldColour; }
public void GenerateTexture() { List <ControlPointColor> cols = new List <ControlPointColor>(colourControlPoints); List <ControlPointAlpha> alphas = new List <ControlPointAlpha>(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(CreateControlPointColor(1.0f, Color.white)); } if (cols[0].dataValue > 0.0f) { cols.Insert(0, CreateControlPointColor(0.0f, Color.white)); } // Add alpha points at beginning and end if (alphas.Count == 0 || alphas[alphas.Count - 1].dataValue < 1.0f) { alphas.Add(CreateControlPointAlpha(1.0f, 1.0f)); } if (alphas[0].dataValue > 0.0f) { alphas.Insert(0, CreateControlPointAlpha(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++; } ControlPointColor leftCol = cols[iCurrColour]; ControlPointColor rightCol = cols[iCurrColour + 1]; ControlPointAlpha leftAlpha = alphas[iCurrAlpha]; ControlPointAlpha 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(); }