-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add paletteLerp for p5.strands (closes #8751) #8817
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
Open
LalitNarayanYadav
wants to merge
1
commit into
processing:dev-2.0
Choose a base branch
from
LalitNarayanYadav:feat/strands-palette-lerp
base: dev-2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,3 +110,5 @@ const builtInGLSLFunctions = { | |
| export const strandsBuiltinFunctions = { | ||
| ...builtInGLSLFunctions, | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,16 @@ import { ancestor, recursive } from 'acorn-walk'; | |
| import escodegen from 'escodegen'; | ||
| import { UnarySymbolToName } from './ir_types'; | ||
| import * as FES from './strands_FES'; | ||
|
|
||
| // Registry of strands functions that take raw array literals as arguments. | ||
| // Maps functionName → Set of argument indices that should NOT be | ||
| // converted to vectors by the ArrayExpression visitor. | ||
| // This generalizes the paletteLerp special-case so any future function | ||
| // taking array parameters can register here without modifying ArrayExpression. | ||
| const ARRAY_ARG_FUNCTIONS = { | ||
| paletteLerp: new Set([0]), // argument 0 is the [[color,pos],...] array | ||
| }; | ||
|
|
||
| let blockVarCounter = 0; | ||
| let loopVarCounter = 0; | ||
| function replaceBinaryOperator(codeSource) { | ||
|
|
@@ -563,12 +573,85 @@ const ASTCallbacks = { | |
| node.arguments = []; | ||
| } | ||
| }, | ||
|
|
||
| // Rewrite paletteLerp([[color,pos],...], t) → __p5.paletteLerp([c,...], [p,...], t) | ||
| // Must run before ArrayExpression so the child arrays carry _isPaletteLerpArg | ||
| // and are not wrapped in strandsNode (which would mis-type them as vectors). | ||
|
|
||
| CallExpression(node, state, ancestors) { | ||
| if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) { | ||
| return; | ||
| } | ||
| if (node.callee?.type !== 'Identifier' || node.callee?.name !== 'paletteLerp') { | ||
| return; | ||
| } | ||
| const args = node.arguments; | ||
| if (args.length !== 2) { | ||
| throw new Error( | ||
| `paletteLerp() requires 2 arguments: (colorStops[], t) — got ${args.length}.\n` + | ||
| `Usage: paletteLerp([[color(r,g,b), pos], ...], t)` | ||
| ); | ||
| } | ||
| const [stopsArg, tArg] = args; | ||
| if (stopsArg.type !== 'ArrayExpression') { | ||
| throw new Error( | ||
| `paletteLerp() first argument must be an array literal: [[color(...), pos], ...]` | ||
| ); | ||
| } | ||
| const stops = stopsArg.elements; | ||
| if (stops.length < 2 || stops.length > 8) { | ||
| throw new Error( | ||
| `paletteLerp() requires 2–8 color stops, got ${stops.length}.` | ||
| ); | ||
| } | ||
| for (let i = 0; i < stops.length; i++) { | ||
| if (stops[i].type !== 'ArrayExpression' || stops[i].elements.length !== 2) { | ||
| throw new Error( | ||
| `paletteLerp() stop ${i} must be a 2-element array: [color(...), position]` | ||
| ); | ||
| } | ||
| } | ||
| // Split pairs into two parallel arrays | ||
| const colorsArr = { | ||
| type: 'ArrayExpression', | ||
| elements: stops.map(s => s.elements[0]), | ||
| _isPaletteLerpArg: true, | ||
| }; | ||
| const positionsArr = { | ||
| type: 'ArrayExpression', | ||
| elements: stops.map(s => s.elements[1]), | ||
| _isPaletteLerpArg: true, | ||
| }; | ||
| // Rewrite in-place to __p5.paletteLerp(colors, positions, t) | ||
| node.callee = { type: 'Identifier', name: '__p5.paletteLerp' }; | ||
| node.arguments = [colorsArr, positionsArr, tArg]; | ||
| }, | ||
|
|
||
|
|
||
| // The callbacks for AssignmentExpression and BinaryExpression handle | ||
| // operator overloading including +=, *= assignment expressions | ||
|
|
||
|
|
||
| ArrayExpression(node, state, ancestors) { | ||
| if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) { | ||
| return; | ||
| } | ||
| // Don't wrap arrays that are arguments to functions expecting raw arrays. | ||
| // Walk ancestors to find the nearest CallExpression and check the registry. | ||
| for (let i = ancestors.length - 1; i >= 0; i--) { | ||
| const a = ancestors[i]; | ||
| if (a.type === 'CallExpression') { | ||
| const name = a.callee?.name; | ||
| if (name && ARRAY_ARG_FUNCTIONS[name]) { | ||
| const argIndex = a.arguments.indexOf(node); | ||
| if (argIndex !== -1 && ARRAY_ARG_FUNCTIONS[name].has(argIndex)) { | ||
| return; | ||
| } | ||
| } | ||
| break; // only check nearest CallExpression | ||
| } | ||
| } | ||
|
|
||
| const original = JSON.parse(JSON.stringify(node)); | ||
| node.type = 'CallExpression'; | ||
| node.callee = { | ||
|
|
@@ -1700,6 +1783,30 @@ export function transpileStrandsToJS(p5, sourceString, srcLocations, scope) { | |
| // First pass: transform .set() calls in control flow to use intermediate variables | ||
| transformSetCallsInControlFlow(ast, uniformCallbackNames); | ||
|
|
||
| // paletteLerp pre-pass: must run before the main pass so ArrayExpression | ||
| // doesn't wrap [[color,pos],...] as a vector before we can split the pairs. | ||
| ancestor(ast, { | ||
|
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. Do we still need this with the |
||
| CallExpression(node, state, ancestors) { | ||
| if (node.callee?.type !== 'Identifier' || node.callee?.name !== 'paletteLerp') return; | ||
| if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) return; | ||
| const [stopsArg, tArg] = node.arguments; | ||
| if (node.arguments.length !== 2) throw new Error(`paletteLerp() requires 2 arguments: ([[color,pos],...], t)`); | ||
| if (stopsArg.type !== 'ArrayExpression') throw new Error(`paletteLerp() first argument must be an array literal`); | ||
| const stops = stopsArg.elements; | ||
| if (stops.length < 2 || stops.length > 8) throw new Error(`paletteLerp() requires 2–8 color stops, got ${stops.length}`); | ||
| for (let i = 0; i < stops.length; i++) { | ||
| if (stops[i].type !== 'ArrayExpression' || stops[i].elements.length !== 2) | ||
| throw new Error(`paletteLerp() stop ${i} must be [color(...), position]`); | ||
| } | ||
| node.callee = { type: 'Identifier', name: '__p5.paletteLerp' }; | ||
| node.arguments = [ | ||
| { type: 'ArrayExpression', elements: stops.map(s => s.elements[0]) }, | ||
| { type: 'ArrayExpression', elements: stops.map(s => s.elements[1]) }, | ||
| tArg | ||
| ]; | ||
| } | ||
| }, undefined, { uniformCallbackNames }); | ||
|
|
||
| // Second pass: transform everything except if/for statements using normal ancestor traversal | ||
| const nonControlFlowCallbacks = { ...ASTCallbacks }; | ||
| delete nonControlFlowCallbacks.IfStatement; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is it possible to generalize this too? Ideally we don't need anything specific to one function in the transpiler, and it's all generalized enough that we could add to it for another function if needed. I see that currently this is used to split the array of pairs into two separate arrays. Is this needed if we don't do that?