Skip to content

Commit f97d924

Browse files
authored
Merge branch 'dev-2.0' into touch-orbitControl
2 parents 3ad5425 + 6759f35 commit f97d924

3 files changed

Lines changed: 76 additions & 16 deletions

File tree

src/shape/attributes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,9 @@ function attributes(p5, fn){
513513
* Sets the width of the stroke used for points, lines, and the outlines of
514514
* shapes.
515515
*
516-
* Note: `strokeWeight()` is affected by transformations, especially calls to
517-
* <a href="#/p5/scale">scale()</a>.
516+
* Note: In 2D mode, `strokeWeight()` is affected by transformations,
517+
* especially calls to <a href="#/p5/scale">scale()</a>. It isn't affected by
518+
* transformations in WebGL and WebGPU modes.
518519
*
519520
* Calling `strokeWeight()` without an argument returns the current stroke weight as a number.
520521
*

src/strands/strands_api.js

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,54 @@ export function createShaderHooksFunctions(strandsContext, fn, shader) {
795795
hook.set = function(result) {
796796
hook._result = result;
797797
};
798+
hook._active = false;
799+
800+
const numStructArgs = hookType.parameters.filter(
801+
param => param.type && param.type.properties
802+
).length;
803+
let argIdx = -1;
804+
if (numStructArgs === 1) {
805+
argIdx = hookType.parameters.findIndex(
806+
param => param.type && param.type.properties
807+
);
808+
}
809+
if (argIdx >= 0) {
810+
const structParam = hookType.parameters[argIdx];
811+
if (structParam.type.properties) {
812+
const nameMatch = /^get([A-Z0-9]\w*)$/.exec(hookType.name);
813+
const displayName = nameMatch
814+
? nameMatch[1][0].toLowerCase() + nameMatch[1].slice(1)
815+
: hookType.name;
816+
for (const prop of structParam.type.properties) {
817+
const key = prop.name;
818+
Object.defineProperty(hook, key, {
819+
get() {
820+
if (!this._active) {
821+
FES.userError(
822+
'scope error',
823+
`It looks like you're trying to access "${displayName}.${key}" outside of its begin()/end() block.\n\n` +
824+
`Properties of ${displayName} are only available between ` +
825+
`${displayName}.begin() and ${displayName}.end().\n\n` +
826+
`To share data between hooks, use sharedVec3() or sharedFloat() ` +
827+
`to pass values between them.`
828+
);
829+
}
830+
return this._args[this._argIdx][key];
831+
},
832+
set(val) {
833+
if (!this._active) {
834+
FES.userError(
835+
'scope error',
836+
`It looks like you're trying to set "${displayName}.${key}" outside of its begin()/end() block.`
837+
);
838+
}
839+
this._args[this._argIdx][key] = val;
840+
},
841+
enumerable: true,
842+
});
843+
}
844+
}
845+
}
798846

799847
let entryBlockID;
800848
function setupHook() {
@@ -803,25 +851,14 @@ export function createShaderHooksFunctions(strandsContext, fn, shader) {
803851
CFG.addEdge(cfg, cfg.currentBlock, entryBlockID);
804852
CFG.pushBlock(cfg, entryBlockID);
805853
const args = createHookArguments(strandsContext, hookType.parameters);
806-
const numStructArgs = hookType.parameters.filter(param => param.type.properties).length;
807-
let argIdx = -1;
808-
if (numStructArgs === 1) {
809-
argIdx = hookType.parameters.findIndex(param => param.type.properties);
810-
}
854+
hook._active = true;
855+
hook._args = args;
856+
hook._argIdx = argIdx;
811857
hook._properties = [];
812858
for (let i = 0; i < args.length; i++) {
813859
if (i === argIdx) {
814860
for (const key of args[argIdx].structProperties || []) {
815861
hook._properties.push(key);
816-
Object.defineProperty(hook, key, {
817-
get() {
818-
return args[argIdx][key];
819-
},
820-
set(val) {
821-
args[argIdx][key] = val;
822-
},
823-
enumerable: true,
824-
});
825862
}
826863
if (hookType.returnType?.typeName === hookType.parameters[argIdx].type.typeName) {
827864
hook.set(args[argIdx]);
@@ -835,6 +872,7 @@ export function createShaderHooksFunctions(strandsContext, fn, shader) {
835872
};
836873

837874
function finishHook() {
875+
hook._active = false;
838876
const userReturned = hook._result;
839877
strandsContext.activeHook = undefined;
840878

test/unit/webgl/p5.Shader.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,5 +2759,26 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca
27592759
}, { myp5 });
27602760
});
27612761
});
2762+
2763+
test('scope error uses unprefixed hook name', () => {
2764+
myp5.createCanvas(50, 50, myp5.WEBGL);
2765+
2766+
try {
2767+
myp5.baseMaterialShader().modify(() => {
2768+
myp5.getWorldInputs.begin();
2769+
myp5.getWorldInputs.end();
2770+
const pos = myp5.getWorldInputs.position;
2771+
}, { myp5 });
2772+
} catch (e) { /* expected */ }
2773+
2774+
assert.isAbove(mockUserError.mock.calls.length, 0, 'FES.userError should have been called');
2775+
const scopeCall = mockUserError.mock.calls.find(call => call[0] === 'scope error');
2776+
assert.isDefined(scopeCall, 'scope error should have been called');
2777+
const errMsg = scopeCall[1];
2778+
assert.include(errMsg, 'worldInputs');
2779+
assert.notInclude(errMsg, 'getWorldInputs');
2780+
assert.include(errMsg, 'begin()');
2781+
assert.include(errMsg, 'end()');
2782+
});
27622783
});
27632784
});

0 commit comments

Comments
 (0)