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

glslog

v0.0.10

Published

A utility for converting WebGL 1.0 GLSL shaders to JavaScript/TypeScript and debugging WebGL rendering contexts.

Downloads

67

Readme

glslog

A utility for converting WebGL 1.0 GLSL shaders to JavaScript/TypeScript and debugging WebGL rendering contexts.

See the interactive demo.

Description

glslog is a powerful tool designed to aid developers in debugging and understanding WebGL shaders and rendering processes. It provides functionality to:

  • Convert GLSL shader code to JavaScript or TypeScript: This allows you to run and test shader logic directly in JavaScript, making it easier to debug and understand shader behavior.
  • Wrap WebGL contexts: Intercept and log WebGL calls, simulate shader execution, and visualize rendering output on a 2D canvas overlay.

By integrating glslog into your development workflow, you can gain deeper insights into your WebGL applications and streamline the debugging process.

Installation

Install glslog via npm:

npm install --save glslog

Usage

Testing locally

Run:

npm start

Then open: http://localhost:5173/pen/

Converting GLSL to JavaScript or TypeScript

glslog provides two functions, glsl2js and glsl2ts, which convert GLSL shader code into JavaScript and TypeScript code respectively. This is particularly useful for testing and debugging shader logic outside of the GPU.

Example

import { glsl2js, glsl2ts } from 'glslog';

const glslCode = `
void main() {
    gl_FragColor = vec4(1.0);
}
`;

// Convert GLSL to JavaScript
const jsCode = glsl2js(glslCode);
console.log(jsCode);

// Convert GLSL to TypeScript
const tsCode = glsl2ts(glslCode);
console.log(tsCode);

Wrapping a WebGL Context

The wrapContext function allows you to wrap a WebGLRenderingContext to intercept WebGL API calls. This helps in debugging by:

  • Logging WebGL function calls and parameters.
  • Simulating shader execution in JavaScript.
  • Visualizing rendering output on an overlay canvas.

Example

import 'https://unpkg.com/glslog';

const canvas = document.getElementById('myCanvas');
const gl = wrapContext(canvas.getContext('webgl'), console.log);

const vertexShader = gl.createShader(gl.VERTEX_SHADER);
// ... continue with your WebGL setup and rendering code

When using the wrapped context, glslog will create an overlay canvas on top of your original canvas to visualize the output, which is helpful for debugging rendering issues.

API Reference

On browsers, the API functions are exported as globals.

Functions

glsl2js(glslCode: string): string Converts GLSL shader code to JavaScript code.

  • Parameters:
    • glslCode: The GLSL shader code as a string.
  • Returns:
    • A string containing the converted JavaScript code.

glsl2ts(glslCode: string): string Converts GLSL shader code to TypeScript code.

  • Parameters:
    • glslCode: The GLSL shader code as a string.
  • Returns:
    • A string containing the converted TypeScript code.

wrapContext(gl: WebGLRenderingContext, print: (...args: any[]) => void): WebGLRenderingContext Wraps a WebGLRenderingContext to intercept and debug WebGL API calls.

  • Parameters:
    • gl: The original WebGLRenderingContext to be wrapped.
    • print: A function used for logging (e.g., console.log).
  • Returns:
    • A wrapped WebGLRenderingContext that can be used in place of the original.

License

This project is licensed under the MIT License - see the LICENSE file for details.

The distribution package also includes the TypeScript compiler, which is under its own free license.