Playing pipelines and shaders
This page is for developers who want to use files made in ISF Studio in their own web projects. IsfRenderer plays what you made in ISF Studio outside of it: a pipeline (.istpipe), a shader bundle (.istshader), or a plain .fs with its .vs.
Install
npm install @isf-studio/isf-renderer
Play
Playing it takes a canvas and three calls (here with Vue):
<script setup lang="ts">
import { IsfRenderer } from '@isf-studio/isf-renderer';
// Your pipeline (see Vite configuration below)
import pipeline from '@/assets/clouds.istpipe';
const canvas = ref<HTMLCanvasElement>();
let renderer: IsfRenderer;
onMounted(async () => {
renderer = new IsfRenderer();
renderer.bind(canvas.value!);
await renderer.load(pipeline);
});
onBeforeUnmount(() => renderer.dispose());
</script>
<template>
<canvas ref="canvas" />
</template>
In vanilla JavaScript, you would do something like this:
<canvas id="canvas"></canvas>
<script type="module">
import { IsfRenderer } from '@isf-studio/isf-renderer';
const canvas = document.getElementById('canvas');
const renderer = new IsfRenderer();
renderer.bind(canvas);
await renderer.load('/examples/clouds.istpipe');
// Then when you're done
renderer.dispose();
</script>
What load() takes
| You give | It plays |
|---|---|
The URL of a .istpipe | The shader the pipeline compiled to, with the images it carries. |
The URL or the bytes of a .istshader | The shader, its vertex shader and its images. |
The URL of a .fs | The shader; its images are looked for beside it. |
| The text of a fragment shader | The shader. It spans lines, or starts with /*, // or #. |
The bytes: Blob, ArrayBuffer, Uint8Array | Any of the above. |
A .fs takes its vertex shader and its images by option:
await renderer.load('/shaders/wave.fs', {
vertex: '/shaders/wave.vs',
images: { 'imported/noise.png': '/images/noise.png' },
});
An image that the shader imports is looked for in options.images (by its PATH or its name), then in the archive, then beside the .fs it was fetched from.
load() says it holds no compiled shader.Setting inputs
renderer.inputs.map((input) => input.NAME); // ['speed', 'bands', 'tint']
renderer.set('speed', 0.8);
renderer.set('tint', [1, 0.4, 0.2, 1]);
renderer.set('picture', imageBlob); // an image input takes a URL or a Blob
renderer.set('flash', true); // fires an event, once
Playing, pausing, cleaning up
renderer.pause();
renderer.play();
renderer.setSize(1280, 720); // the size it draws at; no argument follows the canvas
renderer.dispose(); // lets go of the canvas, WebGL and the image URLs
The alpha of the picture is left out, so a shader that never writes it (most from Shadertoy) still shows. new IsfRenderer({ opaque: false }), or renderer.setOpaque(false), keeps it: a transparent shader then lets what is behind the canvas through.
renderer.stats().fps; // what the last frames cost
renderer.info; // { description, credit, categories } from the header of the shader
A shader that does not compile rejects load() with the compiler's message. renderer.onError is told about a pass that fails while it draws.
With Vite
import pipeline from './clouds.istpipe' needs the plugin that makes Vite treat these files as assets:
import { isfAssets } from '@isf-studio/isf-renderer/vite';
export default defineConfig({ plugins: [isfAssets()] });
And, once, for TypeScript:
/// <reference types="@isf-studio/isf-renderer/assets" />
The player, as a Vue component
The players of this page are a component of Vue 3 that comes with the package: the canvas, the bar, the panel of the inputs and the sound, ready to use.
<script setup lang="ts">
import { IsfPlayer } from '@isf-studio/isf-renderer/vue';
import '@isf-studio/isf-renderer/vue.css';
</script>
<template>
<IsfPlayer src="/examples/clouds.istpipe" title="Clouds" />
</template>
Its props (vertex, images, ratio, autoplay, clickToPlay, controls, open, sounds) and its events are in the README of the package.