Skip to content

Commit 55ed92f

Browse files
authored
Merge pull request #8530 from perminder-17/Adding-millis()
Refactor global node handling and add millis function
2 parents 8bf8b6b + 12ea94f commit 55ed92f

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

src/strands/strands_api.js

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,45 @@ function _getBuiltinGlobalsCache(strandsContext) {
5353
return strandsContext._builtinGlobals
5454
}
5555

56-
function getBuiltinGlobalNode(strandsContext, name) {
57-
const spec = BUILTIN_GLOBAL_SPECS[name]
58-
if (!spec) return null
56+
function getOrCreateUniformNode(strandsContext, uniformName, typeInfo, defaultValueFn) {
57+
const cache = _getBuiltinGlobalsCache(strandsContext);
5958

60-
const cache = _getBuiltinGlobalsCache(strandsContext)
61-
const uniformName = `_p5_global_${name}`
62-
const cached = cache.nodes.get(uniformName)
63-
if (cached) return cached
59+
const cached = cache.nodes.get(uniformName);
60+
if (cached) return cached;
6461

6562
if (!cache.uniformsAdded.has(uniformName)) {
66-
cache.uniformsAdded.add(uniformName)
63+
cache.uniformsAdded.add(uniformName);
6764
strandsContext.uniforms.push({
6865
name: uniformName,
69-
typeInfo: spec.typeInfo,
70-
defaultValue: () => {
71-
const p5Instance = strandsContext.renderer?._pInst || strandsContext.p5?.instance
72-
return p5Instance ? spec.get(p5Instance) : undefined
73-
},
74-
})
66+
typeInfo,
67+
defaultValue: defaultValueFn,
68+
});
7569
}
7670

77-
const { id, dimension } = build.variableNode(strandsContext, spec.typeInfo, uniformName)
78-
const node = createStrandsNode(id, dimension, strandsContext)
79-
node._originalBuiltinName = name
80-
cache.nodes.set(uniformName, node)
81-
return node
71+
const { id, dimension } = build.variableNode(strandsContext, typeInfo, uniformName);
72+
const node = createStrandsNode(id, dimension, strandsContext);
73+
cache.nodes.set(uniformName, node);
74+
return node;
75+
}
76+
77+
function getBuiltinGlobalNode(strandsContext, name) {
78+
const spec = BUILTIN_GLOBAL_SPECS[name];
79+
if (!spec) return null;
80+
81+
const uniformName = `_p5_global_${name}`;
82+
const instance = strandsContext.renderer?._pInst || strandsContext.p5?.instance;
83+
84+
const node = getOrCreateUniformNode(
85+
strandsContext,
86+
uniformName,
87+
spec.typeInfo,
88+
() => {
89+
return instance ? spec.get(instance) : undefined;
90+
}
91+
);
92+
93+
node._originalBuiltinName = name;
94+
return node;
8295
}
8396

8497
function installBuiltinGlobalAccessors(strandsContext) {
@@ -241,6 +254,7 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
241254
// Add noise function with backend-agnostic implementation
242255
const originalNoise = fn.noise;
243256
const originalNoiseDetail = fn.noiseDetail;
257+
const originalMillis = fn.millis;
244258

245259
strandsContext._noiseOctaves = null;
246260
strandsContext._noiseAmpFalloff = null;
@@ -303,6 +317,21 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
303317
return createStrandsNode(id, dimension, strandsContext);
304318
};
305319

320+
fn.millis = function (...args) {
321+
if (!strandsContext.active) {
322+
return originalMillis.apply(this, args);
323+
}
324+
const instance = strandsContext.renderer?._pInst || strandsContext.p5?.instance;
325+
return getOrCreateUniformNode(
326+
strandsContext,
327+
'_p5_global_millis',
328+
DataType.float1,
329+
() => {
330+
return instance ? instance.millis() : undefined;
331+
}
332+
);
333+
};
334+
306335
// Next is type constructors and uniform functions.
307336
// For some of them, we have aliases so that you can write either a more human-readable
308337
// variant or also one more directly translated from GLSL, or to be more compatible with

0 commit comments

Comments
 (0)