Skip to content

Commit 67c84d5

Browse files
committed
Added a shadertoy example
1 parent 1830e20 commit 67c84d5

5 files changed

Lines changed: 410 additions & 0 deletions

File tree

examples/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_subdirectory(vsganaglyphicstereo)
1515
add_subdirectory(vsgwindows)
1616
add_subdirectory(vsgcameras)
1717
add_subdirectory(vsgvalidate)
18+
add_subdirectory(vsgshadertoy)
1819

1920
if (vsgXchange_FOUND)
2021
add_subdirectory(vsghelloworld)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set(SOURCES
2+
vsgshadertoy.cpp
3+
)
4+
5+
add_executable(vsgshadertoy ${SOURCES})
6+
7+
target_link_libraries(vsgshadertoy vsg::vsg)
8+
9+
install(TARGETS vsgshadertoy RUNTIME DESTINATION bin)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Draw a glowing spot around the mouse position
2+
3+
void mainImage( out vec4 fragColor, in vec2 fragCoord )
4+
{
5+
// Normalized coordinates (from 0 to 1)
6+
vec2 uv = fragCoord / iResolution.xy;
7+
8+
// Correct uv using the aspect ratio to ensure the circle's aspect ratio is maintained
9+
float aspectRatio = 1.0*iResolution.x / iResolution.y;
10+
vec2 correctedUV = vec2(uv.x * aspectRatio, uv.y);
11+
12+
// Background gradient: black at the bottom, dark blue at the top
13+
vec3 bgColor = mix(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.4), uv.y);
14+
15+
// Mouse position in normalized coordinates, corrected for aspect ratio
16+
vec2 mousePos = iMouse.xy / iResolution.xy;
17+
vec2 correctedMousePos = vec2(mousePos.x * aspectRatio, mousePos.y);
18+
19+
// Calculate distance from current fragment to mouse position, corrected for aspect ratio
20+
float distanceToMouse = distance(correctedUV, correctedMousePos);
21+
22+
// Glow effect based on distance to mouse position
23+
float glowIntensity = 0.1 / sqrt(distanceToMouse);
24+
vec3 glowColor = vec3(1.0, 0.0, 0.0) * glowIntensity;
25+
26+
// Combine background and glow color
27+
vec3 color = bgColor + glowColor;
28+
29+
// Output the color
30+
fragColor = vec4(color, 1.0);
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
void mainImage( out vec4 fragColor, in vec2 fragCoord )
2+
{
3+
// Correct aspect ratio
4+
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
5+
6+
// Background gradient
7+
vec3 col = mix(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.4), uv.y * 0.5 + 0.5);
8+
9+
// Light source direction
10+
vec3 lightDir = normalize(vec3(-10.0, -10.0, 20.0));
11+
12+
// Sphere properties
13+
float sphereRadius = 0.20;
14+
vec3 sphereColors[3] = vec3[](vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
15+
16+
// Time factor for rotation
17+
float time = iTime * 0.5;
18+
19+
// Draw each sphere
20+
for(int i = 0; i < 3; i++) {
21+
float angle = time + float(i) * 2.0944; // 120 degrees apart
22+
vec2 position = vec2(sin(angle), cos(angle)) * sphereRadius * 2;
23+
24+
// Sphere shading
25+
float dist = length(uv - position);
26+
if(dist < sphereRadius) {
27+
float diff = dot(lightDir, normalize(vec3(position - uv, sqrt(sphereRadius * sphereRadius - dist * dist))));
28+
diff = clamp(diff, 0.0, 1.0);
29+
30+
// Combine sphere color with light intensity and background
31+
col = mix(col, sphereColors[i] * diff, smoothstep(sphereRadius, sphereRadius - 0.03, dist));
32+
}
33+
}
34+
35+
// Output to screen
36+
fragColor = vec4(col,1.0);
37+
}

0 commit comments

Comments
 (0)