- TransferFunctionEditorWindow: Now displays the histogram and colour palette.

This commit is contained in:
Matias 2019-02-16 12:33:35 +01:00
Родитель 43bf2c36b5
Коммит 1419d545f0
15 изменённых файлов: 423 добавлений и 6 удалений

Просмотреть файл

@ -17,8 +17,20 @@ public class TransferFunctionEditorWindow : EditorWindow
wnd.Show();
}
private Material tfGUIMat = null;
private Material tfPaletteGUIMat = null;
private void OnEnable()
{
tfGUIMat = Resources.Load<Material>("TransferFunctionGUIMat");
tfPaletteGUIMat = Resources.Load<Material>("TransferFunctionPaletteGUIMat");
}
private void OnGUI()
{
if (!Application.isPlaying)
return;
VolumeRenderer volRend = FindObjectOfType<VolumeRenderer>();
if (volRend == null)
return;
@ -29,18 +41,22 @@ public class TransferFunctionEditorWindow : EditorWindow
Rect bgRect = new Rect(0.0f, 0.0f, bgWidth, bgWidth * 0.5f);
tf.GenerateTexture();
Texture2D tfTexture = tf.GetTexture();
GUI.DrawTexture(bgRect, tfTexture);
GUI.color = Color.white;
GUI.Box(new Rect(bgRect.x, bgRect.y + bgRect.height + 20, bgRect.width, 20.0f), "");
tfGUIMat.SetTexture("_TFTex", tf.GetTexture());
tfGUIMat.SetTexture("_HistTex", tf.histogramTexture);
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 = colPoint.colourValue;
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)))

Просмотреть файл

@ -8,7 +8,7 @@ Material:
m_PrefabInternal: {fileID: 0}
m_Name: DirectVolumeRenderingMaterial
m_Shader: {fileID: 4800000, guid: f9df7f8cb5dda3a48a945ab37d6b0d83, type: 3}
m_ShaderKeywords: MODE_SURF _MODE_DVR _MODE_MIP
m_ShaderKeywords: MODE_DVR _MODE_DVR _MODE_MIP
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0

8
Assets/Resources.meta Normal file
Просмотреть файл

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 50d7a13dd2433844dafacbae6138bdaf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -0,0 +1,96 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: TransferFunctionGUIMat
m_Shader: {fileID: 4800000, guid: 9117c7b46d847fc41adc9d9bb2cfa4c4, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _HistTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 10906, guid: 0000000000000000f000000000000000, type: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _TFTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _ColorMask: 15
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _Stencil: 0
- _StencilComp: 8
- _StencilOp: 0
- _StencilReadMask: 255
- _StencilWriteMask: 255
- _Strength: 0.2
- _UVSec: 0
- _UseUIAlphaClip: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

Просмотреть файл

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5c679c40287c6be4ea9d399b2550dc24
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -0,0 +1,96 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: TransferFunctionPaletteGUIMat
m_Shader: {fileID: 4800000, guid: 2b9f004f75e79b848808a5354ffd2c2e, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _HistTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 10906, guid: 0000000000000000f000000000000000, type: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _TFTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _ColorMask: 15
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _Stencil: 0
- _StencilComp: 8
- _StencilOp: 0
- _StencilReadMask: 255
- _StencilWriteMask: 255
- _Strength: 0.2
- _UVSec: 0
- _UseUIAlphaClip: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

Просмотреть файл

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ff490732e49820c42b1fb485f6a0717c
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -55,6 +55,8 @@ public class VolumeRenderer : MonoBehaviour
Texture2D tfTexture = tf.GetTexture();
tf.histogramTexture = HistogramTextureGenerator.GenerateHistogramTexture(dataset);
GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_DataTex", tex);
GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_NoiseTex", noiseTexture);
GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_TFTex", tfTexture);

Просмотреть файл

@ -0,0 +1,57 @@
Shader "VolumeRendering/TransferFunctionPaletteShader"
{
Properties
{
_TFTex("Transfer Function Texture", 2D) = "white" {}
}
SubShader
{
Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _TFTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float4 col = tex2D(_TFTex, float2(i.uv.x, 0.0f));
col.a = 1.0f;
return col;
}
ENDCG
}
}
}

Просмотреть файл

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2b9f004f75e79b848808a5354ffd2c2e
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -0,0 +1,68 @@
Shader "VolumeRendering/TransferFunctionShader"
{
Properties
{
_HistTex ("Histogram Texture", 2D) = "white" {}
_TFTex("Transfer Function Texture", 2D) = "white" {}
}
SubShader
{
Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _HistTex;
sampler2D _TFTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float density = i.uv.x;
float histY = tex2D(_HistTex, float2(density, 0.0f)).r;
float4 tfCol = tex2D(_TFTex, float2(density, 0.0f));
float4 histCol = histY > i.uv.y ? float4(1.0f, 1.0f, 1.0f, 1.0f) : float4(0.0f, 0.0f, 0.0f, 0.0f);
float alpha = tfCol.a;
if (i.uv.y > alpha)
tfCol.a = 0.0f;
float4 col = histCol * 0.5f + tfCol * 0.7f;
return col;
}
ENDCG
}
}
}

Просмотреть файл

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 9117c7b46d847fc41adc9d9bb2cfa4c4
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -6,6 +6,8 @@ public class TransferFunction
public List<TFColourControlPoint> colourControlPoints = new List<TFColourControlPoint>();
public List<TFAlphaControlPoint> alphaControlPoints = new List<TFAlphaControlPoint>();
public Texture2D histogramTexture = null;
private Texture2D texture = null;
Color[] tfCols;

Просмотреть файл

@ -0,0 +1,27 @@
using UnityEngine;
public class HistogramTextureGenerator
{
public static Texture2D GenerateHistogramTexture(VolumeDataset dataset)
{
int numSamples = dataset.maxDataValue + 1;
int[] values = new int[numSamples];
Color[] cols = new Color[numSamples];
Texture2D texture = new Texture2D(numSamples, 1, TextureFormat.RGBAFloat, false);
int maxFreq = 0;
for (int iData = 0; iData < dataset.data.Length; iData++)
{
values[dataset.data[iData]] += 1;
maxFreq = System.Math.Max(values[dataset.data[iData]], maxFreq);
}
for (int iSample = 0; iSample < numSamples; iSample++)
cols[iSample] = new Color(Mathf.Log10((float)values[iSample]) / Mathf.Log10((float)maxFreq), 0.0f, 0.0f, 1.0f);
texture.SetPixels(cols);
texture.Apply();
return texture;
}
}

Просмотреть файл

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f9bcab751cba35d4dbd074d22a7f194c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: