Skip to content

Commit

Permalink
Update EffectMaterial
Browse files Browse the repository at this point in the history
  • Loading branch information
vanruesc committed Dec 29, 2023
1 parent 6dc8ba8 commit da47bd7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 86 deletions.
129 changes: 55 additions & 74 deletions src/materials/EffectMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Texture, Uniform } from "three";
import { EffectShaderSection, EffectShaderSection as Section } from "../enums/EffectShaderSection.js";
import { GBuffer } from "../enums/GBuffer.js";
import { WebGLExtension } from "../enums/WebGLExtension.js";
import { FullscreenMaterial } from "./FullscreenMaterial.js";

import fragmentTemplate from "./shaders/effect.frag";
Expand Down Expand Up @@ -35,142 +33,125 @@ export class EffectMaterial extends FullscreenMaterial {
* The current gBuffer.
*/

get gBuffer(): Record<GBuffer, Texture> {
get gBuffer(): Record<string, Texture | null> {

return this.uniforms.gBuffer.value as Record<GBuffer, Texture>;
return this.uniforms.gBuffer.value as Record<string, Texture | null>;

}

set gBuffer(value: Record<GBuffer, Texture>) {
set gBuffer(value: Record<string, Texture | null>) {

this.uniforms.gBuffer.value = value;

}

/**
* Sets the shader parts.
*
* @param shaderParts - A collection of shader code snippets. See {@link EffectShaderSection}.
* @return This material.
* Indicates whether output color space conversion is enabled.
*/

setShaderParts(shaderParts: Map<EffectShaderSection, string | null>): this {
get colorSpaceConversion(): boolean {

this.fragmentShader = fragmentTemplate
.replace(Section.FRAGMENT_HEAD, shaderParts.get(Section.FRAGMENT_HEAD) ?? "")
.replace(Section.FRAGMENT_MAIN_UV, shaderParts.get(Section.FRAGMENT_MAIN_UV) ?? "")
.replace(Section.FRAGMENT_MAIN_IMAGE, shaderParts.get(Section.FRAGMENT_MAIN_IMAGE) ?? "");
return (this.defines.COLOR_SPACE_CONVERSION !== undefined);

this.vertexShader = vertexTemplate
.replace(Section.VERTEX_HEAD, shaderParts.get(Section.VERTEX_HEAD) ?? "")
.replace(Section.VERTEX_MAIN_SUPPORT, shaderParts.get(Section.VERTEX_MAIN_SUPPORT) ?? "");
}

this.needsUpdate = true;
return this;
set colorSpaceConversion(value: boolean) {

}
if(this.colorSpaceConversion !== value) {

/**
* Sets the shader macros.
*
* @param defines - A collection of preprocessor macro definitions.
* @return This material.
*/
if(value) {

setDefines(defines: Map<string, string | number | boolean>): this {
this.defines.COLOR_SPACE_CONVERSION = "1";

for(const entry of defines.entries()) {
} else {

this.defines[entry[0]] = entry[1];
delete this.defines.COLOR_SPACE_CONVERSION;

}
}

this.needsUpdate = true;
return this;
this.needsUpdate = true;

}

}

/**
* Sets the shader uniforms.
*
* @param uniforms - A collection of uniforms.
* @return This material.
* The current animation time in seconds.
*/

setUniforms(uniforms: Map<string, Uniform>): this {
get time(): number {

for(const entry of uniforms.entries()) {
return this.uniforms.time.value as number;

this.uniforms[entry[0]] = entry[1];
}

}
set time(value: number) {

return this;
this.uniforms.time.value = value;

}

/**
* Sets the required shader extensions.
* Sets the shader parts.
*
* @param extensions - A collection of extensions.
* @param shaderParts - A collection of shader code snippets. See {@link EffectShaderSection}.
* @return This material.
*/

setExtensions(extensions: Set<WebGLExtension>): this {

for(const extension of extensions) {
setShaderParts(shaderParts: Map<EffectShaderSection, string | null>): this {

this.extensions[extension] = true;
this.fragmentShader = fragmentTemplate
.replace(Section.FRAGMENT_MAIN_IMAGE, shaderParts.get(Section.FRAGMENT_MAIN_IMAGE) ?? "")
.replace(Section.FRAGMENT_MAIN_GDATA, shaderParts.get(Section.FRAGMENT_MAIN_GDATA) ?? "")
.replace(Section.FRAGMENT_MAIN_UV, shaderParts.get(Section.FRAGMENT_MAIN_UV) ?? "")
.replace(Section.FRAGMENT_HEAD, shaderParts.get(Section.FRAGMENT_HEAD) ?? "")
.replace(Section.FRAGMENT_HEAD_GBUFFER, shaderParts.get(Section.FRAGMENT_HEAD_GBUFFER) ?? "");

}
this.vertexShader = vertexTemplate
.replace(Section.VERTEX_MAIN_SUPPORT, shaderParts.get(Section.VERTEX_MAIN_SUPPORT) ?? "")
.replace(Section.VERTEX_HEAD, shaderParts.get(Section.VERTEX_HEAD) ?? "");

this.needsUpdate = true;
return this;

}

/**
* Indicates whether output color space conversion is enabled.
* Sets the shader macros.
*
* @param defines - A collection of preprocessor macro definitions.
* @return This material.
*/

get colorSpaceConversion(): boolean {

return (this.defines.COLOR_SPACE_CONVERSION !== undefined);

}

set colorSpaceConversion(value: boolean) {

if(this.colorSpaceConversion !== value) {

if(value) {

this.defines.COLOR_SPACE_CONVERSION = "1";

} else {

delete this.defines.COLOR_SPACE_CONVERSION;
setDefines(defines: Map<string, string | number | boolean>): this {

}
for(const entry of defines.entries()) {

this.needsUpdate = true;
this.defines[entry[0]] = entry[1];

}

this.needsUpdate = true;
return this;

}

/**
* The current animation time in seconds.
* Sets the shader uniforms.
*
* @param uniforms - A collection of uniforms.
* @return This material.
*/

get time(): number {
setUniforms(uniforms: Map<string, Uniform>): this {

return this.uniforms.time.value as number;
for(const entry of uniforms.entries()) {

}
this.uniforms[entry[0]] = entry[1];

set time(value: number) {
}

this.uniforms.time.value = value;
return this;

}

Expand Down
26 changes: 16 additions & 10 deletions src/materials/shaders/effect.frag
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_camera_pars_fragment>
#include <pp_resolution_pars_fragment>
#include <pp_depth_utils_pars_fragment>
#include <pp_colorspace_conversion_pars_fragment>
#include <pp_colorspace_pars_fragment>
#include <pp_colorspace_conversion_pars_fragment>
#include <pp_depth_precision_pars_fragment>
#include <pp_depth_utils_pars_fragment>
#include <pp_frame_buffer_precision_pars_fragment>
#include <pp_gbuffer_output_pars_fragment>
#include <pp_resolution_pars_fragment>

#include <colorspace_pars_fragment>
#include <common>
#include <packing>
#include <dithering_pars_fragment>
#include <packing>

#define packFloatToRGBA(v) packDepthToRGBA(v)
#define unpackRGBAToFloat(v) unpackRGBAToDepth(v)

// TODO GData struct uniform
$FRAGMENT_HEAD_GDATA
$FRAGMENT_HEAD_GBUFFER

uniform GBuffer gBuffer;
uniform float time;
in vec2 vUv;

FRAGMENT_HEAD
$FRAGMENT_HEAD

void main() {

FRAGMENT_MAIN_UV
$FRAGMENT_MAIN_UV
$FRAGMENT_MAIN_GDATA

vec4 color0 = texture(inputBuffer, UV);
vec4 color0 = gData.color;
vec4 color1 = vec4(0.0);

FRAGMENT_MAIN_IMAGE
$FRAGMENT_MAIN_IMAGE

color0.a = clamp(color0.a, 0.0, 1.0);
outputColor = color0;
Expand Down
4 changes: 2 additions & 2 deletions src/materials/shaders/effect.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
uniform float time;
out vec2 vUv;

VERTEX_HEAD
$VERTEX_HEAD

void main() {

vUv = position.xy * 0.5 + 0.5;

VERTEX_MAIN_SUPPORT
$VERTEX_MAIN_SUPPORT

gl_Position = vec4(position.xy, 1.0, 1.0);

Expand Down

0 comments on commit da47bd7

Please sign in to comment.