Solver (class)
The Solver class creates a renderable WebGL program. It also sending uniform values onto the GPU for the WebGL program to be used with it. It also allows for easy set up of render targets whether it is going to be the canvas, or the texture.
Constructor
The class constructor can be called as follows:
var solverInstance = new Abubu.Solver(options) ;
Where options
is an object that can have the following attributes:
-
vertexShader
The source code of the vertex shader of the WebGL solver. The source code needs to be provided as a single string value. If no vertex shader is provided the following shader be assumed by default:
#version 300 es precision highp float, int ; in vec3 position; // 3D coordinate of the vertices out vec2 pixPos ; out vec2 pixelPosition ; out vec2 cc ; out vec3 pixCrd ; // interpolated fragment coordinate void main() { cc = position.xy ; pixPos = position.xy ; pixelPosition = position.xy ; pixCrd = position.xyz ; gl_Position = vec4(position.xy*2.-1.,0.,1.0); }
-
fragmentShader
The source code for the fragment shader of the WebGL solver. The source code must be written in GLSL language and should be provided as a single string of code.
There is no default fragment shader and the shader source code must be provided.
-
uniforms
The object of uniforms. Each attribute of the object must match a uniform name in one of the shaders (either the vertex or fragment shaders). Each attribute is an object which must contain a name and value. See more on uniforms below.
-
canvas
An HTML canvas element. If this option is provided, the result of the rendering will be renderred on the provided canvas.
-
targets
An object of render targets. Each attribute of the targets object must match a output value of the fragment shader. Each attributes should have an integer
location
attribute and a renderabletarget
attribute. -
geometry
The geometry object is optional. The default geometry is:
{ vertices : [ 1,1,0 , 0,1,0 , 1,0,0 , 0,0,0 ] , noVertices : 4 , noCoords : 3 , primitive : 'triangle_strip' , /* other options : 'triangle' , 'line', 'point' */ }
-
cullFace
A boolean representing whether cull facing needs to be applied. Default value is
false
. -
depthTest
A boolean representing whether a depth test needs to be applied when rendering. Default value is
false
.
Instance attributes
The following attributes are accessible
-
fragmentShader
-
vertexShader
-
uniforms
-
canvas
-
targets
-
cullFace
-
depthTest
Instance methods
-
.render()
renders the solver ;
-
.run()
runs the solver (same as
.render()
);
Uniform types
The following uniform types are available:
Type name | Type description | GLSL type(s) |
---|---|---|
t | texture | sampler2D isampler2D usampler2D |
s | sampler Other attributes: wrapS, wrapT, minFilter, magFilter. | sampler2D isampler2D usampler2D |
i | integer value | int |
i2 | 2D integer vector | ivec2 |
i3 | 3D integer vector | ivec3 |
i4 | 4D integer vector | ivec4 |
u | unsigned integer value | uint |
u2 | 2D unsigned integer vector | uvec2 |
u3 | 3D unsigned integer vector | uvec3 |
u4 | 4D unsigned integer vector | uvec4 |
iv | One dimensional array of integers | int [] |
i2v | One dimensional array of 2D integer vectors | ivec2 [] |
i3v | One dimensional array of 3D integer vectors | ivec3 [] |
i4v | One dimensional array of 4D integer vectors | ivec4 [] |
uv | One dimensional array of unsigned integers | uint [] |
u2v | One dimensional array of 2D unsigned integer vectors | uvec2 [] |
u3v | One dimensional array of 3D unsigned integer vectors | uvec3 [] |
u4v | One dimensional array of 4D integer vectors | uvec4 [] |
f | float value | float |
v2 | 2D float vector | vec2 |
v3 | 3D float vector | vec3 |
v4 | 4D float vector | vec4 |
fv | One dimensional array of float values | float [] |
v2v | One dimensional array of 2D float vectors | vec2 [] |
v3v | One dimensional array of 3D float vectors | vec3 [] |
v4v | One dimensional array of 4D float vectors | vec4 [] |
mat2 or mat2x2 | 2x2 float matrix | mat2 or mat2x2 |
mat3 or mat3x3 | 3x3 float matrix | mat3 or mat3x3 |
mat4 or mat4x4 | 4x4 float matrix | mat4 or mat4x4 |
mat2x3 | 2x3 float matrix | mat2x3 |
mat2x4 | 2x4 float matrix | mat2x4 |
mat3x2 | 3x2 float matrix | mat3x2 |
mat3x4 | 3x4 float matrix | mat3x4 |
mat4x3 | 4x3 float matrix | mat4x3 |
mat4x2 | 4x2 float matrix | mat4x2 |
The values of integer arrays should be of type Int32Array
JavaScript with appropriate length so that the memory field matches the memory fields in the shader. Similarly, unsigned arrays are initialized with Uint32Array
, and float arrays by Float32Array
JavaScript arrays.
Example
For more examples on setting up solvers and how they can be used, checkout the examples in the Tutorials section. Various examples show you how to setup geometries, shaders and uniforms, and render targets.