littlejsengine 1.6.92 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -203,8 +203,8 @@ function glFlush()
203
203
 
204
204
  // draw all the sprites in the batch and reset the buffer
205
205
  glContext.bufferSubData(gl_ARRAY_BUFFER, 0,
206
- glPositionData.subarray(0, glBatchCount * gl_VERTICES_PER_QUAD * gl_INDICIES_PER_VERT));
207
- glContext.drawArrays(gl_TRIANGLES, 0, glBatchCount * gl_VERTICES_PER_QUAD);
206
+ glPositionData.subarray(0, glBatchCount * gl_INDICIES_PER_VERT));
207
+ glContext.drawArrays(gl_TRIANGLE_STRIP, 0, glBatchCount);
208
208
  glBatchCount = 0;
209
209
  glBatchAdditive = glAdditive;
210
210
  }
@@ -225,39 +225,75 @@ function glCopyToContext(context, forceDraw)
225
225
  }
226
226
 
227
227
  /** Add a sprite to the gl draw list, used by all gl draw functions
228
- * @param x
229
- * @param y
230
- * @param sizeX
231
- * @param sizeY
232
- * @param angle
233
- * @param uv0X
234
- * @param uv0Y
235
- * @param uv1X
236
- * @param uv1Y
237
- * @param rgba
238
- * @param [rgbaAdditive=0]
228
+ * @param {Number} x
229
+ * @param {Number} y
230
+ * @param {Number} sizeX
231
+ * @param {Number} sizeY
232
+ * @param {Number} angle
233
+ * @param {Number} uv0X
234
+ * @param {Number} uv0Y
235
+ * @param {Number} uv1X
236
+ * @param {Number} uv1Y
237
+ * @param {Number} rgba
238
+ * @param {Number} [rgbaAdditive=0]
239
239
  * @memberof WebGL */
240
240
  function glDraw(x, y, sizeX, sizeY, angle, uv0X, uv0Y, uv1X, uv1Y, rgba, rgbaAdditive=0)
241
241
  {
242
- // flush if there is no room for more verts or if different blend mode
243
- if (glBatchCount == gl_MAX_BATCH || glBatchAdditive != glAdditive)
242
+ // flush if there is not enough room or if different blend mode
243
+ const vertCount = 6;
244
+ if (glBatchCount >= gl_MAX_BATCH-vertCount || glBatchAdditive != glAdditive)
244
245
  glFlush();
245
246
 
246
247
  // prepare to create the verts from size and angle
247
248
  const c = Math.cos(angle)/2, s = Math.sin(angle)/2;
248
249
  const cx = c*sizeX, cy = c*sizeY, sx = s*sizeX, sy = s*sizeY;
249
-
250
- // setup 2 triangles to form a quad
251
- for(let i=6, offset = glBatchCount++ * gl_VERTICES_PER_QUAD * gl_INDICIES_PER_VERT; i--;)
250
+ const positionData =
251
+ [
252
+ x-cx+sy, y+cy+sx, uv0X, uv0Y,
253
+ x-cx-sy, y-cy+sx, uv0X, uv1Y,
254
+ x+cx+sy, y+cy-sx, uv1X, uv0Y,
255
+ x+cx-sy, y-cy-sx, uv1X, uv1Y,
256
+ ];
257
+
258
+ // setup 2 triangle strip quad
259
+ for(let i = vertCount, offset = glBatchCount * gl_INDICIES_PER_VERT; i--;)
252
260
  {
253
- const a = i-4&&i>1, b = i-5&&i-2&&i-1;
254
- glPositionData[offset++] = x + (a?-cx:cx) + (b?sy:-sy);
255
- glPositionData[offset++] = y + (b?cy:-cy) + (a?sx:-sx);
256
- glPositionData[offset++] = a ? uv0X : uv1X;
257
- glPositionData[offset++] = b ? uv0Y : uv1Y;
261
+ let j = clamp(i-1, 0, 3)*4; // degenerate tri at ends
262
+ glPositionData[offset++] = positionData[j++];
263
+ glPositionData[offset++] = positionData[j++];
264
+ glPositionData[offset++] = positionData[j++];
265
+ glPositionData[offset++] = positionData[j++];
258
266
  glColorData[offset++] = rgba;
259
267
  glColorData[offset++] = rgbaAdditive;
260
268
  }
269
+ glBatchCount += vertCount;
270
+ }
271
+
272
+ /** Add a convex polygon to the gl draw list
273
+ * @param {Array} points - Array of Vector2 points
274
+ * @param {Number} rgba - Color of the polygon
275
+ * @memberof WebGL */
276
+ function glDrawPoints(points, rgba)
277
+ {
278
+ // flush if there is not enough room or if different blend mode
279
+ const vertCount = points.length + 2;
280
+ if (glBatchCount >= gl_MAX_BATCH-vertCount || glBatchAdditive != glAdditive)
281
+ glFlush();
282
+
283
+ // setup triangle strip from list of points
284
+ for(let i = vertCount, offset = glBatchCount * gl_INDICIES_PER_VERT; i--;)
285
+ {
286
+ const j = clamp(i-1, 0, vertCount-3); // degenerate tri at ends
287
+ const h = j>>1;
288
+ const point = points[j%2? h : vertCount-3-h];
289
+ glPositionData[offset++] = point.x;
290
+ glPositionData[offset++] = point.y;
291
+ glPositionData[offset++] = 0; // uvx
292
+ glPositionData[offset++] = 0; // uvy
293
+ glColorData[offset++] = 0; // nothing to tint
294
+ glColorData[offset++] = rgba; // apply rgba via additive
295
+ }
296
+ glBatchCount += vertCount;
261
297
  }
262
298
 
263
299
  ///////////////////////////////////////////////////////////////////////////////
@@ -351,14 +387,14 @@ function glRenderPostProcess()
351
387
  glContext.uniform1i(uniformLocation('iChannel0'), 0);
352
388
  glContext.uniform1f(uniformLocation('iTime'), time);
353
389
  glContext.uniform3f(uniformLocation('iResolution'), mainCanvas.width, mainCanvas.height, 1);
354
- glContext.drawArrays(gl_TRIANGLES, 0, 3);
390
+ glContext.drawArrays(gl_TRIANGLE_STRIP, 0, 3);
355
391
  }
356
392
 
357
393
  ///////////////////////////////////////////////////////////////////////////////
358
394
  // store gl constants as integers so their name doesn't use space in minifed
359
395
  const
360
396
  gl_ONE = 1,
361
- gl_TRIANGLES = 4,
397
+ gl_TRIANGLE_STRIP = 5,
362
398
  gl_SRC_ALPHA = 770,
363
399
  gl_ONE_MINUS_SRC_ALPHA = 771,
364
400
  gl_BLEND = 3042,
@@ -389,6 +425,6 @@ gl_UNPACK_FLIP_Y_WEBGL = 37440,
389
425
  // constants for batch rendering
390
426
  gl_VERTICES_PER_QUAD = 6,
391
427
  gl_INDICIES_PER_VERT = 6,
392
- gl_MAX_BATCH = 1<<16,
428
+ gl_MAX_BATCH = 1e5,
393
429
  gl_VERTEX_BYTE_STRIDE = (4 * 2) * 2 + (4) * 2, // vec2 * 2 + (char * 4) * 2
394
- gl_VERTEX_BUFFER_SIZE = gl_MAX_BATCH * gl_VERTICES_PER_QUAD * gl_VERTEX_BYTE_STRIDE;
430
+ gl_VERTEX_BUFFER_SIZE = gl_MAX_BATCH * gl_VERTEX_BYTE_STRIDE;