mirror of
https://github.com/psychopy/psychojs.git
synced 2025-05-10 10:40:54 +00:00
832 lines
32 KiB
HTML
832 lines
32 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: visual/GratingStim.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: visual/GratingStim.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>/**
|
|
* Grating Stimulus.
|
|
*
|
|
* @author Alain Pitiot, Nikita Agafonov
|
|
* @version 2021.2.0
|
|
* @copyright (c) 2017-2020 Ilixa Ltd. (http://ilixa.com) (c) 2020-2022 Open Science Tools Ltd. (https://opensciencetools.org)
|
|
* @license Distributed under the terms of the MIT License
|
|
*/
|
|
|
|
import * as PIXI from "pixi.js-legacy";
|
|
import {AdjustmentFilter} from "@pixi/filter-adjustment";
|
|
import { Color } from "../util/Color.js";
|
|
import { to_pixiPoint } from "../util/Pixi.js";
|
|
import * as util from "../util/Util.js";
|
|
import { VisualStim } from "./VisualStim.js";
|
|
import defaultQuadVert from "./shaders/defaultQuad.vert";
|
|
import imageShader from "./shaders/imageShader.frag";
|
|
import sinShader from "./shaders/sinShader.frag";
|
|
import sqrShader from "./shaders/sqrShader.frag";
|
|
import sawShader from "./shaders/sawShader.frag";
|
|
import triShader from "./shaders/triShader.frag";
|
|
import sinXsinShader from "./shaders/sinXsinShader.frag";
|
|
import sqrXsqrShader from "./shaders/sqrXsqrShader.frag";
|
|
import circleShader from "./shaders/circleShader.frag";
|
|
import gaussShader from "./shaders/gaussShader.frag";
|
|
import crossShader from "./shaders/crossShader.frag";
|
|
import radRampShader from "./shaders/radRampShader.frag";
|
|
import raisedCosShader from "./shaders/raisedCosShader.frag";
|
|
|
|
/**
|
|
* Grating Stimulus.
|
|
*
|
|
* @name module:visual.GratingStim
|
|
* @class
|
|
* @extends VisualStim
|
|
* @param {Object} options
|
|
* @param {String} options.name - the name used when logging messages from this stimulus
|
|
* @param {Window} options.win - the associated Window
|
|
* @param {String | HTMLImageElement} [options.tex="sin"] - the name of the predefined grating texture or image resource or the HTMLImageElement corresponding to the texture
|
|
* @param {String | HTMLImageElement} [options.mask] - the name of the mask resource or HTMLImageElement corresponding to the mask
|
|
* @param {String} [options.units= "norm"] - the units of the stimulus (e.g. for size, position, vertices)
|
|
* @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 {Array.<number>} [options.pos= [0, 0]] - the position of the center of the stimulus
|
|
* @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 {Color} [options.color= "white"] - Foreground color of the stimulus. Can be String like "red" or "#ff0000" or Number like 0xff0000.
|
|
* @param {number} [options.opacity= 1.0] - Set the opacity of the stimulus. Determines how visible the stimulus is relative to background.
|
|
* @param {number} [options.contrast= 1.0] - Set the contrast of the stimulus, i.e. scales how far the stimulus deviates from the middle grey. Ranges [-1, 1].
|
|
* @param {number} [options.depth= 0] - the depth (i.e. the z order)
|
|
* @param {boolean} [options.interpolate= false] - Whether to interpolate (linearly) the texture in the stimulus. Currently supports only image based gratings.
|
|
* @param {String} [options.blendmode= "avg"] - blend mode of the stimulus, determines how the stimulus is blended with the background. Supported values: "avg", "add", "mul", "screen".
|
|
* @param {boolean} [options.autoDraw= false] - whether or not the stimulus should be automatically drawn on every frame flip
|
|
* @param {boolean} [options.autoLog= false] - whether or not to log
|
|
*/
|
|
|
|
export class GratingStim extends VisualStim
|
|
{
|
|
/**
|
|
* An object that keeps shaders source code and default uniform values for them.
|
|
* Shader source code is later used for construction of shader programs to create respective visual stimuli.
|
|
* @name module:visual.GratingStim.#SHADERS
|
|
* @type {Object}
|
|
*
|
|
* @property {Object} imageShader - Renders provided image with applied effects (coloring, phase, frequency).
|
|
* @property {String} imageShader.shader - shader source code for the image based grating stimuli.
|
|
* @property {Object} imageShader.uniforms - default uniforms for the image based shader.
|
|
* @property {float} imageShader.uniforms.uFreq=1.0 - how much times image repeated within grating stimuli.
|
|
* @property {float} imageShader.uniforms.uPhase=0.0 - offset of the image along X axis.
|
|
* @property {float} imageShader.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} sin - Creates 2d sine wave image as if 1d sine graph was extended across Z axis and observed from above.
|
|
* {@link https://en.wikipedia.org/wiki/Sine_wave}
|
|
* @property {String} sin.shader - shader source code for the sine wave stimuli
|
|
* @property {Object} sin.uniforms - default uniforms for sine wave shader
|
|
* @property {float} sin.uniforms.uFreq=1.0 - frequency of sine wave.
|
|
* @property {float} sin.uniforms.uPhase=0.0 - phase of sine wave.
|
|
* @property {float} sin.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} sqr - Creates 2d square wave image as if 1d square graph was extended across Z axis and observed from above.
|
|
* {@link https://en.wikipedia.org/wiki/Square_wave}
|
|
* @property {String} sqr.shader - shader source code for the square wave stimuli
|
|
* @property {Object} sqr.uniforms - default uniforms for square wave shader
|
|
* @property {float} sqr.uniforms.uFreq=1.0 - frequency of square wave.
|
|
* @property {float} sqr.uniforms.uPhase=0.0 - phase of square wave.
|
|
* @property {float} sqr.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} saw - Creates 2d sawtooth wave image as if 1d sawtooth graph was extended across Z axis and observed from above.
|
|
* {@link https://en.wikipedia.org/wiki/Sawtooth_wave}
|
|
* @property {String} saw.shader - shader source code for the sawtooth wave stimuli
|
|
* @property {Object} saw.uniforms - default uniforms for sawtooth wave shader
|
|
* @property {float} saw.uniforms.uFreq=1.0 - frequency of sawtooth wave.
|
|
* @property {float} saw.uniforms.uPhase=0.0 - phase of sawtooth wave.
|
|
* @property {float} saw.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} tri - Creates 2d triangle wave image as if 1d triangle graph was extended across Z axis and observed from above.
|
|
* {@link https://en.wikipedia.org/wiki/Triangle_wave}
|
|
* @property {String} tri.shader - shader source code for the triangle wave stimuli
|
|
* @property {Object} tri.uniforms - default uniforms for triangle wave shader
|
|
* @property {float} tri.uniforms.uFreq=1.0 - frequency of triangle wave.
|
|
* @property {float} tri.uniforms.uPhase=0.0 - phase of triangle wave.
|
|
* @property {float} tri.uniforms.uPeriod=1.0 - period of triangle wave.
|
|
* @property {float} tri.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} sinXsin - Creates an image of two 2d sine waves multiplied with each other.
|
|
* {@link https://en.wikipedia.org/wiki/Sine_wave}
|
|
* @property {String} sinXsin.shader - shader source code for the two multiplied sine waves stimuli
|
|
* @property {Object} sinXsin.uniforms - default uniforms for shader
|
|
* @property {float} sinXsin.uniforms.uFreq=1.0 - frequency of sine wave (both of them).
|
|
* @property {float} sinXsin.uniforms.uPhase=0.0 - phase of sine wave (both of them).
|
|
* @property {float} sinXsin.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} sqrXsqr - Creates an image of two 2d square waves multiplied with each other.
|
|
* {@link https://en.wikipedia.org/wiki/Square_wave}
|
|
* @property {String} sqrXsqr.shader - shader source code for the two multiplied sine waves stimuli
|
|
* @property {Object} sqrXsqr.uniforms - default uniforms for shader
|
|
* @property {float} sqrXsqr.uniforms.uFreq=1.0 - frequency of sine wave (both of them).
|
|
* @property {float} sqrXsqr.uniforms.uPhase=0.0 - phase of sine wave (both of them).
|
|
* @property {float} sqrXsqr.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} circle - Creates a filled circle shape with sharp edges.
|
|
* @property {String} circle.shader - shader source code for filled circle.
|
|
* @property {Object} circle.uniforms - default uniforms for shader.
|
|
* @property {float} circle.uniforms.uRadius=1.0 - Radius of the circle. Ranges [0.0, 1.0], where 0.0 is circle so tiny it results in empty stim
|
|
* and 1.0 is circle that spans from edge to edge of the stim.
|
|
* @property {float} circle.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} gauss - Creates a 2d Gaussian image as if 1d Gaussian graph was rotated arount Y axis and observed from above.
|
|
* {@link https://en.wikipedia.org/wiki/Gaussian_function}
|
|
* @property {String} gauss.shader - shader source code for Gaussian shader
|
|
* @property {Object} gauss.uniforms - default uniforms for shader
|
|
* @property {float} gauss.uniforms.uA=1.0 - A constant for gaussian formula (see link).
|
|
* @property {float} gauss.uniforms.uB=0.0 - B constant for gaussian formula (see link).
|
|
* @property {float} gauss.uniforms.uC=0.16 - C constant for gaussian formula (see link).
|
|
* @property {float} gauss.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} cross - Creates a filled cross shape with sharp edges.
|
|
* @property {String} cross.shader - shader source code for cross shader
|
|
* @property {Object} cross.uniforms - default uniforms for shader
|
|
* @property {float} cross.uniforms.uThickness=0.2 - Thickness of the cross. Ranges [0.0, 1.0], where 0.0 thickness makes a cross so thin it becomes
|
|
* invisible and results in an empty stim and 1.0 makes it so thick it fills the entire stim.
|
|
* @property {float} cross.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} radRamp - Creates 2d radial ramp image.
|
|
* @property {String} radRamp.shader - shader source code for radial ramp shader
|
|
* @property {Object} radRamp.uniforms - default uniforms for shader
|
|
* @property {float} radRamp.uniforms.uSqueeze=1.0 - coefficient that helps to modify size of the ramp. Ranges [0.0, Infinity], where 0.0 results in ramp being so large
|
|
* it fills the entire stim and Infinity makes it so tiny it's invisible.
|
|
* @property {float} radRamp.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*
|
|
* @property {Object} raisedCos - Creates 2d raised-cosine image as if 1d raised-cosine graph was rotated around Y axis and observed from above.
|
|
* {@link https://en.wikipedia.org/wiki/Raised-cosine_filter}
|
|
* @property {String} raisedCos.shader - shader source code for raised-cosine shader
|
|
* @property {Object} raisedCos.uniforms - default uniforms for shader
|
|
* @property {float} raisedCos.uniforms.uBeta=0.25 - roll-off factor (see link).
|
|
* @property {float} raisedCos.uniforms.uPeriod=0.625 - reciprocal of the symbol-rate (see link).
|
|
* @property {float} raisedCos.uniforms.uAlpha=1.0 - value of the alpha channel.
|
|
*/
|
|
static #SHADERS = {
|
|
imageShader: {
|
|
shader: imageShader,
|
|
uniforms: {
|
|
uFreq: 1.0,
|
|
uPhase: 0.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
sin: {
|
|
shader: sinShader,
|
|
uniforms: {
|
|
uFreq: 1.0,
|
|
uPhase: 0.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
sqr: {
|
|
shader: sqrShader,
|
|
uniforms: {
|
|
uFreq: 1.0,
|
|
uPhase: 0.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
saw: {
|
|
shader: sawShader,
|
|
uniforms: {
|
|
uFreq: 1.0,
|
|
uPhase: 0.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
tri: {
|
|
shader: triShader,
|
|
uniforms: {
|
|
uFreq: 1.0,
|
|
uPhase: 0.0,
|
|
uPeriod: 1.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
sinXsin: {
|
|
shader: sinXsinShader,
|
|
uniforms: {
|
|
uFreq: 1.0,
|
|
uPhase: 0.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
sqrXsqr: {
|
|
shader: sqrXsqrShader,
|
|
uniforms: {
|
|
uFreq: 1.0,
|
|
uPhase: 0.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
circle: {
|
|
shader: circleShader,
|
|
uniforms: {
|
|
uRadius: 1.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
gauss: {
|
|
shader: gaussShader,
|
|
uniforms: {
|
|
uA: 1.0,
|
|
uB: 0.0,
|
|
uC: 0.16,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
cross: {
|
|
shader: crossShader,
|
|
uniforms: {
|
|
uThickness: 0.2,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
radRamp: {
|
|
shader: radRampShader,
|
|
uniforms: {
|
|
uSqueeze: 1.0,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
},
|
|
raisedCos: {
|
|
shader: raisedCosShader,
|
|
uniforms: {
|
|
uBeta: 0.25,
|
|
uPeriod: 0.625,
|
|
uColor: [1., 1., 1.],
|
|
uAlpha: 1.0
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Default size of the Grating Stimuli in pixels.
|
|
* @name module:visual.GratingStim.#DEFAULT_STIM_SIZE_PX
|
|
* @type {Array}
|
|
* @default [256, 256]
|
|
*/
|
|
static #DEFAULT_STIM_SIZE_PX = [256, 256]; // in pixels
|
|
|
|
static #BLEND_MODES_MAP = {
|
|
avg: PIXI.BLEND_MODES.NORMAL,
|
|
add: PIXI.BLEND_MODES.ADD,
|
|
mul: PIXI.BLEND_MODES.MULTIPLY,
|
|
screen: PIXI.BLEND_MODES.SCREEN
|
|
};
|
|
|
|
constructor({
|
|
name,
|
|
tex = "sin",
|
|
win,
|
|
mask,
|
|
pos,
|
|
units,
|
|
sf = 1.0,
|
|
ori,
|
|
phase,
|
|
size,
|
|
color,
|
|
colorSpace,
|
|
opacity,
|
|
contrast = 1,
|
|
depth,
|
|
interpolate,
|
|
blendmode,
|
|
autoDraw,
|
|
autoLog,
|
|
maskParams
|
|
} = {})
|
|
{
|
|
super({ name, win, units, ori, opacity, depth, pos, size, autoDraw, autoLog });
|
|
|
|
this._adjustmentFilter = new AdjustmentFilter({
|
|
contrast
|
|
});
|
|
this._addAttribute("tex", tex);
|
|
this._addAttribute("mask", mask);
|
|
this._addAttribute("SF", sf, GratingStim.#SHADERS[tex] ? GratingStim.#SHADERS[tex].uniforms.uFreq || 1.0 : 1.0);
|
|
this._addAttribute("phase", phase, GratingStim.#SHADERS[tex] ? GratingStim.#SHADERS[tex].uniforms.uPhase || 0.0 : 0.0);
|
|
this._addAttribute("color", color, "white");
|
|
this._addAttribute("colorSpace", colorSpace, "RGB");
|
|
this._addAttribute("contrast", contrast, 1.0, () => {
|
|
this._adjustmentFilter.contrast = this._contrast;
|
|
});
|
|
this._addAttribute("blendmode", blendmode, "avg");
|
|
this._addAttribute("interpolate", interpolate, false);
|
|
|
|
// estimate the bounding box:
|
|
this._estimateBoundingBox();
|
|
|
|
if (this._autoLog)
|
|
{
|
|
this._psychoJS.experimentLogger.exp(`Created ${this.name} = ${this.toString()}`);
|
|
}
|
|
|
|
if (!Array.isArray(this.size) || this.size.length === 0) {
|
|
this.size = util.to_unit(GratingStim.#DEFAULT_STIM_SIZE_PX, "pix", this.win, this.units);
|
|
}
|
|
this._size_px = util.to_px(this.size, this.units, this.win);
|
|
}
|
|
|
|
/**
|
|
* Setter for the tex attribute.
|
|
*
|
|
* @name module:visual.GratingStim#setTex
|
|
* @public
|
|
* @param {HTMLImageElement | string} tex - the name of built in shader function or name of the image resource or HTMLImageElement corresponding to the image
|
|
* @param {boolean} [log= false] - whether of not to log
|
|
*/
|
|
setTex(tex, log = false)
|
|
{
|
|
const response = {
|
|
origin: "GratingStim.setTex",
|
|
context: "when setting the tex of GratingStim: " + this._name,
|
|
};
|
|
|
|
try
|
|
{
|
|
let hasChanged = false;
|
|
|
|
// tex is undefined: that's fine but we raise a warning in case this is a symptom of an actual problem
|
|
if (typeof tex === "undefined")
|
|
{
|
|
this.psychoJS.logger.warn("setting the tex of GratingStim: " + this._name + " with argument: undefined.");
|
|
this.psychoJS.logger.debug("set the tex of GratingStim: " + this._name + " as: undefined");
|
|
}
|
|
else if (GratingStim.#SHADERS[tex] !== undefined)
|
|
{
|
|
// tex is a string and it is one of predefined functions available in shaders
|
|
this.psychoJS.logger.debug("the tex is one of predefined functions. Set the tex of GratingStim: " + this._name + " as: " + tex);
|
|
const curFuncName = this.getTex();
|
|
hasChanged = curFuncName ? curFuncName !== tex : true;
|
|
}
|
|
else
|
|
{
|
|
// tex is a string: it should be the name of a resource, which we load
|
|
if (typeof tex === "string")
|
|
{
|
|
tex = this.psychoJS.serverManager.getResource(tex);
|
|
}
|
|
|
|
// tex should now be an actual HTMLImageElement: we raise an error if it is not
|
|
if (!(tex instanceof HTMLImageElement))
|
|
{
|
|
throw "the argument: " + tex.toString() + " is not an image\" }";
|
|
}
|
|
|
|
this.psychoJS.logger.debug("set the tex of GratingStim: " + this._name + " as: src= " + tex.src + ", size= " + tex.width + "x" + tex.height);
|
|
const existingImage = this.getTex();
|
|
hasChanged = existingImage ? existingImage.src !== tex.src : true;
|
|
}
|
|
|
|
this._setAttribute("tex", tex, log);
|
|
|
|
if (hasChanged)
|
|
{
|
|
this._onChange(true, true)();
|
|
}
|
|
}
|
|
catch (error)
|
|
{
|
|
throw Object.assign(response, { error });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Setter for the mask attribute.
|
|
*
|
|
* @name module:visual.GratingStim#setMask
|
|
* @public
|
|
* @param {HTMLImageElement | string} mask - the name of the mask resource or HTMLImageElement corresponding to the mask
|
|
* @param {boolean} [log= false] - whether of not to log
|
|
*/
|
|
setMask(mask, log = false)
|
|
{
|
|
const response = {
|
|
origin: "GratingStim.setMask",
|
|
context: "when setting the mask of GratingStim: " + this._name,
|
|
};
|
|
|
|
try
|
|
{
|
|
// mask is undefined: that's fine but we raise a warning in case this is a sympton of an actual problem
|
|
if (typeof mask === "undefined")
|
|
{
|
|
this.psychoJS.logger.warn("setting the mask of GratingStim: " + this._name + " with argument: undefined.");
|
|
this.psychoJS.logger.debug("set the mask of GratingStim: " + this._name + " as: undefined");
|
|
}
|
|
else if (GratingStim.#SHADERS[mask] !== undefined)
|
|
{
|
|
// mask is a string and it is one of predefined functions available in shaders
|
|
this.psychoJS.logger.debug("the mask is one of predefined functions. Set the mask of GratingStim: " + this._name + " as: " + mask);
|
|
}
|
|
else
|
|
{
|
|
// mask is a string: it should be the name of a resource, which we load
|
|
if (typeof mask === "string")
|
|
{
|
|
mask = this.psychoJS.serverManager.getResource(mask);
|
|
}
|
|
|
|
// mask should now be an actual HTMLImageElement: we raise an error if it is not
|
|
if (!(mask instanceof HTMLImageElement))
|
|
{
|
|
throw "the argument: " + mask.toString() + " is not an image\" }";
|
|
}
|
|
|
|
this.psychoJS.logger.debug("set the mask of GratingStim: " + this._name + " as: src= " + mask.src + ", size= " + mask.width + "x" + mask.height);
|
|
}
|
|
|
|
this._setAttribute("mask", mask, log);
|
|
|
|
this._onChange(true, false)();
|
|
}
|
|
catch (error)
|
|
{
|
|
throw Object.assign(response, { error });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the size of the display image, which is either that of the GratingStim or that of the image
|
|
* it contains.
|
|
*
|
|
* @name module:visual.GratingStim#_getDisplaySize
|
|
* @private
|
|
* @return {number[]} the size of the displayed image
|
|
*/
|
|
_getDisplaySize()
|
|
{
|
|
let displaySize = this.size;
|
|
|
|
if (typeof displaySize === "undefined")
|
|
{
|
|
// use the size of the pixi element, if we have access to it:
|
|
if (typeof this._pixi !== "undefined" && this._pixi.width > 0)
|
|
{
|
|
const pixiContainerSize = [this._pixi.width, this._pixi.height];
|
|
displaySize = util.to_unit(pixiContainerSize, "pix", this.win, this.units);
|
|
}
|
|
}
|
|
|
|
return displaySize;
|
|
}
|
|
|
|
/**
|
|
* Estimate the bounding box.
|
|
*
|
|
* @name module:visual.GratingStim#_estimateBoundingBox
|
|
* @function
|
|
* @override
|
|
* @protected
|
|
*/
|
|
_estimateBoundingBox()
|
|
{
|
|
const size = this._getDisplaySize();
|
|
if (typeof size !== "undefined")
|
|
{
|
|
this._boundingBox = new PIXI.Rectangle(
|
|
this._pos[0] - size[0] / 2,
|
|
this._pos[1] - size[1] / 2,
|
|
size[0],
|
|
size[1],
|
|
);
|
|
}
|
|
|
|
// TODO take the orientation into account
|
|
}
|
|
|
|
/**
|
|
* Generate PIXI.Mesh object based on provided shader function name and uniforms.
|
|
*
|
|
* @name module:visual.GratingStim#_getPixiMeshFromPredefinedShaders
|
|
* @function
|
|
* @protected
|
|
* @param {String} shaderName - name of the shader. Must be one of the SHADERS
|
|
* @param {Object} uniforms - a set of uniforms to supply to the shader. Mixed together with default uniform values.
|
|
* @return {Pixi.Mesh} Pixi.Mesh object that represents shader and later added to the scene.
|
|
*/
|
|
_getPixiMeshFromPredefinedShaders (shaderName = "", uniforms = {}) {
|
|
const geometry = new PIXI.Geometry();
|
|
geometry.addAttribute(
|
|
"aVertexPosition",
|
|
[
|
|
0, 0,
|
|
this._size_px[0], 0,
|
|
this._size_px[0], this._size_px[1],
|
|
0, this._size_px[1]
|
|
],
|
|
2
|
|
);
|
|
geometry.addAttribute(
|
|
"aUvs",
|
|
[0, 0, 1, 0, 1, 1, 0, 1],
|
|
2
|
|
);
|
|
geometry.addIndex([0, 1, 2, 0, 2, 3]);
|
|
const vertexSrc = defaultQuadVert;
|
|
const fragmentSrc = GratingStim.#SHADERS[shaderName].shader;
|
|
const uniformsFinal = Object.assign({}, GratingStim.#SHADERS[shaderName].uniforms, uniforms);
|
|
const shader = PIXI.Shader.from(vertexSrc, fragmentSrc, uniformsFinal);
|
|
return new PIXI.Mesh(geometry, shader);
|
|
}
|
|
|
|
/**
|
|
* Set phase value for the function.
|
|
*
|
|
* @name module:visual.GratingStim#setPhase
|
|
* @public
|
|
* @param {number} phase - phase value
|
|
* @param {boolean} [log= false] - whether of not to log
|
|
*/
|
|
setPhase (phase, log = false) {
|
|
this._setAttribute("phase", phase, log);
|
|
if (this._pixi instanceof PIXI.Mesh) {
|
|
this._pixi.shader.uniforms.uPhase = -phase;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set color space value for the grating stimulus.
|
|
*
|
|
* @name module:visual.GratingStim#setColorSpace
|
|
* @public
|
|
* @param {String} colorSpaceVal - color space value
|
|
* @param {boolean} [log= false] - whether of not to log
|
|
*/
|
|
setColorSpace (colorSpaceVal = "RGB", log = false) {
|
|
let colorSpaceValU = colorSpaceVal.toUpperCase();
|
|
if (Color.COLOR_SPACE[colorSpaceValU] === undefined) {
|
|
colorSpaceValU = "RGB";
|
|
}
|
|
const hasChanged = this._setAttribute("colorSpace", colorSpaceValU, log);
|
|
if (hasChanged) {
|
|
this.setColor(this._color);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set foreground color value for the grating stimulus.
|
|
*
|
|
* @name module:visual.GratingStim#setColor
|
|
* @public
|
|
* @param {Color} colorVal - color value, can be String like "red" or "#ff0000" or Number like 0xff0000.
|
|
* @param {boolean} [log= false] - whether of not to log
|
|
*/
|
|
setColor (colorVal = "white", log = false) {
|
|
const colorObj = (colorVal instanceof Color) ? colorVal : new Color(colorVal, Color.COLOR_SPACE[this._colorSpace])
|
|
this._setAttribute("color", colorObj, log);
|
|
if (this._pixi instanceof PIXI.Mesh) {
|
|
this._pixi.shader.uniforms.uColor = colorObj.rgbFull;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Determines how visible the stimulus is relative to background.
|
|
*
|
|
* @name module:visual.GratingStim#setOpacity
|
|
* @public
|
|
* @param {number} [opacity=1] opacity - The value should be a single float ranging 1.0 (opaque) to 0.0 (transparent).
|
|
* @param {boolean} [log= false] - whether of not to log
|
|
*/
|
|
setOpacity (opacity = 1, log = false) {
|
|
this._setAttribute("opacity", opacity, log);
|
|
if (this._pixi instanceof PIXI.Mesh) {
|
|
this._pixi.shader.uniforms.uAlpha = opacity;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set spatial frequency value for the function.
|
|
*
|
|
* @name module:visual.GratingStim#setSF
|
|
* @public
|
|
* @param {number} sf - spatial frequency value
|
|
* @param {boolean} [log=false] - whether or not to log
|
|
*/
|
|
setSF (sf, log = false) {
|
|
this._setAttribute("SF", sf, log);
|
|
if (this._pixi instanceof PIXI.Mesh) {
|
|
this._pixi.shader.uniforms.uFreq = sf;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set blend mode of the grating stimulus.
|
|
*
|
|
* @name module:visual.GratingStim#setBlendmode
|
|
* @public
|
|
* @param {String} blendMode - blend mode, can be one of the following: ["avg", "add", "mul", "screen"].
|
|
* @param {boolean} [log=false] - whether or not to log
|
|
*/
|
|
setBlendmode (blendMode = "avg", log = false) {
|
|
this._setAttribute("blendmode", blendMode, log);
|
|
if (this._pixi !== undefined) {
|
|
let pixiBlendMode = GratingStim.#BLEND_MODES_MAP[blendMode];
|
|
if (pixiBlendMode === undefined) {
|
|
pixiBlendMode = PIXI.BLEND_MODES.NORMAL;
|
|
}
|
|
if (this._pixi.filters) {
|
|
this._pixi.filters[this._pixi.filters.length - 1].blendMode = pixiBlendMode;
|
|
} else {
|
|
this._pixi.blendMode = pixiBlendMode;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Whether to interpolate (linearly) the texture in the stimulus.
|
|
*
|
|
* @name module:visual.GratingStim#setInterpolate
|
|
* @public
|
|
* @param {boolean} interpolate - interpolate or not.
|
|
* @param {boolean} [log=false] - whether or not to log
|
|
*/
|
|
setInterpolate (interpolate = false, log = false) {
|
|
this._setAttribute("interpolate", interpolate, log);
|
|
if (this._pixi instanceof PIXI.Mesh && this._pixi.shader.uniforms.uTex instanceof PIXI.Texture) {
|
|
this._pixi.shader.uniforms.uTex.baseTexture.scaleMode = interpolate ? PIXI.SCALE_MODES.LINEAR : PIXI.SCALE_MODES.NEAREST;
|
|
this._pixi.shader.uniforms.uTex.baseTexture.update();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the stimulus, if necessary.
|
|
*
|
|
* @name module:visual.GratingStim#_updateIfNeeded
|
|
* @private
|
|
*/
|
|
_updateIfNeeded()
|
|
{
|
|
if (!this._needUpdate)
|
|
{
|
|
return;
|
|
}
|
|
this._needUpdate = false;
|
|
|
|
// update the PIXI representation, if need be:
|
|
if (this._needPixiUpdate)
|
|
{
|
|
this._needPixiUpdate = false;
|
|
let shaderName;
|
|
let shaderUniforms;
|
|
let currentUniforms = {};
|
|
if (typeof this._pixi !== "undefined")
|
|
{
|
|
if (this._pixi instanceof PIXI.Mesh) {
|
|
Object.assign(currentUniforms, this._pixi.shader.uniforms);
|
|
}
|
|
this._pixi.destroy(true);
|
|
}
|
|
this._pixi = undefined;
|
|
|
|
// no image to draw: return immediately
|
|
if (typeof this._tex === "undefined")
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (this._tex instanceof HTMLImageElement)
|
|
{
|
|
shaderName = "imageShader";
|
|
let shaderTex = PIXI.Texture.from(this._tex, {
|
|
wrapMode: PIXI.WRAP_MODES.REPEAT,
|
|
scaleMode: this._interpolate ? PIXI.SCALE_MODES.LINEAR : PIXI.SCALE_MODES.NEAREST
|
|
});
|
|
shaderUniforms = {
|
|
uTex: shaderTex,
|
|
uFreq: this._SF,
|
|
uPhase: this._phase,
|
|
uColor: this._color.rgbFull
|
|
};
|
|
}
|
|
else
|
|
{
|
|
shaderName = this._tex;
|
|
shaderUniforms = {
|
|
uFreq: this._SF,
|
|
uPhase: this._phase,
|
|
uColor: this._color.rgbFull
|
|
};
|
|
}
|
|
this._pixi = this._getPixiMeshFromPredefinedShaders(shaderName, Object.assign(shaderUniforms, currentUniforms));
|
|
this._pixi.pivot.set(this._pixi.width * 0.5, this._pixi.width * 0.5);
|
|
this._pixi.filters = [this._adjustmentFilter];
|
|
|
|
// add a mask if need be:
|
|
if (typeof this._mask !== "undefined")
|
|
{
|
|
if (this._mask instanceof HTMLImageElement)
|
|
{
|
|
this._pixi.mask = PIXI.Sprite.from(this._mask);
|
|
this._pixi.mask.width = this._size_px[0];
|
|
this._pixi.mask.height = this._size_px[1];
|
|
this._pixi.addChild(this._pixi.mask);
|
|
}
|
|
else
|
|
{
|
|
// for some reason setting PIXI.Mesh as .mask doesn't do anything,
|
|
// rendering mask to texture for further use.
|
|
const maskMesh = this._getPixiMeshFromPredefinedShaders(this._mask);
|
|
const rt = PIXI.RenderTexture.create({
|
|
width: this._size_px[0],
|
|
height: this._size_px[1]
|
|
});
|
|
this.win._renderer.render(maskMesh, {
|
|
renderTexture: rt
|
|
});
|
|
const maskSprite = new PIXI.Sprite.from(rt);
|
|
this._pixi.mask = maskSprite;
|
|
this._pixi.addChild(maskSprite);
|
|
}
|
|
}
|
|
|
|
// since _pixi.width may not be immediately available but the rest of the code needs its value
|
|
// we arrange for repeated calls to _updateIfNeeded until we have a width:
|
|
if (this._pixi.width === 0)
|
|
{
|
|
this._needUpdate = true;
|
|
this._needPixiUpdate = true;
|
|
return;
|
|
}
|
|
}
|
|
|
|
this._pixi.zIndex = this._depth;
|
|
this.opacity = this._opacity;
|
|
|
|
// set the scale:
|
|
const displaySize = this._getDisplaySize();
|
|
this._size_px = util.to_px(displaySize, this.units, this.win);
|
|
const scaleX = this._size_px[0] / this._pixi.width;
|
|
const scaleY = this._size_px[1] / this._pixi.height;
|
|
this._pixi.scale.x = this.flipHoriz ? -scaleX : scaleX;
|
|
this._pixi.scale.y = this.flipVert ? scaleY : -scaleY;
|
|
|
|
// set the position, rotation, and anchor (image centered on pos):
|
|
let pos = to_pixiPoint(this.pos, this.units, this.win);
|
|
this._pixi.position.set(pos.x, pos.y);
|
|
this._pixi.rotation = -this.ori * Math.PI / 180;
|
|
|
|
// re-estimate the bounding box, as the texture's width may now be available:
|
|
this._estimateBoundingBox();
|
|
}
|
|
}
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-core.html">core</a></li><li><a href="module-data.html">data</a></li><li><a href="module-sound.html">sound</a></li><li><a href="module-util.html">util</a></li><li><a href="module-visual.html">visual</a></li></ul><h3>Classes</h3><ul><li><a href="FaceDetector_FaceDetector.html">FaceDetector</a></li><li><a href="module.data.MultiStairHandler.html">MultiStairHandler</a></li><li><a href="module.data.QuestHandler.html">QuestHandler</a></li><li><a href="module-core.BuilderKeyResponse.html">BuilderKeyResponse</a></li><li><a href="module-core.EventManager.html">EventManager</a></li><li><a href="module-core.GUI.html">GUI</a></li><li><a href="module-core.Keyboard.html">Keyboard</a></li><li><a href="module-core.KeyPress.html">KeyPress</a></li><li><a href="module-core.Logger.html">Logger</a></li><li><a href="module-core.MinimalStim.html">MinimalStim</a></li><li><a href="module-core.Mouse.html">Mouse</a></li><li><a href="module-core.PsychoJS.html">PsychoJS</a></li><li><a href="module-core.ServerManager.html">ServerManager</a></li><li><a href="module-core.Window.html">Window</a></li><li><a href="module-data.ExperimentHandler.html">ExperimentHandler</a></li><li><a href="module-data.MultiStairHandler.html">MultiStairHandler</a></li><li><a href="module-data.QuestHandler.html">QuestHandler</a></li><li><a href="module-data.Shelf.html">Shelf</a></li><li><a href="module-data.TrialHandler.html">TrialHandler</a></li><li><a href="module-sound.AudioClip.html">AudioClip</a></li><li><a href="module-sound.AudioClipPlayer.html">AudioClipPlayer</a></li><li><a href="module-sound.Microphone.html">Microphone</a></li><li><a href="module-sound.Sound.html">Sound</a></li><li><a href="module-sound.TonePlayer.html">TonePlayer</a></li><li><a href="module-sound.TrackPlayer.html">TrackPlayer</a></li><li><a href="module-sound.Transcriber.html">Transcriber</a></li><li><a href="module-sound.Transcript.html">Transcript</a></li><li><a href="module-util.Clock.html">Clock</a></li><li><a href="module-util.Color.html">Color</a></li><li><a href="module-util.CountdownTimer.html">CountdownTimer</a></li><li><a href="module-util.EventEmitter.html">EventEmitter</a></li><li><a href="module-util.MixinBuilder.html">MixinBuilder</a></li><li><a href="module-util.MonotonicClock.html">MonotonicClock</a></li><li><a href="module-util.PsychObject.html">PsychObject</a></li><li><a href="module-util.Scheduler.html">Scheduler</a></li><li><a href="module-visual.ButtonStim.html">ButtonStim</a></li><li><a href="module-visual.Camera.html">Camera</a></li><li><a href="module-visual.FaceDetector.html">FaceDetector</a></li><li><a href="module-visual.Form.html">Form</a></li><li><a href="module-visual.GratingStim.html">GratingStim</a></li><li><a href="module-visual.ImageStim.html">ImageStim</a></li><li><a href="module-visual.MovieStim.html">MovieStim</a></li><li><a href="module-visual.Polygon.html">Polygon</a></li><li><a href="module-visual.Rect.html">Rect</a></li><li><a href="module-visual.ShapeStim.html">ShapeStim</a></li><li><a href="module-visual.Slider.html">Slider</a></li><li><a href="module-visual.TextBox.html">TextBox</a></li><li><a href="module-visual.TextStim.html">TextStim</a></li><li><a href="module-visual.VisualStim.html">VisualStim</a></li></ul><h3>Interfaces</h3><ul><li><a href="module-sound.SoundPlayer.html">SoundPlayer</a></li></ul><h3>Mixins</h3><ul><li><a href="module-core.WindowMixin.html">WindowMixin</a></li><li><a href="module-util.ColorMixin.html">ColorMixin</a></li></ul>
|
|
</nav>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Mon May 23 2022 12:29:28 GMT+0200 (Central European Summer Time)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|