From c9256256c3d8b1da2a0cb063b1c8bb9ab8f484f9 Mon Sep 17 00:00:00 2001 From: NUZEROVI Date: Sat, 29 Oct 2022 14:14:35 +0800 Subject: [PATCH] add inverse masking function for de-occlusion component --- Assets/Debugging/ShaderDebugging_Mask.cs | 70 + Assets/Debugging/ShaderDebugging_Mask.cs.meta | 11 + .../DirectVolumeRenderingMaterial_Mask.mat | 100 ++ ...irectVolumeRenderingMaterial_Mask.mat.meta | 8 + Assets/Resources/VolumeContainer_Mask.prefab | 81 ++ .../VolumeContainer_Mask.prefab.meta | 7 + Assets/Scripts/GUI/UI/AnalysisBtn.cs | 75 +- Assets/Scripts/GUI/UI/BtnEvent.cs | 32 +- .../GUI/UI/BtnSetLensShapePressedColor.cs | 20 +- Assets/Scripts/GUI/UI/BtnSetPressed.cs | 28 +- .../Scripts/GUI/UI/BtnSetSizePressedColor.cs | 13 +- Assets/Scripts/GUI/UI/SliderEvent.cs | 13 +- Assets/Scripts/GUI/UI/ToggleEvent.cs | 67 +- .../InteractiveController/MouseEvent/Hover.cs | 42 +- .../VolumeObject/VolumeObjectFactory.cs | 12 +- .../VolumeObject/VolumeObjectFactory_Mask.cs | 78 ++ .../VolumeObjectFactory_Mask.cs.meta | 11 + .../VolumeObject/VolumeRenderedObject_Mask.cs | 198 +++ .../VolumeRenderedObject_Mask.cs.meta | 11 + .../DirectVolumeRenderingShader_Mask.shader | 1245 +++++++++++++++++ ...rectVolumeRenderingShader_Mask.shader.meta | 9 + 21 files changed, 2083 insertions(+), 48 deletions(-) create mode 100644 Assets/Debugging/ShaderDebugging_Mask.cs create mode 100644 Assets/Debugging/ShaderDebugging_Mask.cs.meta create mode 100644 Assets/Materials/DirectVolumeRenderingMaterial_Mask.mat create mode 100644 Assets/Materials/DirectVolumeRenderingMaterial_Mask.mat.meta create mode 100644 Assets/Resources/VolumeContainer_Mask.prefab create mode 100644 Assets/Resources/VolumeContainer_Mask.prefab.meta create mode 100644 Assets/Scripts/VolumeObject/VolumeObjectFactory_Mask.cs create mode 100644 Assets/Scripts/VolumeObject/VolumeObjectFactory_Mask.cs.meta create mode 100644 Assets/Scripts/VolumeObject/VolumeRenderedObject_Mask.cs create mode 100644 Assets/Scripts/VolumeObject/VolumeRenderedObject_Mask.cs.meta create mode 100644 Assets/Shaders/DirectVolumeRenderingShader_Mask.shader create mode 100644 Assets/Shaders/DirectVolumeRenderingShader_Mask.shader.meta diff --git a/Assets/Debugging/ShaderDebugging_Mask.cs b/Assets/Debugging/ShaderDebugging_Mask.cs new file mode 100644 index 00000000..b3ba0468 --- /dev/null +++ b/Assets/Debugging/ShaderDebugging_Mask.cs @@ -0,0 +1,70 @@ +using UnityEngine; + +namespace UnityVolumeRendering +{ + public class ShaderDebugging_Mask : MonoBehaviour + { + public GameObject target_Mask; + + private Material material_Mask; + private ComputeBuffer buffer_Mask; + public Vector4[] element_Mask; + private string label_Mask; + private Renderer render_Mask; + + MeshRenderer meshRenderer_Mask; + Material mat_Mask; + + + void Load() + { + buffer_Mask = new ComputeBuffer(10, 16, ComputeBufferType.Default); + element_Mask = new Vector4[10]; + for(int i=0; i < buffer_Mask.count; i++) + { + element_Mask[i] = new Vector4(0, 0, 0, 0); + } + buffer_Mask.SetData(element_Mask); + + label_Mask = string.Empty; + render_Mask = target_Mask.GetComponent(); + material_Mask = render_Mask.material; + } + + void Start() + { + Load(); + VolumeRenderedObject_Mask[] objects_Mask = FindObjectsOfType(); + if (objects_Mask.Length == 1) + { + meshRenderer_Mask = objects_Mask[0].meshRenderer; + mat_Mask = meshRenderer_Mask.material; + } + } + + void Update() + { + Graphics.ClearRandomWriteTargets(); + material_Mask.SetPass(0); + material_Mask.SetBuffer("buffer_Mask", buffer_Mask); + Graphics.SetRandomWriteTarget(2, buffer_Mask, false); + + buffer_Mask.GetData(element_Mask); + + label_Mask = (element_Mask != null && render_Mask.isVisible) ? element_Mask[0].ToString() : string.Empty; + } + + void OnGUI() + { + GUIStyle style = new GUIStyle(); + + GUI.color = Color.white; + style.fontSize = 32; + } + + void OnDestroy() + { + buffer_Mask.Dispose(); + } + } +} \ No newline at end of file diff --git a/Assets/Debugging/ShaderDebugging_Mask.cs.meta b/Assets/Debugging/ShaderDebugging_Mask.cs.meta new file mode 100644 index 00000000..b097c76a --- /dev/null +++ b/Assets/Debugging/ShaderDebugging_Mask.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4d8c1e4e62204c24b957f1d3a489ac23 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Materials/DirectVolumeRenderingMaterial_Mask.mat b/Assets/Materials/DirectVolumeRenderingMaterial_Mask.mat new file mode 100644 index 00000000..ee28aeb1 --- /dev/null +++ b/Assets/Materials/DirectVolumeRenderingMaterial_Mask.mat @@ -0,0 +1,100 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: DirectVolumeRenderingMaterial_Mask + m_Shader: {fileID: 4800000, guid: a20d4a69f209bf64ea10de5efd333aa5, type: 3} + m_ShaderKeywords: MODE_DVR TF2D_ON _MODE_DVR _MODE_MIP + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: + - pass0 + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DataTex: + 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} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _GradientTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 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} + - _NoiseTex: + 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} + - _SegmentTex: + 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 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _MaxVal: 1 + - _Metallic: 0 + - _MinVal: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} diff --git a/Assets/Materials/DirectVolumeRenderingMaterial_Mask.mat.meta b/Assets/Materials/DirectVolumeRenderingMaterial_Mask.mat.meta new file mode 100644 index 00000000..7e9ccca1 --- /dev/null +++ b/Assets/Materials/DirectVolumeRenderingMaterial_Mask.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dff8f83be22f4104b838e8253440b566 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/VolumeContainer_Mask.prefab b/Assets/Resources/VolumeContainer_Mask.prefab new file mode 100644 index 00000000..50027b17 --- /dev/null +++ b/Assets/Resources/VolumeContainer_Mask.prefab @@ -0,0 +1,81 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &1642054173293210335 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 3483431779698201852} + - component: {fileID: 4600922654780393457} + - component: {fileID: 1185744897911437941} + m_Layer: 0 + m_Name: VolumeContainer_Mask + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &3483431779698201852 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1642054173293210335} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!33 &4600922654780393457 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1642054173293210335} + m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} +--- !u!23 &1185744897911437941 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1642054173293210335} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RenderingLayerMask: 4294967295 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: dff8f83be22f4104b838e8253440b566, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 0 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 diff --git a/Assets/Resources/VolumeContainer_Mask.prefab.meta b/Assets/Resources/VolumeContainer_Mask.prefab.meta new file mode 100644 index 00000000..f8fda686 --- /dev/null +++ b/Assets/Resources/VolumeContainer_Mask.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 222363ee9b11d324c9f631adba97ec3b +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/GUI/UI/AnalysisBtn.cs b/Assets/Scripts/GUI/UI/AnalysisBtn.cs index a5dcc22c..1d801feb 100644 --- a/Assets/Scripts/GUI/UI/AnalysisBtn.cs +++ b/Assets/Scripts/GUI/UI/AnalysisBtn.cs @@ -19,8 +19,8 @@ public class AnalysisBtn : MonoBehaviour public string isoRangeFilePath; public string tFFilePath; public string segTexturePath; - MeshRenderer meshRenderer; - Material mat; + MeshRenderer meshRenderer, meshRenderer_Mask; + Material mat, mat_Mask; public float[] isoRange = new float[20]; public Vector4[] isoCluster = new Vector4[20]; @@ -73,6 +73,7 @@ void BtnClick(string btn) endianness = initData.endianness; ImportRawData(); GetIsoRangeAndSetTF(); + GetIsoRangeAndSetTF_Mask(); } } @@ -106,14 +107,20 @@ private void ImportRawData() } VolumeRenderedObject obj = VolumeObjectFactory.CreateObject(dataset); - - if(eulerX == 180.0f) + VolumeRenderedObject_Mask obj_Mask = VolumeObjectFactory_Mask.CreateObject(dataset); + + if (eulerX == 180.0f) { obj.gameObject.transform.rotation *= Quaternion.AngleAxis(90, Vector3.right); - }else if(eulerX == 0.0f) + obj_Mask.gameObject.transform.rotation *= Quaternion.AngleAxis(90, Vector3.right); + } + else if(eulerX == 0.0f) { obj.gameObject.transform.rotation *= Quaternion.AngleAxis(-90, Vector3.right); + obj_Mask.gameObject.transform.rotation *= Quaternion.AngleAxis(-90, Vector3.right); } + + } else { @@ -161,12 +168,66 @@ void GetIsoRangeAndSetTF() //{ TransferFunction newTF = TransferFunctionDatabase.LoadTransferFunction(tFFilePath); if (newTF != null) + { objects[0].transferFunction = newTF; //objects[0].SetVisibilityWindow(0.58f, 1.0f); objects[0].UpdateMaterialProperties(); - // } + // } + } + + + } + + + void GetIsoRangeAndSetTF_Mask() + { + VolumeRenderedObject_Mask[] objects_Mask = FindObjectsOfType(); + if (objects_Mask.Length == 1) + { + meshRenderer_Mask = objects_Mask[0].meshRenderer; + mat_Mask = meshRenderer_Mask.material; + } + //if(isoRangeFilePath != "") + //{ + List fileLines = File.ReadAllLines(isoRangeFilePath).ToList(); + + int range = 0; //dataset.GetMinDataValue(); + int Count = 0; + foreach (string line in fileLines) + { + range += int.Parse(line); + + isoRange[Count] = range; + + if (Count == 0) + { + isoCluster[Count] = new Vector4(0, isoRange[Count] - 1); + } + else + { + isoCluster[Count] = new Vector4(isoRange[Count - 1], isoRange[Count] - 1); + } + Count++; + } + + mat_Mask.SetInt("_isoCount", Count); + mat_Mask.SetFloatArray("_isoRange", isoRange); + mat_Mask.SetVectorArray("_isoCluster", isoCluster); + // } + + //if (tFFilePath != "") + //{ + TransferFunction newTF = TransferFunctionDatabase.LoadTransferFunction(tFFilePath); + if (newTF != null) + { + objects_Mask[0].transferFunction = newTF; + //objects[0].SetVisibilityWindow(0.58f, 1.0f); + objects_Mask[0].UpdateMaterialProperties(); + // } + } + } - + } } \ No newline at end of file diff --git a/Assets/Scripts/GUI/UI/BtnEvent.cs b/Assets/Scripts/GUI/UI/BtnEvent.cs index 3e830cae..45465f75 100644 --- a/Assets/Scripts/GUI/UI/BtnEvent.cs +++ b/Assets/Scripts/GUI/UI/BtnEvent.cs @@ -9,8 +9,8 @@ public class BtnEvent : MonoBehaviour { public Button btn; - MeshRenderer meshRenderer; - Material mat; + MeshRenderer meshRenderer, meshRenderer_Mask; + Material mat, mat_Mask; //float viewCamIndex; void Start() @@ -32,23 +32,34 @@ void BtnClick(string btn) mat = meshRenderer.material; } + VolumeRenderedObject_Mask[] objects_Mask = FindObjectsOfType(); + if (objects_Mask.Length == 1) + { + meshRenderer_Mask = objects_Mask[0].meshRenderer; + mat_Mask = meshRenderer_Mask.material; + } + if (btn == "RightBtn") { - objects[0].gameObject.transform.rotation *= Quaternion.AngleAxis(90, Vector3.forward); + objects[0].gameObject.transform.rotation *= Quaternion.AngleAxis(90, Vector3.forward); + //objects_Mask[0].gameObject.transform.rotation *= Quaternion.AngleAxis(90, Vector3.forward); } else if (btn == "LeftBtn") { - objects[0].gameObject.transform.rotation *= Quaternion.AngleAxis(-90, Vector3.forward); + objects[0].gameObject.transform.rotation *= Quaternion.AngleAxis(-90, Vector3.forward); + //objects_Mask[0].gameObject.transform.rotation *= Quaternion.AngleAxis(-90, Vector3.forward); } else if (btn == "DownBtn") { objects[0].gameObject.transform.rotation *= Quaternion.AngleAxis(-90, Vector3.right); + //objects_Mask[0].gameObject.transform.rotation *= Quaternion.AngleAxis(-90, Vector3.right); } else if (btn == "UpBtn") { - objects[0].gameObject.transform.rotation *= Quaternion.AngleAxis(90, Vector3.right); + objects[0].gameObject.transform.rotation *= Quaternion.AngleAxis(90, Vector3.right); + //objects_Mask[0].gameObject.transform.rotation *= Quaternion.AngleAxis(90, Vector3.right); } var localVector = objects[0].gameObject.transform.InverseTransformDirection(Vector3.forward); @@ -67,7 +78,16 @@ void BtnClick(string btn) mat.SetFloat("RotationX", objects[0].gameObject.transform.rotation.eulerAngles.x); mat.SetFloat("RotationY", objects[0].gameObject.transform.rotation.eulerAngles.y); mat.SetFloat("RotationZ", objects[0].gameObject.transform.rotation.eulerAngles.z); - + + mat_Mask.SetFloat("_localDepth", localDepth); + mat_Mask.SetFloat("_depthNP", depthNP); + + mat_Mask.SetFloat("RotationX", objects[0].gameObject.transform.rotation.eulerAngles.x); + mat_Mask.SetFloat("RotationY", objects[0].gameObject.transform.rotation.eulerAngles.y); + mat_Mask.SetFloat("RotationZ", objects[0].gameObject.transform.rotation.eulerAngles.z); + + + } diff --git a/Assets/Scripts/GUI/UI/BtnSetLensShapePressedColor.cs b/Assets/Scripts/GUI/UI/BtnSetLensShapePressedColor.cs index 86d1d033..f0d0a21b 100644 --- a/Assets/Scripts/GUI/UI/BtnSetLensShapePressedColor.cs +++ b/Assets/Scripts/GUI/UI/BtnSetLensShapePressedColor.cs @@ -9,8 +9,8 @@ public class BtnSetLensShapePressedColor : MonoBehaviour { private Button btn; - MeshRenderer meshRenderer; - Material mat; + MeshRenderer meshRenderer, meshRenderer_Mask; + Material mat, mat_Mask; void Start() { @@ -30,7 +30,14 @@ public void SetColor(string name) { meshRenderer = objects[0].meshRenderer; mat = meshRenderer.material; - } + } + + VolumeRenderedObject_Mask[] objects_Mask = FindObjectsOfType(); + if (objects_Mask.Length == 1) + { + meshRenderer_Mask = objects_Mask[0].meshRenderer; + mat_Mask = meshRenderer_Mask.material; + } int nums = GameObject.Find("SetLensShapeBtn Group").transform.childCount; @@ -76,9 +83,10 @@ public void SetColor(string name) //_LensShapeNums[lensNum] = btnIndex; mat.SetFloatArray("_LensIndexs", _LensIndexs); - //sliderObj.sliNumOn = btnIndex; - //sliderObj.GetComponent().value = _CircleSize[btnIndex]; - + mat_Mask.SetFloatArray("_LensIndexs", _LensIndexs); + //sliderObj.sliNumOn = btnIndex; + //sliderObj.GetComponent().value = _CircleSize[btnIndex]; + //mat.SetInt("_CurrentWidgetNum", btnIndex); diff --git a/Assets/Scripts/GUI/UI/BtnSetPressed.cs b/Assets/Scripts/GUI/UI/BtnSetPressed.cs index 980206dc..ad5d8046 100644 --- a/Assets/Scripts/GUI/UI/BtnSetPressed.cs +++ b/Assets/Scripts/GUI/UI/BtnSetPressed.cs @@ -8,7 +8,10 @@ namespace UnityVolumeRendering public class BtnSetPressed : MonoBehaviour { private Button btn; - + private MeshRenderer meshRenderer, meshRenderer_Mask; + private Material mat, mat_Mask; + private VolumeRenderedObject[] objects; + private VolumeRenderedObject_Mask[] objects_Mask; void Start() { btn = this.GetComponent