Skip to content

Commit

Permalink
Find first function for preamble
Browse files Browse the repository at this point in the history
  • Loading branch information
tsherif committed Feb 19, 2020
1 parent ac7da55 commit 0714612
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions modules/shadertools/src/lib/assemble-shaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import injectShader, {DECLARATION_INJECT_MARKER} from './inject-shader';
import transpileShader from './transpile-shader';
import {assert} from '../utils';

const REGEX_START_OF_MAIN = /void main\s*\([^)]*\)\s*\{\n?/;
const REGEX_FIRST_FUNCTION = /[ \t]*\w+[ \t]+\w+\s*\([^)]*\)\s*\{\n?/;
const INJECT_SHADER_DECLARATIONS = `\n\n${DECLARATION_INJECT_MARKER}\n\n`;

const SHADER_TYPE = {
Expand Down Expand Up @@ -67,9 +67,14 @@ function assembleShader(
versionLine = `#version ${glslVersion}`;
}

const mainIndex = sourceLines.findIndex(l => l.match(REGEX_START_OF_MAIN));
const corePreamble = sourceLines.slice(0, mainIndex).join('\n');
const coreMain = sourceLines.slice(mainIndex).join('\n');
let functionIndex = sourceLines.findIndex(l => l.match(REGEX_FIRST_FUNCTION));

if (functionIndex === -1) {
functionIndex = 0;
}

const corePreamble = sourceLines.slice(0, functionIndex).join('\n');
const coreMain = sourceLines.slice(functionIndex).join('\n');

// Combine Module and Application Defines
const allDefines = {};
Expand Down
2 changes: 1 addition & 1 deletion modules/shadertools/src/lib/inject-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const MODULE_INJECTORS = {

export const DECLARATION_INJECT_MARKER = '__LUMA_INJECT_DECLARATIONS__'; // Uniform/attribute declarations

const REGEX_START_OF_MAIN = /void main\s*\([^)]*\)\s*\{\n?/; // Beginning of main
const REGEX_START_OF_MAIN = /void\s+main\s*\([^)]*\)\s*\{\n?/; // Beginning of main
const REGEX_END_OF_MAIN = /}\n?[^{}]*$/; // End of main, assumes main is last function
const fragments = [];

Expand Down

0 comments on commit 0714612

Please sign in to comment.