sparcoon 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -84,17 +84,28 @@ function buildFXDiffuseMaterial(varyings, blending, useAlphaHashing, alphaTest,
84
84
  }
85
85
  `
86
86
  : "";
87
+ const seenCacheKeys = new Set();
88
+ const uniqueHelpers = (nodes) => nodes
89
+ .filter((n) => {
90
+ if (seenCacheKeys.has(n.cacheKey)) {
91
+ return false;
92
+ }
93
+ seenCacheKeys.add(n.cacheKey);
94
+ return true;
95
+ })
96
+ .map((n) => n.helperFunctions)
97
+ .join("\n");
87
98
  const fragmentPreamble = [
88
99
  PARTICLE_DEFINES,
89
100
  "varying vec2 p_uv;",
90
101
  varyingDeclarations.join("\n"),
91
102
  albedoNodes.flatMap((n) => n.uniformDeclarations).join("\n"),
92
- albedoNodes.map((n) => n.helperFunctions).join("\n"),
103
+ uniqueHelpers(albedoNodes),
93
104
  normalNodes.flatMap((n) => n.uniformDeclarations).join("\n"),
94
- normalNodes.map((n) => n.helperFunctions).join("\n"),
105
+ uniqueHelpers(normalNodes),
95
106
  blendReorientedNormalsHelper,
96
107
  emissionNodes.flatMap((n) => n.uniformDeclarations).join("\n"),
97
- emissionNodes.map((n) => n.helperFunctions).join("\n"),
108
+ uniqueHelpers(emissionNodes),
98
109
  ].join("\n");
99
110
  for (const node of albedoNodes) {
100
111
  Object.assign(shader.uniforms, node.uniforms);
@@ -59,12 +59,23 @@ function buildFXUnlitMaterial(varyings, blending, useAlphaHashing, alphaTest, al
59
59
  mvPosition.xy += billboardOffset;
60
60
  gl_Position = projectionMatrix * mvPosition;
61
61
  `);
62
+ const seenCacheKeys = new Set();
63
+ const uniqueHelpers = (nodes) => nodes
64
+ .filter((n) => {
65
+ if (seenCacheKeys.has(n.cacheKey)) {
66
+ return false;
67
+ }
68
+ seenCacheKeys.add(n.cacheKey);
69
+ return true;
70
+ })
71
+ .map((n) => n.helperFunctions)
72
+ .join("\n");
62
73
  const fragmentPreamble = [
63
74
  PARTICLE_DEFINES,
64
75
  "varying vec2 p_uv;",
65
76
  varyingDeclarations.join("\n"),
66
77
  albedoNodes.flatMap((n) => n.uniformDeclarations).join("\n"),
67
- albedoNodes.map((n) => n.helperFunctions).join("\n"),
78
+ uniqueHelpers(albedoNodes),
68
79
  ].join("\n");
69
80
  for (const node of albedoNodes) {
70
81
  Object.assign(shader.uniforms, node.uniforms);
@@ -2,19 +2,21 @@ import { buildGradientTexture } from '../../miscellaneous/miscellaneous.js';
2
2
  import { checkSRGBSupport } from '../../miscellaneous/webglCapabilities.js';
3
3
  import { FXColorNode } from './FXColorNode.js';
4
4
 
5
+ const INSTANCES = [];
5
6
  class FXColorOverLifeNode extends FXColorNode {
6
7
  constructor(colors) {
7
8
  if (colors.length === 0) {
8
9
  throw new Error("FXColorOverLifeNode: colors array cannot be empty");
9
10
  }
10
11
  super();
12
+ INSTANCES.push(this);
13
+ const uniformName = `u_ColorOverLife_${INSTANCES.length - 1}`;
11
14
  this._private_gradientTexture = buildGradientTexture(colors);
12
15
  this.affectsDepth = true;
13
16
  this.cacheKey = "color-over-life";
14
- this.uniformDeclarations = ["uniform sampler2D u_ColorOverLife;"];
17
+ this.uniformDeclarations = [`uniform sampler2D ${uniformName};`];
15
18
  this.uniforms = {
16
- // eslint-disable-next-line @typescript-eslint/naming-convention
17
- u_ColorOverLife: { value: this._private_gradientTexture },
19
+ [uniformName]: { value: this._private_gradientTexture },
18
20
  };
19
21
  this.helperFunctions = `
20
22
  vec4 sampleColorOverLife(sampler2D gradientTexture) {
@@ -28,10 +30,14 @@ class FXColorOverLifeNode extends FXColorNode {
28
30
  return color;
29
31
  }
30
32
  `;
31
- this.colorExpression = "sampleColorOverLife(u_ColorOverLife)";
33
+ this.colorExpression = `sampleColorOverLife(${uniformName})`;
32
34
  }
33
35
  destroy() {
34
36
  this._private_gradientTexture.dispose();
37
+ const index = INSTANCES.indexOf(this);
38
+ if (index !== -1) {
39
+ INSTANCES.splice(index, 1);
40
+ }
35
41
  }
36
42
  }
37
43
 
@@ -8,4 +8,5 @@ export interface FXAnimatedTextureNodeOptions {
8
8
  }
9
9
  export declare class FXAnimatedTextureNode extends FXTextureNode {
10
10
  constructor({ texture, columns, rows, interpolate }: FXAnimatedTextureNodeOptions);
11
+ destroy(): void;
11
12
  }
@@ -1,26 +1,29 @@
1
1
  import { checkSRGBSupport } from '../../miscellaneous/webglCapabilities.js';
2
2
  import { FXTextureNode } from './FXTextureNode.js';
3
3
 
4
+ const INSTANCES = [];
4
5
  class FXAnimatedTextureNode extends FXTextureNode {
5
6
  constructor({ texture, columns, rows, interpolate = false }) {
6
7
  super();
8
+ INSTANCES.push(this);
9
+ const idx = INSTANCES.length - 1;
10
+ const uniformTexture = `u_AnimatedTexture_${idx}`;
11
+ const uniformColumns = `u_AnimatedTextureColumns_${idx}`;
12
+ const uniformRows = `u_AnimatedTextureRows_${idx}`;
7
13
  const gammaCorrection = checkSRGBSupport()
8
14
  ? ""
9
15
  : "color = vec4(pow(color.rgb, vec3(2.2)), color.a);";
10
16
  this.affectsDepth = true;
11
17
  this.cacheKey = interpolate ? "animated-interpolated" : "animated";
12
18
  this.uniformDeclarations = [
13
- "uniform sampler2D u_AnimatedTexture;",
14
- "uniform float u_AnimatedTextureColumns;",
15
- "uniform float u_AnimatedTextureRows;",
19
+ `uniform sampler2D ${uniformTexture};`,
20
+ `uniform float ${uniformColumns};`,
21
+ `uniform float ${uniformRows};`,
16
22
  ];
17
23
  this.uniforms = {
18
- // eslint-disable-next-line @typescript-eslint/naming-convention
19
- u_AnimatedTexture: { value: texture },
20
- // eslint-disable-next-line @typescript-eslint/naming-convention
21
- u_AnimatedTextureColumns: { value: columns },
22
- // eslint-disable-next-line @typescript-eslint/naming-convention
23
- u_AnimatedTextureRows: { value: rows },
24
+ [uniformTexture]: { value: texture },
25
+ [uniformColumns]: { value: columns },
26
+ [uniformRows]: { value: rows },
24
27
  };
25
28
  this.helperFunctions = interpolate
26
29
  ? `
@@ -72,8 +75,13 @@ class FXAnimatedTextureNode extends FXTextureNode {
72
75
  return color;
73
76
  }
74
77
  `;
75
- this.colorExpression =
76
- "sampleAnimatedTexture(u_AnimatedTexture, p_uv, u_AnimatedTextureColumns, u_AnimatedTextureRows)";
78
+ this.colorExpression = `sampleAnimatedTexture(${uniformTexture}, p_uv, ${uniformColumns}, ${uniformRows})`;
79
+ }
80
+ destroy() {
81
+ const index = INSTANCES.indexOf(this);
82
+ if (index !== -1) {
83
+ INSTANCES.splice(index, 1);
84
+ }
77
85
  }
78
86
  }
79
87
 
@@ -2,4 +2,5 @@ import type { Texture } from "three";
2
2
  import { FXTextureNode } from "./FXTextureNode";
3
3
  export declare class FXStaticTextureNode extends FXTextureNode {
4
4
  constructor(texture: Texture);
5
+ destroy(): void;
5
6
  }
@@ -1,15 +1,17 @@
1
1
  import { checkSRGBSupport } from '../../miscellaneous/webglCapabilities.js';
2
2
  import { FXTextureNode } from './FXTextureNode.js';
3
3
 
4
+ const INSTANCES = [];
4
5
  class FXStaticTextureNode extends FXTextureNode {
5
6
  constructor(texture) {
6
7
  super();
8
+ INSTANCES.push(this);
9
+ const uniformName = `u_StaticTexture_${INSTANCES.length - 1}`;
7
10
  this.affectsDepth = true;
8
11
  this.cacheKey = "static";
9
- this.uniformDeclarations = ["uniform sampler2D u_StaticTexture;"];
12
+ this.uniformDeclarations = [`uniform sampler2D ${uniformName};`];
10
13
  this.uniforms = {
11
- // eslint-disable-next-line @typescript-eslint/naming-convention
12
- u_StaticTexture: { value: texture },
14
+ [uniformName]: { value: texture },
13
15
  };
14
16
  this.helperFunctions = `
15
17
  vec4 sampleStaticTexture(sampler2D colorTexture, vec2 uv) {
@@ -19,7 +21,13 @@ class FXStaticTextureNode extends FXTextureNode {
19
21
  return color;
20
22
  }
21
23
  `;
22
- this.colorExpression = "sampleStaticTexture(u_StaticTexture, p_uv)";
24
+ this.colorExpression = `sampleStaticTexture(${uniformName}, p_uv)`;
25
+ }
26
+ destroy() {
27
+ const index = INSTANCES.indexOf(this);
28
+ if (index !== -1) {
29
+ INSTANCES.splice(index, 1);
30
+ }
23
31
  }
24
32
  }
25
33
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sparcoon",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "particles",