项目作者: luruke

项目描述 :
🔮 Tiny helper for three.js to debug and write shaders
高级语言: JavaScript
项目地址: git://github.com/luruke/magicshader.git
创建时间: 2019-04-04T10:46:11Z
项目社区:https://github.com/luruke/magicshader

开源协议:

下载


🔮 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

  1. npm i -D magicshader

and just use it instead of RawShaderMaterial:

  1. import MagicShader from 'magicshader';
  2. 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:

  1. const material = new MagicShader({
  2. vertexShader: `
  3. precision highp float;
  4. attribute vec3 position;
  5. uniform mat4 modelViewMatrix;
  6. uniform mat4 projectionMatrix;
  7. void main() {
  8. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  9. }
  10. `,
  11. fragmentShader: `
  12. precision highp float;
  13. uniform vec3 color; // ms({ value: '#ff0000' })
  14. void main() {
  15. gl_FragColor = vec4(color, 1.0);
  16. }
  17. `
  18. });

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?

  1. const material = new MagicShader({
  2. name: 'Cube Shader!',
  3. vertexShader: `
  4. precision highp float;
  5. attribute vec3 position;
  6. uniform mat4 modelViewMatrix;
  7. uniform mat4 projectionMatrix;
  8. uniform vec3 translate; // ms({ value: [0, 0, 0], step: 0.01 })
  9. uniform float scale; // ms({ value: 0.5, options: { small: 0.5, medium: 1, big: 2 } })
  10. uniform mat4 aMatrix4; // ms({ value: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] })
  11. void main() {
  12. vec3 pos = position + translate;
  13. pos *= scale;
  14. gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
  15. }
  16. `,
  17. fragmentShader: `
  18. precision highp float;
  19. uniform vec3 color; // ms({ value: '#ff0000' })
  20. uniform float brightness; // ms({ value: 0, range: [0, 0.5], step: 0.1 })
  21. uniform vec2 dummyValue; // ms({ value: [1024, 768], range: [[0, 2000], [0, 1500]] })
  22. uniform bool visible; // ms({ value: 1, name: 'Visibility' })
  23. uniform int test; // ms({ value: 0 })
  24. void main() {
  25. gl_FragColor = vec4(color + brightness, 1.0);
  26. }
  27. `
  28. });

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

  1. import MagicShader, { gui } from 'magicshader';
  2. 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)