1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 18:50:54 +00:00

Fix for incorrect mask position, when grating vertices centered around (0, 0);

This commit is contained in:
lgtst 2022-09-20 02:01:03 +01:00
parent f483f6de1c
commit 25aef3d500

View File

@ -279,6 +279,7 @@ export class GratingStim extends VisualStim
* @param {number} [options.sf=1.0] - spatial frequency of the function used in grating stimulus * @param {number} [options.sf=1.0] - spatial frequency of the function used in grating stimulus
* @param {number} [options.phase=0.0] - phase of the function used in grating stimulus, multiples of period of that function * @param {number} [options.phase=0.0] - phase of the function used in grating stimulus, multiples of period of that function
* @param {Array.<number>} [options.pos= [0, 0]] - the position of the center of the stimulus * @param {Array.<number>} [options.pos= [0, 0]] - the position of the center of the stimulus
* @param {string} [options.anchor = "center"] - sets the origin point of the stim
* @param {number} [options.ori= 0.0] - the orientation (in degrees) * @param {number} [options.ori= 0.0] - the orientation (in degrees)
* @param {number} [options.size] - the size of the rendered image (DEFAULT_STIM_SIZE_PX will be used if size is not specified) * @param {number} [options.size] - the size of the rendered image (DEFAULT_STIM_SIZE_PX will be used if size is not specified)
* @param {Color} [options.color= "white"] - Foreground color of the stimulus. Can be String like "red" or "#ff0000" or Number like 0xff0000. * @param {Color} [options.color= "white"] - Foreground color of the stimulus. Can be String like "red" or "#ff0000" or Number like 0xff0000.
@ -296,6 +297,7 @@ export class GratingStim extends VisualStim
win, win,
mask, mask,
pos, pos,
anchor,
units, units,
sf = 1.0, sf = 1.0,
ori, ori,
@ -313,7 +315,7 @@ export class GratingStim extends VisualStim
maskParams maskParams
} = {}) } = {})
{ {
super({ name, win, units, ori, opacity, depth, pos, size, autoDraw, autoLog }); super({ name, win, units, ori, opacity, depth, pos, anchor, size, autoDraw, autoLog });
this._adjustmentFilter = new AdjustmentFilter({ this._adjustmentFilter = new AdjustmentFilter({
contrast contrast
@ -741,9 +743,13 @@ export class GratingStim extends VisualStim
} }
else else
{ {
// for some reason setting PIXI.Mesh as .mask doesn't do anything, // For some reason setting PIXI.Mesh as .mask doesn't do anything,
// rendering mask to texture for further use. // rendering mask to texture for further use.
const maskMesh = this._getPixiMeshFromPredefinedShaders(this._mask); const maskMesh = this._getPixiMeshFromPredefinedShaders(this._mask);
// Since mesh is centered around [0, 0] (has vertices going around it),
// offsetting maskMesh position to properly cover render target texture.
maskMesh.position.set(this._size_px[0] * 0.5, this._size_px[1] * 0.5);
const rt = PIXI.RenderTexture.create({ const rt = PIXI.RenderTexture.create({
width: this._size_px[0], width: this._size_px[0],
height: this._size_px[1], height: this._size_px[1],
@ -756,6 +762,8 @@ export class GratingStim extends VisualStim
this._pixi.mask = maskSprite; this._pixi.mask = maskSprite;
this._pixi.addChild(maskSprite); this._pixi.addChild(maskSprite);
} }
// Since mesh is centered around [0, 0], setting mask's anchor to center to properly cover target image.
this._pixi.mask.anchor.set(0.5);
} }
// since _pixi.width may not be immediately available but the rest of the code needs its value // since _pixi.width may not be immediately available but the rest of the code needs its value