Skip to content

Commit 6a13423

Browse files
committed
Use shader data tracker
1 parent f6c8673 commit 6a13423

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

src/materials/EffectMaterial.ts

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Texture, Uniform } from "three";
1+
import { IUniform, Texture, Uniform } from "three";
22
import { EffectShaderSection } from "../enums/EffectShaderSection.js";
33
import { EffectShaderSection as Section } from "../enums/EffectShaderSection.js";
4+
import { ShaderDataTracker } from "../utils/ShaderDataTracker.js";
45
import { FullscreenMaterial } from "./FullscreenMaterial.js";
56

67
import fragmentTemplate from "./shaders/effect.frag";
@@ -15,16 +16,10 @@ import vertexTemplate from "./shaders/effect.vert";
1516
export class EffectMaterial extends FullscreenMaterial {
1617

1718
/**
18-
* Keeps track of custom uniforms.
19+
* Keeps track of shader data.
1920
*/
2021

21-
private readonly customUniforms: Map<string, Uniform>;
22-
23-
/**
24-
* Keeps track of custom defines.
25-
*/
26-
27-
private readonly customDefines: Map<string, string | number | boolean>;
22+
private readonly shaderDataTracker: ShaderDataTracker;
2823

2924
/**
3025
* Constructs a new effect material.
@@ -43,8 +38,7 @@ export class EffectMaterial extends FullscreenMaterial {
4338
}
4439
});
4540

46-
this.customUniforms = new Map<string, Uniform>();
47-
this.customDefines = new Map<string, string | number | boolean>();
41+
this.shaderDataTracker = new ShaderDataTracker();
4842

4943
// Ensure that gl_FragColor is defined in the default shader.
5044
this.fragmentShader = "#include <pp_default_output_pars_fragment>\n\n" + this.fragmentShader;
@@ -150,23 +144,10 @@ export class EffectMaterial extends FullscreenMaterial {
150144

151145
setDefines(defines: Map<string, string | number | boolean>): this {
152146

153-
// Reset defines.
154-
for(const key of this.customDefines.keys()) {
155-
156-
delete this.defines[key];
147+
this.shaderDataTracker
148+
.applyDefines(this, defines)
149+
.trackDefines(defines);
157150

158-
}
159-
160-
this.customDefines.clear();
161-
162-
for(const entry of defines.entries()) {
163-
164-
this.defines[entry[0]] = entry[1];
165-
this.customDefines.set(entry[0], entry[1]);
166-
167-
}
168-
169-
this.needsUpdate = true;
170151
return this;
171152

172153
}
@@ -180,26 +161,20 @@ export class EffectMaterial extends FullscreenMaterial {
180161
* @return This material.
181162
*/
182163

183-
setUniforms(uniforms: Map<string, Uniform>): this {
184-
185-
// Reset uniforms.
186-
for(const key of this.customUniforms.keys()) {
187-
188-
delete this.uniforms[key];
189-
190-
}
164+
setUniforms(uniforms: Map<string, IUniform>): this {
191165

192-
this.customUniforms.clear();
166+
this.shaderDataTracker
167+
.applyUniforms(this, uniforms)
168+
.trackUniforms(uniforms);
193169

194-
for(const entry of uniforms.entries()) {
170+
return this;
195171

196-
this.uniforms[entry[0]] = entry[1];
197-
this.customUniforms.set(entry[0], entry[1]);
172+
}
198173

199-
}
174+
override dispose(): void {
200175

201-
this.uniformsNeedUpdate = true;
202-
return this;
176+
super.dispose();
177+
this.shaderDataTracker.dispose();
203178

204179
}
205180

0 commit comments

Comments
 (0)