This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
/
GLShader.js
94 lines (82 loc) · 2.23 KB
/
GLShader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var compileProgram = require('./shader/compileProgram'),
extractAttributes = require('./shader/extractAttributes'),
extractUniforms = require('./shader/extractUniforms'),
setPrecision = require('./shader/setPrecision'),
generateUniformAccessObject = require('./shader/generateUniformAccessObject');
/**
* Helper class to create a webGL Shader
*
* @class
* @memberof PIXI.glCore
* @param gl {WebGLRenderingContext}
* @param vertexSrc {string|string[]} The vertex shader source as an array of strings.
* @param fragmentSrc {string|string[]} The fragment shader source as an array of strings.
* @param precision {string} The float precision of the shader. Options are 'lowp', 'mediump' or 'highp'.
* @param attributeLocations {object} A key value pair showing which location eact attribute should sit eg {position:0, uvs:1}
*/
var Shader = function(gl, vertexSrc, fragmentSrc, precision, attributeLocations)
{
/**
* The current WebGL rendering context
*
* @member {WebGLRenderingContext}
*/
this.gl = gl;
if(precision)
{
vertexSrc = setPrecision(vertexSrc, precision);
fragmentSrc = setPrecision(fragmentSrc, precision);
}
/**
* The shader program
*
* @member {WebGLProgram}
*/
// First compile the program..
this.program = compileProgram(gl, vertexSrc, fragmentSrc, attributeLocations);
/**
* The attributes of the shader as an object containing the following properties
* {
* type,
* size,
* location,
* pointer
* }
* @member {Object}
*/
// next extract the attributes
this.attributes = extractAttributes(gl, this.program);
this.uniformData = extractUniforms(gl, this.program);
/**
* The uniforms of the shader as an object containing the following properties
* {
* gl,
* data
* }
* @member {Object}
*/
this.uniforms = generateUniformAccessObject( gl, this.uniformData );
};
/**
* Uses this shader
*
* @return {PIXI.glCore.GLShader} Returns itself.
*/
Shader.prototype.bind = function()
{
this.gl.useProgram(this.program);
return this;
};
/**
* Destroys this shader
* TODO
*/
Shader.prototype.destroy = function()
{
this.attributes = null;
this.uniformData = null;
this.uniforms = null;
var gl = this.gl;
gl.deleteProgram(this.program);
};
module.exports = Shader;