mattdesl / three-glslify

[OUTDATED] no longer needed as of glslify@2 and above
MIT License
8 stars 0 forks source link

three-glslify

experimental

DEPRECATED !

This bridge is no longer necessary in glslify@2.x and above. See three-glslify-example for a full example of using ThreeJS + glslify.


A helper to turn glslify shader into a ThreeJS shader object.

Typical example:

var THREE = require('three')

//inline our shader code
var glslify = require('glslify')
var source = glslify({
    vertex: './foo.vert',
    fragment: './foo.frag',
    sourceOnly: true
})

//create a shader that ThreeJS will recognize
var createShader = require('three-glslify')(THREE)
var myShader = createShader(source)

//we can now use it in ShaderMaterial or EffectComposer
var pass = new THREE.ShaderPass( myShader )
effectComposer.addPass(pass)

The returned myShader object looks like this, with new instances of Texture, Vector2, etc. based on the uniform type. Attributes start with empty arrays.

{
    vertexShader: '... source ...',
    fragmentShader: '... source ...',
    uniforms: {
        u_tex0: { type: 't', value: new THREE.Texture() },
        ... etc
    },
    attributes: {
        displacement: { type: 'f', value: [] }
    }
}

Usage

NPM

createShader(source, options)

Creates a shader with the given source (from glslify, using sourceOnly) and options.

Options:

Gotchas

Since ThreeJS release cycles are unpredictable and don't use semantic versioning, this module may break with newer versions of ThreeJS. It currently works with three 0.68.0 (r68). Send issues or PRs if you find versioning problems.

ThreeJS distinguishes between an array of THREE.Vector3 objects, and a flattened vec3 type represented by floats (v3v vs fv). This module only sees the vec3 array type, and assumes they are to be represented by an array of THREE.Vector3 elements. Same with ivec3, vec4, etc.

License

MIT, see LICENSE.md for details.