p5
Version:
[](https://www.npmjs.com/package/p5)
69 lines (68 loc) • 2.04 kB
JavaScript
import { TypeInfoFromGLSLName } from "../strands/ir_types.js";
//#region src/webgl/shaderHookUtils.js
function getShaderHookTypes(shader, hookName) {
let fullSrc = shader._vertSrc;
let body = shader.hooks.vertex[hookName];
if (!body) {
body = shader.hooks.fragment[hookName];
fullSrc = shader._fragSrc;
}
if (!body) throw new Error(`Can't find hook ${hookName}!`);
const nameParts = hookName.split(/\s+/g);
const functionName = nameParts.pop();
const returnType = nameParts.pop();
const returnQualifiers = [...nameParts];
const parameterMatch = /\(([^)]*)\)/.exec(body);
if (!parameterMatch) throw new Error(`Couldn't find function parameters in hook body:\n${body}`);
const structProperties = (structName) => {
const structDefMatch = new RegExp(`struct\\s+${structName}\\s*{([^}]*)}`).exec(fullSrc);
if (!structDefMatch) return void 0;
const properties = [];
for (const defSrc of structDefMatch[1].split(";")) {
const parts = defSrc.trim().split(/\s+|,/g);
const typeName = parts.shift();
const names = [...parts];
const typeProperties = structProperties(typeName);
for (const name of names) {
const dataType = TypeInfoFromGLSLName[typeName] || null;
properties.push({
name,
type: {
typeName,
qualifiers: [],
properties: typeProperties,
dataType
}
});
}
}
return properties;
};
const parameters = parameterMatch[1].split(",").map((paramString) => {
const parts = paramString.trim().split(/\s+/g);
const name = parts.pop();
const typeName = parts.pop();
return {
name,
type: {
typeName,
qualifiers: [...parts],
properties: structProperties(typeName),
dataType: TypeInfoFromGLSLName[typeName] || null
}
};
});
const dataType = TypeInfoFromGLSLName[returnType] || null;
return {
name: functionName,
returnType: {
typeName: returnType,
qualifiers: returnQualifiers,
properties: structProperties(returnType),
dataType
},
parameters
};
}
//#endregion
export { getShaderHookTypes };