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

Fix shader pass can't use SubShader content cause by scope loss when ShaderLab parse Pass #2393

Merged
merged 33 commits into from
Oct 25, 2024
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5a26f3d
fix(shader-lab): compatible with empty macro
Sway007 Sep 20, 2023
5ecc318
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 20, 2023
cafc24f
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 20, 2023
dc69489
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 21, 2023
221b7b6
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 21, 2023
0d45d9c
fix(shader-lab): add break and continue syntax
Sway007 Sep 21, 2023
0f14c3f
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 27, 2023
8871d9b
fix: typo
Sway007 Oct 11, 2023
3b4ffd7
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 1, 2023
f649a58
fix(shader-lab): Make usepass compatible with buitin shader
Sway007 Nov 1, 2023
598fc56
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 2, 2023
e33a66f
fix(shader-lab): compatible with no varying variable
Sway007 Nov 2, 2023
41ef06f
feat(shader-lab): detect mismatch return type
Sway007 Nov 6, 2023
b5214fc
fix(shader-lab): renderState assignment
Sway007 Nov 7, 2023
f36ce02
feat: extend material loader data type
Sway007 Nov 13, 2023
ff8b7c2
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 13, 2023
634236f
fix(shader-lab): glsl type pattern
Sway007 Nov 13, 2023
91e6fa4
fix(shader-lab): glsl type pattern
Sway007 Nov 13, 2023
3932448
fix: switch case break
Sway007 Nov 15, 2023
671cace
fix: array index loss
Sway007 Jun 11, 2024
92b972e
feat: merge
Sway007 Jun 11, 2024
9226d38
fix: test-case
Sway007 Jun 11, 2024
ff6a69a
fix: test-case
Sway007 Jun 12, 2024
83b9ca2
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Jun 13, 2024
f510c2a
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Jul 15, 2024
3bcaeb0
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Aug 5, 2024
79a7a0c
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Aug 5, 2024
fa7131c
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Aug 6, 2024
d4ea485
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Aug 26, 2024
2af1c65
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 10, 2024
a828991
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 13, 2024
24cb887
fix: new scope when parse pass
Sway007 Oct 10, 2024
a69da3e
fix: example
Sway007 Oct 10, 2024
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
Prev Previous commit
fix: example
Sway007 committed Oct 10, 2024
commit a69da3ee68b765fca41debab6b263349be7e16f4
71 changes: 35 additions & 36 deletions examples/shader-lab-multi-pass.ts
Original file line number Diff line number Diff line change
@@ -18,11 +18,11 @@ import {
Texture2D,
Vector3,
Vector4,
WebGLEngine,
} from '@galacean/engine';
import { ShaderLab } from '@galacean/engine-shader-lab';
import { OrbitControl } from '@galacean/engine-toolkit-controls';
import * as dat from 'dat.gui';
WebGLEngine
} from "@galacean/engine";
import { ShaderLab } from "@galacean/engine-shader-lab";
import { OrbitControl } from "@galacean/engine-toolkit-controls";
import * as dat from "dat.gui";

const LAYER = 40;

