Skip to content

Commit 3a850c6

Browse files
authored
Merge pull request #7210 from processing/fix/webgl
Fix most WebGL tests
2 parents 0ee4505 + d1887a2 commit 3a850c6

25 files changed

+352
-369
lines changed

eslint.config.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default [
2+
{
3+
ignores: [
4+
"preview/*",
5+
"docs/reference/assets/**/*",
6+
"docs/assets/**/*",
7+
"lib/*",
8+
"rollup.config.mjs",
9+
"utils/sample-linter.mjs"
10+
]
11+
}
12+
];

src/color/p5.Color.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ p5.Color = class Color {
122122
}else if(vals.length === 1){
123123
vals = [vals[0], vals[0], vals[0]];
124124
}
125-
alpha = alpha ? alpha / pInst._colorMaxes[pInst._colorMode][3] : 1;
125+
alpha = alpha !== undefined
126+
? alpha / pInst._colorMaxes[pInst._colorMode][3]
127+
: 1;
126128

127129
// _colorMode can be 'rgb', 'hsb', or 'hsl'
128130
// These should map to color.js color space
@@ -488,6 +490,14 @@ p5.Color = class Color {
488490
return to(this.color, 'hsl').coords[2] / 100 * this.maxes[this.mode][2];
489491
}
490492
}
493+
494+
get _array() {
495+
return [...this.color.coords, this.color.alpha];
496+
}
497+
498+
get levels() {
499+
return this._array.map(v => v * 255);
500+
}
491501
};
492502

493503
export default p5.Color;

src/core/friendly_errors/validate_params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ if (typeof IS_MINIFIED !== 'undefined') {
215215
// if (queryResult.hasOwnProperty('overloads')) {
216216
// // add all the overloads
217217
// for (let i = 0; i < queryResult.overloads.length; i++) {
218-
// overloads.push({ formats: queryResult.overloads[i].params });
218+
// overloads.push({ formats: queryResult.overloads[i].params || [] });
219219
// }
220220
// } else {
221221
// // no overloads, just add the main method definition

src/core/p5.Renderer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ p5.Renderer = class Renderer extends p5.Element {
132132
}
133133

134134
get (x, y, w, h) {
135-
const pd = this._pInst._pixelDensity;
135+
const pixelsState = this._pixelsState;
136+
const pd = pixelsState._pixelDensity;
136137
const canvas = this.canvas;
137138

138139
if (typeof x === 'undefined' && typeof y === 'undefined') {

src/core/p5.Renderer2D.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ class Renderer2D extends Renderer {
414414
}
415415

416416
loadPixels() {
417-
const pixelsState = this._pInst; // if called by p5.Image
417+
const pixelsState = this._pixelsState; // if called by p5.Image
418418

419419
const pd = pixelsState._pixelDensity;
420420
const w = this.width * pd;
@@ -430,7 +430,7 @@ class Renderer2D extends Renderer {
430430
// round down to get integer numbers
431431
x = Math.floor(x);
432432
y = Math.floor(y);
433-
const pixelsState = this._pInst;
433+
const pixelsState = this._pixelsState;
434434
if (imgOrCol instanceof p5.Image) {
435435
this.drawingContext.save();
436436
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
@@ -503,7 +503,7 @@ class Renderer2D extends Renderer {
503503
}
504504

505505
updatePixels(x, y, w, h) {
506-
const pixelsState = this._pInst;
506+
const pixelsState = this._pixelsState;
507507
const pd = pixelsState._pixelDensity;
508508
if (
509509
x === undefined &&

src/core/rendering.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ p5.prototype.createCanvas = function (w, h, renderer, canvas) {
177177
defaultId = `defaultCanvas${i}`;
178178
c.id = defaultId;
179179
c.classList.add(defaultClass);
180+
} else if (
181+
this._renderer &&
182+
Object.getPrototypeOf(this._renderer) !== renderers[r].prototype
183+
) {
184+
// Handle createCanvas() called with 2D mode after a 3D canvas is made
185+
if (this.canvas.parentNode) {
186+
this.canvas.parentNode.removeChild(this.canvas); //replace the existing defaultCanvas
187+
}
188+
const thisRenderer = this._renderer;
189+
this._elements = this._elements.filter(e => e !== thisRenderer);
190+
c = document.createElement('canvas');
191+
c.id = defaultId;
192+
c.classList.add(defaultClass);
180193
} else {
181194
// resize the default canvas if new one is created
182195
c = this.canvas;

src/image/loading_displaying.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ p5.prototype.loadImage = async function(
120120
});
121121

122122
return fetch(path, req)
123-
.then(response => {
123+
.then(async response => {
124124
// GIF section
125125
const contentType = response.headers.get('content-type');
126126
if (contentType === null) {
@@ -129,8 +129,8 @@ p5.prototype.loadImage = async function(
129129
);
130130
}
131131
if (contentType && contentType.includes('image/gif')) {
132-
response.arrayBuffer().then(
133-
arrayBuffer => {
132+
await response.arrayBuffer().then(
133+
arrayBuffer => new Promise((resolve, reject) => {
134134
if (arrayBuffer) {
135135
const byteArray = new Uint8Array(arrayBuffer);
136136
try{
@@ -141,6 +141,7 @@ p5.prototype.loadImage = async function(
141141
failureCallback,
142142
(pImg => {
143143
self._decrementPreload();
144+
resolve(pImg);
144145
}).bind(self)
145146
);
146147
}catch(e){
@@ -151,9 +152,11 @@ p5.prototype.loadImage = async function(
151152
} else {
152153
console.error(e);
153154
}
155+
reject(e);
154156
}
155157
}
156-
},
158+
})
159+
).catch(
157160
e => {
158161
if (typeof failureCallback === 'function') {
159162
failureCallback(e);
@@ -167,39 +170,43 @@ p5.prototype.loadImage = async function(
167170
// Non-GIF Section
168171
const img = new Image();
169172

170-
img.onload = () => {
171-
pImg.width = pImg.canvas.width = img.width;
172-
pImg.height = pImg.canvas.height = img.height;
173+
await new Promise((resolve, reject) => {
174+
img.onload = () => {
175+
pImg.width = pImg.canvas.width = img.width;
176+
pImg.height = pImg.canvas.height = img.height;
173177

174-
// Draw the image into the backing canvas of the p5.Image
175-
pImg.drawingContext.drawImage(img, 0, 0);
176-
pImg.modified = true;
177-
if (typeof successCallback === 'function') {
178-
successCallback(pImg);
179-
}
180-
self._decrementPreload();
181-
};
182-
183-
img.onerror = e => {
184-
p5._friendlyFileLoadError(0, img.src);
185-
if (typeof failureCallback === 'function') {
186-
failureCallback(e);
178+
// Draw the image into the backing canvas of the p5.Image
179+
pImg.drawingContext.drawImage(img, 0, 0);
180+
pImg.modified = true;
181+
if (typeof successCallback === 'function') {
182+
successCallback(pImg);
183+
}
184+
resolve();
187185
self._decrementPreload();
188-
} else {
189-
console.error(e);
186+
};
187+
188+
img.onerror = e => {
189+
p5._friendlyFileLoadError(0, img.src);
190+
if (typeof failureCallback === 'function') {
191+
failureCallback(e);
192+
self._decrementPreload();
193+
} else {
194+
console.error(e);
195+
}
196+
reject();
197+
};
198+
199+
// Set crossOrigin in case image is served with CORS headers.
200+
// This will let us draw to the canvas without tainting it.
201+
// See https://developer.mozilla.org/en-US/docs/HTML/CORS_Enabled_Image
202+
// When using data-uris the file will be loaded locally
203+
// so we don't need to worry about crossOrigin with base64 file types.
204+
if (path.indexOf('data:image/') !== 0) {
205+
img.crossOrigin = 'Anonymous';
190206
}
191-
};
192-
193-
// Set crossOrigin in case image is served with CORS headers.
194-
// This will let us draw to the canvas without tainting it.
195-
// See https://developer.mozilla.org/en-US/docs/HTML/CORS_Enabled_Image
196-
// When using data-uris the file will be loaded locally
197-
// so we don't need to worry about crossOrigin with base64 file types.
198-
if (path.indexOf('data:image/') !== 0) {
199-
img.crossOrigin = 'Anonymous';
200-
}
201-
// start loading the image
202-
img.src = path;
207+
// start loading the image
208+
img.src = path;
209+
});
203210
}
204211
pImg.modified = true;
205212
return pImg;

src/webgl/light.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,24 +1642,23 @@ p5.prototype.spotLight = function (
16421642
);
16431643
return this;
16441644
}
1645-
this._renderer.spotLightDiffuseColors.push(
1645+
this._renderer.spotLightDiffuseColors = [
16461646
color._array[0],
16471647
color._array[1],
16481648
color._array[2]
1649-
);
1649+
];
16501650

1651-
Array.prototype.push.apply(
1652-
this._renderer.spotLightSpecularColors,
1653-
this._renderer.specularColors
1654-
);
1651+
this._renderer.spotLightSpecularColors = [
1652+
...this._renderer.specularColors
1653+
];
16551654

1656-
this._renderer.spotLightPositions.push(position.x, position.y, position.z);
1655+
this._renderer.spotLightPositions = [position.x, position.y, position.z];
16571656
direction.normalize();
1658-
this._renderer.spotLightDirections.push(
1657+
this._renderer.spotLightDirections = [
16591658
direction.x,
16601659
direction.y,
16611660
direction.z
1662-
);
1661+
];
16631662

16641663
if (angle === undefined) {
16651664
angle = Math.PI / 3;
@@ -1675,8 +1674,8 @@ p5.prototype.spotLight = function (
16751674
}
16761675

16771676
angle = this._renderer._pInst._toRadians(angle);
1678-
this._renderer.spotLightAngle.push(Math.cos(angle));
1679-
this._renderer.spotLightConc.push(concentration);
1677+
this._renderer.spotLightAngle = [Math.cos(angle)];
1678+
this._renderer.spotLightConc = [concentration];
16801679

16811680
this._renderer._enableLighting = true;
16821681

src/webgl/p5.Framebuffer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ p5.Framebuffer = class Framebuffer {
624624
}
625625

626626
if (this.useDepth) {
627-
this.depth = new FramebufferTexture(this, 'depthTexture');
627+
this.depth = new p5.FramebufferTexture(this, 'depthTexture');
628628
const depthFilter = gl.NEAREST;
629629
this.depthP5Texture = new p5.Texture(
630630
this.target._renderer,
@@ -637,7 +637,7 @@ p5.Framebuffer = class Framebuffer {
637637
this.target._renderer.textures.set(this.depth, this.depthP5Texture);
638638
}
639639

640-
this.color = new FramebufferTexture(this, 'colorTexture');
640+
this.color = new p5.FramebufferTexture(this, 'colorTexture');
641641
const filter = this.textureFiltering === constants.LINEAR
642642
? gl.LINEAR
643643
: gl.NEAREST;
@@ -945,7 +945,7 @@ p5.Framebuffer = class Framebuffer {
945945
* </div>
946946
*/
947947
createCamera() {
948-
const cam = new FramebufferCamera(this);
948+
const cam = new p5.FramebufferCamera(this);
949949
cam._computeCameraDefaultSettings();
950950
cam._setDefaultCamera();
951951
this.target._renderer._curCamera = cam;

src/webgl/p5.Quat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ p5.Quat = class {
7171
* @param {p5.Vector} [p] vector to rotate on the axis quaternion
7272
*/
7373
rotateVector(p) {
74-
return new p5.Vector.mult( p, this.w*this.w - this.vec.dot(this.vec) )
74+
return p5.Vector.mult( p, this.w*this.w - this.vec.dot(this.vec) )
7575
.add( p5.Vector.mult( this.vec, 2 * p.dot(this.vec) ) )
7676
.add( p5.Vector.mult( this.vec, 2 * this.w ).cross( p ) )
7777
.clampToZero();

src/webgl/p5.RendererGL.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ p5.RendererGL = class RendererGL extends Renderer {
692692
throw new Error('It looks like `beginGeometry()` is being called while another p5.Geometry is already being build.');
693693
}
694694
this.geometryBuilder = new GeometryBuilder(this);
695+
this.geometryBuilder.prevFillColor = [...this.curFillColor];
696+
this.curFillColor = [-1, -1, -1, -1];
695697
}
696698

697699
/**
@@ -707,6 +709,7 @@ p5.RendererGL = class RendererGL extends Renderer {
707709
throw new Error('Make sure you call beginGeometry() before endGeometry()!');
708710
}
709711
const geometry = this.geometryBuilder.finish();
712+
this.curFillColor = this.geometryBuilder.prevFillColor;
710713
this.geometryBuilder = undefined;
711714
return geometry;
712715
}
@@ -793,14 +796,14 @@ p5.RendererGL = class RendererGL extends Renderer {
793796
}
794797
}
795798

796-
_getParam() {
799+
_getMaxTextureSize() {
797800
const gl = this.drawingContext;
798801
return gl.getParameter(gl.MAX_TEXTURE_SIZE);
799802
}
800803

801804
_adjustDimensions(width, height) {
802805
if (!this._maxTextureSize) {
803-
this._maxTextureSize = this._getParam();
806+
this._maxTextureSize = this._getMaxTextureSize();
804807
}
805808
let maxTextureSize = this._maxTextureSize;
806809
let maxAllowedPixelDimensions = p5.prototype._maxAllowedPixelDimensions;
@@ -1355,7 +1358,7 @@ p5.RendererGL = class RendererGL extends Renderer {
13551358
*/
13561359

13571360
loadPixels() {
1358-
const pixelsState = this._pInst;
1361+
const pixelsState = this._pixelsState;
13591362

13601363
//@todo_FES
13611364
if (this._pInst._glAttributes.preserveDrawingBuffer !== true) {
@@ -1387,7 +1390,7 @@ p5.RendererGL = class RendererGL extends Renderer {
13871390

13881391
updatePixels() {
13891392
const fbo = this._getTempFramebuffer();
1390-
fbo.pixels = this._pInst.pixels;
1393+
fbo.pixels = this._pixelsState.pixels;
13911394
fbo.updatePixels();
13921395
this._pInst.push();
13931396
this._pInst.resetMatrix();
@@ -1452,7 +1455,7 @@ p5.RendererGL = class RendererGL extends Renderer {
14521455
this._curCamera._resize();
14531456

14541457
//resize pixels buffer
1455-
const pixelsState = this._pInst;
1458+
const pixelsState = this._pixelsState;
14561459
if (typeof pixelsState.pixels !== 'undefined') {
14571460
pixelsState._setProperty(
14581461
'pixels',

src/webgl/shaders/light.vert

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ void main(void) {
3333
}
3434
}
3535

36-
vColor = (uUseVertexColor ? aVertexColor : uMaterialColor);
36+
vColor = ((uUseVertexColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor);
3737
}

src/webgl/shaders/normal.vert

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ void main(void) {
1919
gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
2020
vVertexNormal = normalize(vec3( uNormalMatrix * aNormal ));
2121
vVertTexCoord = aTexCoord;
22-
vColor = (uUseVertexColor ? aVertexColor : uMaterialColor);
22+
vColor = ((uUseVertexColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor);
2323
}

src/webgl/shaders/phong.vert

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ void main(void) {
4040
}
4141
}
4242

43-
vColor = (uUseVertexColor ? aVertexColor : uMaterialColor);
43+
vColor = ((uUseVertexColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor);
4444
}

0 commit comments

Comments
 (0)