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

CamerasetReplacementShader support ReplacementFailureStrategy when failed #2163

Merged
merged 4 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CameraType } from "./enums/CameraType";
import { DepthTextureMode } from "./enums/DepthTextureMode";
import { Downsampling } from "./enums/Downsampling";
import { MSAASamples } from "./enums/MSAASamples";
import { ReplacementFailureStrategy } from "./enums/ReplacementFailureStrategy";
import { Shader } from "./shader/Shader";
import { ShaderData } from "./shader/ShaderData";
import { ShaderMacroCollection } from "./shader/ShaderMacroCollection";
Expand Down Expand Up @@ -103,6 +104,8 @@ export class Camera extends Component {
/** @internal */
_replacementSubShaderTag: ShaderTagKey = null;
/** @internal */
_replacementFailureStrategy: ReplacementFailureStrategy = null;
/** @internal */
@ignoreClone
_cameraIndex: number = -1;

Expand Down Expand Up @@ -584,6 +587,7 @@ export class Camera extends Component {
context.virtualCamera = virtualCamera;
context.replacementShader = this._replacementShader;
context.replacementTag = this._replacementSubShaderTag;
context.replacementFailureStrategy = this._replacementFailureStrategy;

// compute cull frustum.
if (this.enableFrustumCulling && this._frustumChangeFlag.flag) {
Expand Down Expand Up @@ -616,28 +620,35 @@ export class Camera extends Component {
* Set the replacement shader.
* @param shader - Replacement shader
* @param replacementTagName - Sub shader tag name
* @param failureStrategy - Replacement failure strategy, @defaultValue `ReplacementFailureStrategy.KeepOriginalShader`
*
* @remarks
* If replacementTagName is not specified, the first sub shader will be replaced.
* If replacementTagName is specified, the replacement shader will find the first sub shader which has the same tag value get by replacementTagKey.
* If replacementTagName is specified, the replacement shader will find the first sub shader which has the same tag value get by replacementTagKey. If failed to find the sub shader, the strategy will be determined by failureStrategy.
*/
setReplacementShader(shader: Shader, replacementTagName?: string);
setReplacementShader(shader: Shader, replacementTagName?: string, failureStrategy?: ReplacementFailureStrategy);

/**
* Set the replacement shader.
* @param shader - Replacement shader
* @param replacementTag - Sub shader tag
* @param failureStrategy - Replacement failure strategy, @defaultValue `ReplacementFailureStrategy.KeepOriginalShader`
*
* @remarks
* If replacementTag is not specified, the first sub shader will be replaced.
* If replacementTag is specified, the replacement shader will find the first sub shader which has the same tag value get by replacementTagKey.
* If replacementTag is specified, the replacement shader will find the first sub shader which has the same tag value get by replacementTagKey. If failed to find the sub shader, the strategy will be determined by failureStrategy.
*/
setReplacementShader(shader: Shader, replacementTag?: ShaderTagKey);
setReplacementShader(shader: Shader, replacementTag?: ShaderTagKey, failureStrategy?: ReplacementFailureStrategy);

setReplacementShader(shader: Shader, replacementTag?: string | ShaderTagKey): void {
setReplacementShader(
shader: Shader,
replacementTag?: string | ShaderTagKey,
failureStrategy: ReplacementFailureStrategy = ReplacementFailureStrategy.KeepOriginalShader
): void {
this._replacementShader = shader;
this._replacementSubShaderTag =
typeof replacementTag === "string" ? ShaderTagKey.getByName(replacementTag) : replacementTag;
this._replacementFailureStrategy = failureStrategy;
}

/**
Expand All @@ -646,6 +657,7 @@ export class Camera extends Component {
resetReplacementShader(): void {
this._replacementShader = null;
this._replacementSubShaderTag = null;
this._replacementFailureStrategy = null;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/RenderPipeline/BasicRenderPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BackgroundMode } from "../enums/BackgroundMode";
import { BackgroundTextureFillMode } from "../enums/BackgroundTextureFillMode";
import { CameraClearFlags } from "../enums/CameraClearFlags";
import { DepthTextureMode } from "../enums/DepthTextureMode";
import { ReplacementFailureStrategy } from "../enums/ReplacementFailureStrategy";
import { Shader } from "../shader/Shader";
import { ShaderPass } from "../shader/ShaderPass";
import { RenderQueueType } from "../shader/enums/RenderQueueType";
Expand Down Expand Up @@ -211,13 +212,16 @@ export class BasicRenderPipeline {
const replacementSubShaders = replacementShader.subShaders;
const { replacementTag } = context;
if (replacementTag) {
const materialSubShaderTagValue = materialSubShader.getTagValue(replacementTag);
for (let i = 0, n = replacementSubShaders.length; i < n; i++) {
const subShader = replacementSubShaders[i];
if (subShader.getTagValue(replacementTag) === materialSubShader.getTagValue(replacementTag)) {
if (subShader.getTagValue(replacementTag) === materialSubShaderTagValue) {
this.pushRenderDataWithShader(context, data, subShader.passes, renderStates);
break;
return;
}
}
context.replacementFailureStrategy === ReplacementFailureStrategy.KeepOriginalShader &&
this.pushRenderDataWithShader(context, data, materialSubShader.passes, renderStates);
} else {
this.pushRenderDataWithShader(context, data, replacementSubShaders[0].passes, renderStates);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/RenderPipeline/RenderContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Matrix, Vector4 } from "@galacean/engine-math";
import { Camera } from "../Camera";
import { VirtualCamera } from "../VirtualCamera";
import { ReplacementFailureStrategy } from "../enums/ReplacementFailureStrategy";
import { Shader, ShaderProperty } from "../shader";
import { ShaderTagKey } from "../shader/ShaderTagKey";

Expand Down Expand Up @@ -28,6 +29,7 @@ export class RenderContext {

replacementShader: Shader;
replacementTag: ShaderTagKey;
replacementFailureStrategy: ReplacementFailureStrategy;

flipProjection = false;
viewMatrix: Matrix;
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/enums/ReplacementFailureStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The strategy to use when a shader replacement fails.
*/
export enum ReplacementFailureStrategy {
/** Keep the original shader. */
KeepOriginalShader,
/** Do not render. */
DoNotRender
}
69 changes: 35 additions & 34 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
export { Platform } from "./Platform";
export { Canvas } from "./Canvas";
export { Engine } from "./Engine";
export { Platform } from "./Platform";
export { SystemInfo } from "./SystemInfo";
export { Canvas } from "./Canvas";

export { Scene } from "./Scene";
export { SceneManager } from "./SceneManager";

export { Entity } from "./Entity";
export { BoolUpdateFlag } from "./BoolUpdateFlag";
export { Camera } from "./Camera";
export { Component } from "./Component";
export { Script } from "./Script";
export { DependentMode, dependentComponents } from "./ComponentsDependencies";
export type { EngineConfiguration } from "./Engine";
export type { EngineSettings } from "./EngineSettings";
export { Entity } from "./Entity";
export { Renderer } from "./Renderer";
export { dependentComponents, DependentMode } from "./ComponentsDependencies";
export { Camera } from "./Camera";
export { Script } from "./Script";
export { Transform } from "./Transform";
export { BoolUpdateFlag } from "./BoolUpdateFlag";
export type { EngineSettings } from "./EngineSettings";
export type { EngineConfiguration } from "./Engine";

export { request } from "./asset/request";
export { Loader } from "./asset/Loader";
export { ContentRestorer } from "./asset/ContentRestorer";
export { ResourceManager, resourceLoader } from "./asset/ResourceManager";
export { AssetPromise } from "./asset/AssetPromise";
export type { LoadItem } from "./asset/LoadItem";
export { AssetType } from "./asset/AssetType";
export { ContentRestorer } from "./asset/ContentRestorer";
export type { LoadItem } from "./asset/LoadItem";
export { Loader } from "./asset/Loader";
export { ReferResource } from "./asset/ReferResource";
export { ResourceManager, resourceLoader } from "./asset/ResourceManager";
export { request } from "./asset/request";

export * from "./RenderPipeline/index";
export * from "./base";

export * from "./2d/index";
export { Background } from "./Background";
export * from "./Layer";
export * from "./Utils";
export * from "./animation/index";
export * from "./clone/CloneManager";
export { BackgroundMode } from "./enums/BackgroundMode";
export { DepthTextureMode } from "./enums/DepthTextureMode";
export { FogMode } from "./enums/FogMode";
export { BackgroundTextureFillMode } from "./enums/BackgroundTextureFillMode";
export { CameraClearFlags } from "./enums/CameraClearFlags";
export { CameraType } from "./enums/CameraType";
export { MSAASamples } from "./enums/MSAASamples";
export { Downsampling } from "./enums/Downsampling";
export { ColorSpace } from "./enums/ColorSpace";
export { BackgroundTextureFillMode } from "./enums/BackgroundTextureFillMode";
export { XRManager } from "./xr/XRManager";
export { DepthTextureMode } from "./enums/DepthTextureMode";
export { Downsampling } from "./enums/Downsampling";
export { FogMode } from "./enums/FogMode";
export { MSAASamples } from "./enums/MSAASamples";
export { ReplacementFailureStrategy } from "./enums/ReplacementFailureStrategy";
export * from "./env-probe/index";
export * from "./graphic/index";
export * from "./input/index";
export * from "./lighting/index";
export * from "./shadow/index";
export * from "./material/index";
export * from "./texture/index";
export * from "./graphic/index";
export * from "./2d/index";
export * from "./shaderlib/index";
export * from "./animation/index";
export * from "./mesh/index";
export * from "./sky/index";
export * from "./particle/index";
export * from "./trail/index";
export * from "./env-probe/index";
export * from "./shader/index";
export * from "./Layer";
export * from "./clone/CloneManager";
export * from "./renderingHardwareInterface/index";
export * from "./physics/index";
export * from "./Utils";
export * from "./renderingHardwareInterface/index";
export * from "./shader/index";
export * from "./shaderlib/index";
export * from "./shadow/index";
export * from "./sky/index";
export * from "./texture/index";
export * from "./trail/index";
export { XRManager } from "./xr/XRManager";

// Export for CanvasRenderer plugin.
export { Basic2DBatcher } from "./RenderPipeline/Basic2DBatcher";
Expand Down
25 changes: 24 additions & 1 deletion tests/src/core/Camera.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Camera, CameraClearFlags, Entity, Layer } from "@galacean/engine-core";
import { Camera, CameraClearFlags, Entity, Layer, ReplacementFailureStrategy, Shader } from "@galacean/engine-core";
import { Matrix, Ray, Vector2, Vector3, Vector4 } from "@galacean/engine-math";
import { WebGLEngine } from "@galacean/engine-rhi-webgl";
import { expect } from "chai";
Expand Down Expand Up @@ -78,11 +78,34 @@ describe("camera test", function () {
camera.orthographicSize = expectedOrthographicSize;
expect(camera.orthographicSize).to.eq(expectedOrthographicSize);
camera.orthographicSize = orthographicSize;
const testVS = `
void main() {
gl_Position = vec4(1.0, 1.0, 1.0, 1.0);
}`;

const testFS = `
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
`;

const shader = Shader.create("TestReplaceShader", testVS, testFS);

// Test ReplacementShader
camera.setReplacementShader(shader, "CanReplace");
expect(camera["_replacementShader"]).to.eq(shader);
expect(camera["_replacementSubShaderTag"].name).to.eq("CanReplace");
expect(camera["_replacementFailureStrategy"]).to.eq(ReplacementFailureStrategy.KeepOriginalShader);

camera.setReplacementShader(shader, "CanReplace", ReplacementFailureStrategy.DoNotRender);
expect(camera["_replacementShader"]).to.eq(shader);
expect(camera["_replacementSubShaderTag"].name).to.eq("CanReplace");
expect(camera["_replacementFailureStrategy"]).to.eq(ReplacementFailureStrategy.DoNotRender);

camera.resetReplacementShader();
expect(camera["_replacementShader"]).to.eq(null);
expect(camera["_replacementSubShaderTag"]).to.eq(null);
expect(camera["_replacementFailureStrategy"]).to.eq(null);
});

it("static void function", () => {
Expand Down
Loading