Pixel-Composer/shaders/sh_cell_noise/sh_cell_noise.fsh

85 lines
2.2 KiB
Text
Raw Normal View History

2022-01-13 05:24:03 +01:00
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
2023-12-23 09:39:55 +01:00
uniform vec2 dimension;
uniform vec2 position;
2024-03-31 05:36:11 +02:00
uniform float rotation;
2023-12-23 09:39:55 +01:00
uniform int pattern;
2024-03-23 07:26:11 +01:00
uniform float seed;
2022-01-13 05:24:03 +01:00
2023-12-23 09:39:55 +01:00
uniform vec2 scale;
uniform int scaleUseSurf;
uniform sampler2D scaleSurf;
uniform float contrast;
uniform float middle;
uniform float radiusScale;
uniform float radiusShatter;
#define PI 3.14159265359
2023-02-23 07:02:19 +01:00
#define TAU 6.283185307179586
2023-07-21 12:40:20 +02:00
vec2 random2( vec2 p ) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453); }
2022-01-13 05:24:03 +01:00
2023-07-21 12:40:20 +02:00
float random (in vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453123); }
2022-01-13 05:24:03 +01:00
void main() {
2023-12-23 09:39:55 +01:00
#region params
float sca = scale.x;
float scaMax = max(scale.x, scale.y);
if(scaleUseSurf == 1) {
vec4 _vMap = texture2D( scaleSurf, v_vTexcoord );
sca = mix(scale.x, scale.y, (_vMap.r + _vMap.g + _vMap.b) / 3.);
}
2024-03-31 05:36:11 +02:00
float ang = rotation;
#endregion
vec2 pos = position / dimension;
vec2 st = (v_vTexcoord - pos) * mat2(cos(ang), -sin(ang), sin(ang), cos(ang)) * sca;
2022-01-13 05:24:03 +01:00
vec3 color = vec3(.0);
2023-12-25 12:51:12 +01:00
float m_dist = 1.;
2024-03-31 05:36:11 +02:00
if(pattern < 2) {
vec2 i_st = floor(st);
vec2 f_st = fract(st);
2024-03-31 05:36:11 +02:00
for (int y = -1; y <= 1; y++)
for (int x = -1; x <= 1; x++) {
vec2 neighbor = vec2(float(x),float(y));
vec2 point = random2(pattern == 0? mod(i_st + neighbor, scaMax) : i_st + neighbor);
2022-01-13 05:24:03 +01:00
2024-03-31 05:36:11 +02:00
point = 0.5 + 0.5 * sin(seed + 6.2831 * point);
vec2 _diff = neighbor + point - f_st;
float dist = length(_diff);
m_dist = min(m_dist, dist);
}
2024-03-31 05:36:11 +02:00
} else if(pattern == 2) {
2023-12-23 09:39:55 +01:00
for (int j = 0; j <= int(sca / 2.); j++) {
2024-03-31 05:36:11 +02:00
2023-12-23 09:39:55 +01:00
int _amo = int(sca) + int(float(j) * radiusShatter);
for (int i = 0; i <= _amo; i++) {
2024-03-31 05:36:11 +02:00
float angl = ang + TAU / float(_amo) * float(i) + float(j) + random(vec2(0.684, 1.387)) + seed;
float rad = pow(float(j) / sca, radiusScale) * sca * .5 + random(vec2(angl)) * 0.1;
vec2 point = vec2(cos(angl) * rad, sin(angl) * rad) + pos;
vec2 _diff = point - v_vTexcoord;
float dist = length(_diff);
m_dist = min(m_dist, dist);
}
}
}
2022-01-13 05:24:03 +01:00
color += m_dist;
vec3 c = middle + (color - middle) * contrast;
gl_FragColor = vec4(c, 1.0);
2022-01-13 05:24:03 +01:00
}