Skip to content

Commit

Permalink
#55 Create shader loader system.
Browse files Browse the repository at this point in the history
  • Loading branch information
aggregate1166877 committed Sep 24, 2021
1 parent 3a327aa commit 80fdf14
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
19 changes: 19 additions & 0 deletions shaders/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
All game shaders are placed in this directory.

Only `.vert` and `.frag` files are loaded; all other extensions are ignored.

Shader language used: GLSL ES2.

Usage:
```js
import getShader from './shaders';
const shader = getShader('shaderName');
console.log(shader);
// { vertex: '...', fragment: '...' }
```

References:
* https://www.khronos.org/files/opengles_shading_language.pdf
* https://www.khronos.org/opengl/wiki/Shader
* https://www.khronos.org/opengl/wiki/Vertex_Shader
* https://www.khronos.org/opengl/wiki/Fragment_Shader
70 changes: 70 additions & 0 deletions shaders/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import fs from 'fs';

// All loaded shaders are stored here.
const shader = {};
// Amount of files that need to be loaded / skipped.
let shadersToLoad = null;
// If true, all shaders have been loaded.
let discoveryComplete = false;
// Used as keys in `shader`.
const term = {
vert: 'vertexShader',
frag: 'fragmentShader',
}

// Counts the amount of shaders loaded. Calls 'onComplete' if loaded shader
// count equals initial total count.
function checkAllComplete (onComplete) {
if (!--shadersToLoad) {
discoveryComplete = true;
onComplete();
}
}

function discoverShaders(onComplete=()=>{}) {
if (discoveryComplete) {
return onComplete();
}
fs.readdir('./shaders', (error, files) => {
if (error) {
discoveryComplete = true;
console.error('Could not read shader directory.');
return onComplete();
}

shadersToLoad = files.length;
for (let i = 0, len = files.length; i < len; i++) {
const file = files[i];
if (!file.includes('.vert') && !file.includes('.frag')) {
// We currently support .vert and .frag. This also weeds out .js.
checkAllComplete(onComplete);
continue;
}

fs.readFile('./shaders/' + file, (error, data) => {
if (error) {
console.error(`Could not read shader '${file}':`, error);
return checkAllComplete(onComplete);
}

const baseName = file.replace(/\.[^/.]+$/, '');
const shaderType = term[file.split('.').pop()];

if (!shader[baseName]) {
shader[baseName] = {};
}
shader[baseName][shaderType] = data.toString();
checkAllComplete(onComplete);
});
}
});
}

function getShader(name) {
return shader[name];
}

export {
discoverShaders,
getShader,
}

0 comments on commit 80fdf14

Please sign in to comment.