Skip to content

Instantly share code, notes, and snippets.

@cmdr2
Last active July 22, 2025 05:07
Show Gist options
  • Select an option

  • Save cmdr2/2939955cb5f94ae5c1ce625a825e0225 to your computer and use it in GitHub Desktop.

Select an option

Save cmdr2/2939955cb5f94ae5c1ce625a825e0225 to your computer and use it in GitHub Desktop.
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
VERT_SHADER = """
uniform mat4 ModelViewProjectionMatrix;
in vec2 texCoord;
in vec3 position;
out vec3 pos;
out vec2 texCoord_interp;
void main() {
pos = position;
gl_Position = ModelViewProjectionMatrix * vec4(position, 1.0);
texCoord_interp = texCoord;
}
"""
FRAG_SHADER = """
in vec3 pos;
in vec2 texCoord_interp;
out vec4 FragColor;
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
vec3 srgb_to_linear(vec3 x) {
bvec3 cutoff = lessThanEqual(x, vec3(0.04045));
vec3 lower = x / 12.92;
vec3 higher = pow((x + 0.055) / 1.055, vec3(2.4));
return mix(higher, lower, cutoff);
}
void main() {
vec2 coord = texCoord_interp; // 0 to 1
coord = coord * 2.0 - 1.0; // -1 to 1
float r = length(coord);
if (r > 1.0) {
FragColor = vec4(0.0);
return;
}
float angle = atan(coord.y, coord.x);
float hue = angle / (2.0 * 3.14) + 0.5;
float saturation = r;
float value = 1; // 0.0 to 1.0
vec3 hsv = hsv2rgb(vec3(hue, saturation, value));
vec3 linear = srgb_to_linear(hsv);
FragColor = vec4(linear, 1.0);
}
"""
shader = gpu.types.GPUShader(VERT_SHADER, FRAG_SHADER)
W, H = 1, 1
batch = batch_for_shader(
shader,
"TRIS",
{
"position": [(0, 0, 0), (0, H, 0), (W, H, 0), (W, H, 0), (W, 0, 0), (0, 0, 0)],
"texCoord": ((0, 0), (0, 1), (1, 1), (1, 1), (1, 0), (0, 0)),
},
)
def draw():
batch.draw(shader)
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment