Skip to content

Commit 5bbd7f3

Browse files
Supported instanced draw on WebGL 1 (#124)
* Supported instanced draw on WebGL 1 * TILES-5073 Bumped version to 0.13.0
1 parent b2fcfef commit 5bbd7f3

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "2gl",
3-
"version": "0.12.0",
3+
"version": "0.13.0",
44
"description": "WebGL library for 2GIS projects",
55
"repository": {
66
"type": "git",

src/Buffer.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,37 @@ class Buffer {
6060
*
6161
* Если используется первый раз, добавляет данные в контекст WebGL.
6262
*
63-
* @param {WebGLRenderingContext} gl
63+
* @param {WebGLRenderingContext | WebGL2RenderingContext} gl
6464
* @param {?Number} location Положение аттрибута для связывания данных с переменными в шейдере
6565
* @param {?BufferBindOptions} options Параметры передаваемые в функцию vertexAttribPointer, если их нет,
6666
* то используются параметры конкретного буфера. Параметры должны быть переданы все.
67+
* @param {?ANGLE_instanced_arrays} instancesExt Экстеншн для работы с instanced буферами,
6768
*/
68-
bind(gl, location, options) {
69+
bind(gl, location, options, instancesExt) {
6970
if (!this._glBuffer) {
7071
this.prepare(gl);
7172
}
7273

7374
if (this.type === Buffer.ArrayBuffer) {
7475
gl.bindBuffer(gl.ARRAY_BUFFER, this._glBuffer);
7576

77+
location = location || 0;
7678
options = options || this.options;
7779

7880
gl.vertexAttribPointer(location, options.itemSize, this._toGlParam(gl, options.dataType),
7981
options.normalized, options.stride, options.offset);
8082

8183
if (options.instanceDivisor) {
82-
gl.vertexAttribDivisor(location, options.instanceDivisor);
84+
if (gl instanceof WebGLRenderingContext) {
85+
if (instancesExt) {
86+
instancesExt.vertexAttribDivisorANGLE(location, options.instanceDivisor);
87+
} else {
88+
console.error('Can\'t set up instanced attribute divisor. ' +
89+
'Missing ANGLE_instanced_arrays extension');
90+
}
91+
} else {
92+
gl.vertexAttribDivisor(location, options.instanceDivisor);
93+
}
8394
}
8495
} else if (this.type === Buffer.ElementArrayBuffer) {
8596
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._glBuffer);

src/BufferChannel.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class BufferChannel {
3030
* Связывает данные с контекстом WebGL с нужными параметрами.
3131
* Вызывает {@link Buffer#bind} исходного буфера.
3232
*/
33-
bind(gl, location) {
34-
this._buffer.bind(gl, location, this.options);
33+
bind(gl, location, instancesExt) {
34+
this._buffer.bind(gl, location, this.options, instancesExt);
3535
}
3636
}
3737

src/Vao.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ class Vao {
1515

1616
/**
1717
* WebGL экстеншен, в котором был инициализирован буфер.
18-
* Используется только для удаления vao, подумать хорошо, прежде чем использовать для чего-то ещё.
1918
*/
20-
this._ext = null;
19+
this._vaoExt = null;
2120
}
2221

2322
/**
@@ -26,9 +25,10 @@ class Vao {
2625
* @param {State} Стейт рендера
2726
*/
2827
bind(state) {
29-
const ext = state.extensions.OES_vertex_array_object;
28+
const vaoExt = state.extensions.OES_vertex_array_object;
29+
const instancesExt = state.extensions.ANGLE_instanced_arrays;
3030

31-
this._bind(state.gl, ext);
31+
this._bind(state.gl, vaoExt, instancesExt);
3232

3333
return this;
3434
}
@@ -60,17 +60,17 @@ class Vao {
6060
return this;
6161
}
6262

63-
_bind(gl, ext) {
63+
_bind(gl, vaoExt, instancesExt) {
6464
if (!this._vao) {
65-
this._prepare(gl, ext);
65+
this._prepare(gl, vaoExt, instancesExt);
6666
} else {
6767
this._glBindVertexArray(this._vao);
6868
}
6969
}
7070

71-
_prepare(gl, ext) {
71+
_prepare(gl, vaoExt, instancesExt) {
7272
this._gl = gl;
73-
this._ext = ext;
73+
this._vaoExt = vaoExt;
7474

7575
this._vao = this._glCreateVertexArray();
7676
this._glBindVertexArray(this._vao);
@@ -84,13 +84,13 @@ class Vao {
8484
if (shaderAttribute.index !== true) {
8585
gl.enableVertexAttribArray(shaderAttribute.location);
8686
}
87-
attributes[name].bind(gl, shaderAttribute.location, undefined);
87+
attributes[name].bind(gl, shaderAttribute.location, instancesExt);
8888
}
8989
}
9090

9191
_glCreateVertexArray() {
9292
const gl = this._gl;
93-
const ext = this._ext;
93+
const ext = this._vaoExt;
9494
if (this._isWebGL2(gl)) {
9595
return gl.createVertexArray();
9696
} else if (ext) {
@@ -101,7 +101,7 @@ class Vao {
101101

102102
_glBindVertexArray(vao) {
103103
const gl = this._gl;
104-
const ext = this._ext;
104+
const ext = this._vaoExt;
105105
if (this._isWebGL2(gl)) {
106106
gl.bindVertexArray(vao);
107107
} else if (ext) {
@@ -114,7 +114,7 @@ class Vao {
114114

115115
_glDeleteVertexArray(vao) {
116116
const gl = this._gl;
117-
const ext = this._ext;
117+
const ext = this._vaoExt;
118118
if (this._isWebGL2(gl)) {
119119
gl.deleteVertexArray(vao);
120120
} else if (ext) {

0 commit comments

Comments
 (0)