Coder Social home page Coder Social logo

glsl-template-loader's Introduction

GLSL Webpack loader

This loader supports template variables and the #include directive.

#include will include the entire contents of another glsl file, this happens at compile time and uses webpacks resolver to find the file requested. The loader will then give you a function where you can supply template variables at runtime.

Install

npm install glsl-template-loader --save-dev

Configuring Webpack

resolve: {
  extensions: ['', '.webpack.js', '.js', '.vert', '.frag', '.glsl']
},
module: {
  loaders: [{
    test: /\.(glsl|vert|frag)$/,
    loader: 'shader'
  }]
},
// Default values (can be ommited)
glsl: {
  varPrefix: '$' // Every valid name that starts with this symbol will be treated as a template variable
}

Write some shaders

shaders/shader.vert

attribute vec2 a_Position;
attribute vec3 a_Color;

varying vec3 v_Color;

// The content of shaders/util/reduce-red.glsl file will be inlined here
#include ./util/reduce-red;

void main(void) {
  v_Color = reduceR(a_Color);
  gl_Position = vec4(a_Position, 0.0, 1.0);
}

shaders/shader.frag

precision highp float;

varying vec3 v_Color;

#include ./util/reduce-red;

void main() {
  gl_FragColor = vec4(reduceR(v_Color), 0.5);
}

shaders/util/reduce-red.glsl

vec3 reduceR(vec3 color) {
  // We arge going to use a template variable $reduce that would be inlined with it's value
  // Note that we use $reduce.0 to transform int values from template to float
  // Alternatively we can use cast float($reduce) or pass float string to template
  return vec3(color.r / $reduce.0, color.g, color.b);
}

Import your shader templates

es6

import createVertexShader from 'shader.vert';
import createFragmentShader from 'shader.frag';

es5

var createVertexShader = require('shader.vert');
var createFragmentShader = require('shader.frag');

Create shaders

// That's how we pass our reduce variable to templates
const vertexShader = createVertexShader({ reduce: 5 });
const fragmentShader = createFragmentShader({ reduce: 2 });

License

MIT

glsl-template-loader's People

Contributors

yofreke avatar yuri-karadzhov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.