Skip to content

Commit 6006d47

Browse files
authored
Merge branch 'dev-2.0' into docs-smoothstep-strands
2 parents 96b9f49 + aaff6a7 commit 6006d47

26 files changed

Lines changed: 716 additions & 126 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"test/**/*.js": "eslint",
2020
"utils/**/*.{js,mjs}": "eslint"
2121
},
22-
"version": "2.2.0",
22+
"version": "2.2.1-rc.0",
2323
"dependencies": {
2424
"@davepagurek/bezier-path": "^0.0.2",
2525
"@japont/unicode-range": "^1.0.0",

src/color/p5.Color.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,26 +364,45 @@ class Color {
364364
* </code>
365365
* </div>
366366
*/
367-
toString(format) {
368-
if (format === undefined && this._defaultStringValue !== undefined) {
367+
toString(format) {
368+
if (format === undefined && this._defaultStringValue !== undefined) {
369369
return this._defaultStringValue;
370+
}
371+
372+
let outputFormat = format;
373+
if (format === '#rrggbb') {
374+
outputFormat = 'hex';
370375
}
371376

372377
const key = `${this._color.space.id}-${this._color.coords.join(',')}-${this._color.alpha}-${format}`;
373378
let colorString = serializationMap.get(key);
374379

375-
if(!colorString){
380+
if (!colorString) {
376381
colorString = serialize(this._color, {
377-
format
382+
format: outputFormat
378383
});
384+
385+
if (format === '#rrggbb') {
386+
colorString = String(colorString);
387+
if (colorString.length === 4) {
388+
const r = colorString[1];
389+
const g = colorString[2];
390+
const b = colorString[3];
391+
colorString = `#${r}${r}${g}${g}${b}${b}`;
392+
}
393+
if (colorString.length > 7) {
394+
colorString = colorString.slice(0, 7);
395+
}
396+
colorString = colorString.toLowerCase();
397+
}
398+
379399
if (serializationMap.size > 1000) {
380400
serializationMap.delete(serializationMap.keys().next().value)
381401
}
382402
serializationMap.set(key, colorString);
383403
}
384404
return colorString;
385405
}
386-
387406
/**
388407
* Checks the contrast between two colors. This method returns a boolean
389408
* value to indicate if the two color has enough contrast. `true` means that

src/dom/p5.MediaElement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class MediaElement extends Element {
6666
source.src = newValue;
6767
elt.appendChild(source);
6868
self.elt.src = newValue;
69-
self.modified = true;
69+
self._modified = true;
7070
}
7171
});
7272

src/strands/strands_api.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,86 @@ import * as FES from './strands_FES'
2121
import { getNodeDataFromID } from './ir_dag'
2222
import { StrandsNode, createStrandsNode } from './strands_node'
2323

24+
const BUILTIN_GLOBAL_SPECS = {
25+
width: { typeInfo: DataType.float1, get: (p) => p.width },
26+
height: { typeInfo: DataType.float1, get: (p) => p.height },
27+
mouseX: { typeInfo: DataType.float1, get: (p) => p.mouseX },
28+
mouseY: { typeInfo: DataType.float1, get: (p) => p.mouseY },
29+
pmouseX: { typeInfo: DataType.float1, get: (p) => p.pmouseX },
30+
pmouseY: { typeInfo: DataType.float1, get: (p) => p.pmouseY },
31+
winMouseX: { typeInfo: DataType.float1, get: (p) => p.winMouseX },
32+
winMouseY: { typeInfo: DataType.float1, get: (p) => p.winMouseY },
33+
pwinMouseX: { typeInfo: DataType.float1, get: (p) => p.pwinMouseX },
34+
pwinMouseY: { typeInfo: DataType.float1, get: (p) => p.pwinMouseY },
35+
frameCount: { typeInfo: DataType.float1, get: (p) => p.frameCount },
36+
deltaTime: { typeInfo: DataType.float1, get: (p) => p.deltaTime },
37+
displayWidth: { typeInfo: DataType.float1, get: (p) => p.displayWidth },
38+
displayHeight: { typeInfo: DataType.float1, get: (p) => p.displayHeight },
39+
windowWidth: { typeInfo: DataType.float1, get: (p) => p.windowWidth },
40+
windowHeight: { typeInfo: DataType.float1, get: (p) => p.windowHeight },
41+
mouseIsPressed: { typeInfo: DataType.bool1, get: (p) => p.mouseIsPressed },
42+
}
43+
44+
function _getBuiltinGlobalsCache(strandsContext) {
45+
if (!strandsContext._builtinGlobals || strandsContext._builtinGlobals.dag !== strandsContext.dag) {
46+
strandsContext._builtinGlobals = {
47+
dag: strandsContext.dag,
48+
nodes: new Map(),
49+
uniformsAdded: new Set(),
50+
}
51+
}
52+
// return the cache
53+
return strandsContext._builtinGlobals
54+
}
55+
56+
function getBuiltinGlobalNode(strandsContext, name) {
57+
const spec = BUILTIN_GLOBAL_SPECS[name]
58+
if (!spec) return null
59+
60+
const cache = _getBuiltinGlobalsCache(strandsContext)
61+
const uniformName = `_p5_global_${name}`
62+
const cached = cache.nodes.get(uniformName)
63+
if (cached) return cached
64+
65+
if (!cache.uniformsAdded.has(uniformName)) {
66+
cache.uniformsAdded.add(uniformName)
67+
strandsContext.uniforms.push({
68+
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+
})
75+
}
76+
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
82+
}
83+
84+
function installBuiltinGlobalAccessors(strandsContext) {
85+
if (strandsContext._builtinGlobalsAccessorsInstalled) return
86+
87+
const getRuntimeP5Instance = () => strandsContext.renderer?._pInst || strandsContext.p5?.instance
88+
89+
for (const name of Object.keys(BUILTIN_GLOBAL_SPECS)) {
90+
const spec = BUILTIN_GLOBAL_SPECS[name]
91+
Object.defineProperty(window, name, {
92+
get: () => {
93+
if (strandsContext.active) {
94+
return getBuiltinGlobalNode(strandsContext, name);
95+
}
96+
const inst = getRuntimeP5Instance()
97+
return spec.get(inst);
98+
},
99+
})
100+
}
101+
strandsContext._builtinGlobalsAccessorsInstalled = true
102+
}
103+
24104
//////////////////////////////////////////////
25105
// User nodes
26106
//////////////////////////////////////////////
@@ -419,6 +499,8 @@ function enforceReturnTypeMatch(strandsContext, expectedType, returned, hookName
419499
return returnedNodeID;
420500
}
421501
export function createShaderHooksFunctions(strandsContext, fn, shader) {
502+
installBuiltinGlobalAccessors(strandsContext)
503+
422504
// Add shader context to hooks before spreading
423505
const vertexHooksWithContext = Object.fromEntries(
424506
Object.entries(shader.hooks.vertex).map(([name, hook]) => [name, { ...hook, shaderContext: 'vertex' }])

src/webgl/p5.Framebuffer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Framebuffer {
6262
this.renderer.framebuffers.add(this);
6363

6464
this._isClipApplied = false;
65+
this._useCanvasFormat = settings._useCanvasFormat || false;
6566

6667
this.dirty = { colorTexture: false, depthTexture: false };
6768

src/webgl/p5.Texture.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ class Texture {
225225
data.setModified && data.setModified(true);
226226
}
227227
} else if (this.isSrcP5Image) {
228+
if (data.gifProperties) {
229+
data._animateGif(this._renderer._pInst);
230+
}
228231
// for an image, we only update if the modified field has been set,
229232
// for example, by a call to p5.Image.set
230233
if (data.isModified()) {

src/webgl/utils.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,14 @@ export function setWebGLUniformValue(shader, uniform, data, getTexture, gl) {
281281
"You're trying to use a number as the data for a texture." +
282282
"Please use a texture.",
283283
);
284-
return this;
284+
return;
285285
}
286286
gl.activeTexture(data);
287287
gl.uniform1i(location, data);
288288
} else {
289289
gl.activeTexture(gl.TEXTURE0 + uniform.samplerIndex);
290290
uniform.texture = data instanceof Texture ? data : getTexture(data);
291291
gl.uniform1i(location, uniform.samplerIndex);
292-
if (uniform.texture.src.gifProperties) {
293-
uniform.texture.src._animateGif(this._pInst);
294-
}
295292
}
296293
break;
297294
case gl.SAMPLER_CUBE:

0 commit comments

Comments
 (0)