I am having trouble to extract the function in a glsl script using javascript.
Here is the glsl content :
precision highp float;
varying vec4 v_color;
#if USE_TEXTURE
varying vec2 v_uv0;
uniform sampler2D texture;
#endif
vec2 sfx_func_uv_to_01(vec4 ucrect, vec2 uv) {
return vec2(
sfx_func_uv_to_01(ucrect[0], ucrect[0] + ucrect[2], uv.x),
1.0 - sfx_func_uv_to_01(ucrect[1], ucrect[1] - ucrect[3], uv.y)
);
}
vec2 sfx_func_uv_to_01(vec4 ucrect, vec2 uv) {
return vec2(1.0, 2.0);
}
vec4 sfx_fs_output_uv_quarter(vec2 uv, vec4 color) {
vec4 result = color;
return result;
}
void main () {
vec2 uv = v_uv0;
vec4 color = texture2D(texture, uv);
gl_FragColor = color;
}
And I want to extract the 4 functions in the script with the body. So I tried the reg:
/^(vec4|vec3|vec2|float|int).*\{.*\}/gms
And the js code is :
for (const match of glsl1.matchAll(/^(vec4|vec3|vec2|float|int).*\{.*\}/gms)) {
console.log(match[0]);
}
Instead of 4 functions, I only get one result :
vec2 sfx_func_uv_to_01(vec4 ucrect, vec2 uv) {
return vec2(
sfx_func_uv_to_01(ucrect[0], ucrect[0] + ucrect[2], uv.x),
1.0 - sfx_func_uv_to_01(ucrect[1], ucrect[1] - ucrect[3], uv.y)
);
}
vec2 sfx_func_uv_to_01(vec4 ucrect, vec2 uv) {
return vec2(1.0, 2.0);
}
vec4 sfx_fs_output_uv_quarter(vec2 uv, vec4 color) {
vec4 result = color;
return result;
}
void main () {
vec2 uv = v_uv0;
vec4 color = texture2D(texture, uv);
gl_FragColor = color;
}
I searched for solution of non-greedy mode for multiline text but came in vain.
Any advice will be appreciated, thanks :)