Skip to content

Commit 02ce652

Browse files
Unity vertex shader debugging example
1 parent b1f08b4 commit 02ce652

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

debugger.shader

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Description: https://forum.unity.com/threads/how-to-print-shaders-var-please.26052/#post-5160875
2+
3+
Shader "Debugger"
4+
{
5+
Properties
6+
{
7+
_MainTex ("Texture", 2D) = "white" {}
8+
}
9+
SubShader
10+
{
11+
Cull Off
12+
Pass
13+
{
14+
CGPROGRAM
15+
#pragma vertex VSMain
16+
#pragma geometry GSMain
17+
#pragma fragment PSMain
18+
#pragma target 5.0
19+
20+
sampler2D _MainTex;
21+
22+
struct Data
23+
{
24+
float4 vertex : SV_Position;
25+
float2 uv : TEXCOORD0;
26+
float number : VALUE;
27+
};
28+
29+
// "PrintValue" function by P.Malin
30+
float PrintValue( float2 vCoords, float fValue, float fMaxDigits, float fDecimalPlaces )
31+
{
32+
if ((vCoords.y < 0.0) || (vCoords.y >= 1.0)) return 0.0;
33+
bool bNeg = ( fValue < 0.0 );
34+
fValue = abs(fValue);
35+
float fBiggestIndex = max(floor(log2(abs(fValue)) / log2(10.0)), 0.0);
36+
float fDigitIndex = fMaxDigits - floor(vCoords.x);
37+
float fCharBin = 0.0;
38+
if(fDigitIndex > (-fDecimalPlaces - 1.01))
39+
{
40+
if(fDigitIndex > fBiggestIndex)
41+
{
42+
if((bNeg) && (fDigitIndex < (fBiggestIndex+1.5))) fCharBin = 1792.0;
43+
}
44+
else
45+
{
46+
if(fDigitIndex == -1.0)
47+
{
48+
if(fDecimalPlaces > 0.0) fCharBin = 2.0;
49+
}
50+
else
51+
{
52+
float fReducedRangeValue = fValue;
53+
if(fDigitIndex < 0.0) { fReducedRangeValue = frac( fValue ); fDigitIndex += 1.0; }
54+
float fDigitValue = (abs(fReducedRangeValue / (pow(10.0, fDigitIndex))));
55+
int x = int(floor(fDigitValue - 10.0 * floor(fDigitValue/10.0)));
56+
fCharBin = x==0?480599.0:x==1?139810.0:x==2?476951.0:x==3?476999.0:x==4?350020.0:x==5?464711.0:x==6?464727.0:x==7?476228.0:x==8?481111.0:x==9?481095.0:0.0;
57+
}
58+
}
59+
}
60+
float result = (fCharBin / pow(2.0, floor(frac(vCoords.x) * 4.0) + (floor(vCoords.y * 5.0) * 4.0)));
61+
return floor(result - 2.0 * floor(result/2.0));
62+
}
63+
64+
Data VSMain( float4 vertex:POSITION, float2 uv:TEXCOORD0 )
65+
{
66+
Data VS;
67+
VS.uv = uv;
68+
VS.vertex = vertex;
69+
VS.number = 12.34; //vertex shader variable value to print
70+
return VS;
71+
}
72+
73+
[maxvertexcount(9)]
74+
void GSMain( triangle Data patch[3], inout TriangleStream<Data> stream, uint id:SV_PRIMITIVEID )
75+
{
76+
Data GS;
77+
for (uint i = 0; i < 3; i++)
78+
{
79+
GS.vertex = UnityObjectToClipPos(patch[i].vertex);
80+
GS.uv = patch[i].uv;
81+
GS.number = patch[i].number;
82+
stream.Append(GS);
83+
}
84+
stream.RestartStrip();
85+
86+
if (id == 0) // determine quad
87+
{
88+
for (uint i = 0; i < 6; i++)
89+
{
90+
float u = float(i) - 2.0 * floor(float(i)/2.0);
91+
float v = sign(fmod(126.0,fmod(float(i),6.0)+6.0));
92+
GS.uv = float2(u, 1.0 - v) + 10000.0; // UV offset
93+
GS.vertex = float4(sign(u)+0.5, sign(v)+0.5, 0.3, 1.0);
94+
GS.number = patch[0].number;
95+
stream.Append(GS);
96+
}
97+
}
98+
stream.RestartStrip();
99+
}
100+
101+
float4 PSMain(Data PS) : SV_Target
102+
{
103+
float value = PS.number;
104+
if (PS.uv.x > 9000.0) // determine quad
105+
{
106+
float2 font = float2(24.0, 30.0);
107+
float2 position = float2(_ScreenParams.x - 250.0, 15.0);
108+
float3 base = PrintValue( (PS.vertex.xy - position) / font, value, 6.0, 2.0).xxx;
109+
return float4(base, 1.0);
110+
}
111+
else return tex2D(_MainTex, PS.uv);
112+
}
113+
ENDCG
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)