2D lighting
2024-09-30 00:02:18
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#define MAX_ITERATIONS 10
#define MIN_DISTANCE 0.01f
#define PI 3.14159265358979f

float rand(float co) { return fract(sin(co*(91.3458)) * 47453.5453); }


struct Material
{
vec3 emission;
float intensity;
};

struct NearestData
{
float dist;
Material material;
};

float union_sdf(float d1, float d2)
{
return min(d1, d2);
}

float subtract_sdf(float d1, float d2)
{
return max(d1, -d2);
}

float sunion_sdf(float d1, float d2, float k)
{

float h = clamp( 0.5 + 0.5*(d2-d1)/k, 0.0, 1.0 );
return mix( d2, d1, h ) - k*h*(1.0-h);
}

float circle_sdf(vec2 point, vec2 pos, float radius)
{
return length(point - pos) - radius;
}

float box_sdf(vec2 point, vec2 center, vec2 size) {
center = point - center;
vec2 r = abs(center) - size;
return min(max(r.x, r.y),0.) + length(max(r,vec2(0,0)));
}

float segment_sdf(vec2 pos, vec2 a, vec2 b)
{
vec2 v = pos - a;
vec2 u = b - a;
float t = max(min(dot(v, u) / dot(u, u), 1.0f), 0.0f);
vec2 d = v - u * t;
return length(d);
}


void test_sample(inout NearestData nearest, Material material, float sample_distance)
{
if (sample_distance < nearest.dist)
{
nearest.dist = sample_distance;
nearest.material = material;
}
}

void sample_sdf(vec2 point, out NearestData nearest)
{
// Material definitions
Material black_material;
Material white_material;
white_material.emission = vec3(1.0f);
white_material.intensity = 1.5f;

Material green_material;
green_material.emission = normalize(vec3(0.4, 0.6, 0.2));
green_material.intensity = 1.20f;

Material purple_material;
purple_material.emission = normalize(vec3(.6, 0.4, 0.8));
purple_material.intensity = 1.0f;


// SDF

// Triangle
vec2 a = vec2(1.2, -0.75);
vec2 b = vec2(1.3, -0.5);
vec2 c = vec2(1.4, -0.7);
test_sample(nearest, white_material, segment_sdf(point, a, b));
test_sample(nearest, white_material, segment_sdf(point, b, c));
test_sample(nearest, white_material, segment_sdf(point, c, a));

//test_sample(nearest, white_material, segment_sdf(point, vec2(0.75, -0.75), vec2(1.3, -0.5)));

test_sample(nearest, purple_material, box_sdf(point, vec2(1.4, 0.3), vec2(0.10f)));
test_sample(nearest, black_material, box_sdf(point, vec2(1.2, 0.3), vec2(0.050f)));


for (float i = 0.0f; i < 3.0f; i++)
{
test_sample(nearest, white_material, box_sdf(point, vec2(0, -i * 0.1f), vec2(0.0050f)));
}


test_sample(nearest, green_material, circle_sdf(point, vec2(-0.3, -0.3), 0.2f));

test_sample(nearest, black_material, circle_sdf(point, vec2(-1, 0.6), 0.6f));
test_sample(nearest, black_material, box_sdf(point, vec2(0.75, 0), vec2(0.5, 0.01)));

vec2 moon_pos = vec2(-0.8, -0.6);
float moon_radius = 0.3f;
float moon_distance = subtract_sdf(circle_sdf(point, moon_pos, moon_radius), circle_sdf(point, moon_pos + vec2(moon_radius * 0.5, 0), moon_radius));
test_sample(nearest, green_material, moon_distance);

// Subtraction box
vec2 box_pos = vec2(0.8, 0.3);
float box_size = 0.2;
float cut_size = 0.35f;
float main_cube_sd = box_sdf(point, box_pos, vec2(box_size));
float cut_horizontal_sd = box_sdf(point, box_pos - vec2(box_size * 0.5, 0), vec2(box_size * 2.0f, box_size * cut_size));
float cut_vertical_sd = box_sdf(point, box_pos - vec2(0.0f, box_size * 0.5), vec2(box_size * cut_size, box_size * 2.0f));
float cut_sphere_sd = circle_sdf(point, box_pos, 0.2f);

float k = .015f;
float cut_sd = sunion_sdf(cut_horizontal_sd, sunion_sdf(cut_vertical_sd, cut_sphere_sd, k), k);

float subtraction_box_sd = subtract_sdf(main_cube_sd, cut_sd);


test_sample(nearest, purple_material, subtraction_box_sd);

}

void ray_march(vec2 pos, vec2 direction, out vec3 color)
{

color = vec3(0);

for (int i = 0; i < MAX_ITERATIONS; i++)
{
NearestData nearest;
nearest.dist = 10e2;
sample_sdf(pos, nearest);

if (nearest.dist < MIN_DISTANCE)
{
color = nearest.material.emission * nearest.material.intensity;
return;
}
pos += direction * nearest.dist;
}


}


void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord / iResolution.xy;
vec2 pos = 2.0f * (uv - 0.5f); // range [-1, 1]
pos.x *= iResolution.x / iResolution.y; // fix aspect ratio


// Random sampling -------------------------------------------------------------------------------------
float pixel_size = 1.0f / iResolution.x; // Size of a single pixel
float pixels_offset = 2.0f; // Amount of pixels to randomly offset, this could be the blur
float random_value = rand(iTime + pos.x * pos.y);
float random_angle = 2.0f * PI * rand(random_value);
vec2 offset = random_value * pixels_offset * pixel_size * vec2(cos(random_angle), sin(random_angle));
pos += offset;

// Multisampled direction raymarch ---------------------------------------------------------------------
vec3 accumulated_color = vec3(0);
float samples = 4.0f;

for (float i = 0.0f; i < samples; i++)
{
float angle = 2.0f * PI * (i + rand(pos.x * pos.y + iTime + i)) / samples;
vec2 direction = vec2(cos(angle), sin(angle));

vec3 sample_color;
ray_march(pos, direction, sample_color);

accumulated_color += sample_color;
}

vec3 color = accumulated_color / samples;

fragColor = vec4(color, 1.0) + texture(iChannel0, uv);
}