@@ -32,13 +32,13 @@ const shaderLab = new ShaderLab();
const loopPassSource = Array.from({ length: LAYER })
.map((_, index) => {
const step = (1 / LAYER) * index;
const u_furOffset = step % 1 === 0 ? step + '.0' : step;
const u_furOffset = step % 1 === 0 ? step + ".0" : step;
const renderStateSource =
index > 0
? `
BlendState = transparentBlendState;
DepthState = transparentDepthState;
RenderQueueType = RenderQueueType.Transparent;
RenderQueueType = Transparent;
`
: ``;

@@ -136,7 +136,7 @@ const loopPassSource = Array.from({ length: LAYER })
}
`;
})
.join('\n');
.join("\n");

const furShaderSource = `Shader "fur-unlit" {
SubShader "Default" {
@@ -156,14 +156,14 @@ const furShaderSource = `Shader "fur-unlit" {
vec4 POSITION;
vec3 NORMAL;
vec2 TEXCOORD_0;
}
};
struct v2f {
vec2 v_uv;
vec2 v_uv2;
vec3 v_normal;
vec3 v_pos;
}
};
#define PI 3.14159265359
#define RECIPROCAL_PI 0.31830988618
@@ -176,21 +176,20 @@ class RandomGravityScript extends Script {
shaderData: ShaderData;
progress = 0;
onUpdate(deltaTime: number) {
const progress =
0.5 + Math.cos((this.progress = this.progress + deltaTime * 2)) / 2;
this.shaderData.setFloat('u_gravityIntensity', progress);
const progress = 0.5 + Math.cos((this.progress = this.progress + deltaTime * 2)) / 2;
this.shaderData.setFloat("u_gravityIntensity", progress);
}
}

WebGLEngine.create({ canvas: 'canvas', shaderLab }).then((engine) => {
WebGLEngine.create({ canvas: "canvas", shaderLab }).then((engine) => {
engine.canvas.resizeByClientSize();

const furShader = Shader.create(furShaderSource);
const scene = engine.sceneManager.activeScene;
const rootEntity = scene.createRootEntity();

// camera
const cameraEntity = rootEntity.createChild('cameraNode');
const cameraEntity = rootEntity.createChild("cameraNode");
cameraEntity.transform.setPosition(0, 0, 5);
cameraEntity.addComponent(Camera);
cameraEntity.addComponent(OrbitControl);
@@ -199,24 +198,24 @@ WebGLEngine.create({ canvas: 'canvas', shaderLab }).then((engine) => {
.load([
{
type: AssetType.Texture2D,
url: 'https://mdn.alipayobjects.com/huamei_dmxymu/afts/img/A*R75iTZlbVfgAAAAAAAAAAAAADuuHAQ/original',
url: "https://mdn.alipayobjects.com/huamei_dmxymu/afts/img/A*R75iTZlbVfgAAAAAAAAAAAAADuuHAQ/original"
},
{
type: AssetType.Texture2D,
url: 'https://mdn.alipayobjects.com/huamei_dmxymu/afts/img/A*t1s4T7h_1OQAAAAAAAAAAAAADuuHAQ/original',
url: "https://mdn.alipayobjects.com/huamei_dmxymu/afts/img/A*t1s4T7h_1OQAAAAAAAAAAAAADuuHAQ/original"
},
{
type: AssetType.Env,
url: 'https://gw.alipayobjects.com/os/bmw-prod/6470ea5e-094b-4a77-a05f-4945bf81e318.bin',
},
url: "https://gw.alipayobjects.com/os/bmw-prod/6470ea5e-094b-4a77-a05f-4945bf81e318.bin"
}
])
.then((res) => {
const layerTexture = res[0] as Texture2D;
const baseTexture = res[1] as Texture2D;
scene.ambientLight = res[2] as AmbientLight;

// create sphere
const entity = rootEntity.createChild('sphere');
const entity = rootEntity.createChild("sphere");
const renderer = entity.addComponent(MeshRenderer);
renderer.mesh = PrimitiveMesh.createSphere(engine, 0.5, 16);

@@ -225,13 +224,13 @@ WebGLEngine.create({ canvas: 'canvas', shaderLab }).then((engine) => {

const shaderData = material.shaderData;

shaderData.setTexture('u_mainTex', baseTexture);
shaderData.setTexture('u_layerTex', layerTexture);
shaderData.setTexture("u_mainTex", baseTexture);
shaderData.setTexture("u_layerTex", layerTexture);

shaderData.setFloat('u_furLength', 0.5);
shaderData.setVector4('u_uvTilingOffset', new Vector4(5, 5, 0.5, 0.5));
shaderData.setVector3('u_gravity', new Vector3(0, 0, 0));
shaderData.setFloat('u_gravityIntensity', 0);
shaderData.setFloat("u_furLength", 0.5);
shaderData.setVector4("u_uvTilingOffset", new Vector4(5, 5, 0.5, 0.5));
shaderData.setVector3("u_gravity", new Vector3(0, 0, 0));
shaderData.setFloat("u_gravityIntensity", 0);

const randomGravityScript = entity.addComponent(RandomGravityScript);
randomGravityScript.shaderData = shaderData;
@@ -242,25 +241,25 @@ WebGLEngine.create({ canvas: 'canvas', shaderLab }).then((engine) => {
uvOffset: 0.5,
enable: () => {
randomGravityScript.enabled = !randomGravityScript.enabled;
shaderData.setFloat('u_gravityIntensity', 0);
shaderData.setFloat("u_gravityIntensity", 0);
randomGravityScript.progress = 0;
},
}
};

gui.add(debugInfo, 'u_furLength', 0, 1, 0.01).onChange((v) => {
shaderData.setFloat('u_furLength', v);
gui.add(debugInfo, "u_furLength", 0, 1, 0.01).onChange((v) => {
shaderData.setFloat("u_furLength", v);
});
gui.add(debugInfo, 'uvScale', 1, 20, 1).onChange((v) => {
const value = shaderData.getVector4('u_uvTilingOffset');
gui.add(debugInfo, "uvScale", 1, 20, 1).onChange((v) => {
const value = shaderData.getVector4("u_uvTilingOffset");
value.x = value.y = v;
});
gui.add(debugInfo, 'uvOffset', -1, 1, 0.01).onChange((v) => {
const value = shaderData.getVector4('u_uvTilingOffset');
gui.add(debugInfo, "uvOffset", -1, 1, 0.01).onChange((v) => {
const value = shaderData.getVector4("u_uvTilingOffset");
value.z = value.w = v;
});

gui.add(scene.ambientLight, 'diffuseIntensity', 0, 1, 0.01);
gui.add(debugInfo, 'enable').name('pause/resume');
gui.add(scene.ambientLight, "diffuseIntensity", 0, 1, 0.01);
gui.add(debugInfo, "enable").name("pause/resume");
engine.run();
});
});