From 60a7b65cdf8fd9824d99ec78cbdfbcf603f5deff Mon Sep 17 00:00:00 2001 From: Christoph Diehl <1614333+posidron@users.noreply.github.com> Date: Thu, 10 May 2018 08:56:29 +0200 Subject: [PATCH] Fix regex for parsing uniforms, attributes, varyings --- lib/make/webgl.js | 48 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/make/webgl.js b/lib/make/webgl.js index 9154731..5941cb9 100644 --- a/lib/make/webgl.js +++ b/lib/make/webgl.js @@ -57,26 +57,44 @@ class webgl extends make { return random.item(sources) } - static parseUniforms (shader) { - let names = [] - let result = shader.match(/uniform \w+ \w+(?=;)/gm) - if (result) { - result.forEach((v) => names.push(v.split(' ').pop())) + match (shader, regex, group = 1) { + let matches = [] + let match + while (match = regex.exec(shader)) { + matches.push(match[group]) } - return names + return matches } - static parseAttributes (shader) { - let names = [] - let result = shader.match(/attribute \w+ \w+(?=;)/gm) - if (result) { - result.forEach((v) => names.push(v.split(' ').pop())) - } - return names + static parseUniforms (shader, group = 1) { + /* Todo: Parse their individual data types into categories. */ + return match(shader, /uniform .+? (\w+)(?=[\[;])/gm, group) } - static parseFrag (shader) { - return shader.match(/(gl_Frag[^[ =]+)/gm) + static parseAttributes (shader, group = 1) { + return match(shader, /attribute .+? (\w+)(?=;)/gm, group) + } + + static parseVaryings (shader, group = 1) { + return match(shader, /varying .+? (\w+)(?=;)/gm, group) + } + + static parseFragDatav2 (shader, group = 1) { + // #version 200 + return match(shader, /(gl_Frag[^[ =]+)/gm, group) + } + + static parseFragDatav3 (shader, group = 1) { + // #version 300 + return match(shader, /out .+? (\w+)(?=[\[;])/gm, group) + } + + static parseFrag (shader, group = 1) { + let matches = parseFragDatav2(shader) + if (matches.length) { + return matches + } + return parseFragDatav3(shader) } static randomBitmask () {