• Stars
    star
    239
  • Rank 168,241 (Top 4 %)
  • Language
    JavaScript
  • Created over 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔮 Tiny helper for three.js to debug and write shaders

🔮 MagicShader

⚠️ probably won't work with modern version of threejs, last time I tested was with r114
pr is welcome

A thin wrapper on top of RawShaderMaterial, that allows to easily create new uniforms and live-edit them via dat.gui.

No need to create the uniforms manually and bind them with dat.gui.
Just write some comments in your GLSL, and everything will work magically

🕵️‍♂️ How to use

Install via npm

npm i -D magicshader

and just use it instead of RawShaderMaterial:

import MagicShader from 'magicshader';

const material = new MagicShader({...})

The parameters are exactly the same.

🤷‍♀️ Ok...where the magic is?

Now you can add the // ms({}) magic comment after your uniforms.

Example:

const material = new MagicShader({
  vertexShader: `
    precision highp float;
    
    attribute vec3 position;
    uniform mat4 modelViewMatrix;
    uniform mat4 projectionMatrix;
    
    void main() {
      gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    }
  `,
  fragmentShader: `
    precision highp float;

    uniform vec3 color; // ms({ value: '#ff0000' })

    void main() {
      gl_FragColor = vec4(color, 1.0);
    }
  `
});

This will give you:
1

No need to init your uniform or bind dat.gui.
You can just work on your GLSL files.

👨‍💻 What else?

const material = new MagicShader({
  name: 'Cube Shader!',
  vertexShader: `
    precision highp float;
    
    attribute vec3 position;
    uniform mat4 modelViewMatrix;
    uniform mat4 projectionMatrix;

    uniform vec3 translate; // ms({ value: [0, 0, 0], step: 0.01 })
    uniform float scale; // ms({ value: 0.5, options: { small: 0.5, medium: 1, big: 2 } })
    uniform mat4 aMatrix4; // ms({ value: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] })

    void main() {
      vec3 pos = position + translate;
      pos *= scale;

      gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
    }
  `,
  fragmentShader: `
    precision highp float;
    
    uniform vec3 color; // ms({ value: '#ff0000' })
    uniform float brightness; // ms({ value: 0, range: [0, 0.5], step: 0.1 })
    uniform vec2 dummyValue; // ms({ value: [1024, 768], range: [[0, 2000], [0, 1500]] })
    uniform bool visible; // ms({ value: 1, name: 'Visibility' })
    uniform int test; // ms({ value: 0 })

    void main() {
      gl_FragColor = vec4(color + brightness, 1.0);
    }
  `
});

Will result in:
full

🕵️‍ SpectorJS

With the SpectorJS extension enabled, you can live-edit the shaders. You can even add and modify "magic" uniforms on the fly.

full

💅 Ok, cool. Just finished my app and I'm ready to deploy

Then you can hide the dat.gui UI

import MagicShader, { gui } from 'magicshader';
gui.destroy();

😴 TODO

  • Do more tests...
  • add support for sampler2D and FBO?
  • check if it works with firefox/safari shader editor
  • inspect/edit threejs default uniforms (like projectionMatrix)

More Repositories

1

browser-2020

Things you can do with a browser in 2020 ☕️
8,066
star
2

awesome-casestudy

📕 Curated list of technical case studies on WebGL and creative development
2,556
star
3

aladino

🧞‍♂️ Your magic WebGL carpet
JavaScript
843
star
4

human-gpu

🤖 Hello human, I'm sick to be your GPU!!
JavaScript
177
star
5

antipasto

🍽 Juicy starter for three.js
JavaScript
162
star
6

bidello

👨‍🏫 Don't leave your Class alone
JavaScript
56
star
7

clip-rect

✂️ - clip-rect is an helper to create painless `clip: rect()` animations with GSAP.
JavaScript
42
star
8

gl-backend

🚪 POC Detect WebGL rendering backend
JavaScript
33
star
9

adapttext.js

🙌 - AdaptText.js is a dependency free and simple javascript solution capable to fit your text inside the parent element
JavaScript
28
star
10

corrente

evening jelly experiment in WebGL
JavaScript
22
star
11

split-in-lines

Take an HTML element with text, and split it in lines.
JavaScript
18
star
12

tubbbo

〰A little WebGL experiment
JavaScript
17
star
13

appear-animation

Easily add animation when elements comes in the viewport.
JavaScript
11
star
14

dev-excuses-chrome

Chrome, please, gimme an excuse! - A little chrome extension that allow you to easily fetch an excuse from https://api.githunt.io/programmingexcuses and paste it in your current textarea/input field.
JavaScript
9
star
15

monolith

ddd-2018 devx experiment
HTML
6
star
16

three-emoji

emh...emoji!!
HTML
4
star
17

selfielapse

Simple desktop app that take a snapshot a day from your webcam.
JavaScript
4
star
18

totodile

A wild DOMElement appears!
JavaScript
3
star
19

infinite-runner-code-golf

A little exercise in 510byte.
HTML
3
star
20

luruke.com

Personal site
HTML
2
star
21

three.js

JavaScript 3D library.
JavaScript
2
star
22

fastnavigation

a proof of concept for a fast navigation PJAX+localstorage+intent
HTML
1
star
23

hasshhh

Generate number starting from a string seed
JavaScript
1
star
24

vooautoconnect

Keep the connection up using VOO_HOMESPOT wifi
Shell
1
star
25

foglio

HTML
1
star
26

test-babylon-export-gltf

HTML
1
star
27

intl-localematcher

JavaScript
1
star
28

arcoreval

JavaScript
1
star