XenoGL
XenoGL is a lightweight and Object-Oriented wrapper for WebGL2.
Unstable
XenoGL is under unstable yet. APIs may be going to change, which breaks your code. Do not use XenoGL for production softwares.
Install without Node.js
Download the zip file from Release page on GitHub and unzip the file.
Copy build/xenogl.min.js to your directory and append below code to your HTML file:
Install with Node.js
Run the install command.
$ npm install xenogl --save
Then import xenogl in JavaScript file.
const XenoGL = ;
Basic usage
First, create a WebGL2 context from a canvas.
// create a canvas.const canvas = documentbody;canvaswidth = 500;canvasheight = 500;documentbody; // create a context.const xgl = canvas;
Next, create two shaders and a program. And add the program to the context.
// load source as you like.const vertexShaderSource = await ;const fragmentShaderSource = await ; // create shaders.const vertexShader = vertexShaderSource;const fragmentShader = fragmentShaderSource; // create a program.const program = vertexShader: vertexShader fragmentShader: fragmentShader; // add the program to the context.xgl;
You need data to draw. For example, vertex positions and colors.
const vertices = -05 05 00 -05 -05 00 05 05 00 -05 -05 00 05 -05 00 05 05 00; const colors = 10 00 00 10 00 10 00 10 00 00 10 10 00 10 00 10 00 00 00 10 00 00 10 10;
Then, create a buffer from data.
// create attributes which is defined in shaders.const positionAttribute = 'vertexPosition' 3;const colorAttribute = 'color' 4; // create buffers with data and attributes.const positionBuffer = dataOrLength: vertices attributes: positionAttribute dataType: XenoGLFLOAT; const colorBuffer = dataOrLength: colors attributes: colorAttribute dataType: XenoGLFLOAT; // add buffers to the program.program;program;
Finally, draw it!
xgl;
That's all.
Program
You can use multiple programs.
To switch programs, use activateProgram
.
xgl;xgl; xgl;
activateProgram
is a very heavy operation. If you switch programs every frames, it causes performance issues. Because it toggles every attributes on buffers.
If you want just change the program without toggling attributes, use useProgram
instead.
But if you don't have knowledge about OpenGL/WebGL, you should use activateProgram
.
Buffer
You can send data to buffers.
const positionBuffer = attributes: positionAttribute dataType: XenoGLFLOAT; program; positionBuffer;
Interleaved buffer
To make a buffer interleaved, pass an array of attributes to constructor of ArrayBuffer.
const vertices = -300 300 00 // position 00 10 00 10 // color -300 -300 00 10 00 00 10 300 300 00 10 00 00 10 300 -300 00 00 00 10 10; const positionAttribute = 'vertexPosition' 3;const colorAttribute = 'color' 4; const buffer = dataOrLength: vertices attributes: positionAttribute colorAttribute dataType: XenoGLFLOAT usage: XenoGLDYNAMIC_DRAW;
XenoGL detect stride and offset automatically and make the buffer interleaved.
Index buffer
You can create a index buffer by using XenoGL.ElementArrayBuffer
object.
const indices = 0 1 2 1 3 2; const indexBuffer = dataOrLength: indices dataType: XenoGLUNSIGNED_SHORT usage: XenoGLDYNAMIC_DRAW; program;
An ElementArrayBuffer object is treated as an index buffer when it is added to the program.
When you add multiple ElementArrayBuffer to the program, latest one is used as an index buffer.
If you need to choose an index buffer manually, use program.activateElemntArrayBuffer()
.
program;
Other buffers
Not supported yet. Stay tuned.
Uniform variables
To create uniform variables, use XenoGL.Uniform
and add it to the program.
const modelUniform = 'model';const viewUniform = 'view';const projectionUniform = 'projection'; modelUniform;projectionUniform; program;program;program;
XenoGL.Uniform
object has setValue(value, type)
, setVector(vector, type)
and setMatrix(matrix)
to apply a value. type
can be XenoGL.FLOAT, XenoGL.UNSIGNED_SHORT and other data types.
Don't forget to add an uniform to the program.
Vertex Array Objects
XenoGL supports Vertex Array Object(VAO).
const buffer = dataOrLength: particleInitialDataF32 attributes: positionAttr velocityAttr ageAttr lifeAttr dataType: XenoGLFLOAT usage: XenoGLDYNAMIC_COPY; // 2nd arg is optional.const vao = buffer dataOrLength: particleInitialDataF32 // initial data attributes: positionAttr velocityAttr // attributes to enable; // add it to the program.program;
If you activate another VAO, use program.activateVertexArrayObject
.
program;
Uniform Buffer Object
Uniform Buffer Objects(UBO) make you able to share values between programs.
// create a buffer.const sharedUniformBuffer = dataOrLength: 10 00 00 10 dataType: XenoGLFLOAT; // create ubos.const ubo1 = 'param' sharedUniformBuffer;const ubo2 = 'param' sharedUniformBuffer; // add to programs.program1;program2;
Transform Feedback
To use transform feedbacks, first, create a program with additional options.
const program = vertexShader: vs fragmentShader: fs feedbackVaryings: 'vertexPosition' 'vertexVelocity' 'vertexAge' 'vertexLife' // variables to feedback. feedbackBufferMode: XenoGLINTERLEAVED_ATTRIBS // XenoGL.SEPARATE_ATTRIB or XenoGL.INTERLEAVED_ATTRIBS;
Then, create a TransformFeedback
object and add it to the context(not to the program).
const tf = ;xgl;
feedback
method executes a calc and feedback.
tf;
Textures
Using textures, create a Texture2D
object and add it to the context.
const textureSource = await ;const texture = textureSource;xgl;
Source of texture can be img, canvas, video, ImageBitmap, ImageData or ArrayBufferView.
You can crete textures with options.
const texture = textureSource target: XenoGLTEXTURE_2D mipmapLevel: 0 internalFormat?: XenoGLRGBA format: XenoGLRGBA dataType: XenoGLUNSIGNED_BYTE width: 500 height: 500;
To use another texture, use xgl.activateTexture(texture)
.
xgl;
Misc.
xgl;xglclearXenoGLCOLOR_BUFFER_BIT | XenoGLDEPTH_BUFFER_BIT;xgl;xgl;
API Document
For more information, see API Document.