Debugging.shader
Shader "Debugging"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 5.0
RWStructuredBuffer<float4> buffer : register(u1);
float3 test;
struct appdata
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
v2f vert(appdata v)
{
//vertex = UnityObjectToClipPos(vertex);
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
test = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
//o.viewDir = WorldSpaceViewDir(v.vertex);
return o;
}
float4 frag(v2f i) : SV_Target
{
float4 p = float4(0 / 255,255 / 255,0 / 255,1);
if (i.worldPos.x == 0.1 && i.worldPos.y == 0 && i.worldPos.z == 0.1)
p = float4(255 / 255,0 / 255,0 / 255,1);
buffer[0] = p; //write value to buffer
buffer[0] = float4(i.worldPos.x, i.worldPos.y, i.worldPos.z,1);
return p;
}
ENDCG
}
}
ShaderDebugging.cs
using UnityEngine;
public class ShaderDebugging : MonoBehaviour
{
public GameObject target;
private Material material;
private ComputeBuffer buffer;
private Vector4[] element;
private string label;
private Renderer render;
void Load()
{
buffer = new ComputeBuffer(1, 16, ComputeBufferType.Default);
element = new Vector4[1];
label = string.Empty;
render = target.GetComponent<Renderer>();
material = render.material;
}
void Start()
{
Load();
}
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("F3") : string.Empty;
}
void OnGUI()
{
GUIStyle style = new GUIStyle();
style.fontSize = 32;
GUI.Label(new Rect(50, 50, 400, 100), label, style);
Debug.Log(label);
}
void OnDestroy()
{
buffer.Dispose();
}
}