-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(webgl): add global property support for p5.strands #8211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev-2.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,6 +168,73 @@ | |
| } | ||
| } | ||
| } | ||
| ////////////////////////////////////////////// | ||
| // Global p5 properties | ||
| ////////////////////////////////////////////// | ||
| const globalProperties = [ | ||
| { name: 'width', type: DataType.float1 }, | ||
| { name: 'height', type: DataType.float1 }, | ||
| { name: 'mouseX', type: DataType.float1 }, | ||
| { name: 'mouseY', type: DataType.float1 }, | ||
| { name: 'pmouseX', type: DataType.float1 }, | ||
| { name: 'pmouseY', type: DataType.float1 }, | ||
| { name: 'winMouseX', type: DataType.float1 }, | ||
| { name: 'winMouseY', type: DataType.float1 }, | ||
| { name: 'frameCount', type: DataType.int1 }, | ||
| { name: 'focused', type: DataType.bool1 }, | ||
| { name: 'displayWidth', type: DataType.float1 }, | ||
| { name: 'displayHeight', type: DataType.float1 }, | ||
| { name: 'windowWidth', type: DataType.float1 }, | ||
| { name: 'windowHeight', type: DataType.float1 }, | ||
| { name: 'mouseButton', type: DataType.int1 }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In p5 2.x this isn't an int, it's an object: https://beta.p5js.org/reference/p5/mousebutton/ We might need to special case this one by either making each property a uniform, or by just omitting it for now. |
||
| { name: 'mouseIsPressed', type: DataType.bool1 } | ||
| ]; | ||
|
|
||
| const originalDescriptors = {}; | ||
| for (const { name } of globalProperties) { | ||
| originalDescriptors[name] = Object.getOwnPropertyDescriptor(fn, name) || { | ||
| get: function() { return p5.prototype[name]; }, | ||
| configurable: true | ||
| }; | ||
| } | ||
|
|
||
| for (const { name, type } of globalProperties) { | ||
| strandsContext.fnOverrides[name] = originalDescriptors[name]; | ||
|
|
||
| (function(propName, propType, origDescriptor) { | ||
| Object.defineProperty(fn, propName, { | ||
| get: function() { | ||
| if (strandsContext.active) { | ||
| const uniformName = `_p5_${propName}`; | ||
| const existingUniform = strandsContext.uniforms.find(u => u.name === uniformName); | ||
|
|
||
| if (!existingUniform) { | ||
| const { id, dimension } = build.variableNode(strandsContext, propType, uniformName); | ||
| strandsContext.uniforms.push({ | ||
| name: uniformName, | ||
| typeInfo: propType, | ||
| defaultValue: origDescriptor.get ? | ||
| () => origDescriptor.get.call(fn) : | ||
| () => origDescriptor.value | ||
| }); | ||
| return createStrandsNode(id, dimension, strandsContext); | ||
| } else { | ||
| const { id, dimension } = build.variableNode(strandsContext, propType, uniformName); | ||
| return createStrandsNode(id, dimension, strandsContext); | ||
| } | ||
| } else { | ||
| if (origDescriptor.get) { | ||
| return origDescriptor.get.call(this); | ||
| } else { | ||
| return origDescriptor.value; | ||
| } | ||
| } | ||
| }, | ||
| configurable: true, | ||
| enumerable: true | ||
| }); | ||
| })(name, type, originalDescriptors[name]); | ||
| } | ||
| } | ||
| ////////////////////////////////////////////// | ||
| // Per-Hook functions | ||
|
|
@@ -183,7 +250,7 @@ | |
| for (let i = 0; i < structTypeInfo.properties.length; i++) { | ||
| const propertyType = structTypeInfo.properties[i]; | ||
| Object.defineProperty(structNode, propertyType.name, { | ||
| get() { | ||
|
Check failure on line 253 in src/strands/strands_api.js
|
||
| const propNode = getNodeDataFromID(dag, dag.dependsOn[structNode.id][i]) | ||
| const onRebind = (newFieldID) => { | ||
| const oldDeps = dag.dependsOn[structNode.id]; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this is an integer value, it's fairly unintuitive for users of strands for it to actually be a glsl
intinstead of a float, since in JS all numbers are floats, and you'd expectframeCount / 2to have decimals sometimes. So maybe let's turn all the ints into floats here?