* Integrates the given effect. * * @private * @param {String} prefix - A prefix. * @param {Effect} effect - The effect. * @param {EffectShaderData} data - Cumulative shader data.
(prefix, effect, data)
| 49 | */ |
| 50 | |
| 51 | function integrateEffect(prefix, effect, data) { |
| 52 | |
| 53 | let fragmentShader = effect.getFragmentShader(); |
| 54 | let vertexShader = effect.getVertexShader(); |
| 55 | |
| 56 | const mainImageExists = (fragmentShader !== undefined && /mainImage/.test(fragmentShader)); |
| 57 | const mainUvExists = (fragmentShader !== undefined && /mainUv/.test(fragmentShader)); |
| 58 | |
| 59 | data.attributes |= effect.getAttributes(); |
| 60 | |
| 61 | if(fragmentShader === undefined) { |
| 62 | |
| 63 | throw new Error(`Missing fragment shader (${effect.name})`); |
| 64 | |
| 65 | } else if(mainUvExists && (data.attributes & EffectAttribute.CONVOLUTION) !== 0) { |
| 66 | |
| 67 | throw new Error(`Effects that transform UVs are incompatible with convolution effects (${effect.name})`); |
| 68 | |
| 69 | } else if(!mainImageExists && !mainUvExists) { |
| 70 | |
| 71 | throw new Error(`Could not find mainImage or mainUv function (${effect.name})`); |
| 72 | |
| 73 | } else { |
| 74 | |
| 75 | const functionRegExp = /\w+\s+(\w+)\([\w\s,]*\)\s*{/g; |
| 76 | |
| 77 | const shaderParts = data.shaderParts; |
| 78 | let fragmentHead = shaderParts.get(Section.FRAGMENT_HEAD) || ""; |
| 79 | let fragmentMainUv = shaderParts.get(Section.FRAGMENT_MAIN_UV) || ""; |
| 80 | let fragmentMainImage = shaderParts.get(Section.FRAGMENT_MAIN_IMAGE) || ""; |
| 81 | let vertexHead = shaderParts.get(Section.VERTEX_HEAD) || ""; |
| 82 | let vertexMainSupport = shaderParts.get(Section.VERTEX_MAIN_SUPPORT) || ""; |
| 83 | |
| 84 | const varyings = new Set(); |
| 85 | const names = new Set(); |
| 86 | |
| 87 | if(mainUvExists) { |
| 88 | |
| 89 | fragmentMainUv += `\t${prefix}MainUv(UV);\n`; |
| 90 | data.uvTransformation = true; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | if(vertexShader !== null && /mainSupport/.test(vertexShader)) { |
| 95 | |
| 96 | // Build the mainSupport call (with optional uv parameter). |
| 97 | const needsUv = /mainSupport *\([\w\s]*?uv\s*?\)/.test(vertexShader); |
| 98 | vertexMainSupport += `\t${prefix}MainSupport(`; |
| 99 | vertexMainSupport += needsUv ? "vUv);\n" : ");\n"; |
| 100 | |
| 101 | // Collect names of varyings and functions. |
| 102 | for(const m of vertexShader.matchAll(/(?:varying\s+\w+\s+([\S\s]*?);)/g)) { |
| 103 | |
| 104 | // Handle unusual formatting and commas. |
| 105 | for(const n of m[1].split(/\s*,\s*/)) { |
| 106 | |
| 107 | data.varyings.add(n); |
| 108 | varyings.add(n); |
no test coverage detected
searching dependent graphs…