0.0.2 • Published 5 years ago

webgl-dev-utils v0.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

webgl-dev-utils

utils function for native webgl development

Waiting to translate to English. Or see 中文版.

Usage

install

npm i webgl-dev-utils -S

import

import * as webglUtils from 'webgl-dev-utils';

Example

import * as webglUtils from 'webgl-dev-utils';
const canvas = document.createElement('canvas');
const gl = canvas.getContext('webgl');
const vertexShaderCode = `
precision mediump float;
attribute vec2 a_position;
void main(){
	gl_Position =vec4(a_position,0.0,1.0);
}
`;
const fragmentShaderCode = `
precision mediump float;
uniform vec4 u_color;
void main(){
	gl_FragColor = u_color;
}
`;

//create program
const program = webglUtils.createProgram(
    gl,
    vertexShaderCode,
    fragmentShaderCode
);

//create points
const points = new Float32Array([-1, -1, 1, -1, 1, 1, -1, 1]);

//create buffer
const buffer = webglUtils.createArrayBuffer(gl, points);

//set canvas size
const width = 400;
const height = 300;

//set color.(max:1,min:0)
const color = [1, 0, 0, 1];

canvas.width = width;
canvas.height = height;
document.body.appendChild(canvas);

//use program
gl.useProgram(program);

//Sets the attribute's value to the specified value
webglUtils.setProgramAttribute(gl, program, {
    a_position: {
        type: 'pointer',
        value: [2, gl.FLOAT, false, 0, 0]
    }
});

//Sets the uniform's value to the specified value
webglUtils.setProgramUniform(gl, program, {
    u_color: {
        type: 'float',
        value: color
    }
});

//render
gl.viewport(0, 0, width, height);
gl.drawArrays(gl.TRIANGLE_FAN, 0, 4);

API

.createProgram(gl, vertexSource, fragmentSource)

ParamTypeDesc
glWebGLRenderingContext-
vertexSourceStringVertex shader source
fragmentSourceStringFragment shader source

return {WebGLProgram}

.createShader(gl, program, source, type)

ParamTypeDesc
glWebGLRenderingContext-
programWebGLProgram-
sourceStringShader source
typeNumberShader type

return {WebShader}

.createArrayBuffer(gl, data, usage)

.createTextureByImage(gl, image)

.updateTexture(gl, image)

.checkTexture(gl,texture,width,height)

.isPowerOf2(value)

.setProgramAttribute(gl, program, data)

.setProgramUniform(gl, program, data)

NOTE: This method does not currently support matrices.