-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Support for 3D coordinate on Image #7172
base: main
Are you sure you want to change the base?
Changes from 13 commits
4326a02
97efe53
c2d6142
168e0e3
aab2120
7942b73
2a80879
96997fd
ca19e53
1c76a8d
6e31086
247e855
8d81b73
7ae6f8b
ab32870
ed1796a
e764cb1
038a5ad
71027b0
07beef9
1b53827
af0b2e8
9e19785
80844dc
15d0015
f0f57cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1070,6 +1070,108 @@ function _sAssign(sVal, iVal) { | |
* } | ||
* </code> | ||
* </div> | ||
* <div> | ||
* <code> | ||
* let img; | ||
* | ||
* function preload() { | ||
* img = loadImage('assets/moonwalk.jpg'); | ||
* } | ||
* | ||
* function setup() { | ||
* // Create a 3D canvas | ||
* background(200); | ||
* createCanvas(400, 400, WEBGL); | ||
* } | ||
* function draw() { | ||
* image(img, 0, 0, -100); | ||
* describe('An image at the center 100 units away from the camera'); | ||
* } | ||
* </code> | ||
* </div> | ||
* | ||
* <div> | ||
* <code> | ||
* let img; | ||
* function preload() { | ||
* img = loadImage('assets/moonwalk.jpg'); | ||
* } | ||
* | ||
* function setup() { | ||
* // Create a 3D canvas | ||
* createCanvas(400, 400, WEBGL); | ||
* } | ||
* | ||
* function draw() { | ||
* background(200); | ||
* image(img, 0, 0, 400 , 300 , 300); | ||
* describe('Scale image 300 by 300 and zoomin 400 units'); | ||
* } | ||
* </code> | ||
* </div> | ||
* | ||
* <div> | ||
* <code> | ||
* let img; | ||
* function preload() { | ||
* img = loadImage('assets/moonwalk.jpg'); | ||
* } | ||
* | ||
* function setup() { | ||
* // Create a 3D canvas | ||
* createCanvas(400, 400, WEBGL); | ||
* } | ||
* | ||
* function draw() { | ||
* background(200); | ||
* image(img, 0, 0, -400 , 300 , 300); | ||
* describe('Scale image 300 by 300 and zoomout 400 units'); | ||
* } | ||
* </code> | ||
* </div> | ||
* | ||
* <div> | ||
* <code> | ||
* let img; | ||
* function preload() { | ||
* img = loadImage('assets/moonwalk.jpg'); | ||
* } | ||
* | ||
* function setup() { | ||
* // Create a 3D canvas | ||
* createCanvas(400, 400, WEBGL); | ||
* } | ||
* | ||
* function draw() { | ||
* background(200); | ||
* image(img, 0, 0, 0, 400, 400,500, 100, 200, 200); | ||
* describe('Draw a subsection of the image from 500 by 100 units position at the center with size 400x400'); | ||
* } | ||
* </code> | ||
* </div> | ||
* | ||
* <div> | ||
* <code> | ||
* let img; | ||
* function preload() { | ||
* img = loadImage('assets/moonwalk.jpg'); | ||
* } | ||
* | ||
* function setup() { | ||
* // Create a 3D canvas | ||
* createCanvas(400, 400, WEBGL); | ||
* } | ||
* | ||
* function draw() { | ||
* background(200); | ||
* image(img, 0, 0, -200, 400, 400,500, 100, 200, 200); | ||
* describe('Draw a subsection of the image from 500 by 100 units position at the center with size 400x400 and zoomedout by 200 units'); | ||
* } | ||
* | ||
* </code> | ||
* </div> | ||
* | ||
* | ||
*/ | ||
/** | ||
* @method image | ||
|
@@ -1078,6 +1180,8 @@ function _sAssign(sVal, iVal) { | |
* rectangle in which to draw the source image | ||
* @param {Number} dy the y-coordinate of the destination | ||
* rectangle in which to draw the source image | ||
* @param {Number} dz the z-coordinate (depth) of the destination | ||
* rectangle in which to draw the source image (WEBGL only) | ||
* @param {Number} dWidth the width of the destination rectangle | ||
* @param {Number} dHeight the height of the destination rectangle | ||
* @param {Number} sx the x-coordinate of the subsection of the source | ||
|
@@ -1097,6 +1201,7 @@ p5.prototype.image = function( | |
img, | ||
dx, | ||
dy, | ||
dz, | ||
dWidth, | ||
dHeight, | ||
sx, | ||
|
@@ -1110,6 +1215,23 @@ p5.prototype.image = function( | |
// set defaults per spec: https://goo.gl/3ykfOq | ||
|
||
p5._validateParameters('image', arguments); | ||
// check for P5 Graphics instance | ||
let isP5G = img instanceof p5.Graphics ? true : false; | ||
// check for P5 Framebuffer instance | ||
let isP5Fbo = img instanceof p5.Framebuffer ? true : false; | ||
|
||
if(this._renderer instanceof p5.RendererGL === false){ | ||
// From the 3rd arguement shift the assingment one position to the right | ||
yAlign = xAlign; | ||
xAlign = fit; | ||
fit = sHeight; | ||
sHeight = sWidth; | ||
sWidth = sy; | ||
sy = sx; | ||
sx = dHeight; | ||
dHeight = dWidth; | ||
dWidth = dz; | ||
} | ||
|
||
let defW = img.width; | ||
let defH = img.height; | ||
|
@@ -1175,18 +1297,37 @@ p5.prototype.image = function( | |
_sh | ||
); | ||
|
||
// tint the image if there is a tint | ||
this._renderer.image( | ||
img, | ||
vals.sx, | ||
vals.sy, | ||
vals.sw, | ||
vals.sh, | ||
vals.dx, | ||
vals.dy, | ||
vals.dw, | ||
vals.dh | ||
); | ||
//if it is not graphics nor framebuffer but WEGL instance | ||
//default to use 3D rendering | ||
if (this._renderer instanceof p5.RendererGL && !isP5G && !isP5Fbo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like maybe this check should be a check for whether the |
||
if (dz === undefined){dz = 0;} | ||
this._renderer.image3D( | ||
img, | ||
vals.sx, | ||
vals.sy, | ||
dz, | ||
vals.sw, | ||
vals.sh, | ||
vals.dx, | ||
vals.dy, | ||
vals.dw, | ||
vals.dh | ||
); | ||
}else { | ||
// tint the image if there is a tint | ||
// Default 2D rendering | ||
this._renderer.image( | ||
img, | ||
vals.sx, | ||
vals.sy, | ||
vals.sw, | ||
vals.sh, | ||
vals.dx, | ||
vals.dy, | ||
vals.dw, | ||
vals.dh | ||
); | ||
} | ||
}; | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2391,6 +2391,44 @@ p5.RendererGL = class RendererGL extends p5.Renderer { | |
|
||
return triangleVerts; | ||
} | ||
|
||
image3D(img,sx,sy,sz,sWidth,sHeight,dx,dy,dWidth,dHeight) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shares a lot of code with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code uses |
||
const viewport = this.GL.getParameter(this.GL.VIEWPORT); | ||
const width = viewport[2]; | ||
const height = viewport[3]; | ||
dx = (-width / 2) + dx; | ||
dy = (-height / 2) + dy; | ||
this._pInst.push(); | ||
this._pInst.noLights(); | ||
this._pInst.noStroke(); | ||
this._pInst.texture(img); | ||
this._pInst.textureMode(constants.NORMAL); | ||
|
||
// Calculate texture coordinates for subsection | ||
let u0 = sx / img.width; | ||
let u1 = (sx + sWidth) / img.width; | ||
let v0 = sy / img.height; | ||
let v1 = (sy + sHeight) / img.height; | ||
|
||
// Draw a textured rectangle (plane) with the texture coordinates | ||
this.beginShape(); | ||
// Top-left corner | ||
this.vertex(dx, dy, sz, u0, v0); | ||
// Top-right corner | ||
this.vertex(dx + dWidth, dy, sz, u1, v0); | ||
// Bottom-right corner | ||
this.vertex(dx + dWidth, dy + dHeight, sz, u1, u1); | ||
// Bottom-left corner | ||
this.vertex(dx, dy + dHeight, sz, u0, v1); | ||
this.endShape(constants.CLOSE); | ||
|
||
this._pInst.pop(); | ||
|
||
if (this._isErasing) { | ||
this.blendMode(constants.REMOVE); | ||
} | ||
} | ||
|
||
}; | ||
/** | ||
* ensures that p5 is using a 3d renderer. throws an error if not. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think placing this argument here will break calls to
image()
that don't usez
but do use the arguments that follow it.I think we might need to follow the approach of some other 2D/3D functions like
dist
here, where we reinterpret the arguments array based on some criteria:p5.js/src/math/calculation.js
Lines 221 to 227 in 21d7c8c
In this case, the criteria will probably depend on the length of the arguments.
Since this is somewhat complex, I think it would also be a good idea to add some unit tests to make sure that this continues to work for the 2D case as well as the new 3D case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review. I went with the length approach initially .
Odd number of parameters → Use 2D mode.
Even number of parameters → Check if the 10th parameter is a number.
If it's not a number, defer to 2D mode.
If it's a number, use 3D mode.
but starting from the third parameter single arguments become optional
image(img, dx, dy, dWidth, dHeight, sx, sy, [sWidth], [sHeight], [fit], [xAlign], [yAlign])
hence my even/odd length logic fails as soon as the function starts receiving optional arguments.The best solution I got at the moment was, to shift the values of the input argument to the right in case of 2D_rendering.
The results have been successful in every test .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this also work for 3D mode if the z coordinate is omitted?
Also a possible other approach for the even/odd check: you might need to check if the number of arguments up to the
fit
parameter (which should be a string instead of a number) is even or odd. That might mean something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are in WebGL mode and call
image
withoutz
but with some of the options afterz
, does that still work? e.g. this should draw the image fullscreen on the canvas:Just based on the code so far, it looks like in WebGL mode, it never shifts all the arguments over the way it does when
this._renderer instanceof p5.RendererGL === false
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the
ImgeMode(CENTER)
is a trap on WebGL mode. Is there any way to workaround this ? we could add a newimageModeGL
that supportsWebGL
coordinates ?