-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshaderart.nim
103 lines (91 loc) · 3.34 KB
/
shaderart.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ****************************************************************************************
#
# raylib example - An introduction to Shader Art Coding (https://youtu.be/f4s1h2YETNY)
#
# Example originally created with naylib 5.2
#
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
# BSD-like license that allows static linking with closed source software
#
# Copyright (c) 2024 Antonis Geralis (@planetis-m)
#
# ****************************************************************************************
import raylib
const
screenWidth = 600
screenHeight = 600
const
shaderCode = """
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Output fragment color
out vec4 finalColor;
// Custom variables
uniform float seconds;
uniform vec2 size;
// NOTE: Render size values should be passed from code
const float renderWidth = 600;
const float renderHeight = 600;
vec3 palette(float t) {
vec3 a = vec3(0.5, 0.5, 0.5);
vec3 b = vec3(0.5, 0.5, 0.5);
vec3 c = vec3(1.0, 1.0, 1.0);
vec3 d = vec3(0.263, 0.416, 0.557);
return a + b*cos(6.28318*(c*t + d));
}
void main() {
vec2 uv = fragTexCoord*2.0 - 1.0;
uv.x *= renderWidth/renderHeight;
vec2 uv0 = uv;
vec3 tmpColor = vec3(0.0);
for (float i = 0.0; i < 4.0; i++) {
uv = fract(uv*1.5) - 0.5;
float d = length(uv)*exp(-length(uv0));
vec3 col = palette(length(uv0) + i*0.4 + seconds*0.4);
d = sin(d*8.0 + seconds)/8.0;
d = abs(d);
d = pow(0.01/d, 1.2);
tmpColor += col*d;
}
finalColor = vec4(tmpColor, 1.0);
}
"""
proc main =
# Initialization
# --------------------------------------------------------------------------------------
initWindow(screenWidth, screenHeight, "raylib [shaders] example - Shader Art Coding")
defer: closeWindow() # Close window and OpenGL context
# Create a RenderTexture2D to be used for render to texture
let target = loadRenderTexture(getScreenWidth(), getScreenHeight())
# Load shader and setup location points and values
let shader = loadShaderFromMemory("", shaderCode)
let secondsLoc = getShaderLocation(shader, "seconds")
var seconds: float32 = 0
setTargetFPS(60) # Set our game to run at 60 frames-per-second
# Main game loop
# --------------------------------------------------------------------------------------
while not windowShouldClose(): # Detect window close button or ESC key
# Update
# ------------------------------------------------------------------------------------
seconds += getFrameTime()
setShaderValue(shader, secondsLoc, seconds)
# ------------------------------------------------------------------------------------
# Draw
# ------------------------------------------------------------------------------------
# Using a render texture to draw
beginTextureMode(target) # Enable drawing to texture
clearBackground(Black) # Clear the render texture
# Draw a rectangle in shader mode to be used as shader canvas
drawRectangle(0, 0, getScreenWidth(), getScreenHeight(), Black)
endTextureMode()
beginDrawing()
clearBackground(Black)
# Begin shader mode
beginShaderMode(shader)
drawTexture(target.texture, Vector2(x: 0, y: 0), White)
endShaderMode()
endDrawing()
# ------------------------------------------------------------------------------------
main()