Skip to content

Commit

Permalink
Load Shader Debugging
Browse files Browse the repository at this point in the history
Use RWStructuredBuffer to read values from pixel shader variables. (reference by przemyslawzaworski)
(https://www.youtube.com/watch?v=ZUPZTet54mU&ab_channel=PrzemyslawZaworski)
  • Loading branch information
NUZEROVI committed Oct 25, 2022
1 parent 284425a commit 5e701e7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Assets/Debugging.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions Assets/Debugging/ShaderDebugging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using UnityEngine;

namespace UnityVolumeRendering
{
public class ShaderDebugging : MonoBehaviour
{
public GameObject target;

private Material material;
private ComputeBuffer buffer;
public Vector4[] element;
private string label;
private Renderer render;

MeshRenderer meshRenderer;
Material mat;


void Load()
{
buffer = new ComputeBuffer(10, 16, ComputeBufferType.Default);
element = new Vector4[10];
for(int i=0; i <buffer.count; i++)
{
element[i] = new Vector4(0, 0, 0, 0);
}
buffer.SetData(element);

label = string.Empty;
render = target.GetComponent<Renderer>();
material = render.material;
}

void Start()
{
Load();
VolumeRenderedObject[] objects = FindObjectsOfType<VolumeRenderedObject>();
if (objects.Length == 1)
{
meshRenderer = objects[0].meshRenderer;
mat = meshRenderer.material;
}
}

void Update()
{
Graphics.ClearRandomWriteTargets();
material.SetPass(0);
material.SetBuffer("buffer", buffer);
Graphics.SetRandomWriteTarget(1, buffer, false);

buffer.GetData(element);

label = (element != null && render.isVisible) ? element[0].ToString() : string.Empty;
}

void OnGUI()
{
GUIStyle style = new GUIStyle();

GUI.color = Color.white;
style.fontSize = 32;
}

void OnDestroy()
{
buffer.Dispose();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Debugging/ShaderDebugging.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Assets/Scripts/VolumeObject/VolumeObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public static VolumeRenderedObject CreateObject(VolumeDataset dataset)
outerObject.transform.localScale = new Vector3((float)Math.Round((decimal)dataset.dimX / maxDim, 2), (float)Math.Round((decimal)dataset.dimY / maxDim, 2), (float)Math.Round((decimal)dataset.dimZ / maxDim, 2));
meshContainer.AddComponent<BoxCollider>();

GameObject ShaderBuffer = new GameObject("ShaderBuffer");
ShaderDebugging shaderDebugObj = ShaderBuffer.AddComponent<ShaderDebugging>();
shaderDebugObj.target = meshContainer;

outerObject.transform.localRotation = Quaternion.Euler(90.0f, 0.0f, 0.0f);

MeshRenderer meshRenderer = meshContainer.GetComponent<MeshRenderer>();
Expand Down

0 comments on commit 5e701e7

Please sign in to comment.