The onParse event is a callback provided to the Abstract constructor that runs after a layer's shader and uniforms have been parsed. It allows you to inject custom functionality that goes beyond the standard layer extension syntax, such as modifying shader code dynamically or updating the debugger (Leva) schema.
Common use cases include:
- Injecting specific shader chunks based on a non-uniform parameter.
- Adding custom controls to the debugger schema.
Note: Non-uniform parameters (parameters that change the shader structure rather than just being passed as uniforms) must be passed to the layer constructor via the args prop in React.
class CustomLayer extends Abstract {
// ... shader definitions ...
mapping: 'uv' | 'world' = 'uv';
constructor(props) {
super(
CustomLayer,
{
name: 'CustomLayer',
...props,
},
(self: CustomLayer) => {
// 1. Add to Leva (debugger) schema
self.schema.push({
value: self.mapping,
label: 'mapping',
options: ['uv', 'world'],
})
// 2. Inject shader chunk based on selected mapping
const mapping = CustomLayer.getMapping(self.mapping)
self.fragmentShader = self.fragmentShader.replace('lamina_mapping_template', mapping)
}
)
}
}
In React, use the args prop to pass these non-uniform values:
<LayerMaterial>
<customLayer
ref={ref}
color="green"
alpha={0.5}
args={[mapping]} // Non-uniform params must use `args`
/>
</LayerMaterial>