littlejsengine 1.9.6 → 1.9.8

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.
@@ -301,7 +301,7 @@ class RandomGenerator
301
301
  */
302
302
  function vec2(x=0, y)
303
303
  {
304
- return typeof x === 'number' ?
304
+ return typeof x == 'number' ?
305
305
  new Vector2(x, y == undefined? x : y) :
306
306
  new Vector2(x.x, x.y);
307
307
  }
@@ -330,13 +330,19 @@ class Vector2
330
330
  * @param {Number} [y] - Y axis location */
331
331
  constructor(x=0, y=0)
332
332
  {
333
- ASSERT(typeof x === 'number' && typeof y === 'number');
333
+ ASSERT(typeof x == 'number' && typeof y == 'number');
334
334
  /** @property {Number} - X axis location */
335
335
  this.x = x;
336
336
  /** @property {Number} - Y axis location */
337
337
  this.y = y;
338
338
  }
339
339
 
340
+ /** Sets values of this vector and returns self
341
+ * @param {Number} [x] - X axis location
342
+ * @param {Number} [y] - Y axis location
343
+ * @return {Vector2} */
344
+ set(x=0, y=0) { this.x=x; this.y=y; return this; }
345
+
340
346
  /** Returns a new vector that is a copy of this
341
347
  * @return {Vector2} */
342
348
  copy() { return new Vector2(this.x, this.y); }
@@ -588,6 +594,15 @@ class Color
588
594
  this.a = a;
589
595
  }
590
596
 
597
+ /** Sets values of this color and returns self
598
+ * @param {Number} [r] - red
599
+ * @param {Number} [g] - green
600
+ * @param {Number} [b] - blue
601
+ * @param {Number} [a] - alpha
602
+ * @return {Color} */
603
+ set(r=1, g=1, b=1, a=1)
604
+ { this.r=r; this.g=g; this.b=b; this.a=a; return this; }
605
+
591
606
  /** Returns a new color that is a copy of this
592
607
  * @return {Color} */
593
608
  copy() { return new Color(this.r, this.g, this.b, this.a); }
@@ -715,14 +730,15 @@ class Color
715
730
  ).clamp();
716
731
  }
717
732
 
718
- /** Returns this color expressed as a rgb color code
733
+ /** Returns this color expressed as a hex color code
719
734
  * @param {Boolean} [useAlpha] - if alpha should be included in result
720
735
  * @return {String} */
721
- toString(useAlpha = true)
722
- {
723
- return `rgb(${this.r*255},${this.g*255},${this.b*255},${useAlpha ? this.a : 0})`;
736
+ toString(useAlpha = true)
737
+ {
738
+ const toHex = (c)=> ((c=c*255|0)<16 ? '0' : '') + c.toString(16);
739
+ return '#' + toHex(this.r) + toHex(this.g) + toHex(this.b) + (useAlpha ? toHex(this.a) : '');
724
740
  }
725
-
741
+
726
742
  /** Set this color from a hex code
727
743
  * @param {String} hex - html hex code
728
744
  * @return {Color} */
@@ -748,6 +764,64 @@ class Color
748
764
  }
749
765
  }
750
766
 
767
+ ///////////////////////////////////////////////////////////////////////////////
768
+ // default colors
769
+
770
+ /** Color - White
771
+ * @type {Color}
772
+ * @memberof Utilities */
773
+ const WHITE = rgb();
774
+
775
+ /** Color - Black
776
+ * @type {Color}
777
+ * @memberof Utilities */
778
+ const BLACK = rgb(0,0,0);
779
+
780
+ /** Color - Gray
781
+ * @type {Color}
782
+ * @memberof Utilities */
783
+ const GRAY = rgb(.5,.5,.5);
784
+
785
+ /** Color - Red
786
+ * @type {Color}
787
+ * @memberof Utilities */
788
+ const RED = rgb(1,0,0);
789
+
790
+ /** Color - Orange
791
+ * @type {Color}
792
+ * @memberof Utilities */
793
+ const ORANGE = rgb(1,.5,0);
794
+
795
+ /** Color - Yellow
796
+ * @type {Color}
797
+ * @memberof Utilities */
798
+ const YELLOW = rgb(1,1,0);
799
+
800
+ /** Color - Green
801
+ * @type {Color}
802
+ * @memberof Utilities */
803
+ const GREEN = rgb(0,1,0);
804
+
805
+ /** Color - Cyan
806
+ * @type {Color}
807
+ * @memberof Utilities */
808
+ const CYAN = rgb(0,1,1);
809
+
810
+ /** Color - Blue
811
+ * @type {Color}
812
+ * @memberof Utilities */
813
+ const BLUE = rgb(0,0,1);
814
+
815
+ /** Color - Purple
816
+ * @type {Color}
817
+ * @memberof Utilities */
818
+ const PURPLE = rgb(.5,0,1);
819
+
820
+ /** Color - Magenta
821
+ * @type {Color}
822
+ * @memberof Utilities */
823
+ const MAGENTA = rgb(1,0,1);
824
+
751
825
  ///////////////////////////////////////////////////////////////////////////////
752
826
 
