Skip to content

Instantly share code, notes, and snippets.

@supertask
Created July 5, 2023 17:27
Show Gist options
  • Save supertask/69d171920db80f48bb075708c5571df9 to your computer and use it in GitHub Desktop.
Save supertask/69d171920db80f48bb075708c5571df9 to your computer and use it in GitHub Desktop.
ShowFloatInShader
float3 ShowFloat(float2 uv, float val, int digits){
int a[210] =
{
1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,0,0,1,1,1, 0,0,0,0,0,0,
1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0,0,0,1,0,0, 0,1,0,0,0,0,
1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,0,0,1,1,1, 1,1,1,1,1,1,
1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0,0,0,1,0,0, 0,1,0,0,0,0,
1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,0,0,1,1,1, 0,0,0,0,0,0
};
//0123456789.E+-
// arranged values to show
val *= 1.000001;
bool signVal = val >= 0.0;
float logged = log(abs(val))/log(10.0);
int powInt = int(floor(logged));
bool signPow = logged >= 0.0;
float floatRaw = abs(val) / pow(10.0, 0.0+powInt);
// pixel idx & pixel size in a quad
uint resX = 4 * (digits + 2) + 1; // sign, point
uint resY = 13;
uint myPixX = uint(uv.x * resX);
uint myPixY = uint(uv.y * resY);
int letter = -1;
uint2 idxLetter = uint2(myPixX/4, myPixY/6);
uint2 uvInLetter = uint2(myPixX%4, myPixY%6);
// find what letter to show from "idx Letter"
if(idxLetter.y == 0){
if(idxLetter.x == 0){
letter = 11;
}
else if(idxLetter.x == 1){
letter = signPow ? 12 : 13;
}
else if(idxLetter.x == 2){
letter = abs(powInt)/10;
}
else if(idxLetter.x == 3){
letter = abs(powInt)%10;
}
}
else if(idxLetter.y == 1){
if(idxLetter.x == 0){
letter = signVal ? 12 : 13;
}
if(idxLetter.x == 1){
letter = int(floatRaw);
}
if(idxLetter.x == 2){
letter = 10;
}
if(idxLetter.x >= 3){
letter = uint(floatRaw*pow(10.0,idxLetter.x-2.0))%10;
}
}
// decide black or white from "letter" and "uvInLetter"
float3 col = 0.0;
if(uvInLetter.y >=1 && uvInLetter.x>=1 && letter>=0){
uint yFromUp = 5-uvInLetter.y;
uint xFromLeft = letter * 3 + (uvInLetter.x-1);
if(a[yFromUp*42 + xFromLeft] == 1){
col = 1.0;
}
}
return col;
}
float3 ShowInt(float2 uv, int val, int digits){
int a[210] =
{
1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,0,0,1,1,1, 0,0,0,0,0,0,
1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0,0,0,1,0,0, 0,1,0,0,0,0,
1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,0,0,1,1,1, 1,1,1,1,1,1,
1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0,0,0,1,0,0, 0,1,0,0,0,0,
1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,0,0,1,1,1, 0,0,0,0,0,0
};
//0123456789.E+-
// arranged values to show
bool signVal = val >= 0;
uint absVal = abs(val);
// pixel idx & pixel size in a quad
uint resX = 4 * (digits + 1) + 1; // sign, point
uint resY = 13;
uint myPixX = uint(uv.x * resX);
uint myPixY = uint(uv.y * resY);
int letter = -1;
uint2 idxLetter = uint2(myPixX/4, myPixY/6);
uint2 uvInLetter = uint2(myPixX%4, myPixY%6);
// find what letter to show from "idx Letter"
absVal *= 10;
if(idxLetter.y == 0){
if(idxLetter.x == 0){
letter = signVal ? 12 : 13;
}
if(idxLetter.x >= 1){
for(uint i=0; i<digits - (idxLetter.x-1); i++){
absVal /= 10;
}
letter = absVal %10;
}
}
// decide black or white from "letter" and "uvInLetter"
float3 col = 0.0;
if(uvInLetter.y >=1 && uvInLetter.x>=1 && letter>=0){
uint yFromUp = 5-uvInLetter.y;
uint xFromLeft = letter * 3 + (uvInLetter.x-1);
if(a[yFromUp*42 + xFromLeft] == 1){
col = 1.0;
}
}
return col;
}
float3 ShowPercentage(float2 uv, float val, int digits){
int a[180] =
{
1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,0,0, 1,1,1,
1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0,0,0, 1,0,1,
1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,0,0, 1,1,1,
1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0,0,0, 1,0,0,
1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,0,0, 1,0,0
};
//0123456789.E+-
// arranged values to show
val *= 1.000001;
// pixel idx & pixel size in a quad
uint resX = 4 * (digits + 2) + 1; // sign, point
uint resY = 13;
uint myPixX = uint(uv.x * resX);
uint myPixY = uint(uv.y * resY);
int letter = -1;
uint2 idxLetter = uint2(myPixX/4, myPixY/6);
uint2 uvInLetter = uint2(myPixX%4, myPixY%6);
// find what letter to show from "idx Letter"
if(idxLetter.y == 0){
if(idxLetter.x == 0){
letter = uint(val * pow(10.0,0.0))%10;
if(letter==0) letter = -1;
}
else if(idxLetter.x == 1){
letter = uint(val * pow(10.0,1.0))%10;
}
else if(idxLetter.x == 2){
letter = uint(val * pow(10.0,2.0))%10;
}
else if(idxLetter.x == 3){
letter = 10;
}
else if(idxLetter.x == digits+1){
letter = 11;
}
else{
letter = uint(val * pow(10.0,idxLetter.x-1))%10;;
}
}
// decide black or white from "letter" and "uvInLetter"
float3 col = 0.0;
if(uvInLetter.y >=1 && uvInLetter.x>=1 && letter>=0){
uint yFromUp = 5-uvInLetter.y;
uint xFromLeft = letter * 3 + (uvInLetter.x-1);
if(a[yFromUp*36 + xFromLeft] == 1){
col = 1.0;
}
}
return col;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment