Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[p5.js 2.0] State machines and renderer refactoring #7270

Draft
wants to merge 22 commits into
base: dev-2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6ef1afc
Concentrate DOM creation of renderer in the createCanvas method
limzykenneth Sep 20, 2024
2f7f824
Indentation
limzykenneth Sep 20, 2024
9c47a4c
Fix p5.Graphics creation
limzykenneth Sep 21, 2024
c39cfa6
p5.Graphics acts as wrapper of p5.Renderer
limzykenneth Sep 21, 2024
263ae57
Fix p5.Graphics.remove and simplify it
limzykenneth Sep 21, 2024
8427978
Fix webgl canvas creation
limzykenneth Sep 21, 2024
5c68e24
Minor adjustment to p5.Renderer and p5.Graphics remove
limzykenneth Sep 21, 2024
e3c3683
Make resizeCanvas() independent of DOM
limzykenneth Sep 22, 2024
f1d8735
Remove renderer createCanvas() method as it is redundant with constru…
limzykenneth Sep 22, 2024
7a91e53
Global width/height read directly from renderer
limzykenneth Sep 22, 2024
b07b438
Move ownership of pixel density to renderer
limzykenneth Sep 22, 2024
55c45ed
Fix a few tests
limzykenneth Sep 22, 2024
beb432f
Fix a few more tests
limzykenneth Sep 22, 2024
8a60cf3
Include p5 instance methods on p5.Graphics
limzykenneth Sep 23, 2024
40b621d
Refactor some WebGL sections to try to fix tests
limzykenneth Sep 23, 2024
418251b
Visual test use constants from direct import
limzykenneth Sep 23, 2024
eff9ac4
Refactor shape out into its own module
limzykenneth Sep 25, 2024
db6e599
Use new module API to attach methods to p5.Graphics
limzykenneth Sep 26, 2024
7cd3fb3
Attach more functions to p5.Graphics
limzykenneth Sep 27, 2024
104bd32
Move blendMode() to color/setting module
limzykenneth Sep 28, 2024
5c70636
Merge branch 'dev-2.0' into 2.0-modules
limzykenneth Oct 8, 2024
27326fe
Convert renderers to use new syntax modules
limzykenneth Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor some WebGL sections to try to fix tests
  • Loading branch information
limzykenneth committed Sep 23, 2024
commit 40b621d6e670e1267415091874caa0309452b4c2
2 changes: 1 addition & 1 deletion src/core/p5.Graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ p5.Graphics = class Graphics {
* </div>
*/
createFramebuffer(options) {
return new p5.Framebuffer(this, options);
return new p5.Framebuffer(this._pInst, options);
}
};

Expand Down
6 changes: 1 addition & 5 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ class Renderer2D extends Renderer {
getFilterGraphicsLayer() {
// create hidden webgl renderer if it doesn't exist
if (!this.filterGraphicsLayer) {
// the real _pInst is buried when this is a secondary p5.Graphics
const pInst =
this._pInst instanceof p5.Graphics ?
this._pInst._pInst :
this._pInst;
const pInst = this._pInst;

// create secondary layer
this.filterGraphicsLayer =
Expand Down
4 changes: 2 additions & 2 deletions src/webgl/3d_primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -3408,10 +3408,10 @@ p5.RendererGL.prototype.image = function(

this._pInst.push();

this._pInst.noLights();
this.noLights();
this._pInst.noStroke();

this._pInst.texture(img);
this.texture(img);
this._pInst.textureMode(constants.NORMAL);

let u0 = 0;
Expand Down
56 changes: 30 additions & 26 deletions src/webgl/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -1744,34 +1744,38 @@ p5.prototype.noLights = function (...args) {
this._assert3d('noLights');
p5._validateParameters('noLights', args);

this._renderer.states.activeImageLight = null;
this._renderer.states._enableLighting = false;

this._renderer.states.ambientLightColors.length = 0;
this._renderer.states.specularColors = [1, 1, 1];

this._renderer.states.directionalLightDirections.length = 0;
this._renderer.states.directionalLightDiffuseColors.length = 0;
this._renderer.states.directionalLightSpecularColors.length = 0;

this._renderer.states.pointLightPositions.length = 0;
this._renderer.states.pointLightDiffuseColors.length = 0;
this._renderer.states.pointLightSpecularColors.length = 0;

this._renderer.states.spotLightPositions.length = 0;
this._renderer.states.spotLightDirections.length = 0;
this._renderer.states.spotLightDiffuseColors.length = 0;
this._renderer.states.spotLightSpecularColors.length = 0;
this._renderer.states.spotLightAngle.length = 0;
this._renderer.states.spotLightConc.length = 0;

this._renderer.states.constantAttenuation = 1;
this._renderer.states.linearAttenuation = 0;
this._renderer.states.quadraticAttenuation = 0;
this._renderer.states._useShininess = 1;
this._renderer.states._useMetalness = 0;
this._renderer.noLights();

return this;
};

p5.RendererGL.prototype.noLights = function() {
this.states.activeImageLight = null;
this.states._enableLighting = false;

this.states.ambientLightColors.length = 0;
this.states.specularColors = [1, 1, 1];

this.states.directionalLightDirections.length = 0;
this.states.directionalLightDiffuseColors.length = 0;
this.states.directionalLightSpecularColors.length = 0;

this.states.pointLightPositions.length = 0;
this.states.pointLightDiffuseColors.length = 0;
this.states.pointLightSpecularColors.length = 0;

this.states.spotLightPositions.length = 0;
this.states.spotLightDirections.length = 0;
this.states.spotLightDiffuseColors.length = 0;
this.states.spotLightSpecularColors.length = 0;
this.states.spotLightAngle.length = 0;
this.states.spotLightConc.length = 0;

this.states.constantAttenuation = 1;
this.states.linearAttenuation = 0;
this.states.quadraticAttenuation = 0;
this.states._useShininess = 1;
this.states._useMetalness = 0;
}

export default p5;
12 changes: 8 additions & 4 deletions src/webgl/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -1892,14 +1892,18 @@ p5.prototype.texture = function (tex) {
tex._animateGif(this);
}

this._renderer.states.drawMode = constants.TEXTURE;
this._renderer.states._useNormalMaterial = false;
this._renderer.states._tex = tex;
this._renderer.states.doFill = true;
this._renderer.texture(tex);

return this;
};

p5.RendererGL.prototype.texture = function(tex) {
this.states.drawMode = constants.TEXTURE;
this.states._useNormalMaterial = false;
this.states._tex = tex;
this.states.doFill = true;
}

/**
* Changes the coordinate system used for textures when they’re applied to
* custom shapes.
Expand Down
28 changes: 21 additions & 7 deletions src/webgl/p5.RendererGL.js
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davepagurek Probably shouldn't update right away but just to let you know in advanced. The refactor for Renderer2D moves all DOM related operation and references to be handled by the renderer's createCanvas method along with reshuffling of the initialization logic around default canvas and others. As such WebGL renderer will also need updating accordingly.

I'm working on getting p5.Graphics to work first before I do a round of clean up refactoring.

Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ p5.RendererGL = class RendererGL extends Renderer {

// Create new canvas
this.canvas = this.elt = elt || document.createElement('canvas');
this._initContext();
// This redundant property is useful in reminding you that you are
// interacting with WebGLRenderingContext, still worth considering future removal
this.GL = this.drawingContext;
this._pInst.drawingContext = this.drawingContext;

if (this._isMainCanvas) {
// for pixel method sharing with pimage
Expand All @@ -450,11 +455,26 @@ p5.RendererGL = class RendererGL extends Renderer {
this.elt.id = 'defaultCanvas0';
this.elt.classList.add('p5Canvas');

const dimensions = this._adjustDimensions(w, h);
w = dimensions.adjustedWidth;
h = dimensions.adjustedHeight;

this.width = w;
this.height = h;

// Set canvas size
this.elt.width = w * this._pixelDensity;
this.elt.height = h * this._pixelDensity;
this.elt.style.width = `${w}px`;
this.elt.style.height = `${h}px`;
this._origViewport = {
width: this.GL.drawingBufferWidth,
height: this.GL.drawingBufferHeight
};
this.viewport(
this._origViewport.width,
this._origViewport.height
);

// Attach canvas element to DOM
if (this._pInst._userNode) {
Expand All @@ -471,17 +491,11 @@ p5.RendererGL = class RendererGL extends Renderer {
}

this._setAttributeDefaults(pInst);
this._initContext();
this.isP3D = true; //lets us know we're in 3d mode

// When constructing a new p5.Geometry, this will represent the builder
this.geometryBuilder = undefined;

// This redundant property is useful in reminding you that you are
// interacting with WebGLRenderingContext, still worth considering future removal
this.GL = this.drawingContext;
this._pInst.drawingContext = this.drawingContext;

// Push/pop state
this.states.uModelMatrix = new p5.Matrix();
this.states.uViewMatrix = new p5.Matrix();
Expand Down Expand Up @@ -786,7 +800,7 @@ p5.RendererGL = class RendererGL extends Renderer {
}

_initContext() {
if (this._pInst._glAttributes.version !== 1) {
if (this._pInst._glAttributes?.version !== 1) {
// Unless WebGL1 is explicitly asked for, try to create a WebGL2 context
this.drawingContext =
this.canvas.getContext('webgl2', this._pInst._glAttributes);
Expand Down
Loading