GithubHelp home page GithubHelp logo

drew-eastmond / shader Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 27 KB

Initialize Shaders out-of-order using typescript.

Home Page: http://www.onyxignition.com/

License: GNU General Public License v3.0

TypeScript 100.00%
javascript shader shaders typescript webgl webgl2 webgpu

shader's Introduction

Shaders-TS

Initialize Shaders out-of-order using typescript.

Use the .connect( ... ) method build help the engine build a dependency mesh that will

const gl: GraphicLibraryContext = canvas.getContext("webgl2", {
  preserveDrawingBuffer: false
});

// fetch the shaders. We can use link or direct source. I will parse and resolve the parameters automatically.
const fragmentShader: FragmentShader = new FragmentShader(`./frag-shader.frag`);
const vertexShader: VertexShader = new VertexShader(`./vert-shader.vert`);

// get me sum of that good ole shaderProgram. Make different `ShaderProgram` based on your needs.
const shaderProgram: ShaderProgram = new ShaderProgram(gl);
shaderProgram.connect(fragmentShader);
shaderProgram.connect(vertexShader);

const positions: number[] = [1, 1, -1, 1, 1, -1,  -1, -1, -1, 1, 1, -1];
const shaderBuffer: ShaderBuffer = new ShaderBuffer
  new ShaderVertexArrayBuffer(
    new Float32Array(positions)), new ShaderAttribute("a_position", new ShaderAttributesProperties(2, gl.FLOAT)
  )
);

// doesn't matter what order we do things because .connect will reorder the state machine
var shaderUniform: ShaderUniform = new ShaderUniform("u_time");
shaderProgram.connect(shaderUniform);

// connect each component sequentially or all in the constructor. Make it your way baby!!!!
const shaderTexture: ShaderTexture = new ShaderTexture("u_texture");
shaderTexture
  .connect(new ShaderTextureProperties({ internal_format: gl.RGBA, format: gl.RGBA, type: gl.UNSIGNED_BYTE }));
shaderTexture.connect(new ShaderTexture2D().connect("./img/leaves.jpg")).connect($texture);
shaderProgram.connect(shaderTexture);

// if we have everything present then compile.
await shaderProgram.$compile();

Rendering phase...

After the program is compiled just call shaderProgram.use(), then update any uniforms and just like that... Your Bussin'

// rendering p

gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);

// let do magic all dependencies are sorted and state machine are set up automagically. (YES MAGIC!!!)
shaderProgram.use();

// this example uses drawArray but draw elements is essentially the same.
shaderProgram.uniforms.get("u_time").uniform1f(i * 0.1);

const primitiveType = gl.TRIANGLES;
const offset = 0;
const count = 6;
gl.drawArrays(primitiveType, offset, count); 

shader's People

Contributors

drew-eastmond avatar drew-sinkingship avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.