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

tweakable-shader

v0.0.7

Published

`TweakableShader` is a package that simplifies the process of creating and tweaking shader materials in Babylon.js. It allows you to easily adjust shader uniforms through a GUI, making it ideal for both development and debugging. The unique feature of thi

Downloads

17

Readme

Tweakable Shader for Babylon.js

Introduction

TweakableShader is a package that simplifies the process of creating and tweaking shader materials in Babylon.js. It allows you to easily adjust shader uniforms through a GUI, making it ideal for both development and debugging. The unique feature of this package is that it reads special comments in your GLSL code to automatically generate the GUI using Tweakpane.

Inspired by this three js package

Description of GIF

Supported Data Types

The package currently supports the following data types for uniforms:

  • Color3
  • Vector3
  • Vector2
  • float
  • int
  • time

Table of Contents

Installation

npm install tweakable-shader

Usage

Here's a simple example to get you started:

import { TweakableShader } from "tweakable-shader";

const shaderMaterial = new TweakableShader("shader-material", scene, {
  vertexShader: `...`,
  fragmentShader: `...`,
  needAlphaBlending: true,
});

The TweakableShader class is used to create a shader material with GUI. It accepts the following parameters:

  • name: It is first parameter, which is passed to shader material as name and also used as folder name in Tweakpane. So its needs to be unique for each instance (required).
  • scene: Babylon js scene in which shader material is being used
  • options: You can pass all the shader material options here the way you'll pass it to babylon's shader material, except vertex shader and fragment shader code should be passed using vertexShader and fragmentShader as shown above.
    • vertexShader: glsl code of vertex shader as string
    • fragmentShader: glsl code of fragment shader as string
    • timeUniform: create time uniform if specified. Expected value {name : <Name Of your time uniform>}
    • And all the other options that babylon js shader material takes

GUI Configuration

To configure the GUI for your uniforms, add special comments in your GLSL code like so:

uniform vec3 color; // ts({ value: {r: 0, g: 255, b: 214, a: 0.5} })
uniform float brightness; // ts({ value: 1.0, min: 0, max:1.0, step: 0.1 })

The package will automatically parse these comments and generate the appropriate GUI using Tweakpane

Examples

Live demo of below example

import * as BABYLON from "@babylonjs/core";
import { TweakableShader } from "tweakable-shader";

// setup babylon js basic scene
const initCamera = (scene) => {
  const camera = new BABYLON.ArcRotateCamera(
    "camera",
    0,
    0,
    10,
    new BABYLON.Vector3(0, 0, 0),
    scene
  );

  camera.attachControl(true);
  camera.setPosition(new BABYLON.Vector3(0, 0, 10));
  return camera;
};

const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas);

const createScene = function (engine) {
  const scene = new BABYLON.Scene(engine);
  scene.clearColor = new BABYLON.Color3(0, 0, 0);
  //CAMERA
  initCamera(scene);

  return scene;
};

const scene = createScene(engine);

engine.runRenderLoop(function () {
  scene.render();
});

window.addEventListener("resize", function () {
  engine.resize();
});

const box = BABYLON.MeshBuilder.CreateBox("box", { size: 2 });

// Create Shader material with GUI using Tweable-shader
const shaderMaterial = new TweakableShader("shader-material", scene, {
  vertexShader: `precision highp float;
                attribute vec3 position;
                uniform mat4 worldViewProjection;

                void main() {
                    vec4 p = vec4(position, 1.);
                    gl_Position = worldViewProjection * p;
                }`,
  fragmentShader: `precision highp float;
                uniform vec3 color; // ts({ value: {r: 0, g: 255, b: 214, a: 0.5} })
                uniform float brightness; // ts({ value: 1.0, min: 0, max:1.0, step: 0.1 })
                uniform float uTime;

                void main() {
                    gl_FragColor = vec4(color , brightness * sin(uTime * 0.05));
                }`,
  needAlphaBlending: true,
  timeUniform: { name: "uTime" }, // creates uTime uniform that updates in render loop
});

box.material = shaderMaterial;

Roadmap

  • More Examples
  • Add support for more data type

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.