npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js2glsl

v0.1.0

Published

An api which transpiles a subset of JS into GLSL

Downloads

8

Readme

JS2GLSL

JS2GLSL is a library that turns javascript into GLSL source. The goal is to allow developers to write shaders in javascript, and then transpile it into shader language.

Try it

http://jdavidberger.github.io/js2glsl-shadertoy/

Demos

Basic Example

var js2glsl = require('js2glsl'); 

function VertexPosition() {
            varyings.uv =  [attributes.position[0],
                            attributes.position[1]];
            return vec3(attributes.position[0],
                        attributes.position[1],
                        attributes.position[2]); 
};

function FragmentColor() {
            return [0.5*(varyings.uv[0]+1.0), 
                    0.5*(varyings.uv[1]+1.0) , 
                    0.5*(Math.cos(uniforms.t)+1.0), 1.0]; 
};

var shaderSrc = js2glsl({VertexPosition: VertexPosition, FragmentColor: FragmentColor});
var shaderSrc = shaderSpec.ShaderSource();
console.log(shaderSrc.vertex);
console.log(shaderSrc.fragment);

Object-Oriented Example

var js2glsl = require('js2glsl'); 

var shaderSpec = new js2glsl.ShaderSpecification();
shaderSpec.getUV = function() {
       return [this.attributes.position[0],
               this.attributes.position[1]];
};
shaderSpec.VertexShader = function () {
    this.varyings.uv =  this.getUV();
    return vec3(this.attributes.position[0],
                this.attributes.position[1],
                this.attributes.position[2]); 
};
shaderSpec.FragmentShader = function () {
    return [0.5*(this.varyings.uv[0]+1.0), 
            0.5*(this.varyings.uv[1]+1.0) , 
            0.5*(Math.cos(this.uniforms.t)+1.0), 1.0]; 
}

var shaderSrc = shaderSpec.ShaderSource();
console.log(shaderSrc.vertex);
console.log(shaderSrc.fragment);

How it works

The javascript functions are parsed into a tree representation by esprima. That representation is then examined for JS specific functions which are switched out for their GLSL counterparts.

Limitations

Since the langauges are very different, not all javascript functions can transpile. The main limitation is that JSON objects aren't supported beyond the special forms from uniform, varying, and attribute.

Limited custom JS functions are supported, although not closures and not much of the JS core library -- bind, apply, map, etc.

The main purpose behind the OO bindings is to allow a more modular design for shaders. A core use case is to have something like a base shader which plots over XY, but different subclasses have different color mappings; so with little to no code duplication, you can get multiple related shaders. Any 'normal' prototype based inheritance method should work. Demos in different transpiled languages are forthcoming.