-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (115 loc) · 3.48 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Expose component
*/
module.exports = function (context) {
/**
* Model
* Create a model.
* @param {Object} options The options for creating the model.
* @param {Float32Array} [options.vertices] The vertices of the model.
* @param {Uint16Array} [options.indices] The indices of the model.
* @param {Float32Array} [options.colors] The per-vertex color.
* @param {Number} [options.drawType] The WebGL primitive type.
* @param {Boolean} [options.dynamic] The flag for caching the geometry.
* @return {Model} The model.
* @api public
*/
function Model (options) {
var options = options || {};
this.id = guid();
this.vertices = options.vertices;
this.indices = options.indices;
this.colors = options.colors;
this.drawType = options.drawType || context.TRIANGLES;
this.dynamic = options.dynamic || false;
};
/**
* Model#bind_verticex
* Bind the vertices of this model to a shader program.
* @param {Program} program The program to bind the vertices to.
* @param {Boolean} update The flat to cache the vertices.
* @return {Model} this for chaining.
* @api public
*/
Model.prototype.bind_vertices = function (program, update) {
if (!this.vertices) {
return this;
}
if (update || this.dynamic) {
program.bind_attribute(this.id + '-vertices', {
name: 'a_position',
data: this.vertices,
size: 3
});
return this;
}
program.bind_attribute(context, this.id + '-vertices');
return this;
};
/**
* Model#bind_colors
* Bind the colors of this model to a shader program.
* @param {WebGLRenderingContext} context A WebGL rendering context.
* @param {Program} program The program to bind the colors to.
* @param {Boolean} update The flag to cache the colors.
* @return {Model} this for chaining.
* @api public
*/
Model.prototype.bind_colors = function (program, update) {
if (!this.colors) {
return this;
}
if (update || this.dynamic) {
program.bind_attribute(this.id + '-colors', {
name : 'a_color',
data: this.colors,
size: 4
});
return this;
}
program.bind_attribute(this.id + '-colors');
return this;
};
/**
* Model#bind_indices
* Bind the indices of this model to a shader program.
* @param {WebGLRenderingContext} context A WebGL rendering context.
* @param {Program} program The program to bind the indices.
* @param {Boolean} update Avoid using cached indices.
* @return {Model} this for chaining.
* @api public
*/
Model.prototype.bind_indices = function (program, update) {
if (!this.indices) {
return this;
}
if (update || this.dynamic) {
program.bind_attribute(this.id + '-indices', {
attributeType: context.ELEMENT_ARRAY_BUFFER
, data: this.indices
});
return this;
}
program.bind_attribute(this.id + '-indices');
return this;
};
/**
* Model#draw
* Draw this Model.
* @param {WebGLRenderingContext} context A WebGL rendering context.
* @return {Model} this for chaining.
* @api public
*/
Model.prototype.draw = function () {
if (this.indices) {
context.drawElements(this.drawType, this.indices.length, context.UNSIGNED_SHORT, 0);
return this;
}
if (this.vertices) {
context.drawArrays(this.drawType, 0, this.vertices.length / 3);
return this;
}
return this;
};
return Model;
};