processShader function

void processShader(
  1. String shaderBody,
  2. List<String> finalInputs,
  3. List<String> samplers,
  4. List<String> processFunctions,
  5. List<String> shaderConstants,
)

Implementation

void processShader(
  String shaderBody,
  List<String> finalInputs,
  List<String> samplers,
  List<String> processFunctions,
  List<String> shaderConstants,
) {
  List<String> shaderLines = shaderBody
      .split(RegExp('\n+', multiLine: true))
      .whereNot((e) => e.trim().isEmpty)
      .whereNot((e) => e.startsWith('#'))
      .whereNot((e) => e.startsWith('precision'))
      .toList();
  bool processFound = false;
  bool mainFound = false;
  for (final element in shaderLines) {
    if (element.endsWith('fragColor;')) {
      continue;
    }
    if (element.startsWith('layout') || element.startsWith('uniform')) {
      if (element.endsWith('screenSize;') ||
          element.endsWith('inputImageTexture;')) {
        continue;
      }
      if (element.contains('sampler2D')) {
        samplers.add(element);
      } else {
        final start = element.lastIndexOf('uniform') + 8;
        finalInputs.add(
          'uniform ${element.substring(
            start,
          )}',
        );
      }
      continue;
    }

    if (element.startsWith('vec4 processColor')) {
      final index = processFunctions
          .where((element) => element.startsWith('vec4 processColor'))
          .length;
      final arguments = element
          .replaceAll('vec4 processColor(', '')
          .replaceAll(')', '')
          .replaceAll('{', '')
          .trim();
      processFunctions.add('vec4 processColor$index($arguments){');
      processFound = true;
      continue;
    }
    if (element.startsWith('}')) {
      if (processFound) {
        processFunctions.add(element);
        processFound = false;
        continue;
      } else if (mainFound) {
        mainFound = false;
        continue;
      }
    }
    if (processFound) {
      processFunctions.add(element);
      continue;
    }

    if (element.startsWith('void main')) {
      mainFound = true;
    }

    if (mainFound) {
      continue;
    }

    if (element
        .contains('luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);')) {
      if (shaderConstants.contains(element)) {
        continue;
      }
    }

    shaderConstants.add(element);
  }
}