753
827
  /**
@@ -87,7 +87,7 @@ function glPreRender()
87
87
 
88
88
  // clear and set to same size as main canvas
89
89
  glContext.viewport(0, 0, glCanvas.width=mainCanvas.width, glCanvas.height=mainCanvas.height);
90
- //glContext.clear(gl_COLOR_BUFFER_BIT); // auto cleared when size is set
90
+ glContext.clear(gl_COLOR_BUFFER_BIT);
91
91
 
92
92
  // set up the shader
93
93
  glContext.useProgram(glShader);
@@ -270,99 +270,6 @@ function glDraw(x, y, sizeX, sizeY, angle, uv0X, uv0Y, uv1X, uv1Y, rgba, rgbaAdd
270
270
  glInstanceCount++;
271
271
  }
272
272
 
273
- ///////////////////////////////////////////////////////////////////////////////
274
- // post processing - can be enabled to pass other canvases through a final shader
275
-
276
- let glPostShader, glPostTexture, glPostIncludeOverlay;
277
-
278
- /** Set up a post processing shader
279
- * @param {String} shaderCode
280
- * @param {Boolean} includeOverlay
281
- * @memberof WebGL */
282
- function glInitPostProcess(shaderCode, includeOverlay=false)
283
- {
284
- ASSERT(!glPostShader, 'can only have 1 post effects shader');
285
- if (headlessMode) return;
286
- if (!shaderCode) // default shader pass through
287
- shaderCode = 'void mainImage(out vec4 c,vec2 p){c=texture(iChannel0,p/iResolution.xy);}';
288
-
289
- // create the shader
290
- glPostShader = glCreateProgram(
291
- '#version 300 es\n' + // specify GLSL ES version
292
- 'precision highp float;'+ // use highp for better accuracy
293
- 'in vec2 p;'+ // position
294
- 'void main(){'+ // shader entry point
295
- 'gl_Position=vec4(p+p-1.,1,1);'+ // set position
296
- '}' // end of shader
297
- ,
298
- '#version 300 es\n' + // specify GLSL ES version
299
- 'precision highp float;'+ // use highp for better accuracy
300
- 'uniform sampler2D iChannel0;'+ // input texture
301
- 'uniform vec3 iResolution;'+ // size of output texture
302
- 'uniform float iTime;'+ // time
303
- 'out vec4 c;'+ // out color
304
- '\n' + shaderCode + '\n'+ // insert custom shader code
305
- 'void main(){'+ // shader entry point
306
- 'mainImage(c,gl_FragCoord.xy);'+ // call post process function
307
- 'c.a=1.;'+ // always use full alpha
308
- '}' // end of shader
309
- );
310
-
311
- // create buffer and texture
312
- glPostTexture = glCreateTexture(undefined);
313
- glPostIncludeOverlay = includeOverlay;
314
-
315
- // hide the original 2d canvas
316
- mainCanvas.style.visibility = 'hidden';
317
- if (glPostIncludeOverlay)
318
- overlayCanvas.style.visibility = 'hidden';
319
- }
320
-
321
- // Render the post processing shader, called automatically by the engine
322
- function glRenderPostProcess()
323
- {
324
- if (!glPostShader || headlessMode) return;
325
-
326
- // prepare to render post process shader
327
- if (glEnable)
328
- {
329
- glFlush(); // clear out the buffer
330
- mainContext.drawImage(glCanvas, 0, 0); // copy to the main canvas
331
- }
332
- else
333
- {
334
- // set the viewport
335
- glContext.viewport(0, 0, glCanvas.width = mainCanvas.width, glCanvas.height = mainCanvas.height);
336
- }
337
-
338
- // copy overlay canvas so it will be included in post processing
339
- glPostIncludeOverlay && mainContext.drawImage(overlayCanvas, 0, 0);
340
-
341
- // setup shader program to draw one triangle
342
- glContext.useProgram(glPostShader);
343
- glContext.bindBuffer(gl_ARRAY_BUFFER, glGeometryBuffer);
344
- glContext.pixelStorei(gl_UNPACK_FLIP_Y_WEBGL, 1);
345
- glContext.disable(gl_BLEND);
346
-
347
- // set textures, pass in the 2d canvas and gl canvas in separate texture channels
348
- glContext.activeTexture(gl_TEXTURE0);
349
- glContext.bindTexture(gl_TEXTURE_2D, glPostTexture);
350
- glContext.texImage2D(gl_TEXTURE_2D, 0, gl_RGBA, gl_RGBA, gl_UNSIGNED_BYTE, mainCanvas);
351
-
352
- // set vertex position attribute
353
- const vertexByteStride = 8;
354
- const pLocation = glContext.getAttribLocation(glPostShader, 'p');
355
- glContext.enableVertexAttribArray(pLocation);
356
- glContext.vertexAttribPointer(pLocation, 2, gl_FLOAT, false, vertexByteStride, 0);
357
-
358
- // set uniforms and draw
359
- const uniformLocation = (name)=>glContext.getUniformLocation(glPostShader, name);
360
- glContext.uniform1i(uniformLocation('iChannel0'), 0);
361
- glContext.uniform1f(uniformLocation('iTime'), time);
362
- glContext.uniform3f(uniformLocation('iResolution'), mainCanvas.width, mainCanvas.height, 1);
363
- glContext.drawArrays(gl_TRIANGLE_STRIP, 0, 4);
364
- }
365
-
366
273
  ///////////////////////////////////////////////////////////////////////////////
367
274
  // store gl constants as integers so their name doesn't use space in minifed
368
275
  const