encapsulate a glsl shader
represents a shader, vertex, fragment, geometry etc
type
The type of this shader, vertex, fragment, geometry
source
the shader source code
getError()
Get the error message (empty if none) for the shader. */
getHandle()
Get the handle of the shader. */
compile()
Compile the shader. A valid context must to current in order to compile the shader.
cleanup();
Source
index.jsimport macro from 'vtk.js/Sources/macros';
const { vtkErrorMacro } = macro;
function vtkShader(publicAPI, model) { model.classHierarchy.push('vtkShader');
publicAPI.compile = () => { let stype = model.context.VERTEX_SHADER;
if ( !model.source || !model.source.length || model.shaderType === 'Unknown' ) { return false; }
if (model.handle !== 0) { model.context.deleteShader(model.handle); model.handle = 0; }
switch (model.shaderType) { case 'Fragment': stype = model.context.FRAGMENT_SHADER; break; case 'Vertex': default: stype = model.context.VERTEX_SHADER; break; }
model.handle = model.context.createShader(stype); model.context.shaderSource(model.handle, model.source); model.context.compileShader(model.handle); const isCompiled = model.context.getShaderParameter( model.handle, model.context.COMPILE_STATUS ); if (!isCompiled) { const lastError = model.context.getShaderInfoLog(model.handle); vtkErrorMacro(`Error compiling shader '${model.source}': ${lastError}`); model.context.deleteShader(model.handle); model.handle = 0; return false; }
return true; };
publicAPI.cleanup = () => { if (model.shaderType === 'Unknown' || model.handle === 0) { return; }
model.context.deleteShader(model.handle); model.handle = 0; model.dirty = true; }; }
const DEFAULT_VALUES = { shaderType: 'Unknown', source: '', error: '', handle: 0, dirty: false, context: null, };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
macro.obj(publicAPI, model); macro.setGet(publicAPI, model, [ 'shaderType', 'source', 'error', 'handle', 'context', ]);
vtkShader(publicAPI, model); }
export const newInstance = macro.newInstance(extend, 'vtkShader');
export default { newInstance, extend };
|