Skip to content

Instantly share code, notes, and snippets.

@pema99
Created May 13, 2023 18:40
Show Gist options
  • Save pema99/69250e33659e4a58b84f5af1a6d026e9 to your computer and use it in GitHub Desktop.
Save pema99/69250e33659e4a58b84f5af1a6d026e9 to your computer and use it in GitHub Desktop.
float3 drawTopArea(float2 uv)
{
float3 color = FOREGROUND_COLOR;
float areaWidth = 1.0 - FRAME_MARGIN * 2;
float areaHeight = 0.35;
float boxWidth = areaWidth / 4.0;
for (uint i = 0; i < 4; i++)
{
float boxHeight = 0.25 + sin(_Time.y * 2.0 + float(i) * 0.5) * 0.1;
float leftCornerRadius = i == 0 ? CORNER_RADIUS : 0.0;
float rightCornerRadius = i == 3 ? CORNER_RADIUS : 0.0;
float boxDist = sdRoundedBoxBottomRight(
translate(uv, float2(boxWidth * i, areaHeight)),
float2(boxWidth, boxHeight),
float4(rightCornerRadius, CORNER_RADIUS, leftCornerRadius, CORNER_RADIUS)
);
float shellDist = shell(boxDist, OUTLINE_WIDTH);
// outer shell
addElement(color, ACTIVE_COLOR, shellDist);
// colored inner portion
float3 innerColor = float4x4(
float4(1.0, 0.0, 0.0, 0.0), // red
float4(1.0, 1.0, 0.0, 0.0), // yellow
float4(0.0, 1.0, 0.0, 0.0), // green
float4(0.0, 0.0, 1.0, 0.0) // blue
)[i % 4] * 0.4;
addElement(color, innerColor, boxDist+OUTLINE_WIDTH);
// Top pivot
float handleDist = sdSphere(
translate(uv, float2(boxWidth * 0.5 + boxWidth * i, areaHeight-boxHeight)),
HANDLE_RADIUS
);
addElement(color, 1.0, handleDist);
}
return color;
}
float3 drawGainSlider(float2 uv)
{
float3 color = FOREGROUND_COLOR;
float areaWidth = 0.6; // these should probaby be passed in instead
float areaHeight = 0.15;
// Background fill
float maxTriangleWidth = areaWidth*0.8;
float bgTriangleDist = inflate(sdTriangleIsosceles(
rotate(translate(uv, float2(0.05, areaHeight * 0.5)), UNITY_PI*0.5),
float2(areaHeight*0.3, maxTriangleWidth)
), 0.002);
addElement(color, INACTIVE_COLOR, bgTriangleDist);
// Current active area
float currentTriangleWidth = maxTriangleWidth * (sin(_Time.y) * 0.5 + 0.5);
float currentTriangleDist = max(bgTriangleDist, uv.x - currentTriangleWidth - 0.05);
addElement(color, ACTIVE_COLOR, currentTriangleDist);
// Slider handle
float handleDist = sdSphere(
translate(uv, float2(currentTriangleWidth + 0.05, areaHeight * 0.5)),
HANDLE_RADIUS
);
addElement(color, ACTIVE_COLOR, handleDist);
// Slider vertical grip
float gripDist = abs(uv.x - currentTriangleWidth - 0.05) - OUTLINE_WIDTH;
addElement(color, ACTIVE_COLOR, gripDist);
return color;
}
float3 drawUI(float2 uv)
{
float3 color = BACKGROUND_COLOR;
// Top area
float2 topAreaOrigin = translate(uv, FRAME_MARGIN);
float topAreaDist = sdRoundedBoxTopLeft(
topAreaOrigin,
float2(1.0 - FRAME_MARGIN * 2, 0.35),
CORNER_RADIUS
);
addElement(color, drawTopArea(topAreaOrigin), topAreaDist);
// Gain slider
float2 gainSliderOrigin = translate(uv, FRAME_MARGIN + float2(0, 0.4));
float gainSliderDist = sdRoundedBoxTopLeft(
gainSliderOrigin,
float2(0.6, 0.15),
CORNER_RADIUS
);
addElement(color, drawGainSlider(gainSliderOrigin), gainSliderDist);
return color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment