Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/strands/strands_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,18 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
const originalp5Fn = fn[typeInfo.fnName];
fn[typeInfo.fnName] = function(...args) {
if (strandsContext.active) {
const { id, dimension } = build.primitiveConstructorNode(strandsContext, typeInfo, args);
return createStrandsNode(id, dimension, strandsContext);
if (args.length === 1 && args[0].dimension && args[0].dimension === typeInfo.dimension) {
const { id, dimension } = build.functionCallNode(strandsContext, typeInfo.fnName, args, {
overloads: [{
params: [args[0].typeInfo()],
returnType: typeInfo,
}]
});
return createStrandsNode(id, dimension, strandsContext);
} else {
const { id, dimension } = build.primitiveConstructorNode(strandsContext, typeInfo, args);
return createStrandsNode(id, dimension, strandsContext);
}
} else if (originalp5Fn) {
return originalp5Fn.apply(this, args);
} else {
Expand Down
18 changes: 13 additions & 5 deletions src/strands/strands_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ export class StrandsNode {
const nodeData = getNodeDataFromID(dag, this.id);
if (nodeData && nodeData.identifier) {
this._originalIdentifier = nodeData.identifier;
}
if (nodeData) {
this._originalBaseType = nodeData.baseType;
this._originalDimension = nodeData.dimension;
}
}
copy() {
return createStrandsNode(this.id, this.dimension, this.strandsContext);
}
typeInfo() {
return {
baseType: this._originalBaseType || BaseType.FLOAT,
dimension: this.dimension
};
}
bridge(value) {
const { dag, cfg } = this.strandsContext;
const orig = getNodeDataFromID(dag, this.id);
Expand All @@ -30,8 +38,8 @@ export class StrandsNode {
newValueID = value.id;
} else {
const newVal = primitiveConstructorNode(
this.strandsContext,
{ baseType, dimension: this.dimension },
this.strandsContext,
{ baseType, dimension: this.dimension },
value
);
newValueID = newVal.id;
Expand Down Expand Up @@ -85,8 +93,8 @@ export class StrandsNode {
newValueID = value.id;
} else {
const newVal = primitiveConstructorNode(
this.strandsContext,
{ baseType, dimension: this.dimension },
this.strandsContext,
{ baseType, dimension: this.dimension },
value
);
newValueID = newVal.id;
Expand Down Expand Up @@ -159,4 +167,4 @@ export function createStrandsNode(id, dimension, strandsContext, onRebind) {
new StrandsNode(id, dimension, strandsContext),
swizzleTrap(id, dimension, strandsContext, onRebind)
);
}
}
4 changes: 2 additions & 2 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ class Shader {
// if our vertex shader failed compilation?
if (!gl.getShaderParameter(this._vertShader, gl.COMPILE_STATUS)) {
const glError = gl.getShaderInfoLog(this._vertShader);
if (typeof IS_MINIFIED !== 'undefined') {
if (typeof IS_MINIFIED !== 'undefined' || typeof p5 === 'undefined') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because I noticed this was crashing in tests when we hit this path due to p5 not being globally available. Ideally we do something where we patch this method onto the class in the addon function, but given the size of these functions, this is maybe a poor fit. Maybe there's a better way to integrate FES into other module files?

console.error(glError);
} else {
p5._friendlyError(
Expand All @@ -551,7 +551,7 @@ class Shader {
// if our frag shader failed compilation?
if (!gl.getShaderParameter(this._fragShader, gl.COMPILE_STATUS)) {
const glError = gl.getShaderInfoLog(this._fragShader);
if (typeof IS_MINIFIED !== 'undefined') {
if (typeof IS_MINIFIED !== 'undefined' || typeof p5 === 'undefined') {
console.error(glError);
} else {
p5._friendlyError(
Expand Down
50 changes: 50 additions & 0 deletions test/unit/visual/cases/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,4 +708,54 @@ visualSuite('WebGL', function() {
screenshot();
});
});

visualSuite('instanced randering', async () => {
visualTest('can draw in a grid with floor()', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const shader = p5.baseMaterialShader().modify(() => {
p5.getWorldInputs((inputs) => {
const id = p5.instanceID();
const gridSize = 5;
const row = p5.floor(id / gridSize);
const col = id - row * gridSize;
const blockInnerSize = 10;
const x = (col - gridSize / 2.0) * blockInnerSize + blockInnerSize/2;
const y = (gridSize / 2.0 - row) * blockInnerSize - blockInnerSize/2;
inputs.position += [x, y, 0];
return inputs;
});
}, { p5 });
p5.shader(shader);
const obj = p5.buildGeometry(() => p5.circle(0, 0, 6))
p5.noStroke();
p5.fill(0);
p5.shader(shader);
p5.model(obj, 25);
screenshot();
});

visualTest('can draw in a grid with int()', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const shader = p5.baseMaterialShader().modify(() => {
p5.getWorldInputs((inputs) => {
const id = p5.instanceID();
const gridSize = 5;
const row = p5.int(id / gridSize);
const col = id - row * gridSize;
const blockInnerSize = 10;
const x = (col - gridSize / 2.0) * blockInnerSize + blockInnerSize/2;
const y = (gridSize / 2.0 - row) * blockInnerSize - blockInnerSize/2;
inputs.position += [x, y, 0];
return inputs;
});
}, { p5 });
p5.shader(shader);
const obj = p5.buildGeometry(() => p5.circle(0, 0, 6))
p5.noStroke();
p5.fill(0);
p5.shader(shader);
p5.model(obj, 25);
screenshot();
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading