Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shader material written in wgsl as shadow depth wrapper #14755

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {
}

public preProcessShaderCode(code: string): string {
return (
`struct ${WebGPUShaderProcessor.InternalsUBOName} {\n yFactor_: f32,\n textureOutputHeight_: f32,\n};\nvar<uniform> ${internalsVarName} : ${WebGPUShaderProcessor.InternalsUBOName};\n` +
RemoveComments(code)
);
// Same check as in webgpuShaderProcessorsGLSL to avoid same ubDelcaration to be injected twice.
const ubDeclaration = `struct ${WebGPUShaderProcessor.InternalsUBOName} {\n yFactor_: f32,\n textureOutputHeight_: f32,\n};\nvar<uniform> ${internalsVarName} : ${WebGPUShaderProcessor.InternalsUBOName};\n`;
const alreadyInjected = code.indexOf(ubDeclaration) !== -1;
return alreadyInjected ? code : ubDeclaration + RemoveComments(code);
}

public varyingProcessor(varying: string, isFragment: boolean, preProcessors: { [key: string]: string }) {
Expand Down
22 changes: 16 additions & 6 deletions packages/dev/core/src/Materials/shadowDepthWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { ShadowGenerator } from "../Lights/Shadows/shadowGenerator";
import { RandomGUID } from "../Misc/guid";
import { DrawWrapper } from "./drawWrapper";
import { EngineStore } from "../Engines/engineStore";
import type { ShaderMaterial } from "./shaderMaterial";
import { ShaderLanguage } from "./shaderLanguage";

/**
* Options to be used when creating a shadow depth material
Expand Down Expand Up @@ -240,22 +242,29 @@ export class ShadowDepthWrapper {
fragmentCode = origEffect.fragmentSourceCodeBeforeMigration;

if (!this.doNotInjectCode) {
// vertex code
const shaderLanguage = (this._baseMaterial as ShaderMaterial).options?.shaderLanguage ?? ShaderLanguage.GLSL;
Popov72 marked this conversation as resolved.
Show resolved Hide resolved
// Declare the shadow map includes
const vertexNormalBiasCode =
this._options && this._options.remappedVariables
? `#include<shadowMapVertexNormalBias>(${this._options.remappedVariables.join(",")})`
: Effect.IncludesShadersStore["shadowMapVertexNormalBias"],
: `#include<shadowMapVertexNormalBias>`,
vertexMetricCode =
this._options && this._options.remappedVariables
? `#include<shadowMapVertexMetric>(${this._options.remappedVariables.join(",")})`
: Effect.IncludesShadersStore["shadowMapVertexMetric"],
: `#include<shadowMapVertexMetric>`,
fragmentSoftTransparentShadow =
this._options && this._options.remappedVariables
? `#include<shadowMapFragmentSoftTransparentShadow>(${this._options.remappedVariables.join(",")})`
: Effect.IncludesShadersStore["shadowMapFragmentSoftTransparentShadow"],
fragmentBlockCode = Effect.IncludesShadersStore["shadowMapFragment"];
: `#include<shadowMapFragmentSoftTransparentShadow>`,
fragmentBlockCode = `#include<shadowMapFragment>`,
vertexExtraDeclartion = `#include<shadowMapVertexExtraDeclaration>`;

vertexCode = vertexCode.replace(/void\s+?main/g, Effect.IncludesShadersStore["shadowMapVertexExtraDeclaration"] + "\nvoid main");
// vertex code
if (shaderLanguage === ShaderLanguage.GLSL) {
vertexCode = vertexCode.replace(/void\s+?main/g, `\n${vertexExtraDeclartion}\nvoid main`);
} else {
vertexCode = vertexCode.replace(/@vertex/g, `\n${vertexExtraDeclartion}\n@vertex`);
}
vertexCode = vertexCode.replace(/#define SHADOWDEPTH_NORMALBIAS|#define CUSTOM_VERTEX_UPDATE_WORLDPOS/g, vertexNormalBiasCode);

if (vertexCode.indexOf("#define SHADOWDEPTH_METRIC") !== -1) {
Expand Down Expand Up @@ -306,6 +315,7 @@ export class ShadowDepthWrapper {
samplers: origEffect.getSamplers(),
defines: join + "\n" + origEffect.defines.replace("#define SHADOWS", "").replace(/#define SHADOW\d/g, ""),
indexParameters: origEffect.getIndexParameters(),
shaderLanguage: (this._baseMaterial as ShaderMaterial).options?.shaderLanguage ?? ShaderLanguage.GLSL,
Popov72 marked this conversation as resolved.
Show resolved Hide resolved
},
engine
);
Expand Down