Skip to content

Commit 48737a5

Browse files
authored
Merge pull request #8254 from processing/fix/strands-int
Fix usage of int() typecast in p5.strands
2 parents 8e03625 + 5935866 commit 48737a5

File tree

8 files changed

+83
-9
lines changed

8 files changed

+83
-9
lines changed

src/strands/strands_api.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,18 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
205205
const originalp5Fn = fn[typeInfo.fnName];
206206
fn[typeInfo.fnName] = function(...args) {
207207
if (strandsContext.active) {
208-
const { id, dimension } = build.primitiveConstructorNode(strandsContext, typeInfo, args);
209-
return createStrandsNode(id, dimension, strandsContext);
208+
if (args.length === 1 && args[0].dimension && args[0].dimension === typeInfo.dimension) {
209+
const { id, dimension } = build.functionCallNode(strandsContext, typeInfo.fnName, args, {
210+
overloads: [{
211+
params: [args[0].typeInfo()],
212+
returnType: typeInfo,
213+
}]
214+
});
215+
return createStrandsNode(id, dimension, strandsContext);
216+
} else {
217+
const { id, dimension } = build.primitiveConstructorNode(strandsContext, typeInfo, args);
218+
return createStrandsNode(id, dimension, strandsContext);
219+
}
210220
} else if (originalp5Fn) {
211221
return originalp5Fn.apply(this, args);
212222
} else {

src/strands/strands_node.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@ export class StrandsNode {
1313
const nodeData = getNodeDataFromID(dag, this.id);
1414
if (nodeData && nodeData.identifier) {
1515
this._originalIdentifier = nodeData.identifier;
16+
}
17+
if (nodeData) {
1618
this._originalBaseType = nodeData.baseType;
1719
this._originalDimension = nodeData.dimension;
1820
}
1921
}
2022
copy() {
2123
return createStrandsNode(this.id, this.dimension, this.strandsContext);
2224
}
25+
typeInfo() {
26+
return {
27+
baseType: this._originalBaseType || BaseType.FLOAT,
28+
dimension: this.dimension
29+
};
30+
}
2331
bridge(value) {
2432
const { dag, cfg } = this.strandsContext;
2533
const orig = getNodeDataFromID(dag, this.id);
@@ -30,8 +38,8 @@ export class StrandsNode {
3038
newValueID = value.id;
3139
} else {
3240
const newVal = primitiveConstructorNode(
33-
this.strandsContext,
34-
{ baseType, dimension: this.dimension },
41+
this.strandsContext,
42+
{ baseType, dimension: this.dimension },
3543
value
3644
);
3745
newValueID = newVal.id;
@@ -85,8 +93,8 @@ export class StrandsNode {
8593
newValueID = value.id;
8694
} else {
8795
const newVal = primitiveConstructorNode(
88-
this.strandsContext,
89-
{ baseType, dimension: this.dimension },
96+
this.strandsContext,
97+
{ baseType, dimension: this.dimension },
9098
value
9199
);
92100
newValueID = newVal.id;
@@ -159,4 +167,4 @@ export function createStrandsNode(id, dimension, strandsContext, onRebind) {
159167
new StrandsNode(id, dimension, strandsContext),
160168
swizzleTrap(id, dimension, strandsContext, onRebind)
161169
);
162-
}
170+
}

src/webgl/p5.Shader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ class Shader {
533533
// if our vertex shader failed compilation?
534534
if (!gl.getShaderParameter(this._vertShader, gl.COMPILE_STATUS)) {
535535
const glError = gl.getShaderInfoLog(this._vertShader);
536-
if (typeof IS_MINIFIED !== 'undefined') {
536+
if (typeof IS_MINIFIED !== 'undefined' || typeof p5 === 'undefined') {
537537
console.error(glError);
538538
} else {
539539
p5._friendlyError(
@@ -551,7 +551,7 @@ class Shader {
551551
// if our frag shader failed compilation?
552552
if (!gl.getShaderParameter(this._fragShader, gl.COMPILE_STATUS)) {
553553
const glError = gl.getShaderInfoLog(this._fragShader);
554-
if (typeof IS_MINIFIED !== 'undefined') {
554+
if (typeof IS_MINIFIED !== 'undefined' || typeof p5 === 'undefined') {
555555
console.error(glError);
556556
} else {
557557
p5._friendlyError(

test/unit/visual/cases/webgl.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,56 @@ visualSuite('WebGL', function() {
709709
});
710710
});
711711

712+
visualSuite('instanced randering', async () => {
713+
visualTest('can draw in a grid with floor()', (p5, screenshot) => {
714+
p5.createCanvas(50, 50, p5.WEBGL);
715+
const shader = p5.baseMaterialShader().modify(() => {
716+
p5.getWorldInputs((inputs) => {
717+
const id = p5.instanceID();
718+
const gridSize = 5;
719+
const row = p5.floor(id / gridSize);
720+
const col = id - row * gridSize;
721+
const blockInnerSize = 10;
722+
const x = (col - gridSize / 2.0) * blockInnerSize + blockInnerSize/2;
723+
const y = (gridSize / 2.0 - row) * blockInnerSize - blockInnerSize/2;
724+
inputs.position += [x, y, 0];
725+
return inputs;
726+
});
727+
}, { p5 });
728+
p5.shader(shader);
729+
const obj = p5.buildGeometry(() => p5.circle(0, 0, 6))
730+
p5.noStroke();
731+
p5.fill(0);
732+
p5.shader(shader);
733+
p5.model(obj, 25);
734+
screenshot();
735+
});
736+
737+
visualTest('can draw in a grid with int()', (p5, screenshot) => {
738+
p5.createCanvas(50, 50, p5.WEBGL);
739+
const shader = p5.baseMaterialShader().modify(() => {
740+
p5.getWorldInputs((inputs) => {
741+
const id = p5.instanceID();
742+
const gridSize = 5;
743+
const row = p5.int(id / gridSize);
744+
const col = id - row * gridSize;
745+
const blockInnerSize = 10;
746+
const x = (col - gridSize / 2.0) * blockInnerSize + blockInnerSize/2;
747+
const y = (gridSize / 2.0 - row) * blockInnerSize - blockInnerSize/2;
748+
inputs.position += [x, y, 0];
749+
return inputs;
750+
});
751+
}, { p5 });
752+
p5.shader(shader);
753+
const obj = p5.buildGeometry(() => p5.circle(0, 0, 6))
754+
p5.noStroke();
755+
p5.fill(0);
756+
p5.shader(shader);
757+
p5.model(obj, 25);
758+
screenshot();
759+
});
760+
});
761+
712762
visualSuite('p5.strands', () => {
713763
visualTest('it recovers from p5.strands errors', (p5, screenshot) => {
714764
p5.createCanvas(50, 50, p5.WEBGL);
796 Bytes
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
796 Bytes
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)